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
WeightedAverage[N](price)Parameters
| Name | Type | Default | Description |
|---|---|---|---|
N | integer | 20 | Lookback period in bars. Determines both the window length and the weighting scheme. |
price | price source | close | The series being averaged. Accepts close, open, high, low, typicalprice, medianprice, or any custom variable. |
Formula
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)
// 20-period weighted moving average of closing prices
myWMA = WeightedAverage[20](close)
RETURN myWMACalculates 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)
// 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
ENDIFThe 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)
// 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). WritingWeightedAverage(20, close)is a syntax error. - Expecting SMA-like smoothness. For the same
N, a WMA is noticeably jumpier than a simple average. When replacingAverage[N]withWeightedAverage[N]in an existing system, signal frequency increases and backtest results change. - Confusing it with
WeightedClose.WeightedCloseis 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
Nvalues make the line almost identical to raw price, adding computation without meaningful smoothing.
Related instructions
Average, simple moving average with equal weights.ExponentialAverage, exponentially decaying weights over all history.WilderAverage, the smoothed average used inside RSI and ATR.HullAverage, low-lag average built from weighted averages.TriangularAverage, double-smoothed average with center-weighted emphasis.TimeSeriesAverage, linear-regression-based moving average.ZLEMA, zero lag exponential moving average.WeightedClose, single-bar weighted price often fed into averages.
