JimsCloseOrders
Download scriptThis script is designed to manage and automate the closing of open trading positions within the MetaTrader 4 platform. It acts as a utility tool that allows users to quickly exit their current trades based on specific conditions set by the user.
How the Logic Works
The script operates by scanning your active trading account and evaluating your open orders against three primary modes of operation. You can choose only one of these actions at a time:
1. Universal Close (All Orders) When this mode is active, the script identifies every open trade in your account and attempts to close them immediately. It does not distinguish between winning or losing positions; it simply flushes all active trades out of the market.
2. Profit-Based Closure This mode allows for a more selective exit strategy. The script scans your active trades but will only execute a closure if the position is currently in a "plus" state (meaning the trade is profitable). This is useful for "taking profits" across multiple positions simultaneously.
3. Loss-Based Closure Conversely, this mode acts as a safety mechanism or a "stop-loss" aggregator. It scans your active trades and only closes those that are currently in a "minus" state (meaning the trade is currently losing money).
Key Technical Behaviors
- Order Selection: The script intelligently identifies different types of trades (such as Buy or Sell orders) to ensure it handles each correctly during the closing process.
- Real-Time Market Data: Before attempting to close any position, the script refreshes its connection to the market data. This ensures that the price it uses to execute the trade is current and accurate.
- Error Reporting: If a trade fails to close for any reason (such as a sudden lack of liquidity or a connection issue), the script will generate a specific error message in the platform's log. This helps you identify exactly why a position remained open.
- Precision Handling: The logic accounts for "slippage," which is the difference between the price you request and the price at which the trade actually executes, helping to ensure smoother transitions during high-volatility moments.
//+------------------------------------------------------------------+
//| JMCloseOrders.mq4|
//| Jedimedic77@gmail.com|
//| Origional Coding Before Chop Copyright © 2007, sx ted|
//| |
//| Several Functions were removed from the origonal to speed it up |
//| and K.I.S.S. its my first attempt at leaening MQL4 good luck guys|
//| Jim Malwitz |
//+------------------------------------------------------------------+
#property show_inputs
//+------------------------------------------------------------------+
//| EX4 imports |
//+------------------------------------------------------------------+
#include <stdlib.mqh>
//+------------------------------------------------------------------+
//| input parameters: |
//+-----------0---+----1----+----2----+----3]------------------------+
extern string _ = "Select One of the Following:";
extern bool CloseOpenOrders = true;
extern bool CloseOrdersWithPlusProfit = false;
extern bool CloseOrdersWithMinusProfit = false;
//+------------------------------------------------------------------+
//| global variables to program: |
//+------------------------------------------------------------------+
double Price[2];
int giSlippage;
//+------------------------------------------------------------------+
//| script program start function |
//+------------------------------------------------------------------+
void start() {
int iOrders=OrdersTotal()-1, i;
if(CloseOpenOrders) {
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)) Print(OrderError());
}
}
else if(CloseOrdersWithPlusProfit) {
for(i=iOrders; i>=0; i--) {
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES) && (OrderProfit() >= 0)) {
if((OrderType()<=OP_SELL) && GetMarketInfo()) {
if(!OrderClose(OrderTicket(),OrderLots(),Price[1-OrderType()],giSlippage)) Print(OrderError());
}
}
}
}
else if(CloseOrdersWithMinusProfit) {
for(i=iOrders; i>=0; i--) {
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES) && (OrderProfit() <= 0)) {
if((OrderType()<=OP_SELL) && GetMarketInfo()) {
if(!OrderClose(OrderTicket(),OrderLots(),Price[1-OrderType()],giSlippage)) Print(OrderError());
}
}
}
}
}
//+------------------------------------------------------------------+
//| Function..: OrderError |
//+------------------------------------------------------------------+
string OrderError() {
int iError=GetLastError();
return(StringConcatenate("Order:",OrderTicket()," GetLastError()=",iError," ",ErrorDescription(iError)));
}
//+------------------------------------------------------------------+
//| Function..: GetMarketInfo |
//+------------------------------------------------------------------+
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);
}
//+------------------------------------------------------------------+
Comments