This script is designed as an automated management tool for the MetaTrader platform, specifically created to streamline the process of closing out specific trading positions. Instead of requiring you to manually find and shut down every individual trade, this script acts as a filter to identify and execute closures based on your predefined criteria.
How the Logic Works
The script functions by scanning through all currently open active trades in your account one by one. It performs a three-step verification process to ensure it only interacts with the specific positions you intend to manage:
1. Symbol Verification First, the script checks if the trade belongs to the specific currency pair or asset you are currently viewing on your screen. This ensures that it doesn't accidentally interfere with trades on other charts or instruments.
2. Identification via "Magic Numbers" The script looks for a unique identification tag known as a "Magic Number." In automated trading, these numbers are used to distinguish between different strategies or groups of trades. The script is configured to recognize three specific IDs (111, 222, and 333). If a trade's ID matches any of these three, the script proceeds to the next step; if it doesn't match, the script ignores that trade and moves on.
3. Execution of Closure Once a matching trade is identified, the script automatically determines the type of position (Buy or Sell) and executes the appropriate closing command:
- For Buy positions: It closes the trade at the current market price.
- For Sell positions: It closes the trade at the current market price.
- For Pending orders: If there are any "limit" or "stop" orders (orders waiting to be filled) that match your criteria, the script will remove them from the market entirely.
Summary for the User
Essentially, this tool serves as a "targeted exit" command. When you run it, the software scans your open list, finds every trade associated with your specific strategy IDs on your current chart, and closes them instantly. This is particularly useful for quickly clearing out specific sets of trades without affecting other active positions in your portfolio.
//+------------------------------------------------------------------+
//| close positions.mq4 |
//| Copyright © 2007, Serega Lykov |
//| http://mtexperts.narod.ru/ |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2007, Serega Lykov"
#property link "http://mtexperts.narod.ru/"
#property show_confirm
extern int MagicNumber_1=111;
extern int MagicNumber_2=222;
extern int MagicNumber_3=333;
//+------------------------------------------------------------------+
//| "close all positions with specific magicnumber at current symbol"|
//+------------------------------------------------------------------+
int start()
{
for(int i=0;i<OrdersTotal();i++)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break;
if(OrderSymbol()==Symbol())
{
int mn=OrderMagicNumber();
if(mn==MagicNumber_1 || mn==MagicNumber_2 || mn==MagicNumber_3)
{
int dir=OrderType();
if(dir==OP_BUY) OrderClose(OrderTicket(),OrderLots(),Bid,3,CLR_NONE);
if(dir==OP_SELL) OrderClose(OrderTicket(),OrderLots(),Ask,3,CLR_NONE);
if(dir>=2) OrderDelete(OrderTicket(),CLR_NONE);
}
}
}
return(0);
}
//+------------------------------------------------------------------+
Comments