Closing all opened orders script !HELP!

 

Hello,

I have tried different scripts from different forums and still can't get one to work or do what I want.

I would like a script that:

-Close all the opened orders even if the orders are on different charts ----> all at once!

Thank you for your time,

Vatco

 
vatco:

I have tried different scripts from different forums and still can't get one to work or do what I want.

rly? which ones?

//+------------------------------------------------------------------+
//| script program start function                                    |
//+------------------------------------------------------------------+
#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();
   }
}
 
forexCoder:

rly? which ones?


I have tried this one, I read your other thread! but nothing happens! Im trying it on my trial account with 3 orders ...

I know the code is doing something because when I do an other action, it asks me if I'm sure I want to stop the script.

(Isn't your script for one chart only?)

Many thanks,

Vatco

 
vatco:
I have tried this one, I read your other thread! but nothing happens! Im trying it on my trial account with 3 orders ...
(Isn't your script for one chart only?)
  1. // Sleep(100);
    int error = GetLastError();
    The sleep clears the error (and is unnecessary.)
  2. Only if you set = true
    extern bool removeFromThisChartOnly = true;
 
Ahhhhhh didnt know that one, thanks!!!
 
////////////////////
////////////////////
////KILL'EM ALL/////
////////////////////
////////////////////
int start(){
int GET_ME_THEM = OrdersTotal();
for(int YES=GET_ME_THEM-1;YES>=0;YES--){
OrderSelect(YES, SELECT_BY_POS);
int WHAT_2_KILL = OrderType(); double KILL_LONG; double KILL_SHORT;
switch(WHAT_2_KILL){
case OP_BUY
: KILL_LONG = OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_BID), 100, Blue ); break;
case OP_SELL
: KILL_SHORT = OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_ASK), 100, Blue ); 
}}
return(0);
}

 

after short testing i found that it may stop unexpectedly.... ad this bit to it /otherwise tested on 50+ opened pos /no problem just keep killin 'em. ;)

while(IsTradeContextBusy()) Sleep (10);
 

Hmz this just got me thinking.

If you use OrderClosePrice() instead of Bid /Ask, does it work on all symbols or jsut current chart?

EDIT: Also another one: do extern vars not work in scripts? arg :p

EDIT2: Just tried it myself, it works with OrderClosePrice() for all positions and symbols. The script is now rewritten.

#include <stdlib.mqh>
extern bool removeFromThisChartOnly = false;

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(),OrderClosePrice()/*price*/,5);
      }else{
         OrderDelete(OrderTicket());
      }
      
      int error = GetLastError();
      if(error > 0)
         Print("Unanticipated error: ", ErrorDescription(error));
      Sleep(100);
      RefreshRates();
   }
}

Pending final deletion at next code revision.

I will also create another copy of this script to have one to delete all positions and orders anywhere and one for just the current symbol.

 

#include the common_functions.mqh and then the entire problem will be reduced to a one-liner: closeOpenOrders(-1, -1);

 

Thanks for the fast response!!

Unfortunately, nothing works.

I have tried:

-Forexcoder: Removed Sleep(100), removeFromThisChartOnly=false, Tried Edit2 and #include the common .mqh fonctions...


-nicwaznego: Tried the code and added the while...

The codes were compiling with 0 errors and 0 warnings, but nothing happens, no error message, nothing...

Thank you for your help,

Vatco

 
vatco:

Thanks for the fast response!!

Unfortunately, nothing works.

I have tried:

-Forexcoder: Removed Sleep(100), removeFromThisChartOnly=false, Tried Edit2 and #include the common .mqh fonctions...


-nicwaznego: Tried the code and added the while...

The codes were compiling with 0 errors and 0 warnings, but nothing happens, no error message, nothing...

Thank you for your help,

Vatco



it mean.... no one can help you there must be something else messing up.... this code it to simple to fail.... ive tried again 10+ 50+ 90+ opened orders... random direction.... 4 demo brokers .... just keep shooting em down.
Reason: