OneOrderControl
Download scriptThis script is designed to manage and identify specific trading positions within the MetaTrader platform by locating a single active order that matches your predefined criteria. Its primary purpose is to act as a "lookup" tool, scanning through all currently open trades to find one that specifically belongs to your account's unique identifier (Magic Number) and the current asset you are viewing.
How the Logic Works
The core functionality of this script follows a systematic search process:
- Initialization and Reset: Every time the script begins its task, it first clears out any previous data. This ensures that information from an old trade doesn't accidentally interfere with the details of a new one.
- Comprehensive Scanning: The script looks at every single open order in your account, starting from the most recent and working backward to the oldest.
- Specific Filtering: While scanning, the script applies two strict filters to ensure it only identifies the correct trade:
- Identity Check: It checks if the "Magic Number" of the trade matches the unique ID you have assigned to your automated trading strategy.
- Asset Check: It confirms that the trade is currently active on the specific currency pair or market symbol you are currently looking at.
- Data Extraction: Once the script finds a single order that meets both of the above criteria, it stops searching immediately and "grabs" all the vital statistics associated with that trade. These include:
- Unique identification (Ticket number)
- Trade type (Buy or Sell)
- Volume (Lot size)
- Entry price, Stop Loss, and Take Profit levels
- Timing of the opening and expiration
- Financial details like current profit, swap fees, and commissions
- Any specific comments attached to the trade
Summary for the User
Think of this script as a specialized "Search and Retrieve" assistant. Instead of you having to manually find a specific trade in a long list of open positions, the script automatically hunts through your account, identifies the specific trade associated with your strategy on the current chart, and pulls all its relevant data into memory for further use.
//+------------------------------------------------------------------+
//| OneOrderControl.mq4 |
//| Copyright © 2006, komposter |
//| mailto:komposterius@mail.ru |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2006, komposter"
#property link "mailto:komposterius@mail.ru"
int _Ticket = 0, _Type = 0; double _Lots = 0.0, _OpenPrice = 0.0, _StopLoss = 0.0;
double _TakeProfit = 0.0; datetime _OpenTime = -1; double _Profit = 0.0, _Swap = 0.0;
double _Commission = 0.0; string _Comment = ""; datetime _Expiration = -1;
void OneOrderInit( int magic )
{
int _GetLastError, _OrdersTotal = OrdersTotal();
_Ticket = 0; _Type = 0; _Lots = 0.0; _OpenPrice = 0.0; _StopLoss = 0.0;
_TakeProfit = 0.0; _OpenTime = -1; _Profit = 0.0; _Swap = 0.0;
_Commission = 0.0; _Comment = ""; _Expiration = -1;
for ( int z = _OrdersTotal - 1; z >= 0; z -- )
{
if ( !OrderSelect( z, SELECT_BY_POS ) )
{
_GetLastError = GetLastError();
Print( "OrderSelect( ", z, ", SELECT_BY_POS ) - Error #", _GetLastError );
continue;
}
if ( OrderMagicNumber() == magic && OrderSymbol() == Symbol() )
{
_Ticket = OrderTicket();
_Type = OrderType();
_Lots = NormalizeDouble( OrderLots(), 1 );
_OpenPrice = NormalizeDouble( OrderOpenPrice(), Digits );
_StopLoss = NormalizeDouble( OrderStopLoss(), Digits );
_TakeProfit = NormalizeDouble( OrderTakeProfit(), Digits );
_OpenTime = OrderOpenTime();
_Profit = NormalizeDouble( OrderProfit(), 2 );
_Swap = NormalizeDouble( OrderSwap(), 2 );
_Commission = NormalizeDouble( OrderCommission(), 2 );
_Comment = OrderComment();
_Expiration = OrderExpiration();
return(0);
}
}
}
Comments