WiseTrader Toolbox

VariablePeriodEMA

Background

The exponential moving average (EMA) is one of the most widely used trend-following tools. Unlike a simple moving average, it weights recent prices more heavily, so it reacts faster to new information while still smoothing out noise. Traders use it to gauge trend direction, as a dynamic support/resistance line, and as the building block of indicators such as MACD.

A standard EMA uses one fixed period for every bar. VariablePeriodEMA accepts a period for each bar instead, so the smoothing constant changes as the market changes — shorter (faster) when the market is volatile and longer (smoother) when it is trending. The per-bar periods normally come from an adaptor such as DominantCycle or DominantCycle, but you can supply any array you build yourself.

Included formula

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

It plots the moving average right on top of price. The Adaptor you pick is what produces the per-bar period this function needs — so the average speeds up and slows down with the market — and an optional Smoother cleans up the price first. The adaptor's bounds, the smoothing and the line colour 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.

VariablePeriodEMA(Price, Periods)
ParameterDescription
Price The price array the moving average is calculated on.
Periods An array (or value) giving the EMA period to use at each bar. Most often this is fed from an adaptor such as the DominantCycle.

Usage

periods = DominantCycle( Close );
Plot( Close, "Price", colorDefault, styleBar );
Plot( VariablePeriodEMA( Close, periods ), "Adaptive EMA", colorRed );