ATR averages the true range (the largest of high–low, high–prevclose and low–prevclose) over N bars. It measures how much an instrument typically moves per bar, so traders size stops and positions to current volatility instead of a fixed number of pips. Copy the version for your platform.
inputs: Length(14);
variables: MyATR(0);
MyATR = AvgTrueRange(Length);
Plot1(MyATR, "ATR");{ATR - Average True Range, period 14}
atrVal := ATR(14);
atrVal#property indicator_separate_window
#property indicator_buffers 1
input int ATR_Period = 14;
double AtrBuffer[];
int OnInit() {
SetIndexBuffer(0, AtrBuffer);
IndicatorSetString(INDICATOR_SHORTNAME, "ATR(" + IntegerToString(ATR_Period) + ")");
return INIT_SUCCEEDED;
}
int OnCalculate(const int rates_total, const int prev_calculated,
const datetime &time[], const double &open[], const double &high[],
const double &low[], const double &close[], const long &tick_volume[],
const long &volume[], const int &spread[]) {
for (int i = prev_calculated; i < rates_total; i++)
AtrBuffer[i] = iATR(NULL, 0, ATR_Period, i);
return rates_total;
}To use it inside dtcharts, paste any version into the Script Converter and it becomes a native indicator on your chart.