backtest using open prices and ticks show different results only trading openings of each candle not yielding matching results Despite efforts

Benrashi Sagev Jacobson

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);
datetime time=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.

Benrashi Sagev Jacobson

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;
          datetime lastTime[1];
          if (CopyTime(_Symbol,_Period,0,1,lastTime)==1 && prevTime!=lastTime[0])
               // ...

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));
  datetime curr_time=(datetime)SeriesInfoInteger(Symbol(),Period(),SERIES_LASTBAR_DATE);

      //--- save open bar time to the static variable


 MqlRates mrate[2];                 
   CopyRates(Symbol(), Period(), 0, 2, mrate);

   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[0].open != dBar_Open || mrate[0].high != dBar_High || mrate[0].low != dBar_Low || mrate[0].close != dBar_Close || mrate[0].tick_volume != lBar_Volume || mrate[0].time != nBar_Time)
         bStart_NewBar = true; // A new BAR has appeared!        

      if(bStart_NewBar == true)
         dBar_Open   = mrate[0].open;      
         dBar_High   = mrate[0].high;
         dBar_Low    = mrate[0].low;
         dBar_Close  = mrate[0].close;                 
         lBar_Volume = mrate[0].tick_volume;
         nBar_Time   = mrate[0].time;
         bStart_NewBar = false;
         OpenBid = Bid;
Benrashi Sagev Jacobson

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.

partial.PNG  54 kb