Modify Orders with MQL4 OrderModify

The MQL4 OrderModify function allows you to modify an order in Metatrader. In previous articles you learned how to open orders, close orders and scan the existing orders. As you should already know, orders can be also modified, for example for pending orders you may want to change the open price, or, for market orders you may want to change the stop loss or take profit price.

MQL4 OrderModify Function

Update an order is quite easy using MQL4, very similar to the use of the OrderClose()  function, to modify an order MQL4 has the OrderModify()  function. We will see some examples here of how to modify existing orders using MQL4 functions.

OrderModify MQL4 Parameters

We can see the details of this function The parameters are:
  • ticket to identify the ticket to modify
  • price is used to change the open price of a pending order
  • stoploss is the new stop loss price
  • takeprofit is the new take profit price
  • expiration is to change the expiration date/time for pending orders
  • arrow_color is to change the color of the “update” arrow on the chart, or to hide it
Like when you open a new order remember to consider that price, stop loss price and take profit price cannot be too close to the current price or the operation will fail.

Why Modifying An Order?

Why would you want to change an order? Here some examples:
  • Trailing stop is maybe one of the most popular reason, you move the stop loss price when the price moves in the direction of your order
  • Move the take profit price, usually done together with a trailing stop
  • change the expiration date of a pending order, to extend it or anticipate the validity
  • change the open price of a pending order in case of a different market condition

MQL4 OrderModify Example

In the example you are going to see we will set a stop loss and take profit price for each order of a currency pair with no set stop loss, the code is If we have some orders open in different pairs we can see that only the orders of the same currency pair of where the script is running are modified. metaeditor-mql4-ordermodify-modifyorder-1 When the script is run it shows metaeditor-mql4-ordermodify-modifyorder-1a If we run it in the EURUSD chart the result is metaeditor-mql4-ordermodify-modifyorder-2 metaeditor-mql4-ordermodify-modifyorder-3 While in the USDJPY metaeditor-mql4-ordermodify-modifyorder-4 metaeditor-mql4-ordermodify-modifyorder-5 Being able to update orders is usually a fundamental feature for an Expert Advisor, there are actually Expert Advisors that only do that. Take some time to understand the code and test it with your orders. I remember when I first started to learn MQL coding, although I had some knowledge of coding many concept were still unclear. To speed up your learning process I would recommend to follow a quick MQL course that will give you the fundamentals you need to create functional Trading Robot. You can check out the following two course, Udemy often has sales on to make the courses even more affordable, and if you subscribe to my newsletter (top and bottom of the page) I will let you know when this happens.

MQL4 Programming for Traders: Build Robust Trading Robots!

Black Algo Trading: Build Your Trading Robot

You can find a examples of scripts that update orders in the download section and in this article. Please leave a comment if you have any feedback or doubt and remember to like us on Social Media!

If you want to save hours of research and coding and you want to see some professional code check this out! You can use it to build your own EA!

We use cookies to ensure that we give you the best experience on our website.