Announcement

Collapse
No announcement yet.

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


                      #11
                      Hello Everyone,


                      I'm new to using C# and am trying to code so that the moving average (EMA) values round in my strategy. I use tick charts but I don't want rounding for the price, I only want rounding for the moving averages.

                      I'd like the strategy to round the moving averages like the below. I think the below represents MidpointRounding.AwayFromZero

                      - if the EMA14 is 40803.4, I want the strategy to round the EMA value down to 40803
                      - if the EMA14 is 40803.5, I want the strategy to round the EMA value up to 40804
                      - if the EMA14 is 40803.8, I want the strategy to round the EMA value up to 40804

                      This is the code I used:

                      Math.Round(EMA2, MidpointRounding.AwayFromZero); -- the EMA2 represents the EMA on one of my tick charts.


                      I inserted my MathRound code above under State.Configure.


                      When I try to compile I get the following 3 errors:
                      Argument 1: cannot convert from 'NinjaTrader.NinjaScript.Indicators.EMA' to 'double'
                      Argument 2: cannot convert from 'System.MidpointRounding' to 'int'
                      Property or indexer 'NinjScriptBase.this[int]' cannot be assigned to -- it is read only


                      Can someone please help me figure out what I'm missing or what I'm doing wrong? Thanks in advance.​

                      Comment


                        #12
                        Originally posted by Ttrade12 View Post
                        Hello Everyone,


                        I'm new to using C# and am trying to code so that the moving average (EMA) values round in my strategy. I use tick charts but I don't want rounding for the price, I only want rounding for the moving averages.

                        I'd like the strategy to round the moving averages like the below. I think the below represents MidpointRounding.AwayFromZero

                        - if the EMA14 is 40803.4, I want the strategy to round the EMA value down to 40803
                        - if the EMA14 is 40803.5, I want the strategy to round the EMA value up to 40804
                        - if the EMA14 is 40803.8, I want the strategy to round the EMA value up to 40804

                        This is the code I used:

                        Math.Round(EMA2, MidpointRounding.AwayFromZero); -- the EMA2 represents the EMA on one of my tick charts.


                        I inserted my MathRound code above under State.Configure.


                        When I try to compile I get the following 3 errors:
                        Argument 1: cannot convert from 'NinjaTrader.NinjaScript.Indicators.EMA' to 'double'
                        Argument 2: cannot convert from 'System.MidpointRounding' to 'int'
                        Property or indexer 'NinjScriptBase.this[int]' cannot be assigned to -- it is read only


                        Can someone please help me figure out what I'm missing or what I'm doing wrong? Thanks in advance.​
                        The issue is that you have syntax errors:
                        1. Your first parameter must be a double in this case, not a series; hence you must specify an index for the EMA: EMA2[0] for example.
                        2. Your second parameter must be the number of integers to which you are rounding values: eg., 2, or 0, which seems to be what you seek.
                        3. Your last parameter will be the enum that you have specified.
                        Your syntax had only 2 values, one wrong, the other wrongly placed because of the missing parameter.

                        Regardless, for the specific purpose that you describe, the method RoundToTickSize() might be a better method to use, as your EMA values will then always have decimal places that match your prices, regardless of instrument. That generally makes much more sense, as your EMA then represents values that can actually be traded, even if they are not to be,

                        All the best.​

                        Comment

                        Latest Posts

                        Collapse

                        Topics Statistics Last Post
                        Started by sidiseo, Today, 07:00 AM
                        0 responses
                        1 view
                        0 likes
                        Last Post sidiseo
                        by sidiseo
                         
                        Started by sidiseo, Today, 06:59 AM
                        0 responses
                        2 views
                        0 likes
                        Last Post sidiseo
                        by sidiseo
                         
                        Started by sonoekuhabara, Today, 06:56 AM
                        0 responses
                        3 views
                        0 likes
                        Last Post sonoekuhabara  
                        Started by sidiseo, Today, 06:54 AM
                        0 responses
                        2 views
                        0 likes
                        Last Post sidiseo
                        by sidiseo
                         
                        Started by sidiseo, Today, 06:52 AM
                        0 responses
                        3 views
                        0 likes
                        Last Post sidiseo
                        by sidiseo
                         
                        Working...
                        X