Working in real-time but not in Backtest??

 

Hi,

Before placing an order, I am trying to check whether any Buy/Sell order is already executed during the current M15 bar.


In real-time it looks like working. but while back-testing it executes more than one order in the same candle. Can some please tell me Why? and how to rectify?


   ...
   datetime thisbar = iTime(Symbol(),PERIOD_M15,0);
   if(!BuyExists(thisbar) &&  Bid > SellStop)
   {
        while(IsTradeContextBusy()) Sleep(100);               
        RefreshRates();         
        OrderSend(Symbol(),OP_BUY,0.1,Ask,3,0,BuyStop+(30000*point),"",MAGICMA,0,Green);
   }

================================

bool BuyExists(datetime bar)
 {
  bool temp;
  int _total=OrdersTotal();
  if(_total==0)return; 
  for(int _i=_total-1;_i>=0;_i--) if(
        OrderSelect(_i, SELECT_BY_POS)                // Only my orders w/
        &&  OrderMagicNumber()  == MAGICMA            // my magic number
        &&  OrderSymbol()       == Symbol()           // and my pair.
        &&  OrderType()         == OP_BUY             // Buy Order
        &&  OrderOpenTime()     > bar                 // Buy Placed in current bar
        
     )
  {
       temp=true;
       break;
  }
  return(temp);
 }

===========================




 
Search for "mql4.com Once-Per-Bar" through Google. It'll give you so many ways to avoid that.
Reason: