What is wrong in this script ? (a delete opposite pending order script)

 

Hi,

I'm new in mql programming and trying to create an EA, which the idea is check a buy stop order if executed then delete the sell order and vice versa..

The buy stop and sell stop order are open manually, the order ticket are use for ea input.

But when I try it in demo, when 1 order is executed, the opposite order is not delete.

Please help, the code below (I know its a poor code since I'm a beginner) :

extern int BuyStop1, SellStop1,BuyStop2,SellStop2;
int st1 = 0,st2=0;
   
int start()
  {
//----
   int order_type;
   
   
   if(OrderSelect(BuyStop1, SELECT_BY_TICKET)==true)
   { 
      if(OrderType()==OP_BUYSTOP) {
         if (st1==0) {
            Alert(BuyStop1 ," executed");
            st1=1;
         }
         if(OrderSelect(SellStop1, SELECT_BY_TICKET)==true) 
         {
            if(OrderType()==OP_SELLSTOP) {
            
               Alert(SellStop1 ," selected and deleted");
               OrderDelete(SellStop1);
               
            }
         }
      }
   }
   
   
   if(OrderSelect(SellStop1, SELECT_BY_TICKET)==true)
   { 
      if(OrderType()==OP_SELLSTOP) {
         if (st2==0) {
            Alert(SellStop1 ," executed");
            st2=1;
         }
         if(OrderSelect(BuyStop1, SELECT_BY_TICKET)==true) 
         {
            if(OrderType()==OP_BUYSTOP) {
            
               Alert(BuyStop1 ," selected and deleted");
               OrderDelete(BuyStop1);
            }
         }
      }
   }
//---
   return(0);
  }


Thanks..

 
When a stop or limit order is executed, it will become an OP_BUY or OP_SELL. You are checking that it has NOT been executed
 

Thanks Gumrai for a little correction..

The basic problem for delete order in this case is because 'allow live trading' is not checked yet..

PROBLEM SOLVED..!!

 
  1. it will become an OP_BUY or OP_SELL

  2. Check that the OrderDelete worked - What are Function return values ? How do I use them ? - MQL4 forum
  3. OrderSelect BY_TICKET will select the deleted ticket out of the history pool next time and the OrderDelete will fail. Clear the ticket number after the delete.
  4. If you accidentally close the chart/terminal, power fails, OS crashes, etc., you've lost the ticket number and the EA has lost control of its orders. Do you recover by doing an OrderSelect loop in init()?
  5. Would you ever write?
    if( (2+2 == 4) == true) ...
    Of course not.
    if(2+2 == 4) // is sufficient.
    So why do you write?
    if(OrderSelect(BuyStop1, SELECT_BY_TICKET)==true)
    Simplify
    if(OrderSelect(BuyStop1, SELECT_BY_TICKET))