I'm working on a custom indicator in NinjaTrader 8 that calculates 2000 tick bars using 1 tick data. However, I'm encountering an issue where my calculated 2000 tick bar end times are not aligning with the 2000 tick bars displayed on the chart. Specifically, my custom bar stats print with a consistent offset, showing 300 ticks remaining on the chart's 2000 tick bar. It will be 300 ticks different every bar, but then when I rerun the indicator it will be a different, consistent offset, so I can't just hard code a certain number. I would like to do this without adding the 2000 tick data as a secondary series if possible.
Here is my code for reference:
using System; using NinjaTrader.Cbi; using NinjaTrader.Gui.Tools; using NinjaTrader.NinjaScript; using NinjaTrader.Data; using System.Windows.Media; namespace NinjaTrader.NinjaScript.Indicators { public class TestIndicator : Indicator { private double currBarHigh = double.MinValue; private double currBarLow = double.MaxValue; private double currBarOpen = 0; private double currBarClose = 0; private int tickCounter = 0; protected override void OnStateChange() { if (State == State.SetDefaults) { Description = @"Custom Indicator"; Name = "TestIndicator"; Calculate = Calculate.OnEachTick; IsOverlay = true; } else if (State == State.Configure) { AddDataSeries(Data.BarsPeriodType.Tick, 1); // Adding tick data series with 2000 ticks per bar } } protected override void OnBarUpdate() { // Ensure the script runs on the primary data series (BarsInProgress == 0) if (BarsInProgress != 0) return; // Ensure there are enough bars for lookback if (CurrentBar < 5) return; double currPrice = Close[0]; // Price of the current tick tickCounter++; // Increase tick by one until 2000 are reached if (tickCounter == 1) { currBarOpen = currPrice; // Set open for new 2000 tick bar } currBarHigh = Math.Max(currBarHigh, currPrice); // Check each tick to see if new high for the bar that is currently printing currBarLow = Math.Min(currBarLow, currPrice); if (tickCounter >= 2000) { currBarClose = Close[0]; // Save close value on the 2000th tick Print($"ending value for bar open: {currBarOpen}"); Print($"ending value for bar high: {currBarHigh}"); Print($"ending value for bar low: {currBarLow}"); Print($"ending value for bar close: {currBarClose}"); // Reset for the next bar tickCounter = 0; currBarHigh = double.MinValue; currBarLow = double.MaxValue; } } } }
Comment