fintech-algorithms

Weighted Moving Average (WMA)

Newest-Heavy Linear Smoothing

Install and import

bash
npm install fintech-algorithms
ts
import { calculateWma } from "fintech-algorithms/technical-indicators/trend-smoothing/wma";

Signature

calculateWma(values, window)

Linearly weighted mean over window observations: the most recent observation carries weight window, the oldest weight 1.

Parameters

NameTypeNotes
values(number | null)[]Observation series in chronological order, oldest first.
nulls: propagate
windownumberNumber of observations in the weighted mean.
min: 1 · integer: true

Returns

(number | null)[] · length same-as-input

Weighted mean per position, null during warm-up.

Warm-up

The first window - 1 positions are null. Warm-up positions are null rather than a partial result, so a consumer never mistakes an incomplete window for a real value.

Errors

  • When window < 1 or is not an integer — throws RangeError

Complexity: time O(n), space O(1).

Worked example

executed Captured by running this function on the input its own test provides. Real output of real code — but not asserted against a published figure.

Input

values
[10, 13, 12, 15, 14, 18]
window
3

Call

calculateWma(values, window)

Returns

array of 6 nulls

[null, null, 12, 13.666666666666666, 14, 16.166666666666668]

Diagrams

Weighted Moving Average (WMA) — wma vs sma
Weighted Moving Average (WMA) — wma weight ladder

Calculation flow

WMA calculation flow
flowchart TD
    A["Accept ordered finite observation"] --> B{"At least n observations?"}
    B -- "No" --> C["Emit null · warming"]
    B -- "Yes, first window" --> D["Build U and L with weights 1 through n"]
    B -- "Yes, later window" --> E["Update L = prior L - prior U + n × incoming"]
    E --> F["Update U = prior U + incoming - outgoing"]
    D --> G["Divide L by n(n+1)/2"]
    F --> G
    G --> H["Emit WMA · ready"]

How it works

This page states the contract — how to call it correctly. The article explains the concept: why it works, and where it breaks.

Read the article →

References

  • NIST Dataplot Weighted Mean
  • NumPy `average`
  • TradingView Moving Averages
  • TradingView Pine Script `ta.wma` reference
  • TA-Lib Functions
  • TA-Lib Generic Moving Average
  • TradingView Hull Moving Average
  • New York Fed Effective Federal Funds Rate data and API observation
  • Claim map and implementation choices
  • Reproducibility notes