Backtest errors & new bar detection

 
void OnTick()
  {
//---
  if(!CheckIfOpenOrdersByMagicNumber(MN))
 { 
   
    int i = OrdersHistoryTotal();
    int a = OrdersHistoryTotal() - 5;
    int b = OrdersHistoryTotal();
    for (i = a;i<b;i++)
    {
      if(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY) == true && OrderMagicNumber() == MN && OrderCloseTime() < OrderOpenTime() + period(Period()))
      { 
        if (OrderType() == OP_BUY)
        {
          int MN = 202;
          double stopLossPrice = High[1]+Point;
          double stoplevel = MarketInfo(NULL, MODE_STOPLEVEL)*GetPipValue();
          double entry = Bid;
          if (MathAbs(entry - stopLossPrice) < stoplevel)
          {entry = entry - (stoplevel-MathAbs(entry - stopLossPrice));}
          int NewOrderId = OrderSend(NULL,OP_SELL,lotsize(maxRiskPerTrade,entry,stopLossPrice),entry,5,stopLossPrice,0,NULL,MN);
          if (NewOrderId < 0) Alert("order rejected. Order error: " + GetLastError());
          
         while (OrderSelect(NewOrderId,SELECT_BY_TICKET)== true)
         {datetime closetime = OrderOpenTime() + period(Period());
            if (closetime < OrderOpenTime() + period(Period()))
               {bool newres;
                  newres = OrderClose(NewOrderId,OrderLots(),Ask,5);
                  if (newres == false)
                   {
                    Alert("Error closing order!");
                  }
                    else 
                  {   
                    Alert("Order closed successfuly");
                }
                }
         }
         
        }
        
        else if (OrderType() == OP_SELL)
        {
          int MN = 202;
          double stopLossPrice = Low[1]- Point;
          double stoplevel = MarketInfo(NULL, MODE_STOPLEVEL)*GetPipValue();
          double entry = Ask;
          if (MathAbs(entry - stopLossPrice) < stoplevel)
          {entry = entry + (stoplevel -MathAbs(entry-stopLossPrice));}
          int NewOrderId = OrderSend(NULL,OP_BUY,lotsize(maxRiskPerTrade,entry,stopLossPrice),entry,5,stopLossPrice,0,NULL,MN);
          if (NewOrderId < 0) Alert("order rejected. Order error: " + GetLastError());
          
        
          while (OrderSelect(NewOrderId,SELECT_BY_TICKET)== true)
          {
           datetime closetime = OrderOpenTime() + period(Period());
           if (closetime < OrderOpenTime() + period(Period()))
            { 
             bool newres;
             newres = OrderClose(NewOrderId,OrderLots(),Bid,5);
             if (newres == false)
                  {
                      Alert("Error closing order!");
                  }
                      else 
                  {   
                     Alert("Order closed successfuly");
                  }
          break;
          }
        }
      }
    }
    }
   if (High[1] > High[2] && Close[1] < Close[2])
   {
      Alert("Going short. Reversal Bar.");
      
      double stopLossPrice = High[1]+ Point;
      double takeProfitPrice = 0;
      double stoplevel = MarketInfo(NULL, MODE_STOPLEVEL)*GetPipValue();
      double entry = Bid;
      if (MathAbs(entry - stopLossPrice) < stoplevel)
      {entry = entry - (stoplevel-MathAbs(entry - stopLossPrice));}
     
      int OrderID = OrderSend(NULL,OP_SELL,lotsize(maxRiskPerTrade,entry,stopLossPrice),entry,5,stopLossPrice,takeProfitPrice,NULL,MN);
      
      if (OrderID < 0) Alert("order rejected. Order error: " + GetLastError());
      
      if(OrderSelect(OrderID,SELECT_BY_TICKET)== true)
      {Alert("order time is: " +OrderOpenTime());
 
      datetime closingtradetime = (OrderOpenTime()+ period(Period()));
    
      Alert ("closing trade time is: " + closingtradetime);
      }  
   
      while (OrderSelect(OrderID,SELECT_BY_TICKET)== true)
    {
      datetime closingtradetime = (OrderOpenTime()+ period(Period()));
      if (closingtradetime <TimeCurrent()) 
     { 
      bool res;
      res = OrderClose(OrderID,OrderLots(),Ask,5);
      
      if (res == false)
              {
               Alert("Error closing order!");
              }
              else 
              {
               Alert("Order closed successfuly");
              }
      break;
     }
     
    
    }
      
 
   }
     else if (Low[1] < Low[2] && Close[1] > Close[2])
   {
      Alert("Going long. Reversal Bar.");
      
      double stopLossPrice = Low[1] - Point;
      double takeProfitPrice = 0;
      double stoplevel = MarketInfo(NULL, MODE_STOPLEVEL)*GetPipValue();
      double entry = Ask;
      if (MathAbs(entry - stopLossPrice) < stoplevel)
      {entry = entry + (stoplevel -MathAbs(entry-stopLossPrice));}
      
      int OrderID = OrderSend(NULL,OP_BUY,lotsize(maxRiskPerTrade,entry,stopLossPrice),entry,5,stopLossPrice,takeProfitPrice,NULL,MN);
      
      if (OrderID < 0) Alert("order rejected. Order error: " + GetLastError());
      
      if(OrderSelect(OrderID,SELECT_BY_TICKET)== true)
      {Alert("order time is: " +OrderOpenTime());
 
      datetime closingtradetime = (OrderOpenTime()+ period(Period()));
    
      Alert ("closing trade time is: " + closingtradetime);
      } 
      while (OrderSelect(OrderID,SELECT_BY_TICKET)== true)
    {
      datetime closingtradetime = (OrderOpenTime()+ period(Period()));
      if (closingtradetime < TimeCurrent()) 
     { 
      bool res;
      res = OrderClose(OrderID,OrderLots(),Bid,5);
      
      if (res == false)
              {
               Alert("Error closing order!");
              }
              else 
              {
               Alert("Order closed successfuly");
              }
      break;
     }
      
      
              }
            }
      
      

I have been having a lot of trouble when making this EA. I want to create a simple reversal bar strategy that enters when parameters are met and exits when a new bar is formed. If I get stopped out I want to take an opposite order(attempted in the for loop code block). I have been having trouble using the timecurrent function in the backtest because it throws Unmatched data errors. I was using it as a way to determine if the current time compared to the orderopentime + its period. How can i get around using the timecurrent function with a simple code to detect a new bar?Would i apply a while loop to it?  When i look for previous orders to see if it was stopped out, am I correct by iterating through it? The period function multiplies the period() by 60 unless it is a 1 min timeframe then it adds 59 to the period. I appreciate your help!! 

Reason: