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
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

  1. Line 127: Declare the variable ticket_id to store the ticket id of the selected trade.
  2. 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().
  3. 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.
  4. Line 133: The variable ticket_id is getting the value of the selected ticket from the function OrderTicket().
  5. 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
Fig.108 Sequence of modification of an active trade or of pending order in MQL4

MQL5

  1. Line 99 – 100: Declare the CTrade library at the top of your code to enable the access to the “trade” library and functions.
  2. Line 104: Declare ticket_id which will be used to store the ticket id of the order or position during the iteration.
  3. 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.
  4. 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.
  5. 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
Fig.109 Sequence of modification of an active trade in MQL5

MQL5

  1. Line 118 – 119: Declare the CTrade library at the top of your code to access the trade library and functions.
  2. Line 122: Declare the variable ticket_id that will store the selected order id.
  3. 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).
  4. Line 127: the variable ticket_id collects the ticket id from the selected order with the function OrderGetTicket().
  5. 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
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.

  1. Line 127: Declare variable ticket_id. This variable will be used to store the ticket id during the iteration in the list of orders.
  2. 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.
  3. 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.
  4. Line 133: ticket_id variable gathers the ticket id of the selected order.
  5. Line 135: IF condition to select trades with P/L higher than 100 pips and only EUR/USD.
  6. Line 136: If the condition is met, set the new stop loss as the selected order’s entry price plus 50 pips.
  7. 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
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.

  1. Line 118 – 119: Declare the CTrade library to access trade libraries and functions. CTrade must be declared on top of your code.
  2. Line 122: Declare variable ticket_id to store the ticket Id of each iterated order.
  3. 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.
  4. Line 127: ticket_id collects the selected order’s ticket id with the function OrderGetTicket().
  5. 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.
  6. Line 130: Set the entry price value, in our example 1.35.
  7. Line 131: Modify the selected order and assign the entry price new value.

Fig.112 Select pending order in MQL5
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.

  1. Line 99 – 100: Declare the CTrade library to access all the trade functionalities at the top of your code file.
  2. Line 104: Declare the variable ticket_id that will be used to store the ticket id for each iterated position.
  3. 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.
  4. Line 109: attribution to variable ticket_id of the selected position’s ticket id with the function PositionGetTicket().
  5. Line 110: IF-condition to filter only “buy” trades and only if the market price is above the 20-period moving average.
  6. Line 112: set the variable stop loss (sl) to the value of the 20-period moving average (MA20).
  7. Line 114: Modify the selected position and assign the new stop loss.

Fig.113 Select an active trade in MQL5
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.

Leave a Reply

6  +  4  =