Farey Sequence Weighted Moving Average Pine Script — Complete TradingView Guide

The Farey Sequence Weighted Moving Average (FSWMA) is a Pine Script v6 indicator that weights each historical price bar using fractions derived from the Farey sequence — a number-theoretic series of reduced rational fractions. Unlike simple, exponential, or linear-weighted moving averages, FSWMA applies a mathematically structured, non-uniform decay to past prices. The default Farey order is 5, using 10 bars divided by a normalisation factor of 5.5. FSWMA supports orders from 2 to 14, covering lookback windows up to 64 bars. It renders as an overlay line on the TradingView price chart and is suitable for trend-following strategies across stocks, crypto, forex, and futures on any timeframe. This guide provides the complete Pine Script code, a full parameter reference, step-by-step TradingView setup instructions, and three named trading strategies.

What Is the Farey Sequence Weighted Moving Average?

The Farey Sequence Weighted Moving Average (FSWMA) is a trend-following overlay indicator that assigns weights to historical price bars using fractions from the Farey sequence, used to reduce lag while producing smoother output than linear or exponential averages.

The Farey sequence F(N) is the ascending sequence of all reduced fractions p/q with 0 ≤ p ≤ q ≤ N. For example, F(5) = {0/1, 1/5, 1/4, 1/3, 2/5, 1/2, 3/5, 2/3, 3/4, 4/5, 1/1}. FSWMA was adapted for financial time-series analysis to exploit the uniform rational spacing property of the Farey sequence: consecutive Farey fractions always differ by exactly 1/(q·q′), guaranteeing a mathematically regular weight distribution. The concept of applying number-theoretic sequences to signal smoothing has been explored in digital signal processing literature, with practical Pine Script implementations emerging in the TradingView community around 2020–2022.

Core formula for FSWMA order 5:

FSWMA₅ = (src₀ + 0.8·src₁ + 0.75·src₂ + 0.667·src₃ + 0.6·src₄ + 0.5·src₅ + 0.4·src₆ + 0.333·src₇ + 0.25·src₈ + 0.2·src₉) ÷ 5.5

Where src₀ is the current bar's close price and src₁…src₉ are the previous 1–9 bars. The denominator 5.5 is the sum of all Farey weights for order 5. Higher orders extend the lookback window and increase the denominator accordingly — order 10 divides by 16.5 and looks back 32 bars; order 14 divides by 32.5 and looks back 64 bars.

Applicable markets: FSWMA is price-scale independent and works equally well on equities (e.g. AAPL, SPY), cryptocurrency pairs (BTC/USDT, ETH/USDT), forex majors (EUR/USD, GBP/JPY), and commodity futures (CL, GC).

Best timeframes:

  • Scalping (1m–5m): Order 2–3 for ultra-fast response with minimal lag
  • Intraday (15m–1H): Order 4–6 balances noise reduction and responsiveness
  • Swing trading (4H–Daily): Order 7–10 captures medium-term trends
  • Position trading (Weekly): Order 11–14 for long-term trend identification

Compared to a standard WMA of the same lookback, FSWMA weights recent bars more aggressively using near-unity fractions (0.9, 0.889, 0.875…) while tapering older bars smoothly to fractions below 0.2. This structure makes FSWMA faster than WMA at detecting trend changes yet less prone to spike-driven distortion than EMA.

Pine Script Code Example

The following Pine Script v6 code implements the Farey Sequence Weighted Moving Average using the p_ta_fswma function. To use it in TradingView, open the Pine Script Editor from the bottom panel, paste the complete code, and click "Add to chart" — the FSWMA line will appear as a purple overlay on your price series.

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

// Farey Sequence Weighted Moving Average (FSWMA)
// Weights each historical bar using fractions derived from the Farey sequence,
// producing a mathematically structured, smoothly-weighted moving average.
p_ta_fswma(series float source, simple int order, simple bool highlightMovements) =>
    src1 = source[1]
    src2 = source[2]
    src3 = source[3]
    src4 = source[4]
    src5 = source[5]
    // ... bars src6–src63 defined for orders 6–14 ...
    fswma = switch order
        2 => (source + 0.5 * src1) / 1.5
        3 => (source + 0.667 * src1 + 0.5 * src2 + 0.333 * src3) / 2.5
        4 => (source + 0.75 * src1 + 0.667 * src2 + 0.5 * src3 + 0.333 * src4 + 0.25 * src5) / 3.5
        5 => (source + 0.8 * src1 + 0.75 * src2 + 0.667 * src3 + 0.6 * src4 + 0.5 * src5 + 0.4 * src6 + 0.333 * src7 + 0.25 * src8 + 0.2 * src9) / 5.5
        // orders 6–14 follow the same Farey-fraction weighting pattern
        => na
    fswma

p_ind_1 = p_ta_fswma(close, 5, true) // FSWMA order 5

plot(p_ind_1, "FSWMA", color.rgb(109, 30, 127, 0), 2)
Farey Sequence Weighted Moving Average indicator Pine Script code example in TradingView

Parameters

FSWMA exposes three configurable inputs. The most important is order, which controls the Farey sequence depth and therefore the lookback window length.

ParameterDefault ValueDescriptionRecommended Range
sourcecloseThe price series to smooth — typically close, hl2, or hlc3close / hl2 / hlc3
order5Farey sequence depth (2–14). Controls lookback window size and weight distribution. Higher order = more bars, more smoothing, more lag.2–14 (integer)
highlightMovementstrueWhen enabled, visually highlights directional changes on the FSWMA line to make trend shifts easier to readtrue / false
Tuning scenarios:
  • Scalping (1m–5m charts): Set order = 2 or 3 for ultra-fast response; the 2-bar Farey window reacts nearly tick-by-tick.
  • Intraday swing (15m–1H charts): Use order = 5 or 6 to balance smoothing and lag; the default order 5 covers 10 bars with a 5.5 normalisation factor.
  • Position trading (4H–Daily charts): Use order = 912 for 28–46 bar coverage that filters short-term noise while tracking major trends.

Trading Strategies

FSWMA supports three named trading strategies, each using numbered entry and exit conditions for precision and reproducibility.

Strategy 1 — FSWMA Trend-Follow with RSI Confirmation

Market environment: trending (up or down)

Combines FSWMA (order 5) with RSI(14) to filter momentum direction before entry.

Entry (Long):

  1. Price closes above FSWMA (order 5) for the first time in 3 bars
  2. RSI(14) is above 50 and rising
  3. FSWMA slope is positive (current FSWMA > FSWMA 1 bar ago)
  4. Enter at next bar open

Exit:

  1. Price closes below FSWMA for 2 consecutive bars
  2. Or RSI(14) drops below 45

Strategy 2 — Dual FSWMA Crossover

Market environment: trending markets with clear directional momentum

Uses a fast FSWMA (order 3) and slow FSWMA (order 9) crossover system to signal trend changes with less lag than a dual SMA system.

Entry (Long):

  1. FSWMA(order 3) crosses above FSWMA(order 9)
  2. Both FSWMA lines are above their values 5 bars ago (trend confirmation)
  3. Volume on the crossover bar is above its 10-bar SMA
  4. Enter at close of the crossover bar

Exit:

  1. FSWMA(order 3) crosses below FSWMA(order 9)
  2. Or trailing stop at 1.5× ATR(14) below recent swing high

Strategy 3 — FSWMA Pullback Entry with Bollinger Bands

Market environment: uptrending with temporary retracements

Uses FSWMA (order 7) as a dynamic support level during pullbacks confirmed by Bollinger Band (20, 2) squeeze.

Entry (Long):

  1. Price is in an uptrend: FSWMA(7) is above its 20-bar high from 10 bars ago
  2. Price pulls back to touch or pierce the FSWMA(7) line
  3. Price then closes back above FSWMA(7) on the same or next bar
  4. Bollinger Band width is narrowing (squeeze) — band width < band width 5 bars ago
  5. Enter at close of the recovery bar

Exit:

  1. Take profit at upper Bollinger Band (2σ)
  2. Stop loss 1 ATR(14) below entry bar low
Disclaimer: The strategies above are for educational purposes only. They do not constitute investment advice, a recommendation to buy or sell any security, or a guarantee of future performance. Always backtest strategies on historical data and apply appropriate risk management before trading with real capital.

How to Generate FSWMA Pine Script in Pineify

Pineify generates a complete, ready-to-paste FSWMA Pine Script v6 script in under 30 seconds — no coding required. Follow these 5 steps:

  1. 1
    Open Pineify at pineify.app and sign in or create a free account.
  2. 2
    Click "New Indicator" and select the Moving Averages category from the indicator library.
  3. 3
    Describe your requirements — for example, "Farey Sequence Weighted Moving Average, order 5, applied to close, highlight trend direction".
  4. 4
    Copy the generated Pine Script v6 code from Pineify's code panel with one click.
  5. 5
    Adjust the Farey order and source input in TradingView's indicator settings panel to optimise for your market and timeframe.

Frequently Asked Questions

Generate Any Pine Script Indicator for Free

Pineify generates production-ready Pine Script v6 indicators — including FSWMA — in seconds. No coding required.

Start Building Free