EA opens unintended orders on every tick although coded to open orders on new bar

 

I have attached a single EA on three different symbols and I have coded that onTick() returns if there's no new bar because my signals are on opening of new bar.

The EA attached to different symbols run as expected most of the time, i.e. the onTick() exits if there's no new bar but sometimes it ignores this and opens trades on every tick until the bar ends.

I also run onTrade() where I write profit of last deal to file and send network request to server with trade results data. (Maybe this creates some kind of logjam??).

I have no clue what would trigger sudden orders on every tick until new bar forms?  Any help would be greatly appreciated. 

void OnTick()
  {

   if(Bars(_Symbol,_Period)<60) 
     {
      Alert("We have less than 60 bars, EA will now exit!!");
      return;
     }
   static datetime Old_Time;
   datetime New_Time[1];
   bool IsNewBar=false;
//--- copying the last bar time to the element New_Time[0]
   int copied=CopyTime(_Symbol,_Period,0,1,New_Time);
   if(copied>0) // ok, the data has been copied successfully
     {
      if(Old_Time!=New_Time[0]) // if old time isn't equal to new bar time
        {
         IsNewBar=true;   // if it isn't a first call, the new bar has appeared
            Print("We have new bar here ",New_Time[0]," old time was ",Old_Time);
            Old_Time=New_Time[0];            // saving bar time
        }
     }
   else
     {
      Alert("Error in copying historical times data, error =",GetLastError());
      ResetLastError();
      return;
     }

//--- EA should only check for new trade if we have a new bar
   if(IsNewBar==false)
     {
      return;
     }

//--- Do we have enough bars to work with
   int Mybars=Bars(_Symbol,_Period);
   if(Mybars<60) // if total bars is less than 60 bars
     {
      Alert("We have less than 60 bars, EA will now exit!!");
      return;
     }

//--- Define some MQL5 Structures we will use for our trade
   MqlTick latest_price;     // To be used for getting recent/latest price quotes
   MqlTradeRequest mrequest;  // To be used for sending our trade requests
   MqlTradeResult mresult;    // To be used to get our trade results
   MqlRates mrate[];         // To be used to store the prices, volumes and spread of each bar
   ZeroMemory(mrequest);     // Initialization of mrequest structure

   ArraySetAsSeries(mrate,true);
   ArraySetAsSeries(TopBuffer,true);
   ArraySetAsSeries(BottomBuffer,true);

//--- Get the last price quote using the MQL5 MqlTick Structure
   if(!SymbolInfoTick(_Symbol,latest_price))
     {
      Alert("Error getting the latest price quote - error:",GetLastError(),"!!");
      return;
     }

//--- Get the details of the latest 3 bars
   if(CopyRates(_Symbol,_Period,0,3,mrate)<0)
     {
      Alert("Error copying rates/history data - error:",GetLastError(),"!!");
      return;
     }


   if(CopyBuffer(top_handle,0,0,3,TopBuffer)<0)
     {
      Alert("Error copying DChannel_Tops_v2 indicator buffer - error:",GetLastError());
      return;
     }


   if(CopyBuffer(bottom_handle,0,0,3,BottomBuffer)<0)
     {
      Alert("Error copying DChannel_Bottoms_v2 indicator buffer - error:",GetLastError());
      return;
     }

//--- Do we have positions opened already?
   bool Buy_opened=false;  // variable to hold the result of Buy opened position
   bool Sell_opened=false; // variable to hold the result of Sell opened position

   if(PositionSelect(_Symbol)==true) // we have an opened position
     {
      if(PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_BUY)
        {
         Buy_opened=true;  
        }
      else
         if(PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_SELL)
           {
            Sell_opened=true; 
           }
     }

//--- Buy and Sell Conditions
   bool sellCondition= false;
   bool buyCondition = false;

   if(TopBuffer[1] != 0.0)
      sellCondition = true; // sell and close buy
   if(BottomBuffer[1] != 0.0)
      buyCondition = true; //buy and close sell


//--- CHECK FOR OPENING BUY AND SELL CONDITIONS
//--- buy condition
   if(buyCondition)
     {
      if(Buy_opened)
        {
         Alert("We already have a Buy Position!!!");
         return;    // Don't open a new Buy Position
        }

      if(!timeInterval(latest_price.time))
        {
         //Alert("Signal is outside of trading time interval");
         return;
        }
      ZeroMemory(mrequest);
      ZeroMemory(mresult);
      mrequest.action=TRADE_ACTION_DEAL; // type of trade operation
      mrequest.symbol=_Symbol;  // symbol
      mrequest.volume=Lot; // volume of value od Lot variable
      mrequest.type=ORDER_TYPE_BUY;  // order type
      mrequest.price=SymbolInfoDouble(Symbol(),SYMBOL_ASK); //price for opening
      mrequest.sl = NormalizeDouble(latest_price.ask - STP*_Point,_Digits);
      mrequest.tp = NormalizeDouble(latest_price.ask + TKP*_Point,_Digits);
      mrequest.deviation=100;
      mrequest.magic = EA_Magic;
      //--- send order
      if(!OrderSend(mrequest,mresult))
         PrintFormat("OrderSend error %d",GetLastError());     // if unable to send the request, output the error code
      //--- information about the operation
      PrintFormat("retcode=%u  deal=%I64u  order=%I64u",mresult.retcode,mresult.deal,mresult.order);
     }
//--- sell order
   if(sellCondition)
     {
      if(Sell_opened)
        {
         Alert("We already have a Sell Position!!!");
         return;    // Don't open a new Sell Position
        }

      if(!timeInterval(latest_price.time))
        {
         //Alert("Signal is outside of trading time interval");
         return;
        }

      ZeroMemory(mrequest);
      ZeroMemory(mresult);
      mrequest.action=TRADE_ACTION_DEAL;                                // immediate order execution
      mrequest.symbol=_Symbol;  // symbol
      mrequest.volume=Lot; // volume of value od Lot variable
      mrequest.type=ORDER_TYPE_SELL; // order type
      mrequest.price=SymbolInfoDouble(Symbol(),SYMBOL_BID); // price for opening
      mrequest.sl = NormalizeDouble(latest_price.bid + STP*_Point,_Digits); // Stop Loss
      mrequest.tp = NormalizeDouble(latest_price.bid - TKP*_Point,_Digits);
      mrequest.deviation=100;       // allowed deviation from the price
      mrequest.magic = EA_Magic;    // MagicNumber of the order
      //--- send the request
      if(!OrderSend(mrequest,mresult))
         PrintFormat("OrderSend error %d",GetLastError());     // if unable to send the request, output the error code
      //--- information about the operation
      PrintFormat("retcode=%u  deal=%I64u  order=%I64u",mresult.retcode,mresult.deal,mresult.order);
     }
// ---end function
  }