DocsStrategy APIindicators

WMA (Weighted Moving Average)

The WMA assigns linearly increasing weights to more recent prices. The most recent price gets the highest weight, the second most recent gets a slightly lower weight, and so on.

Declaration

function init(ctx) { ctx.indicator('wma', 'WMA', { period: 20, source: 'close' }); }

Options

OptionTypeDefaultDescription
periodnumber20Lookback period
sourcestring'close'Price source (open, high, low, close, hl2, hlc3, ohlc4)

Output

Returns a single number[] series.

Accessing Values

function onBar(ctx, i) { const wmaValue = ctx.ind.wma[i]; }

Use Cases

Comparing Moving Average Types

function init(ctx) { ctx.indicator('sma', 'SMA', { period: 20 }); ctx.indicator('ema', 'EMA', { period: 20 }); ctx.indicator('wma', 'WMA', { period: 20 }); } function onBar(ctx, i) { // WMA responds faster than SMA but slower than EMA // Useful as a middle ground between SMA and EMA }

Calculation

$$WMA = \frac{\sum_{i=0}^{N-1} (N - i) \times Price_{t-i}}{\sum_{i=0}^{N-1} (N - i)}$$

Related

  • SMA - Equal-weight moving average
  • EMA - Exponentially weighted
  • DEMA - Double EMA for reduced lag
indicatorwmamoving-averagetrendqsl