Announcement

Collapse
No announcement yet.

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 Geovanny Suaza, 02-11-2026, 06:32 PM
      0 responses
      571 views
      0 likes
      Last Post Geovanny Suaza  
      Started by Geovanny Suaza, 02-11-2026, 05:51 PM
      0 responses
      330 views
      1 like
      Last Post Geovanny Suaza  
      Started by Mindset, 02-09-2026, 11:44 AM
      0 responses
      101 views
      0 likes
      Last Post Mindset
      by Mindset
       
      Started by Geovanny Suaza, 02-02-2026, 12:30 PM
      0 responses
      548 views
      1 like
      Last Post Geovanny Suaza  
      Started by RFrosty, 01-28-2026, 06:49 PM
      0 responses
      548 views
      1 like
      Last Post RFrosty
      by RFrosty
       
      Working...
      X