Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Rounding Question

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

    Rounding Question

    To anyone C# savvy enough to help me out...

    I'm trying to round a number to the hundreds place. Yes, that's hundreds and not hundred-ths place. My example below, shows exactly what I'm talking about.

    myNumber = 213.4444444

    (1) Math.Round(myNumber, 2) = 213.44 <-- hundred-ths
    (2) Math.Round(myNumber, 1) = 213.4 <--ten-ths
    (3) Math.Round(myNumber, 0) = 213
    (4) Math.Round(myNumber, -1) = 210 <--tens
    (5) Math.Round(myNumber, -2) = 200 <--hundreds


    Ultimately, I'm trying to arrive at (5), as I am writing a position sizing algorithm that I want to always result in an even multiple of 100 shares.

    Is there a function that will round to the "left" of the decimal? I know in VB that the code woud be Round(myNumber, -2). However, in C# the Round() function does not take negative numbers.

    Help.

    #2
    fosch, I'll post the reply here that I emailed you for future reference and in case anyone else has the same question.

    This is a function that you will need to place on the same level as OnBarUpdate() (see screenshot for details):
    Code:
    static double Round(double value, int digits)
    {
        if ((digits < -15) || (digits > 15))
            throw new ArgumentOutOfRangeException("digits", "Rounding digits must be between -15 and 15, inclusive.");
    
        if (digits >= 0)
            return Math.Round(value, digits);
    
        double n = Math.Pow(10, -digits);
        return Math.Round(value / n, 0) * n;
    }
    
    protected override void OnBarUpdate()
    {
        if (CurrentBar == 0)
        {
            try
            {
                double number = 213.4444444;
                double newNumber = Round(number, -2);
                Print(newNumber);
                // Prints "200"
            }
            catch (Exception ex)
            {
                Print(ex.ToString());
            }
        }
    }
    The page with the rest of the information (and where I pulled the code from) can be found here.
    Attached Files
    AustinNinjaTrader Customer Service

    Comment


      #3
      Originally posted by fosch View Post
      To anyone C# savvy enough to help me out...

      I'm trying to round a number to the hundreds place. Yes, that's hundreds and not hundred-ths place. My example below, shows exactly what I'm talking about.

      myNumber = 213.4444444

      (1) Math.Round(myNumber, 2) = 213.44 <-- hundred-ths
      (2) Math.Round(myNumber, 1) = 213.4 <--ten-ths
      (3) Math.Round(myNumber, 0) = 213
      (4) Math.Round(myNumber, -1) = 210 <--tens
      (5) Math.Round(myNumber, -2) = 200 <--hundreds


      Ultimately, I'm trying to arrive at (5), as I am writing a position sizing algorithm that I want to always result in an even multiple of 100 shares.

      Is there a function that will round to the "left" of the decimal? I know in VB that the code woud be Round(myNumber, -2). However, in C# the Round() function does not take negative numbers.

      Help.
      fosch,

      double x = Math.Round(myNumber * 0.01, 0) * 100;

      That should do the trick.

      In position sizing you may want the result to be 200 if myNumber is 287. Rounding would give the result of 300.

      You might use:
      double x = Math.Floor(myNumber * 0.01) * 100;
      Last edited by NinjaTrader_Art; 06-29-2010, 07:18 PM.
      ArtSenior Software Developer

      Comment


        #4
        Austin, I received your e-mail right after I made this post. You should've received an e-mail back from me already thanking you. I tried your code out, and it words very nicely. Very robust.

        Art, I love the way that you just broke it down to simple number manipulation.

        Thank you both for your replies. Now, if I can only figure out which one to use.

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by t.kelly4149, Today, 05:36 PM
        1 response
        7 views
        0 likes
        Last Post bltdavid  
        Started by SilverSurfer1, Today, 06:29 PM
        0 responses
        16 views
        0 likes
        Last Post SilverSurfer1  
        Started by trader3000a, Today, 01:06 PM
        1 response
        9 views
        0 likes
        Last Post NinjaTrader_ShawnB  
        Started by marcus2300, Today, 11:45 AM
        3 responses
        31 views
        0 likes
        Last Post marcus2300  
        Started by pel11, Today, 11:06 AM
        0 responses
        13 views
        0 likes
        Last Post pel11
        by pel11
         
        Working...
        X