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

odd maths results

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

    odd maths results

    Sorry to bother you with something that seems so silly.

    I am calculating the difference between to closing values the example being 15.78 and 15.64 the actual difference is 0.14, but when I run through with debug I get the value of .13999999999999879.

    actual code

    difference = Close[0] - Close[1];

    difference is set to 0 earlier to ensure a known value if this line is not executed.

    All the variables are of type double, since the close values in NinjaTrader are doubles.

    I know I must be doing something reaaaalllllly stupid, but any help would be appreciated. I know that the difference is not much, but there should not be any.

    TIA,

    Graham.

    #2
    Graham, you're welcome - what you see is a correctly calculated double value (16 digits), which the output window would not round - please see this link for more info on this topic - http://www.ninjatrader-support2.com/...ead.php?t=3929
    BertrandNinjaTrader Customer Service

    Comment


      #3
      Bertrand,

      Thanks for the reply. Now I know it wasn't me going completely mad.

      I have found a solution using rounding as follows:

      difference = decimal.Round(((decimal)Close[0] - (decimal)Close[1]), RoundingAccuracy);


      where difference is of type decimal and RoundingAccuracy is a constant that I have set to give the appropriate accuracy. The close prices are forced to be of type decimal using casting.

      This seems to work. It maybe of some use to others in the future.

      Graham.

      Comment


        #4
        Graham, thanks for the solution.

        Basically this is a limitation of all modern computer systems - computers are binary, so it is very difficult to represent an exact value with a bunch of 1s and 0s. If you're looking for some more reading, take a look at the section called Accuracy Problems from this wikipedia entry.
        AustinNinjaTrader Customer Service

        Comment


          #5
          Austin,

          thanks for that, I did have a quick look at the links that Bertrand put on his post and there were getting very deep.

          The solution that I have seems to work for me especially since the OHLC values are only to 2 decimal places in the first instance.

          Graham.

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by Belfortbucks, Today, 09:29 PM
          0 responses
          6 views
          0 likes
          Last Post Belfortbucks  
          Started by zstheorist, Today, 07:52 PM
          0 responses
          7 views
          0 likes
          Last Post zstheorist  
          Started by pmachiraju, 11-01-2023, 04:46 AM
          8 responses
          151 views
          0 likes
          Last Post rehmans
          by rehmans
           
          Started by mattbsea, Today, 05:44 PM
          0 responses
          6 views
          0 likes
          Last Post mattbsea  
          Started by RideMe, 04-07-2024, 04:54 PM
          6 responses
          33 views
          0 likes
          Last Post RideMe
          by RideMe
           
          Working...
          X