Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

C# code question

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

    C# code question

    I mostly find C# quite easy to pick up, but occasionally there is something a bit obscure that is not in any of my books and internet searches get me nowhere.
    In the NinjaTrader Indicator DoubleStochastics we find

    double r = MAX(High, Period)[0] - MIN(Low, Period)[0];
    r = r.Compare(0, 0.000000000001) == 0 ? 0 : r;

    Could some kind soul please explain what this .Compare is doing to r ?

    Thanks
    Dave

    #2
    Looks like the intention is: "If the max is only an insignificant amount more than min then assume its a result of floating point rounding error caused by using double instead of decimal. So treat it as zero difference" so r becomes 0.

    But the use of 0.000000000001 for "insignificant" looks arbitrary. Code would probably be better to use TickSize (eg Round2TickSize instead of this odd Compare) since the definition of what precision is significant/insignificant varies according to instrument.

    Comment


      #3
      Yes I see. Not at all elegant is it. I did realise in the end that you can soimetimes get a clue by typing the line again with intellisense on. It says Extensions.Compare(double y, double precision)
      So it is equivalent to

      if (r > -.0.00000000001 && r < 0.00000000001)
      r=0;

      Otherwise r is unchanged

      Thanks for your help DaveE

      Comment


        #4
        That's correct Dave -

        r.Compare(x, y) just compares r to x, with a precision of y.

        So, if r is for example 0.000000000002 -> r.Compare would see that it is not equal to zero.

        But, if r is now 0.0000000000005 -> r.Compare would return that it is equal to zero.

        Comment


          #5
          Hi Bertrand.
          Thanks for your reply.

          It would be more tidy to use the const double Epsilon, supplied for this purpose. Or did you want to distance yourselves further from zero for some reason?

          Dave

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by Geovanny Suaza, 02-11-2026, 06:32 PM
          0 responses
          601 views
          0 likes
          Last Post Geovanny Suaza  
          Started by Geovanny Suaza, 02-11-2026, 05:51 PM
          0 responses
          347 views
          1 like
          Last Post Geovanny Suaza  
          Started by Mindset, 02-09-2026, 11:44 AM
          0 responses
          103 views
          0 likes
          Last Post Mindset
          by Mindset
           
          Started by Geovanny Suaza, 02-02-2026, 12:30 PM
          0 responses
          559 views
          1 like
          Last Post Geovanny Suaza  
          Started by RFrosty, 01-28-2026, 06:49 PM
          0 responses
          558 views
          1 like
          Last Post RFrosty
          by RFrosty
           
          Working...
          X