WiseTrader Toolbox

VariablePeriodATR

Background

Average True Range (ATR), introduced by J. Welles Wilder, measures volatility by averaging the "true range" of each bar — the largest of the high-to-low move and the gaps to the previous close. It does not indicate direction; it simply tells you how much an instrument typically moves. Traders use it to size positions, set volatility-based stops and scale targets.

A standard ATR uses one fixed averaging period. VariablePeriodATR takes a period for each bar, so the volatility window can adapt — for example reacting faster during turbulent periods. Because ATR is always derived from the current symbol's High, Low and Close, those arrays are read internally and the function takes only the periods array.

Included formula

The toolbox ships a ready-made Adaptive ATR indicator built on this function, so you can use it without writing any code. In AmiBroker open the Charts window, expand the WiseTraderToolbox group and drag Adaptive ATR onto a chart.

It plots the Average True Range in its own pane below price. The Adaptor you pick is what produces the per-bar period this function needs — so the volatility window adapts to the market — and an optional Smoother cleans up the input first. The adaptor's bounds, the smoothing and the colours are all set from the indicator's parameters.

The exported function

If you would rather build your own formula, the toolbox exports the calculation as a single AFL function you can call directly — it is the engine inside that indicator.

VariablePeriodATR(Periods)
ParameterDescription
Periods An array (or value) giving the ATR averaging period to use at each bar. Most often this is fed from an adaptor such as the DominantCycle.

Usage

periods = DominantCycle( Close );
atr = VariablePeriodATR( periods );
// Volatility-based trailing stop
stop = Close - 3 * atr;
Plot( Close, "Price", colorDefault, styleBar );
Plot( stop, "ATR Stop", colorRed );