Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

Partner 728x90

Collapse

Volatility Indicator Error: "Value outside of valid range."

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Volatility Indicator Error: "Value outside of valid range."

    Hello,

    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

    #2
    Code can be simplified

    Use a separate DataSeries sigma2 to hold the squares of volatility. This simplifies the calculations.

    Here is the code:

    Code:
    [COLOR=Blue]protected override void [/COLOR]Initialize()
    {
        Add([COLOR=Blue]new[/COLOR] Plot(Color.Red, "RGarch"));
        sigma2 = [COLOR=Blue]new[/COLOR] DataSeries([COLOR=Blue]this[/COLOR]);
    }
    
    [COLOR=Blue]protected override void[/COLOR] OnStartUp()
     {
         lambda = 1 - 2.0/(1 + period);
     }
            
     [COLOR=Blue]protected override void[/COLOR] OnBarUpdate()
     {
        [COLOR=Blue] double[/COLOR] u = Math.Log(High[0]/Low[0]) / (4 * Math.Log(2));
         [COLOR=Blue]if [/COLOR](CurrentBar == 0)
             sigma2.Set(Math.Pow(u, 2));
         [COLOR=Blue]else[/COLOR]
             sigma2.Set(lambda * sigma2[1] + (1-lambda) * Math.Pow(u, 2));
         Value.Set(Close[0] * Math.Sqrt(sigma2[0]));            
     }
    Attached Files

    Comment


      #3
      Hello Harry,

      Thank you for your reply! I will work on this later tonight!

      Tony

      Comment


        #4
        Hi Harry,

        I was able to get it to work and simplified the code. Thanks again for your help. Do you know why NT requires use of the DataSeries class in this situation?

        Regards,
        Tony

        Comment


          #5
          Originally posted by cb4gusto22 View Post
          Hi Harry,

          I was able to get it to work and simplified the code. Thanks again for your help. Do you know why NT requires use of the DataSeries class in this situation?

          Regards,
          Tony
          Hi Tony,

          NT does not require to use a second DataSeries object. I have only used the DataSeries to simplify the recursion, which is done without extracting the root and then squaring again. Therefore my approach uses 2 DataSeries obejcts, the added DataSeries and the PlotSeries. The recursive formula is done with the first (sigma square), and the PlotSeries is only used to extract the root and adjust the scale by multiplying with the last close.

          After I had simplified the formula in line with the EWMA model, it worked.
          Last edited by Harry; 06-04-2014, 09:02 AM.

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by michi08, 10-05-2018, 09:31 AM
          5 responses
          741 views
          0 likes
          Last Post NinjaTrader_ChelseaB  
          Started by The_Sec, Today, 02:29 PM
          0 responses
          1 view
          0 likes
          Last Post The_Sec
          by The_Sec
           
          Started by tsantospinto, 04-12-2024, 07:04 PM
          4 responses
          62 views
          0 likes
          Last Post aligator  
          Started by sightcareclickhere, Today, 01:55 PM
          0 responses
          1 view
          0 likes
          Last Post sightcareclickhere  
          Started by Mindset, 05-06-2023, 09:03 PM
          9 responses
          259 views
          0 likes
          Last Post ender_wiggum  
          Working...
          X