two orders. close one!

 

People thanks very much for the help.

you help a lot and i am very grateful for the help in my learning.

 

In my studies, I'm trying to terminate an order if another order is closed.

 

Example SHELL: 

if Order(1) OrderOpenPrice()-20PIPS then Order(2) CLOSE.

I can not imagine how to program!

please, I'm very doubts to solve.

 

if(TotalOrdersCount()<2 && TotalOrdersCount()==0)
      {
   
      ticket   =    OrderSend(Symbol(),OP_SELL,Lots,Bid,0,0,0,NULL,MagicNumber,0,Blue);
      ticket++;
      
      ticket =    OrderSend(Symbol(),OP_SELL,Lots,Bid,0,0,0,NULL,MagicNumber,0,Red);
      ticket++;
  
      }

int cnt, total=OrdersTotal();
      for(cnt=0;cnt<total;cnt++)
      {
         if (OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES)==true)
         {
             if(OrderType()==OP_SELL)
                  {
                     double stnewpriceshell = OrderOpenPrice();
                     double stshell = OrderStopLoss();
                     
                     
                     if(stshell<=0 && Bid-20*MyPoint<stnewpriceshell)
                     {
                           modify = OrderModify(OrderTicket(),OrderOpenPrice(),stnewpriceshell-10*MyPoint,0,0,clrLightCyan);
                     }
                     
                    
                   
                   }//FIM OPSHELL

                
 
if(TotalOrdersCount()<2 && TotalOrdersCount()==0)
//Doesn't make sense. It is the same as
if(TotalOrdersCount()==0)


      ticket++;   //Why?

      ticket++;   //Why?

Save the 2 ticket numbers as Global Variables

Check the condition of one and if condition is true, close the other.

 
thanks for answering Gumrai.

I tried several times as you said, but could not. 

 

//GLOBAL VAR
int modify;
int ticketbuy,ticketshell;
int ticket;

//FIM GLOBAL VAR   
      if(TotalOrdersCount()<2)
      {
   
      ticket   =    OrderSend(Symbol(),OP_BUY,Lots,Ask,0,0,0,NULL,MagicNumber,0,Blue);
      ticketbuy = OrderTicket();
      
      
      ticket = OrderSend(Symbol(),OP_BUY,Lots,Ask,0,0,0,NULL,MagicNumber,0,Red);
      ticketbuy2 = OrderTicket();
 
      }
      
  
      int cnt, total=OrdersTotal();
      for(cnt=0;cnt<total;cnt++)
      {
         if (OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES)==true)
         if(OrderSymbol()==Symbol()&&OrderMagicNumber()==MagicNumber)
         {
            if(OrderType()==OP_BUY)
            { 
                  double stnewprice = OrderOpenPrice();
               if(stnewprice< stnewprice+20*MyPoint)
               {
                  modify = OrderModify(OrderTicket(),OrderOpenPrice(),stnewprice+20*MyPoint,0,0,clrLightGreen);
                  
               }
               
            }         
         }
      
     }  
     
     
     if(modify==1) int close = OrderClose(ticketbuy2,Lots,Ask,0,0);
     
   }

 THANKS FOR HELPING ME

 
int modify;

You should get into the habit of declaring variables correctly. modify is used as a bool, so should be declared as a bool.

Also, I don't see that it is necessary to declare it Globally as once it is set to true, it will remain true during subsequent ticks. This could result in trades being closed as soon as they are opened.

      ticket   =    OrderSend(Symbol(),OP_BUY,Lots,Ask,0,0,0,NULL,MagicNumber,0,Blue);
      ticketbuy = OrderTicket();
      
      
      ticket = OrderSend(Symbol(),OP_BUY,Lots,Ask,0,0,0,NULL,MagicNumber,0,Red);
      ticketbuy2 = OrderTicket();

No order has been selected, so OrderTicket() should not be used here.

      ticket   =    OrderSend(Symbol(),OP_BUY,Lots,Ask,0,0,0,NULL,MagicNumber,0,Blue);
      if(ticket>0)
        ticketbuy = ticket;
      
      
      ticket = OrderSend(Symbol(),OP_BUY,Lots,Ask,0,0,0,NULL,MagicNumber,0,Red);
      if(ticket>0)
         ticketbuy2 = ticket;   
     if(modify==1) int close = OrderClose(ticketbuy2,Lots,Ask,0,0);

A Buy is closed at Bid, not Ask

Reason: