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

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 pechtri, 06-22-2023, 02:31 AM
                  9 responses
                  122 views
                  0 likes
                  Last Post NinjaTrader_ChelseaB  
                  Started by frankthearm, 04-18-2024, 09:08 AM
                  16 responses
                  66 views
                  0 likes
                  Last Post NinjaTrader_Clayton  
                  Started by habeebft, Today, 01:18 PM
                  1 response
                  5 views
                  0 likes
                  Last Post NinjaTrader_ChelseaB  
                  Started by benmarkal, Today, 12:52 PM
                  2 responses
                  15 views
                  0 likes
                  Last Post benmarkal  
                  Started by f.saeidi, Today, 01:38 PM
                  1 response
                  9 views
                  0 likes
                  Last Post NinjaTrader_BrandonH  
                  Working...
                  X