Problem with unexpected EA stop

 

Hi,

 i´m new to programming but trying my best. Currently i´m trying to program EA which will trade after predefined set of points e.g. open order after last trade +/- x points. My EA is working fine until one moment, When i open a trade pending order will be set +/- points only if tick will take place. That is fine but when that tick will go other directions (not pending direction) it won´t place pending order and when the first order hit SL it should place opposite market order but in this scenario (when pending order is not set) it won´t open opposite trade and EA stops. Please can anybody help me identify the problem ? Thanks in advance

 

 if (BuyTrend == true)
    {
     if (OrderSelect(0,SELECT_BY_POS)!= true) Print("Problem selecting start order", GetLastError());
     double OOP1=OrderOpenPrice();
    
     double SL = StopLoss * Point;
     double range = next * Point;

     for ( int MaxCount = 1; OrdersTotal() >= MaxCount; MaxCount++ )
      {
       double price = (OOP1 + (range * count));
       double sl = (price - SL);
       
        
       RefreshRates();
       if (Ask >= price - (next * Point))   
        {
        
      //  if (Ask >= price - range)
      //   {
          RefreshRates();
          orderbuy = OrderSend(Symbol(),OP_BUYSTOP,volume,price,NULL,sl,NULL,NULL,100,NULL,clrNONE);      
    //     }
      //  else
     //    {
     //     RefreshRates();
     //     order = OrderSend(Symbol(),OP_BUY,volume,price,NULL,sl,NULL,NULL,101,NULL,clrNONE); 
      //   } 
         
         if (orderbuy < 0) Print("Problem s Buystopom ",GetLastError());
         
         if (orderbuy >=0)
          {
           count++; 
           if (OrderSelect((OrdersTotal()-2),SELECT_BY_POS,MODE_TRADES)!= true)
               Print("Problem s vyberom obchodu na sl modifikaciu ",GetLastError());
           double slfa=OrderStopLoss();
           
           for (int crt=OrdersTotal()-3; crt >=0;crt--)     
            {
             if (OrderSelect(crt,SELECT_BY_POS,MODE_TRADES)!= true)
                Print("Problem s vyberom obchodu na sl modifikaciu no.2 ",GetLastError());
             if (OrderModify(OrderTicket(),OrderOpenPrice(),slfa,NULL,NULL,clrNONE)!= true)
                Print("Problem s modifikaciou SL ",GetLastError());
            } 
          }
         else 
          {
           Print("Buy error: ", GetLastError());   
          }  
        }
        
       int last_trade = OrdersHistoryTotal();
       if(last_trade > 0)
        {
         if(OrderSelect(last_trade-1,SELECT_BY_POS,MODE_HISTORY)==true)
          {
           if(OrderSymbol()==Symbol() && OrderClosePrice()==OrderStopLoss())
            {
             RefreshRates();
             if (OrderSend(Symbol(),OP_SELL,volume,Bid,NULL,Ask+SL,NULL,NULL,50,NULL,clrNONE)< 0)
                 Print("Problem s otvorenim protipozicie",GetLastError()); 
             else 
              {
               count = 1;
               SellTrend=true;
               BuyTrend=false;
              }
           //  if (OrderSelect(OrdersTotal()-1,SELECT_BY_POS)!= true) Print("Problem s vyberom sell stopu ",GetLastError());
           //  int type = OrderType();
           //  if (type == OP_BUYSTOP)
            //  {
              if (OrderDelete(orderbuy,clrNONE)!= true) Print("Problem s deletovanim cakacky ",GetLastError());   
             // }
            }
          }
        }     
      }
    }