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

Math.Round method

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

    Math.Round method

    Hi ninjas,

    I've had a round off problem with a strategy cause the floating point issue. A good fellow here called Koganan suggested me this link that would allow me configuring a better format for me and thus eliminating my floating point bad signals problem.

    Rounds a value to the nearest integer or to the specified number of fractional digits.


    Since I'm not an advance programmer, I'd like to know how to implement instructions within my code in order to be able to call this method; from NAMESPACE to following modules.

    I work with EUR.USD with Interactivebrokers, so my input would be in 0.00005 ticksize; since I have to make calculations with average I'd like to work with 0.000000 format.

    Thanks in advance

    #2
    Ny the way, the function Round2TickSize is not good enough cause sometimes rounds to a wrong side, so that's why I need more digits. Obviously the problem come with comparing to Zero.

    Comment


      #3
      Hello pstrusi,
      To assist you further can you give an example of what number you want to round and what exact result you want to have.

      I look forward to assisting you further.
      JoydeepNinjaTrader Customer Service

      Comment


        #4
        I expect to have number with maximum 6 basic digits after zero in order to compare fairly. In the earlier case I expect that number -0.000025 can be the same.
        There are other cases when I get for example a number smaller, for example like -0.00000025 that I'd like to be Zero cause it has too many non meaningful digits.
        I need just 6 meaningful digits after zero.
        Last edited by pstrusi; 06-12-2012, 06:24 AM. Reason: Can't reply, something wrong in the forum

        Comment


          #5
          Hello pstrusi,
          Thanks, but what do you expect the number -0.000025 to be (after the conversion). Are you trying to make it +0.000025?

          I look forward to assisting you further.
          JoydeepNinjaTrader Customer Service

          Comment


            #6
            Originally posted by pstrusi View Post
            Ny the way, the function Round2TickSize is not good enough cause sometimes rounds to a wrong side, so that's why I need more digits. Obviously the problem come with comparing to Zero.
            So then, per the URL link I gave you the first time out, round to six digits, or however many you want.

            RoundedValue = Math.Round(value, 6);

            Does that not round to what you want?

            Comment


              #7
              I realize this post is old, but I had the same issue and came across it so figured I would post the best fix I found for others who happen here:

              You can use this:
              HTML Code:
              double rounded = f * Math.round(x/f);​
              let the instrument TickSize = ts
              So, create a double variable and populate it with the instrument TickSize somewhere, probably State.Configure would be my guess.

              you want a price of something, maybe an indicator price for placing an order that will not be rejected, but lets use Close[0] as a simple example. You could populate any indicator call in its place for indicator values.

              HTML Code:
              ts=TickSize
              HTML Code:
              ts * Math.Round(Close[0] / tsf,0)
              What is it doing...
              1- It takes your price, however many decimals (e.g. .01, .25, .00001 whatever) and divides it by your ts.
              2- it rounds that number to 0 decimal places.
              3- it multiplies the rounded number by the ts to give you the original price rounded to the closest tick size.

              This should be preferred since it is the most accurate and the strategy may be applied to any instrument without changing any decimal places in the code. Assuming you correctly set ts to the tick size.

              This is especially useful for placing orders based on some derived price without getting rejected for invalid price.



              Comment


                #8
                There's also double bar = Instrument.MasterInstrument.RoundToTickSize(foo);.
                Bruce DeVault
                QuantKey Trading Vendor Services
                NinjaTrader Ecosystem Vendor - QuantKey

                Comment


                  #9
                  Yup, I just found
                  HTML Code:
                  Instrument.MasterInstrument.RoundToTickSize(double price);
                  and was coming back to amend this post. But you beat me to it. Glad the information is out there now. ​It is not search friendly.

                  Comment


                    #10
                    Originally posted by QuantKey_Bruce View Post
                    There's also double bar = Instrument.MasterInstrument.RoundToTickSize(foo);.
                    The original poster stated specifically that that method is not suitable for that which he wants to do. I do not see why not, but that is what he claims.

                    Comment

                    Latest Posts

                    Collapse

                    Topics Statistics Last Post
                    Started by ScottW, Today, 06:09 PM
                    1 response
                    4 views
                    0 likes
                    Last Post NinjaTrader_Manfred  
                    Started by ScottWalsh, Today, 06:52 PM
                    0 responses
                    3 views
                    0 likes
                    Last Post ScottWalsh  
                    Started by ftsc2022, 10-25-2022, 12:03 PM
                    5 responses
                    256 views
                    0 likes
                    Last Post KeyonMatthews  
                    Started by Board game geek, 10-29-2023, 12:00 PM
                    14 responses
                    244 views
                    0 likes
                    Last Post DJ888
                    by DJ888
                     
                    Started by Waxavi, 04-19-2024, 02:10 AM
                    4 responses
                    56 views
                    0 likes
                    Last Post sonia0101  
                    Working...
                    X