Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Rounding up doesn't really round up?

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

    Rounding up doesn't really round up?

    Hi guys,

    I'm doing a simple check for the value of 4 EMA lines and I'm getting some unexpected values. I'm hoping someone smarter than I am can guide me in the right direction.

    in State.SetDefaults I have:

    HTML Code:
    EMA1Period = 12;
    EMA2Period = 22;
    EMA3Period = 60;
    EMA4Period = 210;

    in State.DataLoaded I have

    HTML Code:
    EMA1 = EMA(Closes[0], Convert.ToInt32(EMA1Period));
    EMA2 = EMA(Closes[0], Convert.ToInt32(EMA2Period));
    EMA3 = EMA(Closes[0], Convert.ToInt32(EMA3Period));
    EMA4 = EMA(Closes[0], Convert.ToInt32(EMA4Period));

    OnBarUpdate() I'm doing this:

    HTML Code:
    if ((EMA1[1] > EMA2[1])
    && (EMA2[1] > EMA3[1])
    && (EMA3[1] > EMA4[1]))
    {
    
    roundedEMA1 = Instrument.MasterInstrument.RoundToTickSize(EMA1[1]);
    roundedEMA2 = Instrument.MasterInstrument.RoundToTickSize(EMA2[1]);
    roundedEMA3 = Instrument.MasterInstrument.RoundToTickSize(EMA3[1]);
    roundedEMA4 = Instrument.MasterInstrument.RoundToTickSize(EMA4[1]);
    
    Print("");
    Print("EMA1[1]: " + EMA1[1]);
    Print("roundedEMA1: " + roundedEMA1);
    Print("EMA2[1]: " + EMA2[1]);
    Print("roundedEMA2: " + roundedEMA2);
    Print("EMA3[1]: " + EMA3[1]);
    Print("roundedEMA3: " + roundedEMA3);
    Print("EMA4[1]: " + EMA4[1]);
    Print("roundedEMA4: " + roundedEMA4);
    }


    roundedEMA1, roundedEMA2, roundedEMA3 and roundedEMA4 have been set as the type double.

    The print I'm getting is:
    HTML Code:
    EMA1[1]: 3988.67903240544
    roundedEMA1: 3988.75
    EMA2[1]: 3983.36452234448
    roundedEMA2: 3983.25
    EMA3[1]: 3974.44215648095
    roundedEMA3: 3974.5
    EMA4[1]: 3970.64331144022
    roundedEMA4: 3970.75
    My question is why is:
    EMA1 Rounding UP
    EMA2: Rounding Down
    EMA3: Rounding UP
    EMA4: Rounding Up

    Aren't all 4 supposed to Round UP? What's up with EMA2?​

    #2
    Hello Bluebeep,

    You are using Instrument.MasterInstrument.RoundToTickSize(). This is not specifically up or down, this rounds to the nearest tick price.

    Are you wanting to use Math.Ceiling() or Math.Round()?
    Returns the smallest integral value greater than or equal to the specified number.

    https://learn.microsoft.com/en-us/do...tframework-4.8
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Hi NinjaTrader_ChelseaB!

      Thanks for the super fast response.

      I want to simply round up the values so I checked out a help file I found here:


      In there it says: Returns a value that is rounded up...

      The contrary I assume would be here:
      ttps://ninjatrader.com/support/helpGuides/nt8/NT%20HelpGuide%20English.html?roundtoticksize.htm

      This one talks about Rounding Down.

      The whole "Instrument.MasterInstrument..." I just copied from the example and modified to work with my script.

      I just want to make sure that it keeps rounding up to the nearest tick. Does that RoundToTickSize not round up?

      I don't know if the Math.Round will work to get me to the nearest Tick?

      Sorry I'm still confused lol

      Comment


        #4
        RoundToTickSize rounds to the nearest ticksize, and would be (for practical purposes) equivalent to this: Math.Round(SomeValue / TickSize) * TickSize

        Math.Round rounds to the nearest WHOLE NUMBER not tick size
        Bruce DeVault
        QuantKey Trading Vendor Services
        NinjaTrader Ecosystem Vendor - QuantKey

        Comment


          #5
          I'm still wondering why I'm wrong to use what I have used. If the help doc says it will round up, why doesn't it round up? Does the doc need to be updated? Or do we need a 'RoundUpToTickSize' option?

          This is how I'm interpreting it
          - RoundDownToTickSize - Will round down
          - RoundToTickSize - Will choose the closest TickSize to round to

          If I'm right about that, then what about Rounding Up?

          Hope I'm not sounding stubborn... It's just that I just don't get it
          Last edited by Bluebeep; 04-20-2023, 05:48 PM.

          Comment


            #6
            Just to add to this confusion, maybe I should RoundDownToTickSize and add a tick up to get me to where I need to be. Just wondering... If a Round Up option doesn't exist, curious as to why not?
            Last edited by Bluebeep; 04-20-2023, 05:57 PM.

            Comment


              #7
              Rounding up would be equivalent to adding half a tick and rounding. It probably isn't there because it isn't commonly needed. Just about the only circumstance you would do this would be if you're trying to round to the "farthest" e.g. you're shorting and you want the stop to be at some calculated price like an EMA or something, and you want to round "away" from the trade.
              Bruce DeVault
              QuantKey Trading Vendor Services
              NinjaTrader Ecosystem Vendor - QuantKey

              Comment


                #8
                Hello Bluebeep,

                If you are needing to always round up, you could use a custom calculation to do so.

                Or you could check that the value divided by the TickSize is not a whole number (so not at an exact tick value), and then round down and add 1 tick.

                I am happy to submit a request for a RoundUpToTickSize method if you would like.
                Chelsea B.NinjaTrader Customer Service

                Comment


                  #9
                  Thanks guys. I was able to solve it by Rounding Down then adding 1 tick ( and of course adding a comment section explaining why I was going about it this way lol).

                  Thanks NinjaTrader_ChelseaB, I think it would be a good idea to submit a request coz I can imagine half a dozen situations this can be useful.

                  Comment


                    #10
                    Hello Bluebeep,

                    I've submitted a feature request for the development team to consider a RoundUpToTickSize method.

                    Once I have a tracking ID for this request I will post in this thread for future reference.
                    Chelsea B.NinjaTrader Customer Service

                    Comment


                      #11
                      Hello,

                      This request is being tracked with ID # SFT-2633.

                      Please note, we receive many requests and cannot reasonably implement all requested features or changes. Interest is tracked internally and if enough interest is tracked, it would be weighed against how feasible it would be to make those changes to consider implementing, so we cannot offer an ETA or promise of fulfillment.

                      When new features are implemented, they will be listed in the Release Notes page of the Help Guide. The ID number may be different than the internal feature request tracking ID, but the description of the feature will let you know if that feature has been implemented.

                      Release Notes - https://ninjatrader.com/support/help...ease_notes.htm
                      Chelsea B.NinjaTrader Customer Service

                      Comment


                        #12
                        You can use this for rounding to nearest tick size now:

                        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 EMA1[1] from your example. You could populate any indicator call in its place for indicator values.

                        ts=TickSize

                        HTML Code:
                        ts * Math.Round(EMA1[1] / ts,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. If you know for a fact you want to use it on ES futures as an example... just make ts = 0.25

                        In any case, this should ditch the extra decimals.

                        Comment


                          #13
                          I don't follow, if you're rounding to the nearest tick, why you wouldn't do double rounded = Instrument.MasterInstrument.RoundToTickSize(x);.
                          Bruce DeVault
                          QuantKey Trading Vendor Services
                          NinjaTrader Ecosystem Vendor - QuantKey

                          Comment


                            #14
                            Hello HaveGunsWillTravel,

                            Thanks for your note.

                            QuantKey_Bruce is correct. Rounding to the nearest tick size could be done by simply using Instrument.MasterInstrument.RoundToTickSize().

                            See this help guide page about RoundToTickSize(): https://ninjatrader.com/support/help...toticksize.htm
                            <span class="name">Brandon H.</span><span class="title">NinjaTrader Customer Service</span><iframe name="sig" id="sigFrame" src="/support/forum/core/clientscript/Signature/signature.php" frameborder="0" border="0" cellspacing="0" style="border-style: none;width: 100%; height: 120px;"></iframe>

                            Comment

                            Latest Posts

                            Collapse

                            Topics Statistics Last Post
                            Started by NullPointStrategies, Yesterday, 05:17 AM
                            0 responses
                            59 views
                            0 likes
                            Last Post NullPointStrategies  
                            Started by argusthome, 03-08-2026, 10:06 AM
                            0 responses
                            134 views
                            0 likes
                            Last Post argusthome  
                            Started by NabilKhattabi, 03-06-2026, 11:18 AM
                            0 responses
                            75 views
                            0 likes
                            Last Post NabilKhattabi  
                            Started by Deep42, 03-06-2026, 12:28 AM
                            0 responses
                            45 views
                            0 likes
                            Last Post Deep42
                            by Deep42
                             
                            Started by TheRealMorford, 03-05-2026, 06:15 PM
                            0 responses
                            50 views
                            0 likes
                            Last Post TheRealMorford  
                            Working...
                            X