Strategy tester is not following logic, how to figure out why?

 

Hello every one.

Ok, so I'm trying to automate my testing and I'm having issues with getting right results, or getting rid of mistakes that strategy tester, ea, or something makes.

For the moment I'm trying to just make simple backtest ea that trades when indicator crosses 0 line... and there are some mistakes I can't figure out.

For example, trying to make only long trades when indicator moves above 0 line.

Logic should be simple, right?

If CurrentIndicatorValue is greater than 0.0 and PreviousIndicatorValue is lower than 0.0   -> make a trade. Yet mistakes happen and I have no idea why, especially when I try to trade only once per candle at certain time based on daily chart.

Trying to test on GBPCAD pair in range from 2016.12.30 to 2018.12.31, on OANDA MT4 client. There should be 21 trades, which there are using control points model, but 2 of them are misplaced (one missed completely, other in wrong place)

Here is code:

extern int dpo_prod = 14;
extern int dpo_count_bars = 600;
extern double LotSize = 0.01;

int ThisBarTrade = 0;


double StopLoss;
double TakeProfit;
double CurrentIndi, PreviousIndi;

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
void init()
{
   
}

//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
int start()
{
   if (Hour() != 00 && Minute() != 00) //Bars != ThisBarTrade && code closing all and any orders at any time for testing just to see if code is opening orders in right moment
   {
      CloseAll();  
   }
   
   if (Hour() == 22 && Minute() == 00)  //Bars != ThisBarTrade && 21:40 is when I would like to trade, 20 minutes before closing daily candle (code gives 14 trades). 
   {                                    //Yet if time is set for 22:00 results are much closer to manual backtest (21 trades) with two mistakes
      CurrentIndi = indicatorCustom(0);
      PreviousIndi = indicatorCustom(1);
      TakeProfit = iATR(Symbol(), 0, 14, 1);
      StopLoss = TakeProfit * 1.5;

      if(CurrentIndi > 0.0 && PreviousIndi < 0.0)// && CurrentIndi > PreviousIndi) //check for buy trade
      {
         OrderSend(Symbol(), OP_BUY, LotSize, Ask, 3, Ask - StopLoss, Ask + TakeProfit, NULL, 0, 0, Green);
      }
   }
   
   return(1);
}

//+------------------------------------------------------------------+

double indicatorCustom(int shift = 0)
{
   double indicatorValue = iCustom(Symbol(), 0, "used\\DPO", dpo_prod, dpo_count_bars, 0, shift);
 
   return(indicatorValue);
}

int CloseAll() // not my function, but works did not touch anything here
{
 
   int TotalClose=0;  //We want to count how many orders have been closed
   
   //Normalization of the slippage
   if(Digits==3 || Digits==5){
      Slippage=Slippage*10;
   }
   
   //We scan all the orders backwards, this is required as if we start from the first we will have problems with the counters and the loop
   for( int i=OrdersTotal()-1;i>=0;i-- ) {
      
      //We select the order of index i selecting by position and from the pool of market/pending trades
      //If the selection is successful we try to close it
      if(OrderSelect( i, SELECT_BY_POS, MODE_TRADES )){
      
         //We define the close price, which depends on the type of order
         //We retrieve the price for the instrument of the order using MarketInfo(OrderSymbol(),MODE_BID) or MODE_ASK
         //And we normalize the price found
         double ClosePrice;
         RefreshRates();
         if(OrderType()==OP_BUY) ClosePrice=NormalizeDouble(MarketInfo(OrderSymbol(),MODE_BID),Digits);
         if(OrderType()==OP_SELL) ClosePrice=NormalizeDouble(MarketInfo(OrderSymbol(),MODE_ASK),Digits);
         
         //If the order is closed correcly we increment the counter of closed orders
         //If the order fails to be closed we print the error
         if(OrderClose(OrderTicket(),OrderLots(),ClosePrice,3,CLR_NONE)){
            TotalClose++;
         }
         else{
            Print("Order failed to close with error - ",GetLastError());
         }
      }
      //If the OrderSelect() fails we return the cause
      else{
         Print("Failed to select the order - ",GetLastError());
      }  
      
      //We can have a delay if the execution is too fast, Sleep will wait x milliseconds before proceed with the code
      //Sleep(300);
   }
   //If the loop finishes it means there were no more open orders for that pair
   return(TotalClose);

In two cases results are somehow with mistakes.

On screen #2, trade to the left of first line is correct, yet line clearly crosses upwards so there should be next trade to the left of second line and it's not there

Screen #3, trade to the left of line is fine (indicator had positive value on current candle, and negative on previous one), and on previous candle, the firs trade somehow there is trade while values for both current and previous candle are negative... 

And I have no clue how to figure out what is wrong... I will admit that I'm far from best programmer, but how tester can miss trade when conditions are clearly met, or made one where both of them are not met while making 19 other as they should? And there are even more missing trades on other currency pairs. Tried to download historic data, tried not to download historic data still wimple logic is not followed... It's just not making any sense to me...

Files:
 
Print the values of CurrentIndi and PreviousIndi when it comes to open an order.
 

Hi,

So with ControlPoints testing is a fake testing and really low accuracy,that's just for estimation,

So you would test it by every tick,and as you know at exact time hour 22:00 it may can't be reached exactly to the main rule(previous daily candle below 0,current candle above 0),