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

Double difference strange results

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

    Double difference strange results

    Hello,

    I am trying to obtain the difference between two doubles and NT8 return stranges results:


    Code:
        
    double theDif =  0.709 - 0.7091;
    Print("theDif--> "+ theDif);

    And the Output result is:

    theDif--> -9,9999999999989E-05


    The result would be -0,0001



    What I am doing bad? Thank you.

    Last edited by ninjo; 03-11-2019, 10:44 AM.

    #2
    I solved it with Math.Round function:

    Code:
    double theDif =  Math.Round( 0.709 - 0.7091, 4);

    The problem is that I want to claculate the number of ticks between two chart prices.

    Code:
    Math.Round( 0.709 - 0.7091, 4) / TickSize
    Its works and return: -1


    In this case the price have 4 decimals, but how could I set for all markets??

    Thank you

    Comment


      #3
      Hello ninjo,

      Thanks for your post.

      This mathematical behavior is expected with floating point arithmetic. We have some various tips on working with floating point arithmetic in our Tip page below.

      Floating point math - https://ninjatrader.com/support/help...arithmetic.htm

      Rounding the result to your expectation is viable. For being able to identify the number of digits needed for the rounding precision, you could follow an approach similar to those suggested in the publicly available resource below to get the number of significant digits in TickSize.

      Finding the number of places after the decimal point of a number - https://stackoverflow.com/questions/...nt-of-a-double

      Please let us know if we can be of further assistance.
      JimNinjaTrader Customer Service

      Comment


        #4
        Originally posted by ninjo View Post
        I solved it with Math.Round function:

        Code:
        double theDif = Math.Round( 0.709 - 0.7091, 4);

        The problem is that I want to claculate the number of ticks between two chart prices.

        Code:
        Math.Round( 0.709 - 0.7091, 4) / TickSize
        Its works and return: -1


        In this case the price have 4 decimals, but how could I set for all markets??

        Thank you
        Why not just use the NS provided function, RoundToTickSize()?

        ref: https://ninjatrader.com/support/help...toticksize.htm

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by Barry Milan, Today, 10:35 PM
        1 response
        8 views
        0 likes
        Last Post NinjaTrader_Manfred  
        Started by WeyldFalcon, 12-10-2020, 06:48 PM
        14 responses
        1,428 views
        0 likes
        Last Post Handclap0241  
        Started by DJ888, Yesterday, 06:09 PM
        2 responses
        9 views
        0 likes
        Last Post DJ888
        by DJ888
         
        Started by jeronymite, 04-12-2024, 04:26 PM
        3 responses
        40 views
        0 likes
        Last Post jeronymite  
        Started by bill2023, Today, 08:51 AM
        2 responses
        16 views
        0 likes
        Last Post bill2023  
        Working...
        X