How to achieve a fixed 1000-point Y-axis range on the chart for NQ futures (e.g., always showing exactly 1000 points vertically, like 24600–25600 initially, then auto-shifting to 24700–25700 as price rises, without shrinking in sideways moves or expanding in trends), can I use code to dynamically set the scale? Many Thanks
Part of your question is not very clear, and what timeframe but if the visible range of the bars are within 1000 points then the below should work.
bounds = 500;
lvRound = int( LastValue( C )/100 ) *100;
upRange = lvRound + bounds;
dnRange = lvRound - bounds;
Plot( upRange, "", colorDefault, styleNoLine | styleNoLabel );
Plot( dnRange, "", colorDefault, styleNoLine | styleNoLabel );
if your bars extend over 1000 points, only then it will be larger.
SetChartOptions allows setting custom scale programmatically
centerValue = (Highest(Close) + Lowest(Close)) / 2;
range = 1000;
yMin = centerValue - range / 2;
yMax = centerValue + range / 2;
SetChartOptions(0, chartShowDates | chartShowArrows, chartGridShow | chartGridMiddle, yMin, yMax);
I tried this but the "chartGridShow | chartGridMiddle," showed as error 5. 5th argument......
Thanks! Tried but it showed 0 to 40000
Quote from: AFL Function Reference - SETCHARTOPTIONS
ymin, ymax - (new in 5.07) these parameters specify Y-axis minimum and maximum values for custom scaling. If you specify any values that meet the condition ymin < ymax, AmiBroker will turn OFF automatic scaling and use specified min/max values for Y scale. Note that Mode argument controls when these settings are applied (0 - only when new chart is created, 1 - always), when modes 2 and 3 are used - scaling is not changed.
Also the values that you pass must be SCALAR values (not arrays). Your code produces arrays, that can't be used. ymin and ymax MUST BE SCALAR.
As Tomasz wrote, use scalar.
Better do it this way, you get scalar directly.
centerValue = ( HighestVisibleValue( H ) + LowestVisibleValue( L ) ) / 2;