close all pending orders

 

I am trying to close all pending orders but it is closing only half of it

here is the code i am using

if(OrdersTotal()>0){

for (int i=1; i<=OrdersTotal(); i++) //Cycle for all orders..
{
if(OrderSelect(i-1,SELECT_BY_POS)==true)//If there is the next one
{
if(OrderType()>1) //pending orders
OrderDelete(OrderTicket());
}
}
}

}


Thank You

 
You have to loop from reverse so int i = OrdersTotal - 1, to 0, else every time you delete one from beginning, the queue shifts down by 1, but u always delete the next one, that's why you delete only half of them. Code:
#include <stdlib.mqh>
extern bool removeFromThisChartOnly = true;

int start(){
   
   RemoveAllOrders();
   
   return(0);
}
//+------------------------------------------------------------------+




void RemoveAllOrders(){
   for(int i = OrdersTotal() - 1; i >= 0 ; i--){
      OrderSelect(i,SELECT_BY_POS);
      if(OrderSymbol() != Symbol() && removeFromThisChartOnly) continue;
      double price = MarketInfo(OrderSymbol(),MODE_ASK);
      if(OrderType() == OP_BUY) price = MarketInfo(OrderSymbol(),MODE_BID);
      if(OrderType() == OP_BUY || OrderType() == OP_SELL){
         OrderClose(OrderTicket(), OrderLots(),price,5);
      }else{
         OrderDelete(OrderTicket());
      }
      Sleep(100);
      int error = GetLastError();
      if(error > 0)
         Print("Unanticipated error: ", ErrorDescription(error));
      RefreshRates();
   }
}
This also deletes active positions, so modify accordingly.
 
forexCoder:
You have to loop from reverse so int i = OrdersTotal - 1, to 0, else every time you delete one from beginning, the queue shifts down by 1, but u always delete the next one, that's why you delete only half of them. Code: This also deletes active positions, so modify accordingly.


Thanx for you quick response


got one more question

let say i have eurusd price 1.46/1.465 how do i add 20 pips to make it 1.462/1.48

i guess we have use NormalizeDouble but don't know to use it in this situation

 
I don't understand what you're asking. Do you mean when your open position is at that price, when your pending order is at that price or when your simple double variable has that price stored and you would like to modify it?
 
rfbudhwani:
let say i have eurusd price 1.46/1.465 how do i add 20 pips to make it 1.462/1.48
i guess we have use NormalizeDouble but don't know to use it in this situation

In my experience, it is never necessary to use NormalizeDouble

As long as you adjust for 4/5 digit brokers it's easy

//++++ These are adjusted for 5 digit brokers.
int     pips2points;    // slippage  3 pips    3=points    30=points
double  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
}
===========
double priceA = 1.465;                      // 1.4650
double priceA20 = priceA + 20 *pips2dbl;    // 1.4670
double priceB = 1.46;                       // 1.4600
double priceB20 = priceB + 20 *pips2dbl;    // 1.4620
double priceC = 1.46123;                    // 1.46123
double priceC20 = priceC + 20 *pips2dbl;    // 1.46323
 

Yes what WHR said.

If you're just trying to add values, for example you have a current Bid price and you want to create TP and SL prices, you just add the amount of pips to them (or subtract). Usually I dont even bother with points anymore or decimals, I just have the variable stored in the correct double amount ( if it's static), if it's dynamic (so you algorithmicly caclulate what your sl and tp levels should be), you don't have to do it anyway, cause math takes care of that.

Reason: