Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Dynamic Auto Scaling

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

    Dynamic Auto Scaling

    I've created an indicator which plots the 52 week high/low as well as some other plots. There are times when the 52 high/low are far beyond the range of the current price resulting in a unreadable set of candlesticks. I want my other plots to auto scale but not the 52 week high/low if they're not near the current price range being displayed. Is there a way to achieve this using Ninjascript? Do I need to hide the plots programatically using my own criteria? Is it even possible to dynamically hide a single indicator plot using NS?

    Thanks.

    #2
    Hello stoner,

    Thanks for your question.

    By default, the script's Auto Scale behavior will adjust scaling to include objects and plots. You can override the auto scale behavior using OnCalculateMinMax. Please see documentation below for more details.

    OnCalculateMinMax - https://ninjatrader.com/support/help...lateminmax.htm

    I look forward to being of further assistance.

    Comment


      #3
      The sample code in that link was helpful. But I had to modify it a bit and this might help other people. My Indicator has two data series that use the same primary instrument with 2 separate time frames. The main time frame (BarsArray[0]) is driven by the NT charting UI. The second time frame (BarysArray[1]) is uses a hard coded Daily time frame.

      The issue was the sample code below assumes that the Close's underlying array is the BarsArray[0] structure but it wasn't in my case and I was getting an index related exception...

      Code:
      double plotValue = Close.GetValueAt(index);
      So I updated the code to make sure the ChartBars and BarsArray[0] are the same size and then I'm getting the Close from BarsArray[0].GetClose()...

      Code:
      public override void OnCalculateMinMax()
      {
      [B]if (ChartBars.Count == BarsArray[0].Count) {[/B]
            // 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 = [B]BarsArray[0].GetClose[/B](index);
      
               // return min/max of close value
               tmpMin = Math.Min(tmpMin, plotValue);
               tmpMax = Math.Max(tmpMax, plotValue);
            }
      
            // Finally, set the minimum and maximum Y-Axis values to +/- 50 ticks from the primary close value
            MinValue = tmpMin - 10 * TickSize;
            MaxValue = tmpMax + 10 * TickSize;
      [B]   }
         else
         {
            base.OnCalculateMinMax();
         }[/B]
      }
      Not sure why the ChartBars has the correct FromIndex/ToIndex and the wrong underlying BarsArray but it does in my case.

      Hope this helps.

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by Geovanny Suaza, 02-11-2026, 06:32 PM
      0 responses
      579 views
      0 likes
      Last Post Geovanny Suaza  
      Started by Geovanny Suaza, 02-11-2026, 05:51 PM
      0 responses
      334 views
      1 like
      Last Post Geovanny Suaza  
      Started by Mindset, 02-09-2026, 11:44 AM
      0 responses
      101 views
      0 likes
      Last Post Mindset
      by Mindset
       
      Started by Geovanny Suaza, 02-02-2026, 12:30 PM
      0 responses
      554 views
      1 like
      Last Post Geovanny Suaza  
      Started by RFrosty, 01-28-2026, 06:49 PM
      0 responses
      551 views
      1 like
      Last Post RFrosty
      by RFrosty
       
      Working...
      X