I've developed a custom indicator that has been working fine for the past few days and today, it suddenly gave me this "Error on calling 'OnBarUpdate' method on bar 100: Object reference not set to an instance of an object" message and failed to work properly. Also, sometimes the indicator displays wrong values and the values only update to the correct ones on the close of the current bar which should not be the case as it is set to update on each tick. Attached below is the code:
#region Using declarations
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;
using System.Windows.Media;
using System.Xml.Serialization;
using NinjaTrader.Cbi;
using NinjaTrader.Gui;
using NinjaTrader.Gui.Chart;
using NinjaTrader.Gui.SuperDom;
using NinjaTrader.Gui.Tools;
using NinjaTrader.Data;
using NinjaTrader.NinjaScript;
using NinjaTrader.Core.FloatingPoint;
using NinjaTrader.NinjaScript.DrawingTools;
#endregion
//This namespace holds Indicators in this folder and is required. Do not change it.
namespace NinjaTrader.NinjaScript.Indicators
{
public class TestIndicator : Indicator
{
private int Counter = 0;
private double[] HighMaximas;
private double[] LowMinimas;
private double[] SlidingWindow;
protected override void OnStateChange()
{
if (State == State.SetDefaults)
{
Description = @"Enter the description for your new custom Indicator here.";
Name = "Test Indicator";
Calculate = Calculate.OnEachTick;
IsOverlay = true;
DisplayInDataBox = false;
DrawOnPricePanel = false;
DrawHorizontalGridLines = true;
DrawVerticalGridLines = true;
PaintPriceMarkers = true;
ScaleJustification = NinjaTrader.Gui.Chart.ScaleJustification.Right;
//Disable this property if your indicator requires custom values that cumulate with each new market data event.
//See Help Guide for additional information.
IsSuspendedWhileInactive = true;
NumberOfCandles = 100;
WindowLength = 3;
NumberOfValues = 3;
}
else if (State == State.Configure)
{
SlidingWindow = new double[WindowLength];
HighMaximas = new double[NumberOfValues];
LowMinimas = new double[NumberOfValues];
}
}
protected override void OnBarUpdate()
{
if(CurrentBar < NumberOfCandles) return;
for(int i = 0; i < NumberOfCandles; i++)
{
SlidingWindow[0] = Highs[0][i];
SlidingWindow[1] = Highs[0][i+1];
SlidingWindow[2] = Highs[0][i+2];
if((SlidingWindow[1] > SlidingWindow[0]) && (SlidingWindow[1] > SlidingWindow[2]))
{
if(Counter == 0)
{
HighMaximas[Counter] = SlidingWindow[1];
Counter ++;
}
else if(Counter >= 1)
{
if(SlidingWindow[1] > HighMaximas[Counter-1])
{
HighMaximas[Counter] = SlidingWindow[1];
Counter ++;
}
else
{
;
}
}
}
else if(Counter == NumberOfValues)
{
Counter = 0;
break;
}
else
{
;
}
}
for(int i = 0; i < NumberOfCandles; i++)
{
SlidingWindow[0] = Lows[0][i];
SlidingWindow[1] = Lows[0][i+1];
SlidingWindow[2] = Lows[0][i+2];
if((SlidingWindow[1] < SlidingWindow[0]) && (SlidingWindow[1] < SlidingWindow[2]))
{
if(Counter == 0)
{
LowMinimas[Counter] = SlidingWindow[1];
Counter ++;
}
else if(Counter >= 1)
{
if(SlidingWindow[1] < LowMinimas[Counter-1])
{
LowMinimas[Counter] = SlidingWindow[1];
Counter ++;
}
else
{
;
}
}
}
else if(Counter == NumberOfValues)
{
Counter = 0;
break;
}
else
{
;
}
}
Draw.HorizontalLine(this, "Value1", HighMaximas[0], Brushes.Blue);
Draw.HorizontalLine(this, "Value2", HighMaximas[1], Brushes.Blue);
Draw.HorizontalLine(this, "Value3", HighMaximas[2], Brushes.Blue);
Draw.HorizontalLine(this, "Value4", LowMinimas[0], Brushes.Red);
Draw.HorizontalLine(this, "Value5", LowMinimas[1], Brushes.Red);
Draw.HorizontalLine(this, "Value6", LowMinimas[2], Brushes.Red);
} [NinjaScriptProperty]
[Range(1, int.MaxValue)]
[Display(Name="NumberOfCandles", Description="Candles to scan for values", Order=1, GroupName="Parameters")]
public int NumberOfCandles
{ get; set; }
[NinjaScriptProperty]
[Range(1, int.MaxValue)]
[Display(Name="WindowLength", Description="Window length of sliding window", Order=2, GroupName="Parameters")]
public int WindowLength
{ get; set; }
[NinjaScriptProperty]
[Range(1, int.MaxValue)]
[Display(Name="NumberOfValues", Description="Number of values to output", Order=3, GroupName="Parameters")]
public int NumberOfValues
{ get; set; }
}
}
#region NinjaScript generated code. Neither change nor remove.
namespace NinjaTrader.NinjaScript.Indicators
{
public partial class Indicator : NinjaTrader.Gui.NinjaScript.IndicatorRenderBase
{
private TestIndicator[] cacheTestIndicato;
public TestIndicator TestIndicator(int numberOfCandles, int windowLength, int numberOfValues)
{
return TestIndicator(Input, numberOfCandles, windowLength, numberOfValues);
}
public TestIndicator TestIndicator(ISeries<double> input, int numberOfCandles, int windowLength, int numberOfValues)
{
if (cacheTestIndicator != null)
for (int idx = 0; idx < cacheTestIndicator.Length; idx++)
if (cacheTestIndicator[idx] != null && cacheTestIndicator[idx].NumberOfCandles == numberOfCandles && cacheTestIndicator[idx].WindowLength == windowLength && cacheTestIndicator[idx].NumberOfValues == numberOfValues && cacheTestIndicator[idx].EqualsInput(input))
return cacheTestIndicator[idx];
return CacheIndicator<TestIndicator>(new TestIndicator(){ NumberOfCandles = numberOfCandles, WindowLength = windowLength, NumberOfValues = numberOfValues}, input, ref cacheTestIndicator);
}
}
}
namespace NinjaTrader.NinjaScript.MarketAnalyzerColumns
{
public partial class MarketAnalyzerColumn : MarketAnalyzerColumnBase
{
public Indicators.TestIndicator TestIndicator(int numberOfCandles, int windowLength, int numberOfValues)
{
return indicator.TestIndicator(Input, numberOfCandles, windowLength, numberOfValues);
}
public Indicators.TestIndicator TestIndicator(ISeries<double> input , int numberOfCandles, int windowLength, int numberOfValues)
{
return indicator.TestIndicator(input, numberOfCandles, windowLength, numberOfValues);
}
}
}
namespace NinjaTrader.NinjaScript.Strategies
{
public partial class Strategy : NinjaTrader.Gui.NinjaScript.StrategyRenderBase
{
public Indicators.TestIndicator TestIndicator(int numberOfCandles, int windowLength, int numberOfValues)
{
return indicator.TestIndicator(Input, numberOfCandles, windowLength, numberOfValues);
}
public Indicators.TestIndicator TestIndicator(ISeries<double> input , int numberOfCandles, int windowLength, int numberOfValues)
{
return indicator.TestIndicator(input, numberOfCandles, windowLength, numberOfValues);
}
}
}
#endregion

Comment