I'm trying to create a simple oscillator that shows the distance between VWAP and SD2 upper and lower in OrderFlowVWAP using two lines. Each line showing the distance between VWAP and upper/lower, respectively. The below compiles but I get the error message "Error on calling 'OnBarUpdate' method on bar -1". I can't understand why this error is coming up as I have added a CurrentBar condition.
I also added the code snippets from teh language reference to manage the tick data.
What am I missing? Thanks!
public class VWOscillator : Indicator
{
private Series<double> vwap;
private Series<double> s2Upper;
private Series<double> s2Lower;
private Series<double> distanceUpper;
private Series<double> distanceLower;
protected override void OnStateChange()
{
if (State == State.SetDefaults)
{
Description = NinjaTrader.Custom.Resource.NinjaScriptIndicatorDe scriptionPriceOscillator;
Name = "VWOscillator";
IsSuspendedWhileInactive = true;
AddLine(Brushes.DarkGray, 0, NinjaTrader.Custom.Resource.NinjaScriptIndicatorZe roLine);
AddPlot(Brushes.Red, "Distance Upper");
AddPlot(Brushes.Green, "Distance Lower");
}
else if (State == State.DataLoaded)
{
vwap = new Series<double>(this);
s2Upper = new Series<double>(this);
s2Lower = new Series<double>(this);
distanceUpper = new Series<double>(this);
distanceLower = new Series<double>(this);
}
else if (State == State.Configure)
{
AddDataSeries(Data.BarsPeriodType.Tick, 1);
}
}
protected override void OnBarUpdate()
{
if (CurrentBar <= 10) return;
if (BarsInProgress == 0) {
vwap[0] = OrderFlowVWAP(VWAPResolution.Tick, Bars.TradingHours,
VWAPStandardDeviations.Three, 1, 2, 2.5).VWAP[0];
s2Upper[0] = OrderFlowVWAP(VWAPResolution.Tick, Bars.TradingHours,
VWAPStandardDeviations.Three, 1, 2, 2.5).StdDev2Upper[0];
s2Lower[0] = OrderFlowVWAP(VWAPResolution.Tick, Bars.TradingHours,
VWAPStandardDeviations.Three, 1, 2, 2.5).StdDev2Lower[0];
distanceUpper[0] = s2Upper[0] - vwap[0];
distanceLower[0] = s2Lower[0] - vwap[0];
// smoothEma[0] = emaFast[0] - emaSlow[0];
Value[0] = distanceUpper[0];
Value[1] = distanceLower[0];
}
else if (BarsInProgress == 1)
{
// We have to update the secondary tick series of the cached indicator using Tick Resolution to make sure the values we get in BarsInProgress == 0 are in sync
OrderFlowVWAP(BarsArray[0], VWAPResolution.Tick, BarsArray[0].TradingHours,
VWAPStandardDeviations.Three, 1, 2, 2.5).Update(OrderFlowVWAP(BarsArray[0], VWAPResolution.Tick,
BarsArray[0].TradingHours, VWAPStandardDeviations.Three, 1, 2, 2.5).BarsArray[1].Count - 1, 1);
}
}
Comment