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 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 bmartz, 03-12-2024, 06:12 AM
        5 responses
        32 views
        0 likes
        Last Post NinjaTrader_Zachary  
        Started by Aviram Y, Today, 05:29 AM
        4 responses
        12 views
        0 likes
        Last Post Aviram Y  
        Started by algospoke, 04-17-2024, 06:40 PM
        3 responses
        28 views
        0 likes
        Last Post NinjaTrader_Jesse  
        Started by gentlebenthebear, Today, 01:30 AM
        1 response
        8 views
        0 likes
        Last Post NinjaTrader_Jesse  
        Started by cls71, Today, 04:45 AM
        1 response
        7 views
        0 likes
        Last Post NinjaTrader_ChelseaB  
        Working...
        X