I'm new to NinjaTrader*(as a convert from FXCM) and am converting my indicators from Lua to C#. I'm getting a handle on things for the most part, but apparently still have confusion with the way bar timing works.
I have a "trend state" indicator being consumed by another indicator and receive the following error:
Indicator 'TrendState': Error on calling 'OnBarUpdate' method on bar 50: Object reference not set to an instance of an object.
The TrendState indicator works just fine on its own. I know I'm close but have something not quite right. The code is below. I tried to follow how the @ATR, but can't figure this out for the life of me.
What am I missing or doing wrong?
// // PARENT INDICATOR CODE // protected override void OnStateChange() { if (State == State.SetDefaults) { Description = @"My Description"; Name = "TrendState"; Calculate = Calculate.OnEachTick; IsOverlay = true; DisplayInDataBox = true; PaintPriceMarkers = false; DrawHorizontalGridLines = false; DrawVerticalGridLines = false; ScaleJustification = NinjaTrader.Gui.Chart.ScaleJustification.Right; IsSuspendedWhileInactive = true; AddPlot(ChartControl.Properties.ChartBackground, "TrendState"); } } protected override void OnBarUpdate() { if (CurrentBar < 50) { // Need at least 50 bars to work properly, so return "undetermined" state Value[0] = 0; return; } else { // Get the state of the trend Value[0] = DoSomeLogicThatDeterminesState(); // Do some visual UI stuff here to represent state } } // // CONSUMING INDICATOR CODE // protected override void OnBarUpdate() { Print("Candle closed; Opened at: " + Time[0].ToString() + "; CurrentBar: " + CurrentBar + "; O: " + Open[0] + "; C: " + Close[0] + "; H: " + High[0] + "; L: " + Low[0] + "; State: " + State.ToString()); double state = TrendState()[0]; Print(String.Format("Current State: {0}", state)); }
Comment