How to Check for Open Orders with MQL4

Check for Open Orders with MQL4 is a necessary step when building with Expert Advisors and Indicators.

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 code it to do.

Why Check For Open Orders With MQL4

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 coders encounter when they start building their first EA.

How To Check For Open Orders With MQL4

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


While if there are open orders


Possible Checks On 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 check 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

A Note On Learning MQL4

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!

Comments are closed.

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