# Average True Range

This example is a User Defined Study that plots an Average True Range. Cut the script from this page and paste it in the ESPL Script Editor. Display a chart, and then add the study to the chart by clicking on the studies button, and then click on button #2 on the bottom of the study panel.

Edit the parameters for the study and set the 1st parameter to a value for the period in the average, such as 7. Click OK. The Average True Range will plot as a line near the bottom of the chart.

procedure AverageTrueRange;
var
rStudy,rScaleLow: real;
i,iPeriod: integer;
begin
SetUser(ePosition,eChart);
SetUser(ePlot1,True);      {plot 1st array}
SetUser(eShow2,True);      {show 2nd array}
SetUser(eShow3,True);      {show 3rd array}
SetUser(eClose,true);      {check close only box}
rScaleLow:=GetVariable(eScaleLow);
iPeriod:=GetUser(eParm1);
for i:=BarBeginLeft to BarEnd do begin
rStudy:=Average(eTrueRange,i,iPeriod);
SetUser(1,rStudy+rScaleLow,i);
SetUser(2,rScaleLow,i);
SetUser(3,rStudy,i);
end;
end;

begin
if ESPL=52 then AverageTrueRange;
end;

Notes:  rScaleLow will be the low scale value on the chart.  This will be an offset so the study plots on the chart. Any value to be plotted must fall within the range of the chart's scale.

iPeriod is the number of data points to average using a simple average formula.

The for loop makes calculations from the first bar on the left through the last bar on the right. i will be the index for each bar.  This is a standard loop for use in a user defined study. You could think of the for loop as a command to plot the function inside its begin..end block.  In the for loop's begin..end block we write the code for the study calculation.

rStudy is the Average True range value to plot.  It is calculated by using the Average function, passing the eTrueRange constant, bar index, and number of periods in the average.

SetUser(1... is the 1st line we want to draw.  This is one of three arrays that the program can plot. All we have to do is fill the array with values. Notice that we fill the array with the study value + the bottom scale value. We do not want to display this value so we unchecked the Show Value 1 box.

SetUser(2... is the 2nd line we want to draw. I thought it would be nice to have a horizontal line drawn at the bottom of the chart. So this array is filled with the bottom scale value.  We do not want to display this value so we unchecked the Show Value 2 box.

SetUser(3... fills the 3rd array with the pure value of the study.  This value cannot plot because it does not fall in the chart's scale range. However, I want to display the pure value, not an offset value.  So, I placed the pure value in the 3rd array, and told the parameter form to Show Value 3.  The program does not plot this array because we unchecked the Draw Line 3 box.  The parameter form was told to draw the data in arrays 1 and 2, but not in 3.

This is really powerful stuff. I hope this example is simple enough for you to grasp the basic technique used to implement a user defined study, and see the flexibility of interfacing with the parameter form to obtain parameters, and control which arrays are plotted and which display values in the study panel on the left edge of a chart.