Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Round up to the nearest One Integer

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

    Round up to the nearest One Integer

    Hi,

    I have the following script and I want to round up the Value to the nearest one integer. It does not work as intended, instead it rounds up to the nearest 10. Please advise how do I modify it the round up to the nearest 1?

    protected override void OnBarUpdate()
    {
    // Multiplying the high, low, and close values by 4
    double high0 = MultiplyByFour(High[0]);
    double low0 = MultiplyByFour(Low[0]);

    if (CurrentBar == 0)
    {
    Value[0] = high0 - low0;
    }
    else
    {
    double close1 = MultiplyByFour(Close[1]);
    double trueRange = Math.Max(Math.Abs(low0 - close1), Math.Max(high0 - low0, Math.Abs(high0 - close1)));
    Value[0] = ((Math.Min(CurrentBar + 1, Period) - 1) * Value[1] + trueRange) / Math.Min(CurrentBar + 1, Period);
    Value[0] = Math.Round(Value[0]);// Round the ATR value to the nearest integer
    }

    Thank you
    CW​

    #2
    Hello kevtancw,

    Thank you for your post.

    Can you please provide more details? What are the values you are getting compared to the values are you expecting?

    Using Math.Round() is considered general C# education and not NinjaScript specific. Below is a link from a quick google search on how to use Math.Round:

    https://stackoverflow.com/questions/8844674/how-to-round-to-the-nearest-whole-number-in-c-sharp
    Gaby V.NinjaTrader Customer Service

    Comment


      #3
      Hi Gaby,

      I am expecting a Value of 7.
      In my ATRR script I have multiplied the ATR value by 4 and then attempt to round the value up.
      In the attached the ATR value is 1.56, so my ATRR should be 6.24 and rounded up to 7.

      Instead of 7 the output is 10.

      Why is that so?

      CW

      Attached Files

      Comment


        #4
        Hello kevancw,

        This screenshot of the chart doesn't provide any insight into how the script is actually working.

        Try using Math.Round(yourValue, 0, MidpointRounding.AwayFromZero);

        If this doesn't work you'll need to provide debugging output, printing out the value you are rounding and the rounded value. ​
        Gaby V.NinjaTrader Customer Service

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by HappyTrader76, Today, 03:56 PM
        0 responses
        3 views
        0 likes
        Last Post HappyTrader76  
        Started by mjairg, 07-31-2024, 11:48 AM
        7 responses
        101 views
        0 likes
        Last Post Ducman_nl  
        Started by meatskin, Today, 03:07 PM
        0 responses
        13 views
        0 likes
        Last Post meatskin  
        Started by Pa_Baz, 12-14-2024, 09:40 PM
        26 responses
        373 views
        1 like
        Last Post MarioM94  
        Started by crystalet, 12-06-2022, 06:59 AM
        2 responses
        52 views
        1 like
        Last Post mrmark123  
        Working...
        X