How to Scan for Open Orders with MQL4

Expert Advisors can be very tricky to design, when you plan an EA you need to consider that it just follows your rules so be careful about what you tell it to do. An important step when running an expert advisor is checking for existing orders and here we will see how to scan for open orders using the MQL4 language. Assume you are programming an Expert Advisor that places a buy order if the price is above a Simple Moving Average of period 25, also keep in mind what you learned about the structure of an EA and the flow of the code. The Expert Advisor will perform the technical analysis and if the price is above the MA (25) it will place an order, then it will restart at the next tick received checking the prerequisites, managing the orders and then again the technical analysis, if the price is above the MA (25) again it will place another order, and same at the next tick, risking to open orders until you hit the margin. This is a very common and silly mistake that all the coder encounter when they start building their first EA, the solution to this is very easy, you can simply check if there are orders already open for that currency and put a condition: if there are orders open for that currency pair you can tell the EA not to execute the technical analysis or place new orders. MetaTrader and MQL have some native functions to help you in this task, in particular you can use:
  • OrdersTotal()  returns the number of open and pending orders
  • OrderSelect()  selects an order and collect its information
  • OrderSymbol()  returns the currency pair of the selected order
  • Symbol()  returns the currency pair of the current chart where the script/indicator/EA is running
Putting together the above functions with some if and a for loop I can scan all open and pending orders and check if there is any order already open for the currency pair where the EA is running. This is the code If we run the EA and there are no open orders the result is
mql4-scanopenorders-1
No open orders
While if there are open orders
mql4-scanopenorders-2
Open orders
Once the Expert Advisor is able to understand if there are open orders it becomes very easy to add a check and tell the EA not to proceed with Technical Analysis and/or new orders. The type of scan we just saw is a very basic one, this can be improved considering other factors, for example:
  • Check the Magic Number, it is a special identifier to separate orders submitted by an EA
  • Check if an order is BUY or SELL
  • Check if an order is in profit or loss
Learning more about the orders will allow to add additional checks and make your EA more powerful. 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

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!

8 thoughts on “How to Scan for Open Orders with MQL4”

  1. Hi
    How can I find out if there is an open order in the market right now?. excluding the pending orders. In the end I need to know the exact time at which a pending order that I placed transformed into an open order in the market. Thank you and regards.

    • Hi Alejandro,
      What you are trying to do is possible, you just need to filter the orders.
      If you want the current open orders excluding the pending you can use the following:
      if(OrderType()==OP_BUY || OrderType()==OP_SELL){ operation }
      this will consider only the current BUY and SELL orders and not the pending, also you can see the time where an order was opened through OrderOpenTime().
      I hope this answers your question.
      Luca

  2. Hello, I use the same EA for different pairs at once. So during the trading it happens, that it will open more trades at once, but I need BUY/SELL only for one pair to finishing my strategy, then it can make trade for other and so on, pair by pair. What is the best code for prevent or avoid the multiply trades at once? Thank you, Slava

    • if you use the same EA in multiple pairs I suggest you to start using different Magic Numbers and to check the orders filtering by Magic Number and Pair, it is definitely possible.
      Luca

  3. Hi,
    I want to response to the order commit event.
    Is there any system event when order closed (take profit or user manual close)?
    Lior

    • Hi Lior,
      Sorry I am confused to what you want to do.
      You want an event triggered if an order is closed or need the code to close an order?
      Luca

  4. Hi Luca,
    I have a few open orders and i want to write a code that runs when one of them closed.
    I saw that there OnTick event, but no OnOrderClosed or something similar.
    Is there a way to do this by code?
    Thanks,
    Lior

    • Hi Lior,
      Unfortunately there is no native event that is triggered when an order is created, updated or closed.
      However, you can work around this and create your own event to detect an order closed.
      The logic I would use is:
      1) Save the list of current open orders
      2) Set a timer event and check the saved list with the current list
      3) If the current list as missing an order that is present in the saved list then it means this was closed
      4) Execute your code if condition 3 is verified

      You can create an array with the list of Order Ticket Number or write them to file to create your “Saved” list.
      Depending on what you need to include in your code you can either embed the above into an indicator or an expert advisor.

      Does it make sense?

      Luca

Leave a Comment