IsFirstTickOfBar and difference of OnEachTick and OnBarClose execution in NinjaTrader

As we all know, NinjaTrader can be executed in two main modes. Either OnEachTick or OnBarClose. There is also OnPriceChange but we are not going to talk about it in this article.

OnEachTick and OnBarClose control how the OnBarUpdate is called during execution. When execution on EachTick whenever a new tick of data arrives into NinjaTrader the OnBarUpdate is called.

Sometimes, when building strategies we want to execute events based on bar close data. A simple example could be, we want to check the bar close color.

When using OnBarClose we can address Close[0] which would give us the close price of the last closed bar. Whilst executing OnEachTick we have to use Close[1]. Also, when executing OnEachTick we need to catch that exact moment of when the bar actually closes and here we face some challenging differences that we should be aware of especially when writing multi-symbol strategies.

First of all, let us compare the two scenarios.

The illustration is quite self-explanatory. One thing to note is that addressing Close[1] will not return the previous tick during OnEachTick execution, it will always return the last closed bar. Close[0] will always return the last tick available. 

So, if you use Close[1] and IsFirstTickOfBar at the same time you will get the bar close of the last closed bar at the moment when the first tick of the next bar arrived. During OnBarClose execution you will get the bar close event at the moment when it actually arrived into NinjaTrader data provider. 

It is important to understand this difference when building strategies because this can have a significant impact on your design. For example, you might get the first tick of bar to arrive 30 min after the last one closes on a slow market and your logic OnEachTick will execute accordingly whilst OnBarClose will fire the event right at the close. Although this is not very likely to occur it is is just an exaggerated example to make it easier to understand what problems can happen.

The next issue is to do with the execution on multi-symbol strategies. As we know we use BarInProgress command to see which BarsSeries we are on currently inside the OnBarUpdate method. 

Just to recap, the OnBarUpdate method will execute for each symbol individually. For example if you are running a strategy that had had one extra symbol added to it with the AddDataSeries method your OnBarUpdate will fire twice. Once when you get a tick on Symbol 1 and the second time when you get a tick on Symbol 2.

It quite often that you can exclude execution on symbol 2 using:

If (BarInProgress !=0)


After this, you can carry on building your logic on the first symbol simply addressing values from the second symbol. Close[0] or Closes[0][0] returns last tick of symbol 1. Closes[1][0] will return last tick from symbol 2.

One thing that you should be aware of when using this logic is that if you need to use IsFirstTickOfBar you need to be inside the OnBarUpdate method whilst BarInProgress index equals to the dedicated instrument.

In the above example, it is clear that when the first tick of symbol 1 occurs and you try and look back at the last closed bar of symbol two at this point you will not get it. You will actually get the bar that closed before it for the reason that the first tick of bar has not yet arrived on that instrument.

To solve this issue always use the IsFirstTickOfBar inside the context of the particular instrument inside the OnBarUpdate.

Understanding the context of this issue can drive to eliminating other related bugs that can happen during strategy design for NinjaTrader for multi-symbol on tick execution.

Please subscribe to stay updated!