I am trying to work on an indicator using volumetric bars and I get "Indicator 'IndicatorName': Error on calling 'OnBarUpdate' method on bar 1: Index was outside the bounds of the array.
The chart in which I will add the indicator is configured with volumetric bars, but I didn't find how to get information from these bars so I added another volumetric bars to the indicator code as it is indicated in the user guide, using the following code in OnBarUpdate():
NinjaTrader.NinjaScript.BarsTypes.VolumetricBarsTy pe barsType = Bars.BarsSeries.BarsType as
NinjaTrader.NinjaScript.BarsTypes.VolumetricBarsTy pe;
Added the bars, I was able to get the information that I need (using GetBidVolumeForPrice and GetAskVolumeForPrice).
All the logic is inside OnBarUpdate(). The problem is when I assing the result of the indicator to Value[0]. When adding the indicator to the chart, it doesn't work. If I do not include the assignment of the result to Value[0], the indicator looks fine in the chart, but I understand that the indicator is not returning any result, so I won't be able to use the indicator in a strategy.
I'd need to know how to pass the result to Value[0] so I can use the indicator in a strategy.
The indicator code is the following:
{
if (State == State.SetDefaults)
{
Description = "Description here";
Name = "Name here";
Calculate = Calculate.OnBarClose;
IsOverlay = true;
IsSuspendedWhileInactive = true;
...
...
...
}
}
protected override void OnBarUpdate()
{
(Variables assignments here...)
NinjaTrader.NinjaScript.BarsTypes.VolumetricBarsTy pe barsType = Bars.BarsSeries.BarsType as
NinjaTrader.NinjaScript.BarsTypes.VolumetricBarsTy pe;
// Init
(some logic here)
for (double i = Low[0]; i < High[0]; i = i + vTickSize)
{
vBidVol = barsType.Volumes[CurrentBar].GetBidVolumeForPrice(i);
vAskVol = barsType.Volumes[CurrentBar].GetAskVolumeForPrice(i + vTickSize);
(som logic and calculation here)
}
// Show the indicator in the chart
if (P_Show_Result)
{
if ( vAlcista == true
&& vAskImbalances != 0)
{
Draw.Text(this, "Imbalances" + CurrentBar, false, "Imb: " + Convert.ToString(vAskImbalances), 0, Low[0], -50,
Brushes.Green, TextFont, TextAlignment.Center, Brushes.Transparent, Brushes.Transparent, 0);
}
if ( vAlcista == false
&& vBidImbalances != 0)
{
Draw.Text(this, "Imbalances" + CurrentBar, false, "Imb: " + Convert.ToString(vBidImbalances), 0, High[0], +50,
Brushes.Red, TextFont, TextAlignment.Center, Brushes.Transparent, Brushes.Transparent, 0);
}
}
// Return value
Value[0] = vAlcista ? vAskImbalances : vBidImbalances * (-1);
}
#region Properties
[Browsable(false)] // this line prevents the data series from being displayed in the indicator properties dialog, do not remove
[XmlIgnore()] // this line ensures that the indicator can be saved/recovered as part of a chart template, do not remove
public Series<double> vResultado
{
get { return Values[0]; }
}
(Parameters here)
Thanks!
Comment