Should my EA sleep and wait on the isTradeContextBusy MetaTrader function to open an Order?

Probably not.

The MQL code usually looks like this:

while(IsTradeContextBusy())

            Sleep(100);

RefreshRates();

I’m not a fan of sleeping in a while loop – the code can hang forever.  The while loop is rarely needed: An EA is executed on each tick, so if the order fails, it’s retried on the next tick, usually in less than 1 second.  

 
One exception is when the EA is trying to open an order for a currency pair other than the chart it is on. In this case, since the EA is running due to a tick from a different currency, the EA can be exposed to a delay.
 
Another case is when the EA is executing ** a lot ** of code. It runs for so long that the prices change while it is running.  The MQL RefreshRates function makes a request to get the latest prices – this is like requesting a tick.
An important point is that the delay experienced in an EA (and almost all programs) is not due to waiting for processor execution, but usually waiting on I/O (input/output).  In this case the network IO. So it’s very rare to have an EA execute that long – unless you call the sleep function! 
(By the way, the isTradeContextBusy MetaTrader function returns true only when another one of your EAs is trying to open a trade.)

 

Comments are closed.