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 traderqz, Today, 12:06 AM
              10 responses
              18 views
              0 likes
              Last Post traderqz  
              Started by algospoke, 04-17-2024, 06:40 PM
              5 responses
              46 views
              0 likes
              Last Post NinjaTrader_Jesse  
              Started by arvidvanstaey, Today, 02:19 PM
              1 response
              6 views
              0 likes
              Last Post NinjaTrader_Zachary  
              Started by mmckinnm, Today, 01:34 PM
              3 responses
              5 views
              0 likes
              Last Post NinjaTrader_ChelseaB  
              Started by f.saeidi, Today, 01:32 PM
              2 responses
              9 views
              0 likes
              Last Post f.saeidi  
              Working...
              X