EA works but can't send one of the orders. Looking for help.

 
int start()
  {
//----
 double ABUY=0;
   double ASELL=0;
   double BBUY=0;
   double BSELL=0;
   double HABUY=0;
   double HASELL=0;
   double HBBUY=0;
   double HBSELL=0;
   double point = MarketInfo(Symbol(),MODE_POINT);
   double expiration=CurTime()+PERIOD_D1*60;
   
   

   int n;
   int total= OrdersTotal();
   int count=0;
   int counts=0;
   
    
    for(int pos=OrdersTotal()-1; pos >= 0; pos--) if ( 
    OrderSelect(pos, SELECT_BY_POS) 
    &&  OrderMagicNumber() == 1 
    &&  OrderSymbol()      == Symbol() 
    &&  OrderType()        == OP_BUY){ counts++; ABUY=2;                //ABUY
    }
    
    for(pos=OrdersTotal()-1; pos >= 0; pos--) if ( 
    OrderSelect(pos, SELECT_BY_POS) 
    &&  OrderMagicNumber() == 1 
    &&  OrderSymbol()      == Symbol()  
    &&  OrderType()        == OP_BUYSTOP){ counts++; ABUY=1;            //ABUYSTOP
    }           
    
    for(pos=OrdersTotal()-1; pos >= 0; pos--) if ( 
    OrderSelect(pos, SELECT_BY_POS) 
    &&  OrderMagicNumber() == 2 
    &&  OrderSymbol()      == Symbol()  
    &&  OrderType()        == OP_SELL){ counts++; ASELL=2;              //ASELL
    }
    for(pos=OrdersTotal()-1; pos >= 0; pos--) if ( 
    OrderSelect(pos, SELECT_BY_POS) 
    &&  OrderMagicNumber() == 2 
    &&  OrderSymbol()      == Symbol()  
    &&  OrderType()        == OP_SELLSTOP){ counts++; ASELL=1;          //ASELLSTOP
    }
    if (!counts){
    if (!OrderSend( Symbol(), OP_BUYSTOP, 0.1, Ask+6*point, 4,Bid-52*point, Bid+20*point, "A BUY", 1, expiration, Blue)){
        Alert("Unable to send A BUY", GetLastError()); 
       }
    else if (!OrderSend(Symbol(),OP_SELLSTOP,0.1,Bid-8*point,4,Ask+50*point,Ask-22*point,"A SELL",2,expiration,Green)){
       Alert("Disabled to send A SELL", GetLastError());
       }
    
       }
//________________________________________________________________________________Now check variables and send or delete orders________________________________________
       if ( ABUY== 2 ) {
             if ( ASELL!=0 ){
             for(pos=OrdersTotal()-1; pos >= 0; pos--) if (
             OrderSelect(pos, SELECT_BY_POS)                 
             &&  OrderMagicNumber()  == 2) {        
             if (!OrderDelete(OrderTicket())) 
             Alert("OrderDelete(",OrderTicket(),") failed: ", GetLastError());
             }
             }
             
             if (ASELL!=0 ){
             for(pos = OrdersTotal()-1; pos >= 0 ; pos--)
             if (OrderSelect(pos, SELECT_BY_POS)                 // Only my orders w/
             &&  OrderMagicNumber()  == 6                    // my magic number
             &&  OrderSymbol()       == Symbol() ){          // and my pair.
             if (!OrderDelete(OrderTicket()))
             Alert("OrderDelete(",OrderTicket(),") failed: ", GetLastError()); 
             }
             }
             
             if (ASELL!=0 ){                                                                  //Program didn't send this order!! Don't know why?
             if (!OrderSend(Symbol(),OP_SELLSTOP,0.2,Bid-30*point,4,Ask-62*point,Ask-2*point,"HA SELL",6,expiration,Green))
             Alert("CAN NOT SEND HASELL");
             }
         }
return (0);
}
I want from this strategy to check if there exists some orders and change variables if orders exists.
If there is none orders it should send 2 pending orders.
When variables have been changed program should delete orders with MagicNumber 2 and 6 and than send new order with MagicNumber == 6.
Now strategy works but it's not sending order with MagicNumber == 6 (A few last lines of code)
 

Can you discribe in words what it is you are trying to do? I am lazy when it comes to trying to understand the logic of someone else's code.

 
  1. If the command fails, print out WHY
    if (OrderSend(...) < 0) Alert("OrderSend failed ",GetLastError());
    Note you can NOT use
    if (!OrderSend(...
    because it returns a -1 on failure not zero.
  2. Bid-30*point,4,Ask-62*point,Ask-2*point,"HA SELL
    You can not place a TP/SL closer than MarketInfo(Symbol(), MODE_STOPLEVEL)*Point from the open. On IBFX that's 30 points. When I tried to use pending orders the failure was also setting TP/SL too close to market even though when the order would open the stops would have been fine. This may have been related to #4 below.

  3. EA's should adjust for 5 digit brokers TP, SL, AND slippage
    //++++ These are adjusted for 5 digit brokers.
    double  pips2points,    // slippage  3 pips    3=points    30=points
            pips2dbl;       // Stoploss 15 pips    0.0015      0.00150
    int     Digits.pips;    // DoubleToStr(dbl/pips2dbl, Digits.pips)
    int     init(){
        if (Digits == 5 || Digits == 3){    // Adjust for five (5) digit brokers.
                    pips2dbl    = Point*10; pips2points = 10;   Digits.pips = 1;
        } else {    pips2dbl    = Point;    pips2points =  1;   Digits.pips = 0; }
        // OrderSend(... Slippage.Pips * pips2points, Bid - StopLossPips * pips2dbl
    
  4. On ECN brokers, you must open the order first and THEN set the TP/SL.
Reason: