How to fix Close[1] & Close[2]

 

Hi everyone, I have a problem with my EA trades, now I will show you the code with a screenshot of a trade (it happens often)


void OnTimer()
  {
//---
   if (currBars != Bars){
      calculate_price_levels();
      //debug_levels();
      
      check_nearest_levels();
      
      if (is_session()){
         if (Close[2] < Nearest_Up_Level && Close[1] >= Nearest_Up_Level && isAvailableOrder()){ 
            //Print("BUY entry, Nearest_Up_Level:",Nearest_Up_Level, ",Close[2]:",Close[2], ",Close[1]:",Close[1]);
            
            double lot = _get_lot_size (Stop_Loss);
            OpenTrade(Symbol(), OP_BUY, lot);
         }
         
         if (Close[2] > Nearest_Down_Level && Close[1] <= Nearest_Down_Level && isAvailableOrder()){
            //Print("SELL entry, Nearest_Down_Level:",Nearest_Down_Level, ",Close[2]:",Close[2], ",Close[1]:",Close[1]);
            
            double lot = _get_lot_size (Stop_Loss);
            OpenTrade(Symbol(), OP_SELL, lot);
         }
      }
      
      currBars = Bars;
   }
   
   
   if (Trailing_Stop > 0) trail_sl();
  // if (BE > 0) check_be();
   
   int trades_number = _get_number_of_trades();
   if (trades_number < Trades_Number_0) check_last_trade_result();
   
   Trades_Number_0 = trades_number;
   
   Comment("Nearest_Up_Level:",Nearest_Up_Level, ",Nearest_Down_Level:",Nearest_Down_Level);
  
  }


void check_nearest_levels(){
   int size = ArraySize(Price_Levels);
   
   for (int i = 0; i < size; i++){
      if (Close[2] > Price_Levels[i] && Close[2] < Price_Levels[i + 1]){
         Nearest_Down_Level = Price_Levels[i];
         Nearest_Up_Level = Price_Levels[i + 1];
         
         BUY_TP_Price = 0;
         if (size > i + 2) BUY_TP_Price = Price_Levels[i + 2];
         else BUY_TP_Price = Nearest_Up_Level + StepPips * Point * mp;
         
         Next_BUY_TP_Price = 0;
         if (size > i + 3) Next_BUY_TP_Price = Price_Levels[i + 3];
         else Next_BUY_TP_Price = BUY_TP_Price + StepPips * Point * mp;
         
         SELL_TP_Price = 0;
         if (i >= 1) SELL_TP_Price = Price_Levels[i - 1];
         else SELL_TP_Price = Nearest_Down_Level - StepPips * Point * mp;
         
         Next_SELL_TP_Price = 0;
         if (i >= 2) Next_SELL_TP_Price = Price_Levels[i - 2];
         else Next_SELL_TP_Price = SELL_TP_Price - StepPips * Point * mp;
         
         break;
      }
   }
}



positions