Hello, I back tested my expert advisor using open prices only and the results were profitable. My understanding of the back test method is positions is opened or closed on the start of each new candle stick and stop losses and take profits are triggered only on opens correct me if any of what I said is mistaken Please. The opening quotes have the equivalent accuracy to real tick openings, this is the code I used to try to emulate the back test logic.
static double Open;
if(IsNewBar() == true)
StopLoss = Ask - x * _Point;
static datetime t_bar=iTime(_Symbol,PERIOD_M15,0);
if(t_bar != time)
t_bar = time;
Open = Ask;
if(StopLoss > Open) // compare the stop loss to the Ask of the open
The code does not emulate the open prices only back test method and the ea when altered does not gain a profit when back tested on open prices only. Dukacopy tick data is used. I want to publish the EA on the mql5 market place this is the one obstacle. Help from the forum is appreciated Thank you in advance.
I tried other methods to cause the ea to trigger stop losses and take profits stored in variables to close orders after a new bar formed yet when back tested using open prices only
the results do not match when a SL and TP are added when the order is sent.
Is there a way to fix this?
static datetime prevTime=0;
if (CopyTime(_Symbol,_Period,0,1,lastTime)==1 && prevTime!=lastTime)
static datetime last_bar_time=0;
//--- if static variable isn't initialized
static double OpenBid;
//--- it's first call, save bar time and exit
PrintFormat("The last_bar_time variable is initialized with value %s",TimeToString(last_bar_time));
//--- save open bar time to the static variable
CopyRates(Symbol(), Period(), 0, 2, mrate);
// NEW BAR CHECK.
static double dBar_Open;
static double dBar_High;
static double dBar_Low;
static double dBar_Close;
static long lBar_Volume;
static datetime nBar_Time;
if(mrate.open != dBar_Open || mrate.high != dBar_High || mrate.low != dBar_Low || mrate.close != dBar_Close || mrate.tick_volume != lBar_Volume || mrate.time != nBar_Time)
bStart_NewBar = true; // A new BAR has appeared!
if(bStart_NewBar == true)
dBar_Open = mrate.open;
dBar_High = mrate.high;
dBar_Low = mrate.low;
dBar_Close = mrate.close;
lBar_Volume = mrate.tick_volume;
nBar_Time = mrate.time;
bStart_NewBar = false;
OpenBid = Bid;
Update - the code now partially works on every tick different time frames of the gbp usd before a long string of losses that does not occur during open prices only back test and multiple invalid stops errors during both types of back test. Also during open prices only back test trades are opened and closed on the same candle.
An image of the back test is attached.
Please enable the necessary setting in your browser, otherwise you will not be able to log in.