Managing open orders in MQL5

Hi!

I have a strategy implemented in MQL-4, which one I will migrate to MQL-5, trying to use the corresponding APIs (if possible), I mean, not using the MQL-5 classes.

I’m not a trader and I have read some articles to understand the new concepts, and please correct me if I’m somehow wrong:

  1. Orders now are referred to in MQL-5 as pending orders
  2. Position is the sum of opened orders (profit or loss) in a certain asset. In my understanding, if I have two charts operating in GBPJPN, each of it under a different timeframe, the position in that asset will reflect the orders opened in both charts.
  3. Deal is the result of an “executed” position (partially or in its total).

What I need for instance is to know how I modify open orders TP and SL, the same way I do in MQL-4. In MQL-4 we navigate through the orders and check if it’s necessary to offset the values under certain conditions and criteria.

I hope my question is clear enough.

Looking forward for a return and appreciate your help a lot!

Best regards,

Did you see the article - https://www.mql5.com/en/articles/211 ?

I think it could clarify most of your questions.

  1. Orders can be not only pending, but market as well. But market orders are executed fast as a rule (at least if a requested lot is not so large that the order can not be filled for a long time), so you can find them in the history.

  2. Position summs up lot sizes of all deals on a symbol. Deal is created when an order is filled (completely or partially). Timeframe does not have any relation to position behaviour, and this is true (and always was true) for MT4 as well.

  3. Deal is a result of executing an order.

You should modify SL/TP on position using OrderSend function with TRADE_ACTION_SLTP operation type. The only confusion here is that the function should be probably named “SendRequest”, because it is for execution of trade operations in general, not only related to orders.

Orders, Positions and Deals in MetaTrader 5 Orders, Positions and Deals in MetaTrader 5
Creating a robust trading robot cannot be done without an understanding of the mechanisms of the MetaTrader 5 trading system. The client terminal receives the information about the positions, orders, and deals from the trading server. To handle this data properly using the MQL5, it’s necessary to have a good understanding of the interaction between the MQL5-program and the client terminal.

Hi,

Thanks for your reply and help!

I had already taken a look at the article above, but my understanding about it was not correct, according to your clearer explanations.

Let me explain what I’ve just understood, and again I kindly ask for your corrections.

  • I use OrderSend() for pending orders and market orders.
    1) As soon as market orders are executed, they become part of the position on a certain symbol.
    2) If one of the market orders reaches the conditions to be closed, it becomes a deal.
    3) The pending orders will just be executed when the conditions are reached, and then their current behaviour will naturally follow the steps 1) and 2)

Doubt: About modifying an Order by using TRADE_ACTION_SLTP on the request, its description seen in https://www.mql5.com/en/docs/constants/tradingconstants/enum_trade_request_actions is “Modify Stop Loss and Take Profit values of an opened position”. Is it for an open position, not a certain order?

Thanks again!

  1. You may say so. In fact executed orders go to the history. For every order, its entire lot size or partial size is filled by one or more deals, and the deals affects resulting position.

  2. Market order is just an “order” in most common sense of this english word - a command to execute some trade action, so the command can not be closed. If you have an open trade - it is shown as a position, and if you want to close it, you should issue another command (“order”) to do that. Execution of this next order will create new deal, and it in turn change the position. If you mean SL/TP attributes of an order, then if you specify them, they will be transfered to the resulting position and will play a role of conditions for closing this position. Technically SL/TP are similar to special pending orders (for example, limit for TP) and executed in the same way.

You originally asked about modification of “open orders TP and SL”. “Open orders” are positions in MT5, so I answered about modification of SL/TP for a position, but you can of course modify SL/TP for pending orders as well - use TRADE_ACTION_MODIFY.

Here is another somewhat related article - https://www.mql5.com/en/articles/35 - may be you can find useful examples there.

Introduction to MQL5: How to write simple Expert Advisor and Custom Indicator Introduction to MQL5: How to write simple Expert Advisor and Custom Indicator
MetaQuotes Programming Language 5 (MQL5), included in MetaTrader 5 Client Terminal, has many new possibilities and higher performance, compared to MQL4. This article will help you to get acquainted with this new programming language. The simple examples of how to write an Expert Advisor and Custom Indicator are presented in this article. We will also consider some details of MQL5 language, that are necessary to understand these examples.

It seems the article above explains the Position concept differently, if we compare it to the explanation described in "https://www.mql5.com/en/articles/211: " Positions are the contracts, bought or sold on a financial instrument. A long position (Long) is formed as a result of buys in anticipation of a price increase, a short position (Short) is the result of the sale of asset, in the anticipation of future price decrease. For each account, for every financial instrument, there can be only one position. For each symbol, at any given time, there can be only one open position - long or short. ".

So how can we loop through the open orders if there can be only one open position? My understanding is that the position is the sum up of all open orders profit/loss. If I have 3 open orders of Buy and 2 open orders of Sell, can I assure that I have 3 positions of Buy and 2 positions of Sell?

Thanks again and regards,

There is no such a term like open order in MT5. Order is just a command/request to execute a trade action at specific conditions, now or later. Position is a sum of lots from executed and filled (partially or entirely) orders, which documented in corresponding deals. You can find executed orders - say, 3 buys and 2 sells from your example, - in the history , not as “open orders”. You can look up the history of orders and deals if you want to. The executed orders for the same symbol are consolidated, so to speak, in a single position (if buy and sell lots are different) or no position at all (if buys == sells).

“Open” orders in MT5 are only pending orders. You have to forget MT4 for a while if you want to understand how MT5 is working.

You can send 2 kind of request (order) to the server :

  • Market order : if it’s executed, the order is not “open” anymore, it goes to history. The result of this order’s execution is a deal.
    This deal can

    1. open a new position (no starting position),
    2. add to an existing position (you had a BUY position and you sent an order to BUY for example),
    3. reduce an existing position (partial close : you had a BUY position and you sent an order to SELL but with a lower volume than your existing position).
    4. or close a position.(you had a BUY position and you sent an order to SELL with same volume).You can only have 0 or 1 position at any time on a given symbol.
  • Pending order : if it’s executed (accepted by the server), it becomes an “open” order, waiting to be triggered. You can “loop” between these orders.
    A triggered pending order becomes a market order (see previous point). The pending order is then “closed”, removed.
    Of course you can remove (or modify) this pending order before it triggers and becomes a Market order.

Hope that helps.

Thanks a lot for your help!

So, if an order is in execution, I mean, it’s “working” in the market, waiting to have one of its conditions reached to be “closed”, it will be moved to History. And there I can modify it, for example, adjusting its SL or TP during its working, to adjust them to the current market circumstances. Is that so?

Thanks again!

You don’t get it yet I think.

There is no such things as “an order is in execution, I mean, it’s “working” in the market” . If it’s on the market, it’s a position, not an order.

You can of course change the SL/TP of a position.

Thanks for your patience and help!

It’s been really tricky to understand the differences between MT4 and MT5, but I do not give up and, more important, do not hesitate about asking, as I am a completely newbie on it.

After all your explanations, it’s now “hands-on time”. I will begin migrating my MT4 EAs into MT5 and, as soon as new doubts appear, I will come back and ask again.

Thanks a lot!

Hi guys,

Old post, but let me come back to it. As I do not see answer to the query.

I have two deals opened for buy lets call them deal #1 and deal #2. I understand it just one position.

I want deal #1 to have SL on X price and deal #2 on Y price.

Then in tester I will see, two nice “buy” lines closed on different prices.

What would be the way to modify SL of certain deal, according to market moves?

If you need reasons why (well you shouldn’t need taht to answer above but anyway) let say my strategy says buy 2x and as market goes up move #1 SL 100 points up and #2 SL 50 points up.

Please read the topic and the links provided again. You don’t understand what is a deal.

An SL can only be attached to a position, this has nothing to do with a deal.

This topic was done when there was no hedging account with MT5. What you need for your purpose is an hedging account (aka. MT4) and 2 positions.

I use the mt4orders-solution from mql5-forum.
https://www.mql5.com/en/forum/214468#comment_5664532
https://www.mql5.com/en/forum/189799#comment_4869100

The code is the same as in MT4.

1 Like