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

Compare Operator used on a data series

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

    Compare Operator used on a data series

    protected override void OnBarUpdate()
    {
    nom.Set(Close[0] - MIN(Low, PeriodK)[0]);
    den.Set(MAX(High, PeriodK)[0] - MIN(Low, PeriodK)[0]);
    if (den[0].Compare(0, 0.000000000001) == 0)
    fastK.Set(CurrentBar == 0 ? 50 : fastK[1]);
    else
    fastK.Set(Math.Min(100, Math.Max(0, 100 * nom[0] / den[0])));
    // Slow %K == Fast %D
    K.Set(SMA(fastK, Smooth)[0]);
    D.Set(SMA(K, PeriodD)[0]);
    }
    Hi,
    I'm a NinjaTrader newbie and am looking at the code of the Stochastic indicator
    shown above.One line which I don't understand is:
    if (den[0].Compare(0, 0.000000000001) == 0)
    My understanding is that den[0] is calculated as the highest high minus the lowest low value of
    the last PeriodK. (Putting it in the data series as the most recent value)
    I'm not understanding what the compare is doing and I was unable to find it in the
    NinjaScript language help.
    Also for the line below, I thought that 100 * nom[0] / den[0] would always be positive, so I'm
    not sure why it is compared with 0, to find the max.
    fastK.Set(Math.Min(100, Math.Max(0, 100 * nom[0] / den[0])));
    Thank you,
    Ken

    #2
    Originally posted by krubin View Post
    protected override void OnBarUpdate()
    {
    nom.Set(Close[0] - MIN(Low, PeriodK)[0]);
    den.Set(MAX(High, PeriodK)[0] - MIN(Low, PeriodK)[0]);
    if (den[0].Compare(0, 0.000000000001) == 0)
    fastK.Set(CurrentBar == 0 ? 50 : fastK[1]);
    else
    fastK.Set(Math.Min(100, Math.Max(0, 100 * nom[0] / den[0])));
    // Slow %K == Fast %D
    K.Set(SMA(fastK, Smooth)[0]);
    D.Set(SMA(K, PeriodD)[0]);
    }
    Hi,
    I'm a NinjaTrader newbie and am looking at the code of the Stochastic indicator
    shown above.One line which I don't understand is:
    if (den[0].Compare(0, 0.000000000001) == 0)
    My understanding is that den[0] is calculated as the highest high minus the lowest low value of
    the last PeriodK. (Putting it in the data series as the most recent value)
    I'm not understanding what the compare is doing and I was unable to find it in the
    NinjaScript language help.
    Also for the line below, I thought that 100 * nom[0] / den[0] would always be positive, so I'm
    not sure why it is compared with 0, to find the max.
    fastK.Set(Math.Min(100, Math.Max(0, 100 * nom[0] / den[0])));
    Thank you,
    Ken
    Both are statements designed to take care of floating point inaccuracies. Due to these inaccuracies, floating point numbers are almost never equal, so instead of comparing two doubles, one compares their difference from zero to a small user-defined amount (in this case to 12 decimal places).

    So in this instance if the difference between den[0] and 0 is less than 0.000000000001, then it will be assumed that it is 0.

    By the same token at those very small values it is possible for den[0] to end up as a small negative number, so the Math.Max function is used to ensure that it is never recorded as negative, but rather as 0.

    Comment


      #3
      Dear Koganam,

      Thank you for the quick response and the very clear explanation!

      Best Wishes,
      Ken

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by funk10101, Today, 12:02 AM
      1 response
      10 views
      0 likes
      Last Post NinjaTrader_LuisH  
      Started by GLFX005, Today, 03:23 AM
      1 response
      6 views
      0 likes
      Last Post NinjaTrader_Erick  
      Started by nandhumca, Yesterday, 03:41 PM
      1 response
      13 views
      0 likes
      Last Post NinjaTrader_Gaby  
      Started by The_Sec, Yesterday, 03:37 PM
      1 response
      11 views
      0 likes
      Last Post NinjaTrader_Gaby  
      Started by vecnopus, Today, 06:15 AM
      0 responses
      1 view
      0 likes
      Last Post vecnopus  
      Working...
      X