Closing Buy Stop and Sell Stop

 
Do you use the OrderClose() function to close buy stops and sell stops ?  if not is there another way or function to close buy stops and sell stops.
 
On MT4: You can only close open orders. You delete pending orders.
 
Hi, you can use order valid and delete non executed orders. Regards Greg
 
int Try=10;

int f_CloseAll()
{
   for (int ii = OrdersTotal(); ii>=0; ii--)
   {
      res=OrderSelect(ii, SELECT_BY_POS, MODE_TRADES);
      if(OrderMagicNumber()==Magic && OrderSymbol()==Symbol())
      {
         if(OrderType() == OP_BUY)
         {
            int k=0;
            res=false;
            while(!res && k<Try)
            {
               res=OrderClose(OrderTicket(),OrderLots(),Bid,0,clrWhite);
               k++;
               Sleep(100);
            }
         }
         if(OrderType() == OP_SELL)
         {
            int k=0;
            res=false;
            while(!res && k<Try)
            {
               res=OrderClose(OrderTicket(),OrderLots(),Ask,0,clrWhite);
               k++;
               Sleep(100);
            }
         }
         if(OrderType() == OP_BUYLIMIT)
         {
            int k=0;
            res=false;
            while(!res && k<Try)
            {
               res=OrderDelete(OrderTicket());
               k++;
               Sleep(100);
            }
         }
         if(OrderType() == OP_SELLLIMIT)
         {
            int k=0;
            res=false;
            while(!res && k<Try)
            {
               res=OrderDelete(OrderTicket());
               k++;
               Sleep(100);
            }
         }
         if(OrderType() == OP_BUYSTOP)
         {
            int k=0;
            res=false;
            while(!res && k<Try)
            {
               res=OrderDelete(OrderTicket());
               k++;
               Sleep(100);
            }
         }
         if(OrderType() == OP_SELLSTOP)
         {
            int k=0;
            res=false;
            while(!res && k<Try)
            {
               res=OrderDelete(OrderTicket());
               k++;
               Sleep(100);
            }
         }
      }
   }
   return(0);
}

Use the type of Close/Delete you need.

Regards.

 
Clim Fandeev:

Use the type of Close/Delete you need.

Regards.

 if(OrderType() == OP_BUY)
         {
            int k=0;
            res=false;
            while(!res && k<Try)
            {
               RefreshRates();
               res=OrderClose(OrderTicket(),OrderLots(),Bid,0,clrWhite);
               k++;
               if(!res)
                 Sleep(100);
            }
         }

Always RefreshRates() to make sure that Bid/Ask is current.

Probably best to sleep only if the OrderClose() fails.

 
Got it thank you all for your help
 
This worked perfectly, I've been searching for similar thing. Thanks
 
  1. Why did you post your MT4 question in the Root / MT5 General section
  2. instead of the MQL4 section, (bottom of the Root page?)
              General rules and best pratices of the Forum. - General - MQL5 programming forum
    Next time post in the correct place. The moderators will likely move this thread there soon.

  3. You can use OrderClosePrice() instead of Bid/Ask and be direction independent — no need to check order type for close price. But if you potentially close multiple orders, you must call RefreshRates after the server call and before the next OrderSelect.

 

i m getting an error as followed doin so with the post...whts wrong..some one correct me


double CloseAllSellTrades(){

 

      for (int ii = OrdersTotal(); ii>=0; ii--)

      {

      int Try=10;

      res=OrderSelect(ii, SELECT_BY_POS, MODE_TRADES);

      if(OrderMagicNumber()==Magic && OrderSymbol()==Symbol())

      {

         if(OrderType() == OP_SELL)

         {

            int k=0;

            res=false;

            while(!res && k<Try)

            {

               res=OrderClose(OrderTicket(),OrderLots(),Ask,7,clrWhite);

               k++;

               Sleep(100);

            }

         }

      

      }

    }


}


 
Please edit your post and

use the code button (Alt+S) when pasting code

What error do you get?

Reason: