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 to the nearest integer...

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

    Rounding to the nearest integer...

    How does one round to the nearest integer.
    The particular reason is because I want to be able to take a fixed dollar amount, divide it by the risk to get the position size.
    I'm coming up with errors because it's rarely a whole number.

    double POSITIONsize = Math.Round(500/(ATR(Closes[0], 20)[0]*50));

    EnterLongLimit(
    0, false, POSITIONsize, Lows[0][0] + OFFSETentry * TickSize, "Enter Long");

    doesn't seem to do the trick because the system doesn't know what a .3 (for example of a contract is) and "int POSITIONsize" comes up with the same error.

    Thank in advance.

    #2
    8DTK8,

    Please search google for methods of rounding numbers in C#. You should be able to find resources capable of answering your question more in depth than anything I could offer.
    Josh P.NinjaTrader Customer Service

    Comment


      #3
      Thanks,

      I guess a better question is how do you make the doube into an int. The Math.Round() that I used works just fine but the variable that's supposed to represent what is now a whole number still has to be a double instead of an int for it to compile.

      I'm not sure if you're previous response was an answer to my question above. If so I will try to look it up somewhere else.

      Thanks again,


      Comment


        #4
        If you want to convert double to int you can just cast it to an int.

        (int) someDoubleVar
        Josh P.NinjaTrader Customer Service

        Comment


          #5
          Thank you!

          I do lots of thanking in advance, but here it is again after the fact...

          Thank you! Thank you! Thank you! Thank you! Thank you!

          I realize that these are all the most simplistic of programing considerations that anyone who has taken an intro class could answer, but to me whose first encounter with C# is through NT, it's gold.

          It's much appreciated.

          Comment


            #6
            Can we idiots get a clear example? thanks.

            does not work...................

            #region Variables
            private int period = 14;
            private int trueRangeInt = 0;
            private double trueRange = 0;
            #endregion
            .
            .
            .
            .
            .

            if (CurrentBar == 0)
            Value.Set(High[0] - Low[0]);
            else
            {
            trueRange = High[0] - Low[0];
            trueRange = Math.Max(Math.Abs(Low[0] - Close[1]), Math.Max(trueRange, Math.Abs(High[0] - Close[1])));
            trueRange = trueRange / 0.03125;
            trueRangeInt = (int) trueRange;
            Value.Set(((Math.Min(CurrentBar + 1, Period) - 1 ) * Value[1] + (int) trueRange) / Math.Min(CurrentBar + 1, Period));
            }
            .
            .
            .
            It displays x.xx not x

            why???

            thanks.

            Comment


              #7
              What is Value[1]?

              print that out. That looks like your weak link.

              Comment


                #8
                Originally posted by [email protected] View Post
                does not work...................

                #region Variables
                private int period = 14;
                private int trueRangeInt = 0;
                private double trueRange = 0;
                #endregion
                .
                .
                .
                .
                .

                if (CurrentBar == 0)
                Value.Set(High[0] - Low[0]);
                else
                {
                trueRange = High[0] - Low[0];
                trueRange = Math.Max(Math.Abs(Low[0] - Close[1]), Math.Max(trueRange, Math.Abs(High[0] - Close[1])));
                trueRange = trueRange / 0.03125;
                trueRangeInt = (int) trueRange;
                Value.Set(((Math.Min(CurrentBar + 1, Period) - 1 ) * Value[1] + (int) trueRange) / Math.Min(CurrentBar + 1, Period));
                }
                .
                .
                .
                It displays x.xx not x

                why???

                thanks.
                Value is always a double, so your expression will always be evaluated and displayed as a double.

                Comment


                  #9
                  Thank you Sledge and Koganam

                  I will make some adjustments and keep you posted. Thanks!!

                  Comment

                  Latest Posts

                  Collapse

                  Topics Statistics Last Post
                  Started by BarzTrading, Today, 07:25 AM
                  2 responses
                  23 views
                  1 like
                  Last Post BarzTrading  
                  Started by devatechnologies, 04-14-2024, 02:58 PM
                  3 responses
                  20 views
                  0 likes
                  Last Post NinjaTrader_BrandonH  
                  Started by tkaboris, Today, 08:01 AM
                  0 responses
                  4 views
                  0 likes
                  Last Post tkaboris  
                  Started by EB Worx, 04-04-2023, 02:34 AM
                  7 responses
                  163 views
                  0 likes
                  Last Post VFI26
                  by VFI26
                   
                  Started by Mizzouman1, Today, 07:35 AM
                  1 response
                  10 views
                  0 likes
                  Last Post NinjaTrader_Gaby  
                  Working...
                  X