I have made a volatility indicator that is sort of like ATR and GARCH(EWMA); however, it will not plot. The error reads "Value outside of valid range."
The line of code causing the error is: rGarch1 = rGarch1 * Close[0]; (and rGarch2 = rGarch2 * Close[0])
If I comment these snippets out, the code runs fine (but is not scaled correctly since it's just a unitless percentage. I want to multiply it by closing price so I have something in ticks like ATR). If I do not comment these snippets out, it will not plot and I get the error indicated above.
Here is the relevant code:
protected override void Initialize()
{
Overlay = false;
Add(new Plot(Color.Red, "RGarch"));
}
/// <summary>
/// Called on each bar update event (incoming tick)
/// </summary>
protected override void OnBarUpdate()
{
if (CurrentBar == 0)
Value.Set(High[0] - Low[0]);
else if (CurrentBar == 1)
{
double rGarch1 = Math.Sqrt(Lambda * (Math.Pow(Value[1]/Close[1], 2)) + ((1 - Lambda) * Math.Pow((Math.Log(High[0] / Low[0])), 2) / (4 * Math.Log(2))));
rGarch1 = rGarch1 * Close[0];
Value.Set(rGarch1);
}
else
{
double rGarch2 = Math.Sqrt(Lambda * (Math.Pow(Value[1], 2)) + ((1 - Lambda) * Math.Pow((Math.Log(High[0] / Low[0])), 2) / (4 * Math.Log(2))));
rGarch2 = rGarch2 * Close[0];
Value.Set(rGarch2);
}
NinjaTrader support told me that it may have to do with a floating point arithmetic error which is certainly possible; however, I don't think that's the case considering it will plot if I comment out the code snippets indicated above. But, I'm a programming novice so I'd appreciate any help or further enlightening on this issue. Thank you for your help!
Tony
Comment