Yesterday, I wrote a bit about how the backtester simulates ticks. When NinjaTrader encounters a bar where the stop and take profit fall within the bar’s range, it always assumes that the stop was hit first.
I had this issue come up for a client that wanted to sell his strategy to an institutional client. The institution wanted the ability to backtest in order to understand how the strategy performs in broader market conditions. The live results only covered 9 months, which made it difficult for the investor to make an informed decision. A reliable, accurate backtest would allow my client to facilitate the sale.
When we first programmed the strategy, it looked like an abysmal failure. The results showed a 45 degree equity plunge from every trade. He placed his stops at 1/3 of ATR, so naturally, NinjaTrader assumed that they were hit all the time.
The solution that we developed for him used the high time frame chart that the strategy actually used, but we then modified the code to do include what NinjaTrader calls intrabar granularity. Basically, we add tick charts on the backend to force the strategy to update on a tick by tick basis.
It’s actually pretty simple to program. In the Initialize function, you need to add bid and ask tick charts.
protected override void Initialize()
Add(string instrumentName, PeriodType.Tick, 1, MarketDataType.Bid);
Add(string instrumentName, PeriodType.Tick, 1, MarketDataType.Ask);
Then, in the OnBarUpdate() function, you leave the code unchanged. All you need to do is to put it within a simple if-then statment
protected override void OnBarUpdate()
if( BarsInProgress == 0 )
// Do stuff
The backtest results came within $5 of the actual trading results over a period of 9 months! Although backtesting frequently leads to inaccurate results, we were thrilled to reproduce the result with such a minor error.
The major disadvantage to this method is that you must manually type in the instrument name inside of the source code, then compile it, every time that you want to run a backtest on a new instrument. It’s not a great solution, but NT7 unfortunately does not support programmatically calling the InstrumentName from within Initialize().