Table of Contents
To achieve the scenario I’ve described—where each open position has its associated pending order acting as a hedge and where the pending order is automatically deleted if the position hits the Take Profit (TP)—you’ll need to manage this process programmatically in the trading platform (such as MetaTrader 4/5). You can use MQL4 or MQL5 scripting to track and control each position and its corresponding pending order.
Concept Breakdown:
- Multiple Open Positions: You open two or more positions, each with its own TP and associated pending order.
- Pending Order Instead of Stop Loss (SL): Instead of placing a traditional SL for each position, you place a pending order (like a sell limit or buy stop) that acts as a hedge.
- Auto-Delete Pending Orders: If position X hits its TP, the pending order associated with that position should be deleted automatically. However, the pending order for position Y remains until Y either hits its TP or triggers the pending order.
Example:
Let’s assume the following scenario with two open positions:
- Position X:
- Buy 1 lot at 1.1000, TP at 1.1100.
- Pending order: Sell stop at 1.0950 (hedge in case price moves against position X).
- Position Y:
- Buy 1 lot at 1.1200, TP at 1.1300.
- Pending order: Sell stop at 1.1150 (hedge in case price moves against position Y).
Requirements for the Script:
- Tracking the Positions: Each position needs to be tracked, and each should have an associated pending order for hedging.
- Conditional Logic for TP: The associated pending order is deleted when a position hits its TP.
- Managing Active Pending Orders: The pending order for the other position (Y) remains until it’s either triggered or Y hits its TP.
Pseudo Code Example in MQL4/MQL5:
void OnTick()
{
// Check all open positions
for (int i = 0; i < OrdersTotal(); i++)
{
if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
{
// Get order information
int ticket = OrderTicket();
double openPrice = OrderOpenPrice();
double tp = OrderTakeProfit();
double currentPrice = MarketInfo(OrderSymbol(), MODE_BID);
// Assume each position has a corresponding pending order stored in a global variable
int hedgePendingOrderTicket = GetHedgePendingOrderTicket(ticket);
// If the position has hit its TP, close the pending hedge order
if (currentPrice >= tp && OrderType() == OP_BUY)
{
if (OrderSelect(hedgePendingOrderTicket, SELECT_BY_TICKET, MODE_TRADES))
{
OrderDelete(hedgePendingOrderTicket);
Print("Pending order deleted for position ticket: ", ticket);
}
}
}
}
}
// Function to get the pending order ticket associated with a specific open position
int GetHedgePendingOrderTicket(int positionTicket)
{
// This function should return the pending order ticket that matches the open position
// You can store the mapping between position tickets and pending order tickets using global variables or external files.
}
Key Points:
- Tracking Pending Orders: You must map each open position to its pending order. One way is to store the position ticket number and the corresponding pending order ticket number in a global variable or file.
- Automatic Deletion: In the script, as soon as a position hits its TP, the
OrderDelete()
the function is triggered to remove the corresponding pending order. - Independence of Positions: Each position’s TP and hedge (pending order) work independently. If position X hits its TP, its pending order is deleted, but position Y and its pending order continue functioning.
Example Flow:
- Step 1: You open two buy positions (Position X at 1.1000 and Position Y at 1.1200).
- Step 2: You set a TP for each position (Position X: TP at 1.1100, Position Y: TP at 1.1300).
- Step 3: Instead of SL, you place a hedge (pending sell stop order) for each position (Position X: Sell stop at 1.0950, Position Y: Sell stop at 1.1150).
- Step 4: If Position X hits its TP at 1.1100, the pending sell-stop order (1.0950) is automatically deleted.
- Step 5: Position Y remains active, and its pending order (1.1150) remains valid until it hits the TP at 1.1300 or the pending order is triggered.
Better Solution For Hedging Positions!
Conclusion:
This approach can be implemented by writing custom MQL4 or MQL5 code to handle multiple positions and their pending orders. The pending orders are deleted automatically upon hitting the TP. The core idea revolves around tracking each position and its associated pending orders and managing their lifecycle through logic embedded in an Expert Advisor (EA).