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

simple arithmetic

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

    simple arithmetic

    OK, I wanted to check an entry to exit, so saved the Close[0] at the input trigger, and at exit, printed this RG = exit - entry - comm:
    Test RG: Entry Price = 98.61, Exit Price = 98.87, Comm = 0.1, RG = -0.360000000000005

    I admit being surprised at the difficulty in arithmetic accuracy.

    Given the inputs, why is the output off? The result should have the same number of significant digits as the inputs.

    Also, is there a way to round this inaccurate result back to the correct # decimal places to match the underlying instrument?

    #2
    LostTrader, for floating point arithmetic considerations you would want to check into this tip here:



    For tick size related rounding, please see this method:

    BertrandNinjaTrader Customer Service

    Comment


      #3
      Thank you, Bertrand. Somehow I did not expect that to happen with addition.

      What if, in this case, I don't actually want to round to tick size? I really want to use the ToString("+#.00;-#.00;zero") formatting to reduce the output to the correct number of digits. How can this be programmatically set?

      Comment


        #4
        LostTrader, would the tip shared on this blog help you out?

        BertrandNinjaTrader Customer Service

        Comment


          #5
          Rounding a number to Price (not Tick)

          No, Bertrand, it doesn't.

          I want to format (e.g. when printing) a calculation result to the exact same number of significant digits as the PRICE of the current instrument, which as you know varies.
          I do NOT want to round to the TickSize.

          So how can I programmatically detect the number of sig digits on Price?

          Comment


            #6
            Lost Trader,

            Off the top of my head I would say something like:

            Code:
             
            int dec; 
            string str = Close[0].ToString();
            dec = (str.Length - [COLOR=purple]1[/COLOR]) - str.IndexOf([COLOR=#a31515]"."[/COLOR]);
            Would that do what you want?

            VT

            Comment


              #7
              Originally posted by Lost Trader View Post
              No, Bertrand, it doesn't.

              I want to format (e.g. when printing) a calculation result to the exact same number of significant digits as the PRICE of the current instrument, which as you know varies.
              I do NOT want to round to the TickSize.

              So how can I programmatically detect the number of sig digits on Price?
              So only Round2TickSize for the Print() statement itself, without rounding the actual quantity in the code proper.

              Otherwise, just use custom or standard string formatting per C#.

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by Nicholewatkinsi, Today, 10:53 PM
              0 responses
              5 views
              0 likes
              Last Post Nicholewatkinsi  
              Started by dward123, 01-02-2024, 09:59 PM
              4 responses
              175 views
              0 likes
              Last Post Lancer
              by Lancer
               
              Started by ETFVoyageur, Today, 04:00 PM
              2 responses
              19 views
              0 likes
              Last Post ETFVoyageur  
              Started by AaronKTradingForum, Today, 03:44 PM
              1 response
              14 views
              0 likes
              Last Post AaronKTradingForum  
              Started by Felix Reichert, 04-26-2024, 02:12 PM
              11 responses
              80 views
              0 likes
              Last Post Felix Reichert  
              Working...
              X