Broken CLOSE ALL function

 

This is what I am using to close all of my orders at once. There is a chance I will have pending BUYSTOP, SELLSTOP, or open OP_BUY, OP_SELL. I want this function to close them at one time.

I do get orderclose errors. Is there something wrong?

//+------------------------------------------------------------------+
// Close All FUNCTION
//+------------------------------------------------------------------+
void CloseAll(){
for (int i=0; i<OrdersTotal(); i++) {
if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) {
if (OrderSymbol()==Symbol() && OrderMagicNumber()==Symbol.magic) {
if (OrderType()==OP_BUYSTOP) {
OrderClose(OrderTicket(), OrderLots(), Bid, Slippage, Blue);
}
if (OrderType()==OP_SELLSTOP) {
OrderClose(OrderTicket(), OrderLots(), Ask, Slippage, Red);
}
if (OrderType()==OP_BUY) {
OrderClose(OrderTicket(), OrderLots(), Bid, Slippage, Blue);
}
if (OrderType()==OP_SELL) {
OrderClose(OrderTicket(), OrderLots(), Ask, Slippage, Red);
}
}
}
Print("Closed all orders. Current open:" + OrdersTotal());
}

 

Loop the other direction

for(i = OrdersTotal()-1; i>=0; i--){

 

Plenty wrong with this script............

Use this line :-

for (int i=OrdersTotal()-1; i>=0;i--) { 
Instead of :-
for (int i=0; i<OrdersTotal(); i++) { 

Also a BUY_STOP needs to be deleted, not closed, so use OrderDelete for these ones.

//+------------------------------------------------------------------+
// Close All FUNCTION
//+------------------------------------------------------------------+
void CloseAll(){
   for (int i=OrdersTotal()-1; i>=0;i--) { 
      if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) { 
         if (OrderSymbol()==Symbol() && OrderMagicNumber()==Symbol.magic) { 
            if (OrderType()==OP_BUYSTOP||OP_SELLSTOP) OrderDelete(OrderTicket(), CLR_NONE);
            if (OrderType()==OP_BUY||OP_SELL) OrderClose(OrderTicket(), OrderLots(), OrderClosePrice(), Slippage, Blue); 
         }
      } 
   } 
   Print("Closed all orders. Current open:" + OrdersTotal());
}
 
kennyhubbard:

Plenty wrong with this script............

Use this line :-

Instead of :-

Also a BUY_STOP needs to be deleted, not closed, so use OrderDelete for these ones.


Thanks- my first time needing to close an order outside of s/l or target profit.

Appreciate the help

Reason: