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

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.
        BertrandNinjaTrader Customer Service

        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 Irukandji, Today, 04:58 AM
          0 responses
          2 views
          0 likes
          Last Post Irukandji  
          Started by fitspressoburnfat, Today, 04:25 AM
          0 responses
          2 views
          0 likes
          Last Post fitspressoburnfat  
          Started by Skifree, Today, 03:41 AM
          1 response
          4 views
          0 likes
          Last Post Skifree
          by Skifree
           
          Started by usazencort, Today, 01:16 AM
          0 responses
          1 view
          0 likes
          Last Post usazencort  
          Started by kaywai, 09-01-2023, 08:44 PM
          5 responses
          604 views
          0 likes
          Last Post NinjaTrader_Jason  
          Working...
          X