TrendPine Script v6

Weighted Moving Average Pine Script — Complete TradingView Guide

The Weighted Moving Average (WMA) is a trend-following indicator built into Pine Script v6 via ta.wma(source, length). Unlike the Simple Moving Average, WMA assigns linearly increasing weights to recent prices, making it more responsive to current market conditions while still smoothing out short-term noise. The WMA default period of 20 bars is the most common starting point, with 10 and 50 used for short-term and mid-term trend analysis respectively. This guide covers the complete Pine Script v6 implementation using ta.wma(), all configurable parameters with recommended ranges, three concrete WMA trading strategies including its role in the Hull Moving Average (HMA), plus a five-step guide to generating WMA scripts instantly with Pineify.

What Is the Weighted Moving Average?

The Weighted Moving Average is a trend-following overlay indicator that calculates a weighted mean of closing prices over a fixed lookback period, used to identify trend direction, dynamic support/resistance, and crossover trading signals with greater sensitivity to recent price changes than the Simple Moving Average. While the SMA treats every bar in the lookback window equally, the WMA applies a linearly increasing weight: the most recent bar receives the highest weight equal to the period length, the bar before it receives one less unit of weight, and so on down to a weight of 1 for the oldest bar in the window.

The WMA concept originates from classical statistical weighted averages applied to financial time series. It gained prominence in technical analysis during the 1970s and 1980s as traders sought moving averages that would respond faster to recent price changes than the SMA without the recursive complexity of the Exponential Moving Average. The WMA became particularly influential when Alan Hull used it in 2005 as the core building block of the Hull Moving Average (HMA) — one of the most widely cited low-lag moving averages in modern technical analysis. The HMA formula is entirely derived from ta.wma(), combining two WMAs of different lengths to cancel lag.

Core formula: WMA = (n×Pn + (n−1)×Pn−1 + … + 1×P1) ÷ (n×(n+1)÷2), where n is the period length, P1 is the oldest bar in the window, and Pn is the most recent bar. For a 20-period WMA, the denominator is 20×21÷2 = 210. The numerator sums each closing price multiplied by its position weight. In Pine Script v6, the entire calculation is handled by ta.wma(close, 20).

The WMA applies to all liquid asset classes: stocks, crypto, forex, and futures. It performs best in trending markets where the increased sensitivity to recent prices helps traders stay closer to the current trend without excessive lag. Best timeframes are 15m to Daily charts — on shorter timeframes (1m–5m) the WMA can generate excessive noise-driven signals. On daily and weekly charts, WMA 20 and WMA 50 serve as respected dynamic support and resistance levels.

Best Markets

Stocks · Crypto · Forex · Futures

Best Timeframes

15m, 1H, 4H, Daily

Overlay

Yes — plots directly on price chart

WMA Pine Script Code Example

The code below plots a 20-period WMA as a blue line overlaid on the price chart using Pine Script v6. To add it to TradingView, open the Pine Script editor with Alt+P, paste the code, and click Add to chart. Change the 20 in ta.wma(close, 20) to any period that suits your trading style — common choices are 10, 20, and 50.

Pine Script v6
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Pineify

//@version=6
indicator(title="Weighted Moving Average", overlay=true, max_labels_count=500)

// WMA — applies linearly increasing weights to recent prices over the given period
p_ind_1 = ta.wma(close, 20) // WMA with default period 20

// Plot the WMA line on the price chart
plot(p_ind_1, title="WMA", color=color.rgb(41, 98, 255, 0), linewidth=1)

Chart Preview

Weighted Moving Average indicator Pine Script code example in TradingView — WMA 20 plotted as overlay

WMA Parameters

ParameterDefault ValueDescriptionRecommended Range
sourcecloseThe price series to weight and average. Most commonly close, but can be open, high, hl2 (high+low)/2, or any numeric series.Any numeric series
length20Number of bars included in the weighted average. The most recent bar receives weight equal to length; each preceding bar receives one less unit of weight. Larger values produce a smoother line with more lag; smaller values react faster but generate more noise.10–100 depending on style

Tuning Guide by Trading Style

  • Scalping (5m–15m charts): WMA 10–15 — fast weighted average for short-term price mean; higher noise risk in choppy conditions
  • Day trading (15m–1H charts): WMA 20 + WMA 50 — identify intraday trend direction and pullback entry levels
  • Swing trading (4H–Daily charts): WMA 50 — mid-term trend gauge; responds faster than SMA 50 to trend changes
  • HMA construction (any timeframe): WMA is the building block — use WMA(n/2) and WMA(n) together to build a low-lag Hull Moving Average

WMA Trading Strategies

The WMA is most effective as a responsive trend direction line and dynamic support/resistance level. Below are three concrete strategies with specific entry and exit conditions.

Strategy 1 — WMA Dual Crossover (Fast/Slow Trend Signal)

Market environment: trending markets · Best timeframe: 1H, 4H, Daily

A fast WMA(10) crossing above a slow WMA(30) signals a bullish trend shift, while the opposite crossunder signals a bearish trend shift. Because WMA weights recent prices more heavily than SMA, crossovers occur earlier in trend transitions. In Pine Script: ta.crossover(ta.wma(close, 10), ta.wma(close, 30)) detects the bullish crossover.

  1. Calculate fast WMA: wmaFast = ta.wma(close, 10)
  2. Calculate slow WMA: wmaSlow = ta.wma(close, 30)
  3. Long entry: ta.crossover(wmaFast, wmaSlow) — fast WMA crosses above slow WMA
  4. Confirm ADX > 20 to avoid false signals in sideways market conditions
  5. Exit: ta.crossunder(wmaFast, wmaSlow) — fast WMA crosses below slow WMA

Strategy 2 — WMA 20 Pullback Entry (Trend Continuation)

Market environment: trending bull markets · Best timeframe: 4H, Daily

The WMA 50 on the daily chart defines the primary trend. Pullbacks to the WMA 20 offer lower-risk long entries in an established uptrend. Because WMA responds faster to recent prices than SMA, the WMA 20 acts as a tighter trailing support level than the SMA 20.

  1. Calculate macro filter: wma50 = ta.wma(close, 50)
  2. Calculate pullback level: wma20 = ta.wma(close, 20)
  3. Condition 1 (regime): close > wma50 — only take long setups above the 50-period WMA
  4. Condition 2 (entry): price pulls back to touch wma20 then closes back above it on the next bar
  5. Stop loss: below wma50; hold until close below wma20 for 2 consecutive bars

Strategy 3 — WMA + RSI Confirmation (Trend + Momentum)

Market environment: trending and recovering markets · Best timeframe: 1H, 4H

Pairing WMA(20) with RSI(14) filters out low-conviction entries. The WMA 20 identifies the short-term trend direction while the RSI confirms that momentum is bullish but not yet overbought. Enter only when both conditions align simultaneously.

  1. Calculate trend filter: wma20 = ta.wma(close, 20)
  2. Calculate momentum confirmation: rsi14 = ta.rsi(close, 14)
  3. Long entry: close > wma20 AND rsi14 is between 50 and 70 (bullish momentum, not overbought)
  4. Avoid: entries where rsi14 > 70 even if price is above wma20 — overbought conditions increase reversal risk
  5. Exit: close falls below wma20 OR rsi14 drops below 50

Disclaimer: The strategies above are for educational purposes only and do not constitute investment advice. Past performance does not guarantee future results. Always apply proper risk management and position sizing.

How to Generate the WMA Indicator in Pineify

  1. 1

    Open Pineify

    Go to pineify.app and sign in — a free account is sufficient to generate WMA indicators.

  2. 2

    Click "New Indicator"

    Select "Indicator" as the script type from the creation menu on the dashboard.

  3. 3

    Describe the WMA you want

    Type a prompt such as: "Plot a 20-period WMA as a blue line overlay on the price chart using ta.wma(close, 20)." Pineify's AI Coding Agent generates the complete Pine Script v6 code in seconds.

  4. 4

    Copy to TradingView

    Click "Copy to TradingView" to copy the generated code, then open the TradingView Pine Script editor (Alt+P), paste the code, and click "Add to chart".

  5. 5

    Adjust the length parameter

    In the TradingView indicator settings panel, change the WMA length (e.g., from 20 to 10 or 50) to match your trading style — no code editing required.

Frequently Asked Questions

Build Your WMA Indicator in Seconds

Skip the manual coding. Pineify's AI Coding Agent generates complete, ready-to-use Pine Script indicators — including WMA crossover strategies with RSI confirmation — instantly for free.

Try Pineify Free