Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Controlling auto-scaling of chart with OnCalculateMinMax()

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

    Controlling auto-scaling of chart with OnCalculateMinMax()

    I faced a challenge with an indicator I am developing where I needed to plot lines along the foot of the chart, and even though I ensured that the minimum price of each plot was some distance above the ChartPanel.MinValue, my charts (with auto-scaling enabled) would develop a life of their own, and get into a loop whereby the chart would constantly re-scale until price was a thin line at the top of the chart.

    I guessed it was doing this because the underlying framework detected a line that was plotted below what is considered to be a safe minimum price. I followed the process recommended in the online documentation for OnCalculateMinMax() but frustratingly this just seemed to cause more problems.

    ​Eventually I found the problem(s) and I will share them here in the hopes that it saves you going through the frustrating process of trying to figure it out. This is what it looks like (when I got it working):
    Auto-scaling chart with plot at the low price


    They were two things that I needed to change:
    1. My indicator loads multiple time-frames of the same instrument, and this was causing havoc in the OnCalculateMinMax() function which I had copied directly from the example provided.
    2. The example provides a buffer of 50 ticks above and below the chart, which caused strangely scaled charts at different zoom scales​
    The solution was to:
    1. Not use the standard Close.GetValueAt() because of the multiple data series that the indicator contains. Simply changing this to Closes[0].GetValueAt() solved that problem.
    2. Instead of adding a buffer as a number of ticks, I calculate the buffer as a percentage of the full price range (in this case 5%)
    I went a bit further by providing more space at the foot of the chart to accommodate the sine wave lines, and added a try-catch to catch errors.

    Here is the code which successfully handles auto-scaling of the multiple data series indicator:​

    Code:
    public override void OnCalculateMinMax()
    {
       if (State != State.Historical)
       {
          try
          {
             // make sure to always start fresh values to calculate new min/max values
             double tmpMin = double.MaxValue;
             double tmpMax = double.MinValue;
             // For performance optimization, only loop through what is viewable on the chart
             for (int index = ChartBars.FromIndex; index <= ChartBars.ToIndex; index++)
             {
               // since using Close[0] is not guaranteed to be in sync
               // retrieve "Close" value at the current viewable range index
               double plotValue = Closes[0].GetValueAt(index);
    
               // return min/max of close value
               tmpMin = Math.Min(tmpMin, plotValue);
               tmpMax = Math.Max(tmpMax, plotValue);
            }
    
           double buffer = (50 * TickSize); // default
           if(buffer>(tmpMax-tmpMin)/20) buffer=(tmpMax-tmpMin)/20; //5% of price range
           double lowbuffer = buffer;
    
           // adjust by 4 diamonds
           if(lowbuffer<last_diamond_height*4) lowbuffer=last_diamond_height*4;
    
           // Finally, set the minimum and maximum Y-Axis values to the buffers (+/- 50 ticks from the primary close value by default)
           MinValue = tmpMin - lowbuffer;
           MaxValue = tmpMax + buffer;
        }
       catch(Exception ex)
       {
          Print("Error calculating max/min: " + ex.Message);
       }
    }
    Attached Files

    #2
    Hi SentientDave,
    I tried re-creating your strategy but was only able to put two horizontal lines at the bottom but they don't update. Can you share the part where you use the strategies' values to calculate your MA lines? Attached is my attempt at what you did but only showing two horizontal lines instead of MAs.
    Thanks for your help!
    -whiterhino
    Attached Files

    Comment


      #3
      I've been working on autoscaling tool and run into this issue as well. Thanks for the code.

      Comment


        #4
        fwiw, I don't believe OnCalculateMinMax is called in any other state than Realtime.


        FWIW, For my indicators that it might be a "heavy lift" to calculate MinMax (e.g. loops over many different plots or something), I also reduce the times it does the work by wrapping it in something like this which only updates Min/Max when the chart is scrolled, scaled or a new bar is printed. If you're indicator is not Calculate.OnBarClose, this might miss if the values pushing on the last realtime bar, but I find not doing that is ok in some cases and also reduces chart jumps.

        Code:
        int ranMinMax;
        public override void OnCalculateMinMax() {
           int range = ChartBars.ToIndex + 2*ChartBars.FromIndex;
           if (ranMinMax == range)
              return;
           ranMinMax = range;
        ​
        I may be a little OCD about squeezing performance out of NT8 given some issues I've seen...

        Last edited by b.j.d; 03-09-2025, 03:01 AM.

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by Geovanny Suaza, 02-11-2026, 06:32 PM
        0 responses
        553 views
        0 likes
        Last Post Geovanny Suaza  
        Started by Geovanny Suaza, 02-11-2026, 05:51 PM
        0 responses
        324 views
        1 like
        Last Post Geovanny Suaza  
        Started by Mindset, 02-09-2026, 11:44 AM
        0 responses
        100 views
        0 likes
        Last Post Mindset
        by Mindset
         
        Started by Geovanny Suaza, 02-02-2026, 12:30 PM
        0 responses
        543 views
        1 like
        Last Post Geovanny Suaza  
        Started by RFrosty, 01-28-2026, 06:49 PM
        0 responses
        546 views
        1 like
        Last Post RFrosty
        by RFrosty
         
        Working...
        X