Indicators/probuilder · probacktest · proorder · proscreener

WeightedAverage

WeightedAverage in ProBuilder returns the weighted moving average (WMA) of a price over N bars, giving recent bars more weight. Syntax, formula, examples.

Syntax

probuilder
WeightedAverage[N](price)

Parameters

NameTypeDefaultDescription
Ninteger20Lookback period in bars. Determines both the window length and the weighting scheme.
priceprice sourcecloseThe series being averaged. Accepts close, open, high, low, typicalprice, medianprice, or any custom variable.

Formula

code
WMA = (N*price + (N-1)*price[1] + ... + 2*price[N-2] + 1*price[N-1])
      / (N + (N-1) + ... + 2 + 1)

The denominator is the sum of the weights, N * (N + 1) / 2. Weights decrease linearly from the newest bar to the oldest.

How it works

A simple moving average treats every bar in the window equally, so a large move entering or leaving the window shifts the line by the same amount regardless of when it happened. The weighted moving average instead multiplies each bar by its position in the window: the latest bar counts N times as much as the oldest. The result is a line that hugs current price more closely and turns earlier at swing points.

Compared with ExponentialAverage, the WMA uses a hard window: bars older than N periods have zero influence, whereas an EMA gives all past bars a small, exponentially decaying weight. In practice the two behave similarly, with the WMA slightly more sensitive to what happens at the edge of its window.

The WMA is also a building block for other averages. HullAverage, for example, combines several weighted averages of different lengths to cut lag further.

Examples

Example 1, Basic 20-period WMA (Indicator)

probuilder
// 20-period weighted moving average of closing prices
myWMA = WeightedAverage[20](close)
RETURN myWMA

Calculates and plots the 20-bar weighted moving average of the close, the standard usage preserved from the source documentation.

Example 2, WMA versus SMA crossover (ProBacktest)

probuilder
// Fast weighted average against a slower simple average
fastWMA = WeightedAverage[10](close)
slowSMA = Average[40](close)

IF fastWMA CROSSES OVER slowSMA THEN
  BUY 1 CONTRACT AT MARKET
ENDIF
IF fastWMA CROSSES UNDER slowSMA THEN
  SELL AT MARKET
ENDIF

The weighted average serves as the responsive fast line in a classic crossover system, entering long when it crosses above the slower simple average and exiting on the opposite cross.

Example 3, Screening for price above a rising WMA (ProScreener)

probuilder
// Instruments trading above a rising 30-bar weighted average
wma = WeightedAverage[30](close)
rising = wma > wma[5]
SCREENER[close > wma AND rising](((close / wma) - 1) * 100 AS "% above WMA")

Keeps only instruments whose price is above a 30-bar WMA that is itself higher than five bars ago, and ranks them by their distance above the line.

Interpretation

Weighted averages are read like any moving average. Price above a rising WMA indicates an uptrend, price below a falling WMA indicates a downtrend, and crossings between price and the line, or between two WMAs of different lengths, are used as trend-change signals.

Because of its front-loaded weighting, a WMA of length N behaves roughly like a shorter simple average: it turns earlier but also produces more whipsaws in sideways markets. Choosing between Average, WeightedAverage, and ExponentialAverage is mostly a trade-off between lag and noise, not a difference in kind.

Common errors and gotchas

  • Wrong bracket types. The period goes in square brackets and the price source in parentheses: WeightedAverage[20](close). Writing WeightedAverage(20, close) is a syntax error.
  • Expecting SMA-like smoothness. For the same N, a WMA is noticeably jumpier than a simple average. When replacing Average[N] with WeightedAverage[N] in an existing system, signal frequency increases and backtest results change.
  • Confusing it with WeightedClose. WeightedClose is a single-bar price constant, (high + low + 2 * close) / 4. It is a valid input to this function but a different concept entirely.
  • Too short a period. Very small N values make the line almost identical to raw price, adding computation without meaningful smoothing.