• If this is your first visit, you will have to register before you can post. To view messages, please scroll below and select the forum that you would like to visits. Questions? Be sure to check out the Forum FAQ.

Announcement

Collapse
No announcement yet.

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 lunardiplomacy, Today, 07:57 AM
                  0 responses
                  1 view
                  0 likes
                  Last Post lunardiplomacy  
                  Started by Tagliareni, Yesterday, 08:10 PM
                  1 response
                  11 views
                  0 likes
                  Last Post NinjaTrader_PaulH  
                  Started by SDaCosta30, Today, 04:19 AM
                  1 response
                  11 views
                  0 likes
                  Last Post NinjaTrader_PaulH  
                  Started by memonolog, Today, 02:39 AM
                  5 responses
                  22 views
                  0 likes
                  Last Post memonolog  
                  Started by memonolog, Today, 07:07 AM
                  0 responses
                  5 views
                  0 likes
                  Last Post memonolog  
                  Working...
                  X