The approach to modify a position in MQL4 and MQL5 is similar. However, the code structure might differ slightly. MQL4 uses the function OrderModify() while MQL5 uses the function PositionModify() from the CTrade library to modify active trade or market trade. MQL5, unlike MQL4, distinguishes between a pending order and a market order. Pending orders are considered orders and market orders as positions in MQL5. MQL5 uses the CTrade class that belongs to a standard library provided by MetaTrader 5. CTrade contains trade functions that help developers to easily access trade manipulation, such as in this case modifying a trade, deleting an order, or even closing a position. CTrade requires it to be declared on the top of your code to enable access to its functions.
MQL5
Declare CTrade to get access to the trade functions.
Line 10: In the top of your MQL code, declare the CTrade library. #include <Trade\Trade.mqh>
Fig.107 Declaration of CTrade library
Note
- To modify an active trade, use the function OrderModify() and PositionModify() in MQL4 and MQL5 respectively.
- To modify a pending order, which can be a buy stop, buy limit, sell stop and sell limit, use the function OrderModify().
Note that MQL5 has clearly defined the difference between an order and a position, unlike MQL4, and this is reflected in the way classes and libraries are organized in MQL5.
- Orders are pending orders that have not yet been filled.
- Positions are active trades on the market.
MQL4 does not differentiate for modifying a pending order or an active trade. Hence, the function OrderModify() is applicable for both.
What is the sequence to modify a pending order or an active trade?
MQL4
- Line 127: Declare the variable ticket_id to store the ticket id of the selected trade.
- Line 129: The loop statement will iterate for each trade in the list of orders and increment variable i, which counts each of the orders until reaching the total number of orders from OrdersTotal().
- Line 131: If-statement is checking whether an order is selected with the function OrderSelect(). Note that OrderSelect() is selected by the position of the order in the list as per the parameter SELECT_BY_POS.
- Line 133: The variable ticket_id is getting the value of the selected ticket from the function OrderTicket().
- Line 135: Modify the order by providing required parameters to specify new values. Note that ticket_id is provided to determine which order is to be amended by the function OrderModify(). As MQL4 does not make the distinction between a pending order or an active or market order, the function OrderModify() can be used to modify both types of entries.
Fig.108 Sequence of modification of an active trade or of pending order in MQL4
MQL5
- Line 99 – 100: Declare the CTrade library at the top of your code to enable the access to the “trade” library and functions.
- Line 104: Declare ticket_id which will be used to store the ticket id of the order or position during the iteration.
- Line 106: For loop to iterate the list of active trades (market orders). The variable i represents the total number of positions in the list as i is equal to the total number of positions in the list specified by the function PositionsTotal() -1. The minus one is necessary as the index of positions starts from 0.
- Line 109: ticket_id gets the value of the selected position attributed during the iteration. The ticket id is collected using the function PositionGetTicket() with i, which represents the position index.
- Line 111: Call the function PositionModify() to modify trade with the selected ticket_id.
Fig.109 Sequence of modification of an active trade in MQL5
MQL5
- Line 118 – 119: Declare the CTrade library at the top of your code to access the trade library and functions.
- Line 122: Declare the variable ticket_id that will store the selected order id.
- Line 124: for loop will iterate each order from the list of orders. The variable i is equal to the total number of orders minus 1. We are subtracting one as indexes start at zero. Note that i– means that i is decremented (i = i -1).
- Line 127: the variable ticket_id collects the ticket id from the selected order with the function OrderGetTicket().
- Line 129: OrderModify() will modify the selected order as per ticket_id and provide parameters as value to adjust.
Fig.110 Sequence of modification of pending order in MQL5
Case study: How to select for a pending order or an active trade based on a condition and modify its attribute?
MQL4
Case study: Trailing stop: Set a new stop loss once EUR/USD trades are reaching a target profit of 100 pips. The new stop loss will be the trade entry price plus 50 pips.
- Line 127: Declare variable ticket_id. This variable will be used to store the ticket id during the iteration in the list of orders.
- Line 129: for statement: Iterate each order from the list of orders. OrdersTotal() function returns the number of pending orders and market orders. The variable i is incremented by one for each iteration.
- Line 131: The function OrderSelect() points to the order specified by variable i. Note that the order position is i – 1, as the list starts from zero.
- Line 133: ticket_id variable gathers the ticket id of the selected order.
- Line 135: IF condition to select trades with P/L higher than 100 pips and only EUR/USD.
- Line 136: If the condition is met, set the new stop loss as the selected order’s entry price plus 50 pips.
- Line 137: Modify the order with the function OrderModify() with the new stop loss value.
Fig.111 Select pending order and market order or active trade in MQL4
MQL5
Case study: If the price of EUR/USD is reaching a certain price threshold, in our example 1.40, modify every EUR/USD pending order with a new entry price set at a specific price, 1.35 as per our example.
- Line 118 – 119: Declare the CTrade library to access trade libraries and functions. CTrade must be declared on top of your code.
- Line 122: Declare variable ticket_id to store the ticket Id of each iterated order.
- Line 129: for statement: Iterate each order from the list of pending orders. OrdersTotal() is a function that returns the total number of orders. Note that the order index starts with 0, which is the reason why the variable i is equal to the total of orders minus one.
- Line 127: ticket_id collects the selected order’s ticket id with the function OrderGetTicket().
- Line 129: IF condition to filter the modification operation to EUR/USD orders only and only in case EUR/USD price reaches a specific threshold, in our example 1.40.
- Line 130: Set the entry price value, in our example 1.35.
- Line 131: Modify the selected order and assign the entry price new value.
Fig.112 Select pending order in MQL5
MQL5
Case study: Trailing stop using the 20-period moving average. If the trade position is a buy and the current market price is above the 20-period moving average, set the stop loss to the value of the moving average.
- Line 99 – 100: Declare the CTrade library to access all the trade functionalities at the top of your code file.
- Line 104: Declare the variable ticket_id that will be used to store the ticket id for each iterated position.
- Line 106: For-statement. Iterate each order from the list of orders. PositionsTotal() function returns the number of positions (market orders / active trades). Note that the variable i is decremented by one for each iteration. The loop is counting down from the total number of positions minus one (PositionsTotal() -1). We subtract one to the total of the position because position’s index starts at zero.
- Line 109: attribution to variable ticket_id of the selected position’s ticket id with the function PositionGetTicket().
- Line 110: IF-condition to filter only “buy” trades and only if the market price is above the 20-period moving average.
- Line 112: set the variable stop loss (sl) to the value of the 20-period moving average (MA20).
- Line 114: Modify the selected position and assign the new stop loss.
Fig.113 Select an active trade in MQL5
Coding tips:
To avoid any mistake selecting the wrong trade in the case you have many trades running in parallel, perform a loop statement to iterate each order and capture position/order ticket id with functions OrderSelect() and OrderTicket() in MQL4 and functions PositionSelect() and PositionGetTicket() in MQL5. Despite OrderModify() and PositionModify() allowing the use of a symbol to select and modify a trade, the use of ticket id is a more reliable method to select a trade or an order as it is a unique identification number. Therefore, the ticket id selection is the recommended approach.