Jump to content
Come try out the Arcade, Link at the top of the website ×

Recommended Posts


  • Member ID:  1100
  • Group:  ***- Inactive Clan Members
  • Followers:  166
  • Topic Count:  165
  • Topics Per Day:  0.03
  • Content Count:  1038
  • Content Per Day:  0.19
  • Reputation:   623
  • Achievement Points:  9278
  • Solved Content:  0
  • Days Won:  2
  • Joined:  02/07/10
  • Status:  Offline
  • Last Seen:  
  • Birthday:  11/30/1980
  • Device:  Windows

Posted

Hey everyone I need some help in coding. The language is C++ and Im extremely rusty in fact I suck at programming.  Posted below is a code for an automated trading robot for the Foreign Exchange Market using an MT4 Platform (MetaTrader).   I believe it was designed for back testing, but I believe it can be used for forward testing, but it needs a code to do trades on live charts.  If anyone can help me that would be great.

 

 

 

#define MAINSEEK 148
#define BARSIZE 44  // LONG_VALUE + 5 * DOUBLE_VALUE
#define SIGNAL_NONE 0
#define SIGNAL_BUY   1
#define SIGNAL_SELL  2
#define SIGNAL_CLOSEBUY 3
#define SIGNAL_CLOSESELL 4
extern double Stop                     = 15;
extern string phase="buy";





extern bool StopLossMode  = True;
extern int StopLoss = 100;
extern bool TakeProfitMode = False;
extern int TakeProfit = 0;
extern bool UseTrailingStop = False;
extern int TrailingStop = 30;
extern int MaximumRisk=3;
extern int Level1=0;
extern int Slippage = 3;
extern int MagicNumber = 0;
extern int Pips = 5.;
extern double Lots = 0.1;


int BarCount;
int Current;





bool TickCheck = True;



int handle;
bool MainError;

int GetTime( int Pos )
{
int PosTime;

FileSeek(handle, MAINSEEK + Pos, SEEK_SET);
PosTime = FileReadInteger(handle);

return(PosTime);
}

bool FindTimePlace( int SearchTime )
{
int LeftTime, RightTime, PosTime;
int Left, Right, Pos;

Left = 0;
Right = FileSize(handle) - MAINSEEK - BARSIZE;

LeftTime = GetTime(Left);
RightTime = GetTime(Right);

while ((LeftTime < SearchTime) && (SearchTime < RightTime))
{    
Pos = (Left + Right) / 2;
Pos -= Pos % BARSIZE;

if (Pos == Left)
break;

PosTime = GetTime(Pos);

if (SearchTime >= PosTime)
{
Left = Pos;
LeftTime = GetTime(Left);
}
else // if (SearchTime < PosTime)
{
Right = Pos;
RightTime = GetTime(Right);
}
}

if (SearchTime <= RightTime)
{
FileSeek(handle, Left + MAINSEEK, SEEK_SET);
return(TRUE);
}
else
return(FALSE);
}

void init()
{





int handle = FileOpenHistory(Symbol() + Period() + ".hst", FILE_BIN|FILE_READ);

if (handle > 0)
MainError = FALSE;
else
{
MainError = true;

return;
}

MainError = FindTimePlace(Time[0]);

if (!MainError)
FileClose(handle);

return;
}

void deinit()
{
if (MainError)
FileClose(handle);

return;
}

bool GetPrices( int& PriceTime, int& PriceLow, int& PriceHigh)
{
PriceTime = FileReadInteger(handle);
FileSeek(handle, DOUBLE_VALUE, SEEK_CUR);
PriceLow = FileReadDouble(handle) / Point + 0.1;
PriceHigh = FileReadDouble(handle) / Point + 0.1;
FileSeek(handle, 2 * DOUBLE_VALUE, SEEK_CUR);

if (FileTell(handle) + BARSIZE <= FileSize(handle))
return(TRUE);
else
return(FALSE);
}

int GetTimeTrade()
{
static bool FlagUP = TRUE;
static int Min = 999999;
static int Max = 0;
static int NTime;
int ResTime;

int PriceTime, PriceLow, PriceHigh;

while (TRUE)
{
if (!GetPrices(PriceTime, PriceLow, PriceHigh))
return(-1);

if (FlagUP)
{
if (PriceHigh > Max)
{
Max = PriceHigh;
NTime = PriceTime;
}
else if (Max - PriceLow >= Pips)
{
FlagUP = FALSE;
Min = PriceLow;

break;
}
}
else // (FlagUP == FALSE)
{
if (PriceLow < Min)
{
Min = PriceLow;
NTime = PriceTime;
}
else if (PriceHigh - Min >= Pips)
{
FlagUP = TRUE;
Max = PriceHigh;

break;
}
}
}

ResTime = NTime;
NTime = PriceTime;

return(ResTime);
}

void CloseOrder( int Ticket )
{
OrderSelect(Ticket, SELECT_BY_TICKET);

if (OrderType() == OP_BUY)
OrderClose(Ticket, OrderLots(), Bid, 0);
else  // (OrderType() == OP_SELL)
OrderClose(Ticket, OrderLots(), Ask, 0);

return;  
}

int ReverseOrder( int Ticket)
{
if (Ticket == 0)
Ticket = OrderSend(Symbol(), OP_BUY, Lots, Ask, 0, 0, 0);
else
{
OrderSelect(Ticket, SELECT_BY_TICKET);

if (OrderType() == OP_BUY)
{
OrderClose(Ticket, OrderLots(), Bid, 0);
Ticket = OrderSend(Symbol(), OP_SELL, Lots, Bid, 0, 0, 0);
}
else  // (OrderType() == OP_SELL)
{
OrderClose(Ticket, OrderLots(), Ask, 0);
Ticket = OrderSend(Symbol(), OP_BUY, Lots, Ask, 0, 0, 0);
}
}

return(Ticket);
}

void System()
{
static int Ticket = 0;
static int NewTime = 0;

if (NewTime < 0)
return;

if (Time[0] < NewTime)
return;

Ticket = ReverseOrder(Ticket);

NewTime = GetTimeTrade();

if (NewTime < 0)
CloseOrder(Ticket);
}

int start()
{
if (!MainError)
return;

System();

return;
}

 

 

 

(This Robot use history from future data, so in live account there is no future data. you can't trade with this ea. see this :

bool FindTimePlace( int SearchTime )
{
int LeftTime, RightTime, PosTime;
int Left, Right, Pos;

 

 

 

The trading logic is great, but I am not sure how to code it to where it will trade live on the charts.

 


Guest
This topic is now closed to further replies.
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.