Trend / OverlayPine Script v6

Linear Weighted Moving Average Pine Script — Complete TradingView Guide

The Linear Weighted Moving Average (LWMA) in Pine Script is a trend-following overlay that assigns linearly decreasing weights to each bar in the lookback period, so the most recent price data carries the most influence. Unlike a Simple Moving Average (SMA), which treats every bar equally, LWMA reacts faster to recent price changes — making it ideal for trend detection and dynamic support/resistance. This Pineify implementation uses a configurable period (default: 10 bars) and weight (default: 6) parameter, color-codes the line green when rising and red when falling, and emits built-in direction-change signals. It works on all asset classes (stocks, crypto, forex, futures) and all timeframes from 1-minute to weekly charts. This guide covers the complete Pine Script v6 implementation, all configurable parameters, three concrete trading strategies, and step-by-step instructions for generating it instantly with Pineify.

What Is the Linear Weighted Moving Average?

The Linear Weighted Moving Average (LWMA) is a trend-following moving average that weights each bar linearly, giving the most recent bar the highest importance and progressively lower importance to older bars. The concept of weighted moving averages dates to early technical analysis literature in the mid-20th century, with linear weighting emerging as a natural extension of the SMA to reduce lag. LWMA is sometimes called the Weighted Moving Average (WMA) in other platforms.

The core formula for a standard LWMA over n periods is:

LWMA = Σ(price[i] × w[i]) / Σ(w[i]), where w[i] decreases linearly from the most recent bar to the oldest. In the Pineify implementation, the weight for each bar is computed as weight - i - sub, where sub = weight / period - 1. This ensures the weights sum to a consistent divisor across any combination of period and weight.

The Pineify LWMA implementation also includes two auxiliary outputs: direction change signals (buySignal fires on the first rising bar after a fall; sellSignal fires on the first falling bar after a rise) and a color-coded line that turns green when LWMA is rising and red when falling — making trend state instantly visible at a glance.

Applicable markets: LWMA is effective on all liquid markets — equities, cryptocurrency, forex (EUR/USD, GBP/JPY), and futures (ES, NQ, CL). It performs best in trending environments and on instruments with sufficient volatility and volume. Avoid applying LWMA to thinly-traded instruments where price gaps distort the weighted calculation.

Best timeframes: For intraday scalping, use LWMA on 1m–5m charts with period 5–10. For day trading, 15m–1H charts with period 14–21 strike a balance between responsiveness and noise filtering. For swing trading, 4H–Daily charts with period 20–50 are standard. On weekly charts, period 10–20 captures multi-week trends.

Best Markets

Stocks · Crypto · Forex · Futures

Best Timeframes

1m, 15m, 1H, 4H, Daily

Overlay

Yes — plots directly on price chart

LWMA Pine Script Code Example

The script below plots the Linear Weighted Moving Average on the price chart with a color-coded line (green = rising, red = falling) and calculates built-in direction-change signals. To use it, copy the code, open the TradingView Pine Script editor with Alt+P, paste it in, and click Add to chart. Adjust the period and weight values in the p_ta_lwma(close, 10, 6) call to suit your trading style.

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="Linear Weighted Moving Average", overlay=true, max_labels_count=500)

p_ta_lwma(series float source, simple int period, simple int weight) =>
    price = source
    sub = weight / period - 1
    float p = na
    float d = na
    float sum = 0
    float divider = 0
    for i = 0 to period - 1 by 1
        p := price[i] * (weight - i - sub)
        d := weight - i - sub
        sum := sum + p
        divider := divider + d
        divider
    lwma = sum / divider
    lwmaRise = lwma[1] < lwma
    lwmaFall = lwma[1] > lwma
    buySignal = lwmaRise and not lwmaRise[1]
    sellSignal = lwmaFall and not lwmaFall[1]
    [lwma, lwmaRise, lwmaFall, buySignal, sellSignal]

[p_ind_1_lwma, p_ind_1_rise, p_ind_1_fall, p_ind_1_buySignal, p_ind_1_sellSignal] = p_ta_lwma(close, 10, 6)

// LWMA — color green when rising, red when falling
p_ind_1_lwma_color = p_ind_1_rise ? color.rgb(76, 175, 80, 0) : color.rgb(242, 54, 69, 0)
plot(p_ind_1_lwma, "LWMA", p_ind_1_lwma_color, 2)

Chart Preview

Linear Weighted Moving Average indicator Pine Script code example in TradingView

Parameters

ParameterDefault ValueDescriptionRecommended Range
sourcecloseThe price series used as input. Any numeric series: close, open, hl2, or a custom expression.Any numeric series
period10Number of bars in the lookback window. Larger values produce a slower, smoother LWMA line. Smaller values react faster to recent price changes.5–200
weight6Starting weight assigned to the most recent bar. Controls the steepness of the linear weight decay. A higher weight increases the emphasis on recent bars.2–20

Tuning Guide by Trading Style

  • Scalping (1m–5m charts): period 5–10, weight 4–6 — fast reaction to recent price, suited for intraday momentum
  • Day trading (15m–1H charts): period 14–21, weight 6–8 — balances noise reduction with timely signals
  • Swing trading (4H–Daily charts): period 20–50, weight 8–12 — captures multi-day trend direction
  • Position trading (Daily–Weekly charts): period 50–200, weight 10–20 — smooths out noise for long-term trend identification

Trading Strategies Using the LWMA Indicator

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

Strategy 1 — LWMA Direction Change Signal

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

Use the built-in buySignal and sellSignal outputs of the LWMA function. A buy signal fires on the first bar where LWMA turns from falling to rising; a sell signal fires on the reverse. Pair with RSI to filter trades in the direction of the higher-timeframe trend.

  1. Calculate LWMA with default settings: p_ta_lwma(close, 10, 6)
  2. Calculate RSI(14): rsi = ta.rsi(close, 14)
  3. Long entry: buySignal == true AND rsi > 50
  4. Short entry: sellSignal == true AND rsi < 50
  5. Exit: opposite direction-change signal fires (next buySignal for shorts, next sellSignal for longs)

Strategy 2 — Dual LWMA Crossover

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

Use a short-period LWMA (fast) crossing above a long-period LWMA (slow) as the trend entry signal. This is analogous to the classic golden cross / death cross, but LWMA's linear weighting makes it react faster than an SMA crossover while remaining smoother than EMA.

  1. Calculate fast LWMA: [fast, ...] = p_ta_lwma(close, 10, 6)
  2. Calculate slow LWMA: [slow, ...] = p_ta_lwma(close, 30, 10)
  3. Long entry: fast crosses above slow — ta.crossover(fast, slow)
  4. Short entry: fast crosses below slow — ta.crossunder(fast, slow)
  5. Stop loss: place 1 ATR(14) below entry bar low on longs; above entry bar high on shorts

Strategy 3 — LWMA Dynamic Support/Resistance Bounce

Market environment: uptrending markets · Best timeframe: 4H, Daily

In a confirmed uptrend, price often pulls back to the LWMA before continuing higher. This strategy buys the pullback when the LWMA line is green (rising) and price touches or dips below the LWMA. Pair with volume to confirm genuine support.

  1. Confirm uptrend: LWMA(close, 20, 8) is rising (line is green) for at least 3 consecutive bars
  2. Pullback condition: low of current bar ≤ LWMA value (price touches or crosses below LWMA)
  3. Entry trigger: close of the bar is above the LWMA (price reclaims LWMA on close)
  4. Confirm with above-average volume: volume > ta.sma(volume, 20)
  5. Stop loss: below the lowest low of the last 3 bars; target: 2× the stop distance

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 LWMA Indicator in Pineify

  1. 1

    Open Pineify

    Go to pineify.app and sign in — a free account gives you full access to the AI Coding Agent.

  2. 2

    Click "New Indicator"

    Select "Indicator" as the script type from the creation menu to start a blank Pine Script v6 project.

  3. 3

    Describe the LWMA you want

    Type a prompt such as: "Plot a Linear Weighted Moving Average with period 20 and weight 8 on the price chart, color the line green when rising and red when falling." Pineify's AI Coding Agent generates the complete Pine Script v6 code in seconds.

  4. 4

    Copy the generated code to TradingView

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

  5. 5

    Adjust period and weight for your style

    Modify the period and weight arguments in the code or expose them as input() variables so you can tune them from the TradingView indicator settings panel without editing code.

Frequently Asked Questions

Build Your LWMA Indicator in Seconds

Skip the manual coding. Pineify's AI Coding Agent generates complete, ready-to-use Pine Script indicators — including Linear Weighted Moving Average strategies — instantly for free.

Try Pineify Free