Sidechair

ta.wma()

Browse the reference

Returns the weighted moving average, giving the newest bar a weight of length and each older bar one less, down to 1.

Try it in the editor

ta.wma multiplies the newest value in the window by length, the one before by length - 1, and so on to 1 for the oldest, then divides by length * (length + 1) / 2. The weights fall in a straight line.

The first value appears on bar number length, and any na inside the window makes the result na.

Syntax

Pine Script
ta.wma(source: series int/float, length: series int) -> series float

Arguments

NameTypeDefaultDescription
sourceseries int/floatrequiredSeries of values to average.
lengthseries intrequiredNumber of bars in the window and the weight of the newest bar. A whole number of 1 or more.

Returns

series float: the linearly weighted average of the last length values, or na while the window is not full.

Remarks

  • ta.wma follows price more closely than ta.sma of the same length and stays smoother than a short ta.ema.
  • ta.hma and ta.swma are built on this weighting.

Example

Pine ScriptWeighted average of the closeOpen in editor
//@version=6
indicator("WMA", overlay = true)
len = input.int(21, "Length", minval = 1)
plot(ta.wma(close, len), "WMA", color = color.fuchsia, linewidth = 2)
plot(ta.sma(close, len), "SMA", color = color.gray)

See also

Runs on Sidechair