I'm coding an indicator that provides indication if an oscillator is at a local extreme. In concept, it's fairly straightforward. When I apply the indicator to the oscillator indicator panel, it works well.
However, when calling it from a strategy, I'm getting an index out of bounds error. Here's the indicator code:
public class MyIndicator: Indicator
{
private int lastUpdateBar;
private Series<int> peaksValleysIndication;
protected override void OnStateChange()
{
if (State == State.SetDefaults)
{
Description = @"Provides an indication if an oscillator is at a local peak (1) or valley (-1). Otherwise, it's at zero (0).";
Name = "MyIndicator";
Calculate = Calculate.OnBarClose;
IsOverlay = false;
DisplayInDataBox = false;
DrawOnPricePanel = false;
PaintPriceMarkers = false;
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;
NumberBars = 40;
BarsRequiredToPlot = NumberBars + 1;
}
else if (State == State.Configure) {
peaksValleysIndication = new Series<int>(this);
}
else if (State == State.DataLoaded) {
BarsRequiredToPlot = NumberBars + 1;
}
}
protected override void OnBarUpdate()
{
if (CurrentBar < BarsRequiredToPlot) return;
if (CurrentBar > lastUpdateBar) {
//Print("Peaks and Valleys: O:" + Input[1] + " :: Highest:" + HighestBar(Input, NumberBars) + " Lowest: " + LowestBar(Input, NumberBars));
peaksValleysIndication[0] = 0;
int highestBar = HighestBar(Input, NumberBars);
int lowestBar = LowestBar(Input, NumberBars);
if (Input[0] >= Input[highestBar]) {
Draw.ArrowDown(this, "Arrow" + CurrentBar, IsAutoScale, 1, Input[0] + SignalDrawOffset, DownArrowColor);
peaksValleysIndication[0] = -1;
}
else if (Input[0] <= Input[lowestBar]) {
Draw.ArrowUp(this, "Arrow" + CurrentBar, IsAutoScale, 1, Input[0] - SignalDrawOffset, UpArrowColor);
peaksValleysIndication[0] = 1;
}
lastUpdateBar = CurrentBar;
}
}
[HASHTAG="t3322"]region[/HASHTAG] Properties
// Parameters
[NinjaScriptProperty]
[Range(1, int.MaxValue)]
[Display(Name = "Number of Bars", Description = "Number of bars to consider a valid peak or valley.", Order = 1, GroupName = "1. Parameters")]
public int NumberBars { get; set; }
// Settings
[XmlIgnore]
[Display(Name = "Arrow Up Color", Description = "Color for long signals.", GroupName = "2. Settings", Order = 1)]
public Brush UpArrowColor { get; set; }
[Browsable(false)]
public string UpArrowColorSerialize {
get { return Serialize.BrushToString(UpArrowColor); }
set { UpArrowColor = Serialize.StringToBrush(value); }
}
[XmlIgnore]
[Display(Name = "Arrow Down Color", Description = "Color for short signals.", GroupName = "2. Settings", Order = 2)]
public Brush DownArrowColor { get; set; }
[Browsable(false)]
public string DownArrowColorSerialize {
get { return Serialize.BrushToString(DownArrowColor); }
set { DownArrowColor = Serialize.StringToBrush(value); }
}
[Browsable(false)]
[XmlIgnore]
public Series<int> PeaksValleysIndication {
get { return peaksValleysIndication; }
}
[NinjaScriptProperty]
[Range(0, double.MaxValue)]
[Display(Name = "Signal Draw Offset", Description = "Value from the parent indicator to draw the arrows.", GroupName = "2. Settings", Order = 3)]
public double SignalDrawOffset { get; set; }
#endregion
}
}
Here are the relevant parts of code the strategy:
public class MyStrategy : Strategy {
private MyIndicator theInd;
private RSI theRSI;
protected override void OnStateChange() {
if (State == State.DataLoaded) {
theRSI = RSI(4, 3);
theInd= MyIndicator(theRSI, 40, 8);
}
}
protected override void OnBarUpdate() {
if (IsFirstTickOfBar) {
theInd.Update();
int indVal = theInd[1]; // <<< ERROR: Index was outside the bounds of the array.
if (indVal == 1) { ... }
}
}
However, when I try to reference the indication value in the strategy, I get the "Index was outside the bounds of the array." Also, when paused and looking at the indicator and its properties, the Value property shows the error in the VS Watch window.
I get the same error in both Playback and live.
I'm sure I'm missing something simple... A little help please!
Thank you,
Matt

Comment