Scripts: Close Orders

 

Close Orders:

Close all multi currency open orders and cancel pending orders according to various scenarios. Slippage for open orders is calculated for each currency. Choose only one scenario per visit, the script processes the first menu item selected.

Author: Edward Hirsch

 

you have good style of programming, thanks for sharing, i like to know your idea about adding some error handling code to this script, there is a good resourse for getting some idea in http://www.forexmt4.com/_MT4_Includes/OrderReliable_V0_2_5.mqh in OrderCloseReliable Function.

 
Thank you Rasoul for the compliment and of the idea of adding error handling code is very good. Matthew Kennel certainly has been busy. Version 2 of _CloseOrders will #include a library of functions with error handler.
 
sxTed,

Thank you for the Close Orders script. I have been using it for awhile, especially to close multiple open orders and I really appreciate its features.

Would it be possible to add another feature that would allow the script to 'remember' the previous settings? Many times with a fast moving market I want to be able to quickly open your script and always use the same settings, ie: CloseOpenOrders: true. But I am slowed down by the need to make the same multiple mouse moves and clicks just to do the same thing each time.

Perhaps it would be possible for there to be a setting such as "Keep these settings".

Hoping so anyway.
Rich
 
Rich, You could use the "Load" and "Save" (from an Expert Set file) options under the "Inputs" tab, but I think the programmers of MT4 included them to modify many parameters at the same time, whereas in your case for loading the value of just one parameter will require extra key strokes. Alternatively copy just the code required for the "CloseOpenOrders" option to run into a new script.
 
sxTed:
Rich, You could use the "Load" and "Save" (from an Expert Set file) options under the "Inputs" tab, but I think the programmers of MT4 included them to modify many parameters at the same time, whereas in your case for loading the value of just one parameter will require extra key strokes. Alternatively copy just the code required for the "CloseOpenOrders" option to run into a new script.
I realize that your reply probably makes imminent sense but to one who is lacking any software coding skills, it reads like Sanskrit. Is it possible to speak slower using real short words for this programming-challenged seeker of Forex trading tools?

I'm new to the Meta Trader platform and not at all conversant at the MQL4 language. Heck, I was proud of myself for figuring that there might be a way to go beyond the basic tool set in Meta Trader 4 and then finding this website and then finding your script and then figuring out how to incorporate that script into my trading platform and then getting it work with absolutely no instructions. Meta Trader's help files and support really suck if you don't already know how to do something.

Hey, and thanks for your programming skills, willingness to share and quick reply. Whew - my brain hurts. Rich
 

Rich, Deleting the line with "#property show_confirm" in the following script will make it faster but be very careful as you will not be reminded to confirm. Take care.

//+------------------------------------------------------------------+
//|                                             _CloseOpenOrders.mq4 |
//|                                         Copyright © 2007, sx ted |
//| Purpose: Close all multi currency open orders for Rich.          |
//|          Slippage is calculated for each currency.               |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2007, sx ted"
#property show_confirm // Rich remove this line to make it faster but be careful to select the right script
 
//+------------------------------------------------------------------+
//| EX4 imports                                                      |
//+------------------------------------------------------------------+
#include <stdlib.mqh>
 
//+------------------------------------------------------------------+
//| global variables to program:                                     |
//+------------------------------------------------------------------+
double Price[2];
int    giSlippage;
 
//+------------------------------------------------------------------+
//| script program start function                                    |
//+------------------------------------------------------------------+
void start() {
  int iOrders=OrdersTotal()-1, i;
  
  for(i=iOrders; i>=0; i--) {
    if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES) && (OrderType()<=OP_SELL) && GetMarketInfo() && !OrderClose(OrderTicket(),OrderLots(),Price[1-OrderType()],giSlippage)) OrderError();
  }
}
 
//+------------------------------------------------------------------+
//| Function..: OrderError                                           |
//+------------------------------------------------------------------+
void OrderError() {
  int iError=GetLastError();
  Print("Order:",OrderTicket()," GetLastError()=",iError," ",ErrorDescription(iError));
}
 
//+------------------------------------------------------------------+
//| Function..: GetMarketInfo                                        |
//| Returns...: bool Success.                                        |
//+------------------------------------------------------------------+
bool GetMarketInfo() {
  RefreshRates();
  Price[0]=MarketInfo(OrderSymbol(),MODE_ASK);
  Price[1]=MarketInfo(OrderSymbol(),MODE_BID);
  double dPoint=MarketInfo(OrderSymbol(),MODE_POINT);
  if(dPoint==0) return(false);
  giSlippage=(Price[0]-Price[1])/dPoint;
  return(Price[0]>0.0 && Price[1]>0.0);
}
//+------------------------------------------------------------------+
 

Will this script close all orders in only one Tick, or will it take 1 tick per order?

Thank you

Mico Yoshic

 
Mico, Execution of scripts does not depend on incoming quotes. Scripts complete all their work independently in one go except if there is an error (refer to "Program Run" in the dictionary on various methods to stop it). Take care.
 
Hallo, I've got question, because I don't know MQL4. I need close all orders (working and pending) when loss ora equity has (-5000 ex.). How do it?
 
//+------------------------------------------------------------------+
//|                              CloseOrdersLessThanStatedEquity.mq4 |
//|                                          Copyright © 2008, sxTed |
//|                                               sxted@talktalk.net |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2008, sxTed"
#property link      "sxted@talktalk.net"
 
//+------------------------------------------------------------------+
//| EX4 imports                                                      |
//+------------------------------------------------------------------+
#include <stdlib.mqh>
 
//+------------------------------------------------------------------+
//| input parameters:                                                |
//+-----------0---+----1----+----2----+----3]------------------------+
extern double CloseOrdersWhenEquityLessThan = 0.0;
 
//+------------------------------------------------------------------+
//| global variables to program:                                     |
//+------------------------------------------------------------------+
double Price[2];
int    giSlippage;
 
//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init() {
  return(0);
}
 
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit() {
  return(0);
}
 
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start() {
  int iOrders=OrdersTotal(), i;
  if(AccountEquity() < CloseOrdersWhenEquityLessThan && iOrders > 0) {
    for(i=iOrders-1; i>=0; i--) {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)) {
        if((OrderType()<=OP_SELL) && GetMarketInfo()) {
          if(!OrderClose(OrderTicket(),OrderLots(),Price[1-OrderType()],giSlippage)) Print(OrderError());
        }
        else if(OrderType()>OP_SELL) {
          if(!OrderDelete(OrderTicket())) Print(OrderError());
        }
      }
    }
    Alert("Equity low - all open orders closed and pending orders cancelled");
  }
  return(0);
}
 
//+------------------------------------------------------------------+
//| Function..: OrderError                                           |
//+------------------------------------------------------------------+
string OrderError() {
  int iError=GetLastError();
  return(StringConcatenate("Order:",OrderTicket()," GetLastError()=",iError," ",ErrorDescription(iError)));
}
 
//+------------------------------------------------------------------+
//| Function..: GetMarketInfo                                        |
//| Returns...: bool Success.                                        |
//+------------------------------------------------------------------+
bool GetMarketInfo() {
  RefreshRates();
  Price[0]=MarketInfo(OrderSymbol(),MODE_ASK);
  Price[1]=MarketInfo(OrderSymbol(),MODE_BID);
  double dPoint=MarketInfo(OrderSymbol(),MODE_POINT);
  if(dPoint==0) return(false);
  giSlippage=(Price[0]-Price[1])/dPoint;
  return(Price[0]>0.0 && Price[1]>0.0);
}
//+------------------------------------------------------------------+
he74, please try the above expert.