How to sync and plot data correctly when executing multi-timeframe indicators and strategies in NinjaTrader 8


Hello guys! Before I start a long story for this post I really want to thank the support team from NinjaTrader who have helped a lot to clarify the further discussed topic and more than that have actually clarified and helped with far more questions so a big thank you for all of them if any are reading this post!


So!!! The story started when I thought that it would be really great to have an indicator that would look at UVOL and DVOL Nasdaq delta and put that into Bollinger to trade the NQ. You can have a read about the concept here https://ninjacoding.net/ninjatrader/blog/uvoldbol.


So everything went well until we hit the live market. Let’s discuss the code for historical execution first. We are initializing the indicator on NQ but we are importing UVOL and DVOL at the backend so in total we will have 3 Bars objects and our OnBarUpdate will get called 3 times from BarsInProgress 0 to BarsInProgress 2.


Our calculations are quite simple:

1) Once the bar closes on all 3 bars objects grab all the data

2) Subtract DVOL from UVOL and add a new value to Delta Series<double> object.

3) Put the Delta Series object as an input for our Bollinger

4) Plot the data on the primary series which is our NQ


Now, during historical execution, all bars have closed ages ago when you have actually downloaded your data so nothing really stops you from asking for the desired values from the secondary bars objects right at BarsInProgress = 0 like this:


If (BarsInProgress == 0)

{

var uvol = Closes[1][0];

var dVol Closes[2][0];

}


So if this was a 13:00 bar close on NQ you would 100% get the same 13:00 close for the underlying bar data series if they have been downloaded prior. Easy done!!


However, problems begin during live, sim or replay execution because all of the bars arrive into the NinjaTrader api as they arrive! There is no guarantee that when the primary data series has arrived the other 2 will be there already. Neither there is any guarantee about the order in which they will come up. 


Point is the OnBarUpdate will execute as they arrive but now if we call for a Closes[1][0] upon OnBarUpdate during BarsInProgress == 0 we will most probably get data for a previous bar because the current one has not arrived yet!


So what is the solution? Before proceeding with it I also must mention that in order to plot anything you have to plot from BarsInProgress = 0 ie. in order to assign values to any Values[0][0] you have to do it from within the BarsInProgress == 0. 


It’s important to understand this because what I did is I actually wrote a quick workaround that would wait for all the bars to close and then update the Values[0][0] and that produced some wicked plots!))) Later I found a quick note in NinjaTrader documentation that said that all plots should be updated from within the BarsInProgress = 0. So, if we need to plot something this ain’t going to work but if say we are looking to work only with the backend logic then this could be an option.


The only solution is to switch to OnTickExecution. Please note that this will also require to slightly edit the logic for the historical in terms of indexing the bars correctly but in terms, for live, this will provide you with an execution that will be closest to your historical backtest since now from within BarsInProgress = 0 you will get the most recent tick value available at the secondary data series at that point.


Hope this article saved someone a few sleepless nights) Please subscribe.