> Indicator 'SpikeVolume': Error on calling 'OnBarUpdate' method on bar 5: Index was outside the bounds of the array.
using NinjaTrader.NinjaScript.Indicators.Nic;
using System.Windows.Media;
using Nic.Utilities;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel;
using System.Xml.Serialization;
namespace NinjaTrader.NinjaScript.Indicators.Nic
{
public class SpikeVolume : Indicator
{
[Range(1, int.MaxValue), NinjaScriptProperty]
[Display(ResourceType = typeof(Custom.Resource), Order = 0)]
public int Period { get; set; }
[Range(1, int.MaxValue), NinjaScriptProperty]
[Display(ResourceType = typeof(Custom.Resource), Order = 1)]
public int MinPeriod { get; set; }
[Range(1, int.MaxValue), NinjaScriptProperty]
[Display(ResourceType = typeof(Custom.Resource), Order = 2)]
public double SigmaThreshold { get; set; }
[Range(1, int.MaxValue), NinjaScriptProperty]
[Display(ResourceType = typeof(Custom.Resource), Order = 3)]
public double SigmaThreshold2 { get; set; }
[Browsable(false)]
[XmlIgnore()]
public Series<double> Volume1 { get { return Values[0]; } }
[Browsable(false)]
[XmlIgnore()]
public Series<double> Volume2 { get { return Values[1]; } }
[Browsable(false)]
[XmlIgnore()]
public Series<double> Volume3 { get { return Values[2]; } }
private StdDevCalc _stdDevEth;
private StdDevCalc _stdDevRth;
protected override void OnStateChange()
{
if (State == State.SetDefaults)
{
Description = @"Nic Spike Volume";
Name = "Nic Spike Volume";
Calculate = Calculate.OnEachTick;
BarsRequiredToPlot = 0;
IsAutoScale = false;
IsOverlay = false;
DrawOnPricePanel = false;
Period = 50;
MinPeriod = 10;
SigmaThreshold = 2;
SigmaThreshold2 = 3;
}
else if(State == State.SetDefaults)
{
AddPlot(Brushes.DarkGray, "Volume");
AddPlot(Brushes.LightYellow, "Volume Sigma 1");
AddPlot(Brushes.Yellow, "Volume Sigma 2");
}
else if (State == State.Configure)
{
}
else if (State == State.DataLoaded)
{
_stdDevEth = new StdDevCalc(Period);
_stdDevRth = new StdDevCalc(Period);
}
}
protected override void OnBarUpdate()
{
base.OnBarUpdate();
// Attempted Fix Here !
if (CurrentBars[0] < 5 || CurrentBars[1] < 5 || CurrentBars[2] < 5 || CurrentBars[3] < 5) return;
if (CurrentBar < 2) return;
var stdDev = this.IsRTH() ? _stdDevRth : _stdDevEth;
if (IsFirstTickOfBar)
{
stdDev.Update(Volume[1]);
}
var volume = Volume[0];
if (stdDev.Count() < MinPeriod)
{
// FAILS HERE !
Volume1[0] = volume;
return;
}
var sigma = stdDev.Average();
if (volume >= sigma * SigmaThreshold2)
{
Volume3[0] = volume;
}
else if (volume >= sigma * SigmaThreshold)
{
Volume2[0] = volume;
}
else
{
Volume1[0] = volume;
}
}
}
}​

Comment