How to Scan for Open Orders with MQL4
Expert Advisors will be very tough to design,
while you plan an EA you want to contemplate that it simply follows your guidelines
so watch out about what you inform it to do. An vital step when working an skilled
advisor is checking for present orders and right here we are going to see how to
scan for open orders utilizing the MQL4 language.
Assume you’re programming an Expert Advisor that locations a purchase order if the worth
is above a Simple Moving Average of interval 25, additionally remember what you discovered
concerning the construction of an EA and the circulate of the code.
The Expert Advisor will carry out the technical evaluation and if the worth is above the MA (25)
it would place an order,
then it would restart on the subsequent tick acquired checking the stipulations,
managing the orders after which once more the technical evaluation, if the worth is above the MA (25)
once more it would place one other order, and identical on the subsequent tick,
risking to open orders till you hit the margin.
This is a quite common and foolish mistake
that every one the coder encounter once they begin constructing their first EA,
the answer to that is very straightforward, you possibly can merely verify if there are orders already
open for that foreign money and put a situation: if there are orders open for that foreign money
pair you possibly can inform the EA not to execute the technical evaluation or place new orders.
MetaTrader and MQL have some native capabilities to enable you on this job, particularly you should utilize:
- OrdersTotal() returns the variety of open and pending orders
- OrderSelect() selects an order and gather its data
- OrderSymbol() returns the foreign money pair of the chosen order
- Symbol() returns the foreign money pair of the present chart the place the script/indicator/EA is working
Putting collectively the above capabilities with some if and a for loop I can scan all open and pending orders and verify if there may be any order already open for the foreign money pair the place the EA is working. This is the code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | #property copyright “Luca Spinello” #property link "https://mql4tradingautomation.com"
#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 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 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 (OrderSymbol() is equal to the pair where the EA is running (Symbol()) then return true if( OrderSymbol() == Symbol() ) return(true); } //If the loop finishes it mean there were no open orders for that pair return(false); } int OnInit() { return(INIT_SUCCEEDED); } void OnDeinit(const int reason) { } void OnTick() { Print(“Are there open orders for this currency? “,CheckOpenOrders()); |
If we run the EA and there aren’t any open orders the result’s
While if there are open orders
Once the Expert Advisor is in a position to perceive if there are open orders it turns into very straightforward to add a verify and inform the EA not to proceed with Technical Analysis and/or new orders. The kind of scan we simply noticed is a really fundamental one, this may be improved contemplating different components, for instance:
Check the Magic Number, it’s a particular identifier to separate orders submitted by an EA
Check if an order is BUY or SELL
Check if an order is in revenue or loss
Learning extra concerning the orders will enable to add further checks and make your EA extra highly effective. I keep in mind after I first began to study MQL coding, though I had some information of coding many idea had been nonetheless unclear. To pace up your studying course of I’d advocate to observe a fast MQL course that provides you with the basics you want to create practical Trading Robot. You can try the next two course, Udemy usually has gross sales on to make the programs much more inexpensive, and when you subscribe to my e-newsletter (prime and backside of the web page) I’ll let when this occurs.
MQL4 Programming for Traders: Build Robust Trading Robots!
Black Algo Trading: Build Your Trading Robot
Please go away a remark when you have any suggestions or doubt and keep in mind to like us on Social Media!
If you need to save hours of analysis and coding and also you need to see some skilled code verify this out! You can use it to construct your personal EA!
How to Scan for Open Orders with MQL4