CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Feb 2010
    Posts
    4

    Rounding or Modulus

    I have had only one programming class a long time ago and am in over my head (Shake is pretty dumb.) All I want to do is round down to the nearest 0.25 increment.

    Say my number is 1879.92 (closer to 1880 than 1879.75), I would like a value of 1879.75 returned.
    If the number is 1764.27, I would like 1764.25 returned.

    I think modulus only works for returning whole numbers.

    Is there an easier way than using a nested if statement like?

    double price;
    double roundedprice;
    double remainder;

    //price is calculated earlier in another section of code

    roundedprice = Round(price);
    remainder = price - roundedprice;

    if (remainder >= 0.75) { price = roundedprice + 0.75 }
    if (remainder >= 0.50) { price = roundedprice + 0.50 }

    if (remainder >= 0.25) { price = roundedprice + 0.25 }

    if (remainder >= 0.00) { price = roundedprice + 0.00 }

    Any thoughts?

    The actual part of code that I am trying to insert this into follows:
    Code:
                // Condition set 1
                if (DefaultInput[0] > ChandelierSAR(5, 3.5).Chandelier[0]
                    && ToTime(Time[0]) > ToTime(8, 30, 0)
                    && ToTime(Time[0]) < ToTime(15, 0, 0))
                {
                    EnterShortStopLimit(DefaultQuantity, ChandelierSAR(5, 3.5).Chandelier[0], ChandelierSAR(5, 3.5).Chandelier[0], "");
                    Alert("MyAlert0", Priority.High, "", "", 1, Color.White, Color.Black);
                }
    
                // Condition set 2
                if (DefaultInput[0] < ChandelierSAR(5, 3.5).Chandelier[0]
                    && ToTime(Time[0]) > ToTime(8, 30, 0)
                    && ToTime(Time[0]) < ToTime(15, 0, 0))
                {
                    ExitShortStopLimit(ChandelierSAR(5, 3.5).Chandelier[0], ChandelierSAR(5, 3.5).Chandelier[0], "", "");
                }
    Last edited by Master Shake; February 17th, 2010 at 10:03 PM.

  2. #2
    Join Date
    Jun 2001
    Location
    Melbourne/Aus (C# .Net 4.0)
    Posts
    686

    Re: Rounding or Modulus

    Are all of the numbers to a maximum of 2 decimal places?
    Rob
    -
    Ohhhhh.... Old McDonald was dyslexic, E O I O EEEEEEEEEE.......

  3. #3
    Join Date
    Jun 2001
    Location
    Melbourne/Aus (C# .Net 4.0)
    Posts
    686

    Re: Rounding or Modulus

    Still messy but....
    Code:
    price = remainder >= 0.75 ? roundedprice + 0.75 : 
        remainder >= 0.5 ? roundedprice + 0.5 : 
        remainder >= 0.25 ? roundedprice + 0.25 :
        roundedprice;
    Or something along the lines of multiplying by 100, converting to integer, taking the modulo 25, doing the business and dividing by 100....
    Rob
    -
    Ohhhhh.... Old McDonald was dyslexic, E O I O EEEEEEEEEE.......

  4. #4
    Join Date
    Feb 2010
    Posts
    4

    Re: Rounding or Modulus

    Yes, two number decimals only.

  5. #5
    Join Date
    Feb 2010
    Posts
    4

    Re: Rounding or Modulus

    hello, i just give it a try. here's what i did.

    remainder = price - Math.Truncate(price);
    price = Math.Truncate(price) + (Math.Truncate(remainder / 0.25) * .25);


    hope that helps. =)

  6. #6
    Join Date
    Feb 2010
    Posts
    4

    Re: Rounding or Modulus

    I guess I left out a valuable bit of information:

    for "Condition Set 1" the price needs to be rounded down to the nearest quarter

    for "Condition Set 2" the price needs to be rounded UP to the nearest quarter

    I guess I need to rethink the code a bit since I have to insert that portion inside the cases now.

    I had the following: renaming roundedprice = chandyRounded, remainder = chancyRemainder, and price = ChandelierSAR(5, 3.5).Chandelier[0]

    Code:
           private double chandyRounded = 1; // Default setting for ChandyRounded
            private double chandyRemainder = 1; // Default setting for ChandyRemainder
            private double roundUp = 1; // Default setting for RoundUp
            private double roundDown = 1; // Default setting for RoundDown
            // User defined variables (add any user defined variables below)
            #endregion
    
            /// <summary>
            /// This method is used to configure the strategy and is called once before any strategy method is called.
            /// </summary>
            protected override void Initialize()
            {
    
                CalculateOnBarClose = true;
            }
    
            /// <summary>
            /// Called on each bar update event (incoming tick)
            /// </summary>
            protected override void OnBarUpdate()
            {
    			chandyRounded = Round(ChandelierSAR(5, 3.5).Chandelier[0];
    			chandyRemainder = ChanderlierSAR(5, 3.5).Chandelier[0] - chandyRounded;
    			
                // Condition set 1
                if (DefaultInput[0] > ChandelierSAR(5, 3.5).Chandelier[0]
                    && ToTime(Time[0]) > ToTime(8, 30, 0)
                    && ToTime(Time[0]) < ToTime(15, 0, 0))
                {
                    EnterShortStopLimit(DefaultQuantity, ChandelierSAR(5, 3.5).Chandelier[0], ChandelierSAR(5, 3.5).Chandelier[0], "");
                    Alert("MyAlert0", Priority.High, "", "", 1, Color.White, Color.Black);
                }
    
                // Condition set 2
                if (DefaultInput[0] < ChandelierSAR(5, 3.5).Chandelier[0]
                    && ToTime(Time[0]) > ToTime(8, 30, 0)
                    && ToTime(Time[0]) < ToTime(15, 0, 0))
                {
                    ExitShortStopLimit(ChandelierSAR(5, 3.5).Chandelier[0], ChandelierSAR(5, 3.5).Chandelier[0], "", "");
                    
                }
    Last edited by Master Shake; February 17th, 2010 at 10:02 PM.

  7. #7
    Join Date
    Jun 2001
    Location
    Melbourne/Aus (C# .Net 4.0)
    Posts
    686

    Re: Rounding or Modulus

    Note... there is an [Edit] button against your post, to save multiple posts...
    Rob
    -
    Ohhhhh.... Old McDonald was dyslexic, E O I O EEEEEEEEEE.......

  8. #8
    Join Date
    Jun 2001
    Location
    Melbourne/Aus (C# .Net 4.0)
    Posts
    686

    Re: Rounding or Modulus

    Could you please repost the code above between code tags eg:
    [C0DE]
    nicely formatted code goes here
    [/C0DE]

    the zero in CODE should be a letter O.
    Rob
    -
    Ohhhhh.... Old McDonald was dyslexic, E O I O EEEEEEEEEE.......

  9. #9
    Join Date
    Feb 2010
    Posts
    4

    Re: Rounding or Modulus

    Here is what I have so far.

    Code:
            private double chandyRounded = 1; // Default setting for ChandyRounded
            private double chandyRemainder = 1; // Default setting for ChandyRemainder
            private double roundUp = 1; // Default setting for RoundUp
            private double roundDown = 1; // Default setting for RoundDown
            // User defined variables (add any user defined variables below)
            #endregion
    
            /// <summary>
            /// This method is used to configure the strategy and is called once before any strategy method is called.
            /// </summary>
            protected override void Initialize()
            {
    
                CalculateOnBarClose = true;
            }
    
            /// <summary>
            /// Called on each bar update event (incoming tick)
            /// </summary>
            protected override void OnBarUpdate()
            {
    			chandyRounded = Math.Round(ChandelierSAR(5, 3.5).Chandelier[0]);
    			chandyRemainder = ChandelierSAR(5, 3.5).Chandelier[0] - chandyRounded;
    			
                // Condition set 1
                if (DefaultInput[0] > ChandelierSAR(5, 3.5).Chandelier[0]
                    && ToTime(Time[0]) > ToTime(8, 30, 0)
                    && ToTime(Time[0]) < ToTime(15, 0, 0))
                {
                    roundDown = chandyRemainder >= 0.75 ? chandyRounded + 0.75 : 
        				chandyRemainder >= 0.5 ? chandyRounded + 0.5 :
        				chandyRemainder >= 0.25 ? chandyRounded + 0.25 :
        				chandyRemainder;
    				EnterShortStopLimit(DefaultQuantity, roundDown, roundDown, "");
                    Alert("MyAlert0", Priority.High, "", "", 1, Color.White, Color.Black);
                }
    
                // Condition set 2
                if (DefaultInput[0] < ChandelierSAR(5, 3.5).Chandelier[0]
                    && ToTime(Time[0]) > ToTime(8, 30, 0)
                    && ToTime(Time[0]) < ToTime(15, 0, 0))
                {
                    roundUp = chandyRemainder <= 0.25 ? chandyRounded + 0.25 : 
        				chandyRemainder <= 0.5 ? chandyRounded + 0.5 :
        				chandyRemainder <= 0.75 ? chandyRounded + 0.75 :
        				chandyRemainder;
    				ExitShortStopLimit(roundUp, roundUp, "", "")
                }
    Last edited by Master Shake; February 18th, 2010 at 12:01 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured