OnCalculateMinMax()

View as Markdown

Definition

An event driven method which is called while the chart scale is being updated. This method is used to determine the highest and lowest value that can be used for the chart scale. It is only called when the chart object is either set to IsAutoScale while there are multiple charts objects rendered or only a single object would be rendered on the chart.

The indexer used to look up a Series\<t\> value through barsAgo is NOT guaranteed to be in sync when the OnCalculateMinMax() method is called. You will need to use GetValueAt() to obtain a historical value at a specified absolute index.

Method Return Value

This method does not return a value.

Syntax

You must override the method in your NinjaScript object with the following syntax:

public override void OnCalculateMinMax()

Method Parameters

This method does not accept any parameters.

Examples

1protected override void OnStateChange()
2{
3 if (State == State.SetDefaults)
4 {
5 Name = "Example Indicator";
6 IsOverlay = true;
7
8 // set this to true to ensure CalculateMinMix() is called
9 IsAutoScale = true;
10 }
11}
12
13public override void OnCalculateMinMax()
14{
15 // make sure to always start fresh values to calculate new min/max values
16 double tmpMin = double.MaxValue;
17 double tmpMax = double.MinValue;
18
19 // For performance optimization, only loop through what is viewable on the chart
20 for (int index = ChartBars.FromIndex; index <= ChartBars.ToIndex; index++)
21 {
22 // since using Close[0] is not guaranteed to be in sync
23 // retrieve "Close" value at the current viewable range index
24 double plotValue = Close.GetValueAt(index);
25
26 // return min/max of close value
27 tmpMin = Math.Min(tmpMin, plotValue);
28 tmpMax = Math.Max(tmpMax, plotValue);
29 }
30
31 // Finally, set the minimum and maximum Y-Axis values to +/- 50 ticks from the primary close value
32 MinValue = tmpMin - 50 * TickSize;
33 MaxValue = tmpMax + 50 * TickSize;
34}