Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Round an integer to nearest 10

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

    Round an integer to nearest 10

    I have an int that runs a calculation to determine position size. If my code calculates to buy 96 shares, I'd like it to round up to just buy 100 shares to make it more of an even lot. Or if it calculated 94 shares to round down to 90 shares.

    Math.Round appears to only work with decimals and doubles. Any ideas?

    Thanks,

    kc

    #2
    kc,

    This is C# and you may want to google it instead. http://stackoverflow.com/questions/2...st-10-interval
    Josh P.NinjaTrader Customer Service

    Comment


      #3
      Originally posted by NinjaTrader_Josh View Post
      kc,

      This is C# and you may want to google it instead. http://stackoverflow.com/questions/2...st-10-interval
      Thanks that helped. But this is the general programming help forum!

      Here's where I ended up:

      Code:
       
      PositionSize = <insert position size logic>;
      PositionSizeRounded = ((int)(PositionSize/10))*10;

      Comment


        #4
        Glad you got it resolved.
        Josh P.NinjaTrader Customer Service

        Comment


          #5
          Take care kcsystemtrader, your proposal is always rounding down to nearest ten. If you want it to round up too you need to insert a Math.Round() in addition.

          Regards
          Ralph

          Comment


            #6
            Originally posted by Ralph View Post
            Take care kcsystemtrader, your proposal is always rounding down to nearest ten. If you want it to round up too you need to insert a Math.Round() in addition.

            Regards
            Ralph
            You are right and I just noticed it doing that. I'm actually okay with it always rounding down (to be conservative) so I'll probably just leave it alone. Althought, I'm not sure where to place the Math.Round even if I did want to try and have it round up.

            Comment


              #7
              My pleasure to explain:

              Code:
              PositionSizeRounded = ((int)Math.Round(PositionSize/10))*10;

              Comment


                #8
                Originally posted by Ralph View Post
                My pleasure to explain:

                Code:
                PositionSizeRounded = ((int)Math.Round(PositionSize/10))*10;
                Thanks Ralph, that's very helpful. I also found that you can round to nearest 25, 50, 100 whatever, doesn't have to be 10.

                Code:
                        protected int round(int quantity) {
                            int divisor = 1;
                            if(quantity < 200) {
                                divisor = 10;
                            } else if(quantity < 500) {
                                divisor = 25;
                            } else if(quantity < 1000) {
                                divisor = 50;
                            } else {
                                divisor = 100;
                            }
                            return ((int)Math.Round((double)quantity/divisor))*divisor;;

                Comment


                  #9
                  Originally posted by cunparis View Post
                  Code:
                  return ((int)Math.Round(([B][COLOR=blue]double[/COLOR][/B])quantity/divisor))*divisor;;
                  ... and you corrected a weakness in my code example, cunparis. If PositionSize (in my example) is an integer instead of a double value, then it works not as expected. Type-cast is indead required in this case.

                  Thanks
                  Ralph

                  Comment

                  Latest Posts

                  Collapse

                  Topics Statistics Last Post
                  Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                  0 responses
                  571 views
                  0 likes
                  Last Post Geovanny Suaza  
                  Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                  0 responses
                  330 views
                  1 like
                  Last Post Geovanny Suaza  
                  Started by Mindset, 02-09-2026, 11:44 AM
                  0 responses
                  101 views
                  0 likes
                  Last Post Mindset
                  by Mindset
                   
                  Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                  0 responses
                  548 views
                  1 like
                  Last Post Geovanny Suaza  
                  Started by RFrosty, 01-28-2026, 06:49 PM
                  0 responses
                  549 views
                  1 like
                  Last Post RFrosty
                  by RFrosty
                   
                  Working...
                  X