How to get any bar values using a DateTime object in NinjaTrader Strategies


Let’s imagine a scenario where we need to look back at High and Low of a 9:30 bar close during the day. How would we do this?


First of all, inside the OnBarUpdate method, you would need to set look update like this:


var today = Time[0];

var dateTime = new DateTime(today.Year, today.Month, today.Day, 9, 30, 0);


So we get the today value from the current bar and use its year, month and day and set the lookup time as we need.


Next, we would fire up a for cycle that would stop when we reach the desired timestamp.


for (int i = 0; i < CurrentBar; i++)

{

       if (Time[i] == dateTime)

        {

              Print(Time[i] + " High: " + High[i] + " Low: " + Low[i]);

              break;

        }

}


Now some confusion might be there when you see why do we cycle from 0 to the value of CurrentBar? The reason for this is because on every bar close we need to cycle back to the first bar of the chart on the left side and the CurrentBar value will always give you this number.


Download full example below and subscribe please!

Download files