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

    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. ​

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by Geovanny Suaza, 02-11-2026, 06:32 PM
        0 responses
        599 views
        0 likes
        Last Post Geovanny Suaza  
        Started by Geovanny Suaza, 02-11-2026, 05:51 PM
        0 responses
        344 views
        1 like
        Last Post Geovanny Suaza  
        Started by Mindset, 02-09-2026, 11:44 AM
        0 responses
        103 views
        0 likes
        Last Post Mindset
        by Mindset
         
        Started by Geovanny Suaza, 02-02-2026, 12:30 PM
        0 responses
        558 views
        1 like
        Last Post Geovanny Suaza  
        Started by RFrosty, 01-28-2026, 06:49 PM
        0 responses
        557 views
        1 like
        Last Post RFrosty
        by RFrosty
         
        Working...
        X