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
No open orders
While if there are open orders
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!

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

  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.

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

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

  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?

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


  5. Hello Luca,

    Is there a way to check if there was an opened trade on the last bar somehow (and also if it was already closed) ? many thanks, Robert

    • Hi Robert,
      Yes you can do that you can do in different ways, I would use the following
      1) Select the orders through the loop
      2) Get the Open Time of the selected order using OrderOpenTime()
      3) Use the function iBarShift to see the candlestick shift of the open time in the timeframe that you specify
      4) If the shift is zero means the order was in the current candle, 1 is previous candle
      Depending on the loop and selection you use you can run the check for open orders only or closed orders
      Other function that can help is the OrderCloseTime
      Hope it helps


  6. hello Lucas I would like to know how I make my EA open an order by currency pair, currently he only opens one order at a time with the Function. if (OrdersTotal () == 0 what code do I use?

      • so my robot only opens one order at a time, for him to open another order he has to close the first one, I wanted him to open an order in each currency pair, for example, an order in USD / AUD one in EUR / USD, another in EUR / GPB, more like I use the command if (OrdersTotal () == 0 for him I didn’t open many orders in a currency pair he only opens one order at a time, I want to change that by placing him to open an order in each pair of currency

        • Does your EA open orders for pairs different from the current chart? for example you load it on a EURUSD and it can open orders for USDJPY?

          I think you have two options depending on the design of the EA
          1) If the answer is yes than maybe create an array where you keep the count of open orders for every pair
          2) If the answer is no than you scan all the open orders and use a flag to check if there is an open order for the current pair

          Does it make sense?

          • Hi Jefersson,
            I don’t know the logic of your EA and your question is too generic, I don’t understand where is your block, if it is in the submitting the order or check if there are open orders.
            You mentioned in your previous message “if(OrdersTotal () == 0)” and I replied with how the logic should be to work around that

Leave a Comment

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