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 double to four decimal places

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

    Round double to four decimal places

    Hello. Help me how to round double to 0.0001?
    Math.Round(double value, 4)? This returned wrong value.

    #2
    Hi Alexstox,

    Thank you for posting.

    What you have posted is the correct way to achieve this.

    Can you provide the code that you are using this Math.Round in and what values you are getting?
    Cal H.NinjaTrader Customer Service

    Comment


      #3
      I used this to round 3 different ATR values 1 day ago (EOD data, daily bars).
      Math.Round(ATR(100)[1]+ATR(50)[1]+ATR(20)[1]). Is this code correct?

      Comment


        #4
        Alexstox,

        You need to pass through what you want to round the values too.

        Math.Round(ATR(100)[1]+ATR(50)[1]+ATR(20)[1], 4);
        Cal H.NinjaTrader Customer Service

        Comment


          #5
          Originally posted by alexstox View Post
          Hello. Help me how to round double to 0.0001?
          Math.Round(double value, 4)? This returned wrong value.
          What were you expecting? What did it return?

          Comment


            #6
            The value should be >0.0100, but it's only 0.0068

            Comment


              #7
              Alexstox,

              You will then need to adjust the Math.Round() to 2 instead of 4.

              Remember, this returns the number of decimals. 0.0068 is 4 decimal places
              Cal H.NinjaTrader Customer Service

              Comment


                #8
                Originally posted by alexstox View Post
                The value should be >0.0100, but it's only 0.0068
                You will have to specify the rounding method then.
                Code:
                Math.Round(var, 4, MidpointRounding.AwayFromZero);
                where "var" is the variable that you are rounding.

                Comment


                  #9
                  I'm sorry. Maybe I was not correct. The true value is about 0.0135 (I counted manually). But method returned 0.0068. That what I mean.

                  Comment


                    #10
                    Originally posted by alexstox View Post
                    I'm sorry. Maybe I was not correct. The true value is about 0.0135 (I counted manually). But method returned 0.0068. That what I mean.
                    That is impossible in an absolute sense. You may be expecting that result, so you will have to check that the input value is correct: the issue is not with the Rounding method; it is with your input to the method.

                    Check the code that produces the result that you are trying to round: it must be in error, in terms of producing what you intend, as opposed to what is really written.

                    Comment


                      #11
                      I don't understand, why everything is OK now. I will answer later. Will try to reproduce error =)

                      Comment


                        #12
                        Originally posted by alexstox View Post
                        I'm sorry. Maybe I was not correct. The true value is about 0.0135 (I counted manually). But method returned 0.0068. That what I mean.
                        That's 1/2 for some reason.

                        Comment


                          #13
                          Originally posted by sledge View Post
                          that's 1/2 for some reason.
                          :d :d:d:d:d:d

                          Comment


                            #14
                            I'm running into a similar issue here...

                            My strategy is coded to do this equation:
                            Code:
                            result= Math.Round( (HMA(21)[1] / HMA(21)[0]), 8 )
                            That's this: 48.016203463203 / 48.015935064935 = 0.99999441

                            0.99999441 is exactly what I want, and that's exactly what I get.

                            Now, if I want to take that number and others like it and make them a smidge more manageable, I want to take the inverse of it, or, subtract that result from 1:

                            1 - .99999441 = 0.00000559

                            And I would multiply that result by 10000 or something to get something even more manageable.

                            The problem is, in the very same strategy where this works just fine:
                            Code:
                            result= Math.Round( (HMA(21)[1] / HMA(21)[0]), 8 )
                            this does not:
                            Code:
                            modresult= Math.Round( 1 - result, 8  );
                            and neither does this:
                            Code:
                            modresult= Math.Round(  (1 - Math.Round( (HMA(21)[1] / HMA(21)[0]), 8 )  ), 8);
                            because both return a value of this: 5.59E-06.

                            Why does the first example of rounding produce what I want, while the second one still forces it to scientific notation? Both variables are doubles. I want to get a value of 0.00000559. What am I missing here?

                            Thanks in advance,
                            Gary

                            Comment


                              #15
                              Hello,

                              Thank you for the question.

                              This would be based on how C# handles double numbers.

                              In this case, the equation you have is basically correct but if you print the results the notation would be displayed instead.

                              If you take your example and plot the values you should see that the values are correct, only when you print the value would you see the notation.

                              To use the value in your equations, you can just use what you have now or what I was using in the test:
                              Code:
                              double result = Math.Round((HMA(21)[1]/HMA(21)[0]), 8);
                              
                              double modresult= Math.Round( 1 - result, 8  );
                              Plotting modresult works but Print does not, this is because of how ToString works with doubles.

                              To avoid the notation you could do this instead:
                              Code:
                              Print(modresult.ToString("##.#########"));
                              This just specifies that we want to print the additional decimal places. Anywhere you print the value you would need to use the ToString("##.#########") otherwise anywhere you are just checking the value you can leave the variable as is.

                              I look forward to being of further assistance.
                              JesseNinjaTrader Customer Service

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by Waxavi, Today, 02:10 AM
                              0 responses
                              3 views
                              0 likes
                              Last Post Waxavi
                              by Waxavi
                               
                              Started by TradeForge, Today, 02:09 AM
                              0 responses
                              8 views
                              0 likes
                              Last Post TradeForge  
                              Started by Waxavi, Today, 02:00 AM
                              0 responses
                              2 views
                              0 likes
                              Last Post Waxavi
                              by Waxavi
                               
                              Started by elirion, Today, 01:36 AM
                              0 responses
                              4 views
                              0 likes
                              Last Post elirion
                              by elirion
                               
                              Started by gentlebenthebear, Today, 01:30 AM
                              0 responses
                              4 views
                              0 likes
                              Last Post gentlebenthebear  
                              Working...
                              X