$ £ ¥
¥ £ $

How to Check for Open Orders with MQL4

Table of contents

Checking 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. Here we will see how to scan for open orders using the MQL4 coding 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. 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 received tick, 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 limit.

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 simple. You can easily 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 MetaQuotes Language 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 conditions and a for loop you 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:

#property copyright ""
#property link      ""
#property version   "1.00"
#property strict 

// We declare a function CheckOpenOrders of type boolean and we want to return 
// true if there are open orders for the currency pair, false if these isn't any.

bool CheckOpenOrders(){ 
        // We need to scan all the open and pending orders to see if there are any.
        // OrdersTotal returns the total number of market and pending orders.
        // What we do is scan all orders and check if they are of the same symbol as the one where the EA is running.
        for( int i = 0 ; i < OrdersTotal() ; i++ ) { 
                // We select the order of index i selecting by position and from the pool of market/pending trades.
                OrderSelect( i, SELECT_BY_POS, MODE_TRADES ); 
                // If the pair of the order is equal to the pair where the EA is running.
                if (OrderSymbol() == Symbol()) return(true); 
        // If the loop finishes it mean there were no open orders for that pair.
int OnInit() { 

void OnDeinit(const int reason) {


void OnTick() { 
        Print("Are there open orders for this currency? ",CheckOpenOrders()); 

If we run the EA and there are no open orders the result is:

MQL4 EA Checking for Open Orders - No Open Orders Found for Current Symbol

While if there are open orders:

MQL4 EA Checking for Open Orders - Open Orders Found for Current Symbol

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 new orders.

The type of check we just saw is a very basic one. It can be improved by 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.

The best way to learn MQL4 coding is by looking at the well-commented code of existing expert advisors or indicators and by attempting to modify them to your liking.

You can save hours of research and coding by downloading our MT4 Expert Advisor Template, which contains some professional code. You can use it to build your own EA!