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 quantismo, 04-17-2024, 05:13 PM
          4 responses
          30 views
          0 likes
          Last Post quantismo  
          Started by love2code2trade, 04-17-2024, 01:45 PM
          4 responses
          31 views
          0 likes
          Last Post love2code2trade  
          Started by cls71, Today, 04:45 AM
          2 responses
          10 views
          0 likes
          Last Post eDanny
          by eDanny
           
          Started by proptrade13, Today, 11:06 AM
          0 responses
          5 views
          0 likes
          Last Post proptrade13  
          Started by kulwinder73, Today, 10:31 AM
          1 response
          10 views
          0 likes
          Last Post NinjaTrader_Erick  
          Working...
          X