Order Retry Logic
The difference between a backtest and live trading is that nothing ever goes wrong on a backtest. If a strategy traded correctly for EURUSD in 2011 yesterday, you know that the same test will work properly today.
The backtester is not designed nor is it able to catch the types of problems that occur in live trading. I made an effort to list common issues by platform and to detail the most common solutions.
Most older EAs attached stops or take profits to its orders. The NFA rules from around 2009 require all forex trades to enter without any exit conditions attached.
The rule created a nightmare for US MT4 brokers. They were forced to go back, modify MetaTrader and disallow tickets with a stop or limit.
The solution is to confirm correct execution of a trade. Once the trade enters, only then should the expert advisor attempt to add the stop or take profit.
The rule is unfortunate as it requires additional communication time. The process slows order execution, which can cause the trade context is busy error.
Our Expert Advisor Programming Template
Kamal O. asked on Friday about the words RETRYCOUNT and RETRYDELAY in our EA template code. Those 2 words are critical for maintaining code that handles all possible situations, or at least 99.9% of them.
We set them by default to 10 attempts and 1,000 milliseconds, respectively. That is, an order will attempt to order up to 10 separate times. Each attempt will wait at least 1,000 milliseconds (1 second) before making another attempt.
In between attempts, we also check to see if the trade context is busy. If it is not, then we proceed. Otherwise, the code waits for an opportunity to submit the new order up to the specified maximum.
The same retry ad wait logic also applies to submitting stop losses and take profits. One of the most horrifying real world events that a trader can discover is an open trade with no exit conditions attached. Such things really do happen.
The same retry logic is in place in our template code to dramatically reduce the chance of that occurring. We do this for every expert advisor that we program. If you have an expert advisor that does not include retry logic, then contact us about making your EA’s source code more robust.
NinjaTrader largely handles errors and problems for the programmer. However, there are common situations where NinjaTrader’s solution frustrates the user. Disabling strategies and closing all trades whenever an overfill occurs comes to mind.
Anything but the simplest trading strategies are better handled using an unmanaged approach. Managed approaches using pending orders will almost always create a need for rewriting a strategy.
One of the most common reasons NinjaTrader users contract us relates to something going wrong with their live trading. The best way to avoid programming something twice is by making the code ready for real world trading on the first attempt.