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

Rounding a value up or down

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

    Rounding a value up or down

    Hi all;
    Have looked on the Microsoft website and the boffins can't give a moron like me a straight answer so hoping to get one here.

    How do I round a number to an integer?

    Example I want to set a stop of 2 * ATR(14)[1].

    If the value of the 14 period ATR 1 bar ago was 2.37 then 2 * 2.37 = 4.74 so I want my stop to be set 4 points from my entry if I round down or 5 points from entry if I round up.

    Am thinking ............execution.Order.AvgFillPrice - (Math.Round(2*ATR(14)[1])) .........but it wouldn't know whether to round down or up. So is there a 'rounddown' or 'roundup' keyword / operator?

    Thanks in advance,

    Ben.

    #2
    Originally posted by stocktraderbmp View Post
    Hi all;
    Have looked on the Microsoft website and the boffins can't give a moron like me a straight answer so hoping to get one here.

    How do I round a number to an integer?

    Example I want to set a stop of 2 * ATR(14)[1].

    If the value of the 14 period ATR 1 bar ago was 2.37 then 2 * 2.37 = 4.74 so I want my stop to be set 4 points from my entry if I round down or 5 points from entry if I round up.

    Am thinking ............execution.Order.AvgFillPrice - (Math.Round(2*ATR(14)[1])) .........but it wouldn't know whether to round down or up. So is there a 'rounddown' or 'roundup' keyword / operator?

    Thanks in advance,

    Ben.
    Math.Floor() rounds down; Math.Ceiling() rounds up.

    Comment


      #3
      Hi Ben,

      Math.Round is used when you want possibility of rounding up. To take off the decimals and turn 4.74 into 4, there is Math.Truncate

      Print(Math.Truncate(4.74));
      Ryan M.NinjaTrader Customer Service

      Comment


        #4
        Thanks Ryan but this assumes I want to print the value for my own use, I don't I want to use it in a strategy. Can I put my formula in there instead of the int you have in your example so I would have:

        .....execution.Order.AvgFillPrice - (Math.Truncate(2*ATR(14)[1]) * TickSize) .......

        Thanks for your quick response;

        Ben

        Comment


          #5
          Thanks Koganam;

          Will try it, How do you express the number of decimals, obviously as its a stop order I want zero. Would it be .... Math.Floor(2*ATR(14)[1], 0)

          Comment


            #6
            Truncate seems to have removed the script errors for the moment.

            thanks all

            Comment


              #7
              Originally posted by stocktraderbmp View Post
              Thanks Koganam;

              Will try it, How do you express the number of decimals, obviously as its a stop order I want zero. Would it be .... Math.Floor(2*ATR(14)[1], 0)
              You do not. There are none. Math.Floor and Math.Ceiling round to the nearest integer, per the desired direction. If you want it as a double, then cast it after rounding.

              Comment


                #8
                Yeah that's just a quick example but you're welcome to use it any way you like.
                Ryan M.NinjaTrader Customer Service

                Comment


                  #9
                  Brilliant guys,

                  Thanks very much;

                  Ben.

                  Comment


                    #10
                    I'm not sure to understand all the discussion.

                    But for me , if you want to calculate a value and round it to create a stop , you don't have to use C# basic methods.

                    You need to round depending of the instrument you are trading.
                    And there is a method in the NinjaTrader framework to do that :

                    Instrument.MasterInstrument.Round2TickSize(double price)
                    Def : Returns a value that is rounded to the nearest valid value evenly divisible by the instrument's tick size.


                    So you replace the 'price' parameter by your calculation and you have a round that fits the instrument.

                    Comment


                      #11
                      Excellent, will try that as well.

                      Thank you very much have a good xmas.

                      Ben

                      Comment


                        #12
                        Hi,

                        so Math. Round is always rounding "up?"

                        For getting a correct tickprice with the mid in ES I´m using Instrument.MasterInstrument.Round2TickSize((L1 + L2)/2)

                        Eg if the mid of 2 prices in ES is 1997,125 then it is rounded 1997,25?
                        math round of 1997,375 = 1997,50?

                        math round 1997,875 = 1998?

                        math round 1997,675 = 1997,75?

                        Its always rounded up or maybe - when? - rounded down?

                        Thank you
                        Tony

                        Comment


                          #13
                          Hi Long time since I did this but Yes - round2ticksize always rounds up as I understand it - if you want to round to an integer then use math.floor (ceiling) otherwise you have to write script something like

                          double myvalue = (L1+L2)/2
                          then
                          if myvalue is greater than math.floor(myvalue)+0.01and less than math.floor(myvalue)+0.25 then do something
                          else if myvalue is greater than or equal to math.floor(myvalue)+0.25 and less than math.floor(myvalue)+0.5 then do something else etc

                          I would however question the need for this - the best you can do is save 1 tick and that would only be occasionally when taking into account slippage etc. If your profit relies on this 1 tick it might not possibly be the best strategy to be pursuing !

                          This is a quick answer and my coding is a little rusty now but hope it puts you on the right track.

                          Comment


                            #14
                            Originally posted by tonynt View Post
                            Hi,

                            so Math. Round is always rounding "up?"

                            For getting a correct tickprice with the mid in ES I´m using Instrument.MasterInstrument.Round2TickSize((L1 + L2)/2)

                            Eg if the mid of 2 prices in ES is 1997,125 then it is rounded 1997,25?
                            math round of 1997,375 = 1997,50?

                            math round 1997,875 = 1998?

                            math round 1997,675 = 1997,75?

                            Its always rounded up or maybe - when? - rounded down?

                            Thank you
                            Tony
                            Math.Round() is a system method that is completely different from Round2TickSize().

                            Math.Round() rounds mathematically, just the way that we learned in school many years ago. Round2TickSize() should logically round up always, yes. Think of what your information actually means, not what you want it to be mathematically. A price of 1997,675 means that it is greater than 1997 and 2 ticks. As ticks are integer values, if it is more than 2 ticks, then it must logically and practically, be 3 ticks.

                            Comment

                            Latest Posts

                            Collapse

                            Topics Statistics Last Post
                            Started by jaybedreamin, Today, 05:56 PM
                            0 responses
                            2 views
                            0 likes
                            Last Post jaybedreamin  
                            Started by DJ888, 04-16-2024, 06:09 PM
                            6 responses
                            18 views
                            0 likes
                            Last Post DJ888
                            by DJ888
                             
                            Started by Jon17, Today, 04:33 PM
                            0 responses
                            1 view
                            0 likes
                            Last Post Jon17
                            by Jon17
                             
                            Started by Javierw.ok, Today, 04:12 PM
                            0 responses
                            6 views
                            0 likes
                            Last Post Javierw.ok  
                            Started by timmbbo, Today, 08:59 AM
                            2 responses
                            10 views
                            0 likes
                            Last Post bltdavid  
                            Working...
                            X