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
#property copyright "Luca Spinello"
#property link "https://mql4tradingautomation.com"
#property version "1.00"
//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
//We need to scan all the open and pending orders to see if there is there is any
//OrdersTotal return the total number of market and pending orders
//What we do is scan all orders and check if they are of the same symbol of the one where the EA is running
//We select the order of index i selecting by position and from the pool of market/pending trades
//If the pair of the order (OrderSymbol() is equal to the pair where the EA is running (Symbol()) then return true
//If the loop finishes it mean there were no open orders for that pair
Print("Are there open orders for this currency? ",CheckOpenOrders());
If we run the EA and there are no open orders the result is
While if there are 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.