WiseTrader Toolbox

VariablePeriodStDev

Background

Standard deviation is the classic statistical measure of how spread out a set of values is around its mean. Applied to a rolling window of prices it becomes a volatility gauge: a small value means price has been hugging its average, a large value means it has been swinging widely. It is the engine behind Bollinger Bands and is widely used for volatility filters and position sizing.

A standard rolling standard deviation uses one fixed window length. VariablePeriodStDev accepts a period for each bar, so the measurement window can adapt to market conditions. The per-bar periods often come from an adaptor such as the DominantCycle.

Included formula

The toolbox ships a ready-made Adaptive Bollinger Bands 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 Bollinger Bands onto a price chart.

It plots an upper and a lower band around price, using this function for the band width so the bands breathe with volatility. The Adaptor you pick is what produces the per-bar period the bands use, and an optional Smoother cleans up the input first. The band multiplier, 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.

VariablePeriodStDev(Price, Periods)
ParameterDescription
Price The array whose standard deviation is calculated.
Periods An array (or value) giving the window length to use at each bar. Most often this is fed from an adaptor such as the DominantCycle.

Usage

periods = DominantCycle( Close );
sd = VariablePeriodStDev( Close, periods );

// An adaptive Bollinger-style band
mid = VariablePeriodEMA( Close, periods );
Plot( mid + 2 * sd, "Upper", colorGreen );
Plot( mid - 2 * sd, "Lower", colorRed );