TradingView indicator guides

FSWMA: Farey Weights, Bar Counts and Pine Script

This Farey Sequence Weighted Moving Average applies descending positive Farey fractions to recent prices, then divides by their sum. The source below uses hard-coded, rounded coefficients for orders 2–14 and plots order 5 on close. Order is not the number of bars, and the supplied plot has no automatic direction coloring.

Inspect the Pine Script

From sequence order to a weighted price average

A Farey sequence of order n contains the reduced fractions between 0 and 1 whose denominators do not exceed n, sorted by value. See Wolfram’s FareySequence definition. This indicator omits zero, reverses the remaining order and assigns the largest weight to the current source value.

For order 5, the exact fractions are 1, 4/5, 3/4, 2/3, 3/5, 1/2, 2/5, 1/3, 1/4 and 1/5. They apply to the current bar through nine bars ago. The code rounds repeating fractions to three decimals, using 0.667 and 0.333, and divides the weighted sum by 5.5.

FSWMA = sum(weight[i] × source[i]) / sum(weight[i]). All selected weights are positive. On a fully available valid window, the result remains in the source’s price units and between that window’s minimum and maximum. The denominator normalizes the scale: it does not make a price value of 100 mean something different at each order.

Count the terms instead of guessing the lookback

The switch branches use the following counts, including the current bar:

OrderBarsDivisor
221.5
342.5
463.5
5105.5
6126.5
7189.5
82211.5
92814.5
103216.5
114221.5
124623.5
135829.5
146432.5

Order 7 therefore uses 18 observations, and order 10 uses 32. Order 5 on a 15-minute chart spans ten bar intervals, or 150 bar-minutes; the distance between the first and last bar’s timestamps is nine intervals when contiguous. Sessions and gaps change elapsed calendar time.

The selected branch needs all its referenced source values. At order 5 that means ten available observations, not 64 simply because the helper declares history variables through source[63]. Missing selected values propagate through the weighted arithmetic; the function does not replace them with zero.

A reproducible comparison with SMA and WMA

Take ten hypothetical prices from newest to oldest: 110, 108, 106, 104, 102, 100, 98, 96, 94 and 92. With the displayed rounded order-5 weights, FSWMA is 103.552. The equally weighted SMA(10) is 101, while a linearly weighted WMA(10), using weights 10 through 1, is 104. This example does not support a universal claim that FSWMA responds faster than WMA.

For another check, hold the previous nine source values at 100 and set only the newest value to 110. FSWMA becomes 100 + 10/5.5 = 101.818182. WMA(10) has the same current-bar weight, 10/55, and gives the same value on that first change; SMA(10) gives 101. The filters can still differ on subsequent bars because their remaining weights differ.

The weighted mean age of order 5’s rounded coefficients is 3.224 bars, compared with 4.5 for SMA(10) and 3 for WMA(10). This describes response to a steady linear input; it is not a promised delay for every crossover or reversal. Signal timing and whipsaw counts depend on the data and rule.

What the supplied controls actually do

The call uses close, order 5 and true as literals. There are no input controls in the script. The helper accepts highlightMovements but never reads that argument, and the plot color is a fixed purple. Setting that argument to false would not change this output. There are no color-based trade signals, alerts or orders.

To make order editable or color the line by its change, request those as explicit extensions. Specify a supported order, a tie color and whether a direction event must wait for bar close. A rising average and price above the average are separate conditions; neither proves that price is accelerating.

The calculation uses no volume, session classification or institution identification. Gaps are part of the supplied price sequence; there is no continuity requirement beyond the history needed by the selected branch. On an open bar, the current source contributes to the average and may change before close.

Validate a version before testing a trading rule

Check a constant-price window: because each branch’s stored weights sum to its divisor, the output should equal that constant apart from floating-point rounding. Then check an isolated price change and a rising sequence. Match the rounded coefficients if comparing with this source; an exact-fraction version can differ slightly.

Choose a price crossover, slope change or other event explicitly and specify confirmation timing, exits, position sizing and costs. RSI divergence requires defined price and oscillator swings; an RSI value below 30 by itself is not divergence. A volume threshold is an extra observation and does not identify the participants behind it.

For alternative weighting, compare the actual WMA and EMA formulas. No best market, order, reduced-failure percentage or historical win rate is established by this page.

Pine Script v6: implementation and limits

Paste the complete script into Pine Editor. The displayed call selects order 5 and close, producing a fixed purple line after the selected history is available. Inspect the weighted arithmetic before adding controls or signals.

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

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]
    src6 = source[6]
    src7 = source[7]
    src8 = source[8]
    src9 = source[9]
    src10 = source[10]
    src11 = source[11]
    src12 = source[12]
    src13 = source[13]
    src14 = source[14]
    src15 = source[15]
    src16 = source[16]
    src17 = source[17]
    src18 = source[18]
    src19 = source[19]
    src20 = source[20]
    src21 = source[21]
    src22 = source[22]
    src23 = source[23]
    src24 = source[24]
    src25 = source[25]
    src26 = source[26]
    src27 = source[27]
    src28 = source[28]
    src29 = source[29]
    src30 = source[30]
    src31 = source[31]
    src32 = source[32]
    src33 = source[33]
    src34 = source[34]
    src35 = source[35]
    src36 = source[36]
    src37 = source[37]
    src38 = source[38]
    src39 = source[39]
    src40 = source[40]
    src41 = source[41]
    src42 = source[42]
    src43 = source[43]
    src44 = source[44]
    src45 = source[45]
    src46 = source[46]
    src47 = source[47]
    src48 = source[48]
    src49 = source[49]
    src50 = source[50]
    src51 = source[51]
    src52 = source[52]
    src53 = source[53]
    src54 = source[54]
    src55 = source[55]
    src56 = source[56]
    src57 = source[57]
    src58 = source[58]
    src59 = source[59]
    src60 = source[60]
    src61 = source[61]
    src62 = source[62]
    src63 = source[63]

    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
        6 => (source + 0.833 * src1 + 0.8 * src2 + 0.75 * src3 + 0.667 * src4 + 0.6 * src5 + 0.5 * src6 + 0.4 * src7 + 0.333 * src8 + 0.25 * src9 + 0.2 * src10 + 0.167 * src11) / 6.5
        7 => (source + 0.857 * src1 + 0.833 * src2 + 0.8 * src3 + 0.75 * src4 + 0.714 * src5 + 0.667 * src6 + 0.6 * src7 + 0.571 * src8 + 0.5 * src9 + 0.429 * src10 + 0.4 * src11 + 0.333 * src12 + 0.286 * src13 + 0.25 * src14 + 0.2 * src15 + 0.167 * src16 + 0.143 * src17) / 9.5
        8 => (source + 0.875 * src1 + 0.857 * src2 + 0.833 * src3 + 0.8 * src4 + 0.75 * src5 + 0.714 * src6 + 0.667 * src7 + 0.625 * src8 + 0.6 * src9 + 0.571 * src10 + 0.5 * src11 + 0.429 * src12 + 0.4 * src13 + 0.375 * src14 + 0.333 * src15 + 0.286 * src16 + 0.25 * src17 + 0.2 * src18 + 0.167 * src19 + 0.143 * src20 + 0.125 * src21) / 11.5
        9 => (source + 0.889 * src1 + 0.875 * src2 + 0.857 * src3 + 0.833 * src4 + 0.8 * src5 + 0.778 * src6 + 0.75 * src7 + 0.714 * src8 + 0.667 * src9 + 0.625 * src10 + 0.6 * src11 + 0.571 * src12 + 0.556 * src13 + 0.5 * src14 + 0.444 * src15 + 0.429 * src16 + 0.4 * src17 + 0.375 * src18 + 0.333 * src19 + 0.286 * src20 + 0.25 * src21 + 0.222 * src22 + 0.2 * src23 + 0.167 * src24 + 0.143 * src25 + 0.125 * src26 + 0.111 * src27) / 14.5
        10 => (source + 0.9 * src1 + 0.889 * src2 + 0.875 * src3 + 0.857 * src4 + 0.833 * src5 + 0.8 * src6 + 0.778 * src7 + 0.75 * src8 + 0.714 * src9 + 0.7 * src10 + 0.667 * src11 + 0.625 * src12 + 0.6 * src13 + 0.571 * src14 + 0.556 * src15 + 0.5 * src16 + 0.444 * src17 + 0.429 * src18 + 0.4 * src19 + 0.375 * src20 + 0.333 * src21 + 0.3 * src22 + 0.286 * src23 + 0.25 * src24 + 0.222 * src25 + 0.2 * src26 + 0.167 * src27 + 0.143 * src28 + 0.125 * src29 + 0.111 * src30 + 0.1 * src31) / 16.5
        11 => (source + 0.909 * src1 + 0.9 * src2 + 0.889 * src3 + 0.875 * src4 + 0.857 * src5 + 0.833 * src6 + 0.818 * src7 + 0.8 * src8 + 0.778 * src9 + 0.75 * src10 + 0.727 * src11 + 0.714 * src12 + 0.7 * src13 + 0.667 * src14 + 0.636 * src15 + 0.625 * src16 + 0.6 * src17 + 0.571 * src18 + 0.556 * src19 + 0.545 * src20 + 0.5 * src21 + 0.455 * src22 + 0.444 * src23 + 0.429 * src24 + 0.4 * src25 + 0.375 * src26 + 0.364 * src27 + 0.333 * src28 + 0.3 * src29 + 0.286 * src30 + 0.273 * src31 + 0.25 * src32 + 0.222 * src33 + 0.2 * src34 + 0.182 * src35 + 0.167 * src36 + 0.143 * src37 + 0.125 * src38 + 0.111 * src39 + 0.1 * src40 + 0.091 * src41) / 21.5
        12 => (source + 0.917 * src1 + 0.909 * src2 + 0.9 * src3 + 0.889 * src4 + 0.875 * src5 + 0.857 * src6 + 0.833 * src7 + 0.818 * src8 + 0.8 * src9 + 0.778 * src10 + 0.75 * src11 + 0.727 * src12 + 0.714 * src13 + 0.7 * src14 + 0.667 * src15 + 0.636 * src16 + 0.625 * src17 + 0.6 * src18 + 0.583 * src19 + 0.571 * src20 + 0.556 * src21 + 0.545 * src22 + 0.5 * src23 + 0.455 * src24 + 0.444 * src25 + 0.429 * src26 + 0.417 * src27 + 0.4 * src28 + 0.375 * src29 + 0.364 * src30 + 0.333 * src31 + 0.3 * src32 + 0.286 * src33 + 0.273 * src34 + 0.25 * src35 + 0.222 * src36 + 0.2 * src37 + 0.182 * src38 + 0.167 * src39 + 0.143 * src40 + 0.125 * src41 + 0.111 * src42 + 0.1 * src43 + 0.091 * src44 + 0.083 * src45) / 23.5
        13 => (source + 0.923 * src1 + 0.917 * src2 + 0.909 * src3 + 0.9 * src4 + 0.889 * src5 + 0.875 * src6 + 0.857 * src7 + 0.846 * src8 + 0.833 * src9 + 0.818 * src10 + 0.8 * src11 + 0.778 * src12 + 0.769 * src13 + 0.75 * src14 + 0.727 * src15 + 0.714 * src16 + 0.7 * src17 + 0.692 * src18 + 0.667 * src19 + 0.636 * src20 + 0.625 * src21 + 0.615 * src22 + 0.6 * src23 + 0.583 * src24 + 0.571 * src25 + 0.556 * src26 + 0.545 * src27 + 0.538 * src28 + 0.5 * src29 + 0.462 * src30 + 0.455 * src31 + 0.444 * src32 + 0.429 * src33 + 0.417 * src34 + 0.4 * src35 + 0.385 * src36 + 0.375 * src37 + 0.364 * src38 + 0.333 * src39 + 0.308 * src40 + 0.3 * src41 + 0.286 * src42 + 0.273 * src43 + 0.25 * src44 + 0.231 * src45 + 0.222 * src46 + 0.2 * src47 + 0.182 * src48 + 0.167 * src49 + 0.154 * src50 + 0.143 * src51 + 0.125 * src52 + 0.111 * src53 + 0.1 * src54 + 0.091 * src55 + 0.083 * src56 + 0.077 * src57) / 29.5
        14 => (source + 0.929 * src1 + 0.923 * src2 + 0.917 * src3 + 0.909 * src4 + 0.9 * src5 + 0.889 * src6 + 0.875 * src7 + 0.857 * src8 + 0.846 * src9 + 0.833 * src10 + 0.818 * src11 + 0.8 * src12 + 0.786 * src13 + 0.778 * src14 + 0.769 * src15 + 0.75 * src16 + 0.727 * src17 + 0.714 * src18 + 0.7 * src19 + 0.692 * src20 + 0.667 * src21 + 0.643 * src22 + 0.636 * src23 + 0.625 * src24 + 0.615 * src25 + 0.6 * src26 + 0.583 * src27 + 0.571 * src28 + 0.556 * src29 + 0.545 * src30 + 0.538 * src31 + 0.5 * src32 + 0.462 * src33 + 0.455 * src34 + 0.444 * src35 + 0.429 * src36 + 0.417 * src37 + 0.4 * src38 + 0.385 * src39 + 0.375 * src40 + 0.364 * src41 + 0.357 * src42 + 0.333 * src43 + 0.308 * src44 + 0.3 * src45 + 0.286 * src46 + 0.273 * src47 + 0.25 * src48 + 0.231 * src49 + 0.222 * src50 + 0.214 * src51 + 0.2 * src52 + 0.182 * src53 + 0.167 * src54 + 0.154 * src55 + 0.143 * src56 + 0.125 * src57 + 0.111 * src58 + 0.1 * src59 + 0.091 * src60 + 0.083 * src61 + 0.077 * src62 + 0.071 * src63) / 32.5
        => na

    fswma

p_ind_1 = p_ta_fswma(close, 5, true) // FSWMA
plot(p_ind_1, "FSWMA", color.rgb(109, 30, 127, 0), 2)
Existing indicator chart preview
Existing visual preview. Verify exact values against the source; the image is not a performance result.

Specify a checkable adaptation in Pineify

Specify the order-to-bar mapping, coefficient rounding, source and missing-value policy. If you want editable inputs or direction colors, request them explicitly because this reference does not implement them. Ask for a syntax check and compare constant and stepped input sequences before adding strategy orders.

Open Pine Script AI Coding Agent

Educational indicator analysis. No measured trading performance is established here. Syntax validation does not prove formula equivalence, execution assumptions or profitability.

Implementation questions

No. This implementation uses ten positive Farey weights at order 5. The order limits the fractions’ denominators; the number of distinct fractions determines the bar count.

That parameter is present in the helper signature but unused. The final plot sets a fixed purple color. Automatic slope coloring requires additional code; it is not active in this snippet.

No input control is declared here. Edit the literal order in the helper call, or request a version with an input constrained to supported orders 2 through 14. Unsupported orders return na.

No. Division by the sum of the positive weights keeps the result in source-price units. Different orders can yield different prices because they use different windows and weights, not because they have incompatible units.

The intended sequence contains rational fractions, but the displayed switch stores decimal approximations to three places where needed. Compare the stored coefficients when reproducing this version exactly.

No. It checks the script’s syntax. Trading results require a specified strategy, market data, execution assumptions and independent evaluation. This indicator only computes and plots an average.