MavilimW Pine Script: Six WMA Stages, Effective History and Turns
MavilimW feeds one weighted moving average into the next through six stages. The default lengths are 3, 5, 8, 13, 21 and 34, but the final output depends on seventy-nine consecutive source observations, not just the longest stage’s thirty-four. This guide derives that history and distinguishes a line color from a one-bar turn event.
Inspect the Pine ScriptThe recurrence and its public attribution
With seeds a and b, the six lengths are a, b, a+b, a+2b, 2a+3b and 3a+5b. Each stage applies a linearly weighted moving average to the preceding stage’s output; only the first stage reads close. The default a=3, b=5 gives 3/5/8/13/21/34.
KivancOzbilgic’s published MavilimW adaptation credits @mavilim0732 with the original indicator. Earlier public MavilimW publications also predate 2023. This calculation is a finite WMA cascade, not a proprietary Jurik algorithm or a volatility-adaptive filter.
Why the default needs 79 bars
A length-N WMA combines N observations. Cascading two finite filters of lengths L1 and L2 produces a combined support of L1+L2−1; each further stage adds its length minus one. Six stages therefore use sum(lengths)−5 observations. Here the sum is 84, so the final support is 79.
| Seeds | Six lengths | Combined observations | Mean age of weights |
|---|---|---|---|
| 2 / 3 | 2, 3, 5, 8, 13, 21 | 47 | 15.333333 bars |
| 3 / 5 | 3, 5, 8, 13, 21, 34 | 79 | 26 bars |
| 5 / 8 | 5, 8, 13, 21, 34, 55 | 131 | 43.333333 bars |
On uninterrupted valid data, the default first produces a value on observation 79. A direction comparison needs observation 80; a slope-turn event needs a further previous slope, making observation 81 the first eligible one. Missing observations require separate attention to Pine’s data handling.
For a linearly weighted average, mean sample age is (N−1)/3. Ages add across the cascade, giving (sum(lengths)−6)/3. This describes the offset on a steady linear input after initialization; it is not a promised delay between arbitrary market reversals and signals. Convert bar counts to calendar time only after accounting for the chart’s sessions and gaps.
Both seeds matter, and the second has the larger coefficient
The sum of the six lengths is 8a+12b. Increasing a by one, with b fixed, adds eight observations to combined support. Increasing b by one, with a fixed, adds twelve. The claim that the first seed always has three times the effect is incompatible with this recurrence.
Changing only a from 3 to 5 while leaving b=5 gives 5/5/10/15/25/40, not 5/8/13/21/34/55. The latter changes both inputs. Its longest stage is 55, not 89. These relationships help isolate a parameter change before attributing any difference in a chart to a particular cause.
A small immediate response is still a response
For positive integer lengths and valid data, each WMA has nonnegative normalized weights. Their convolution does too, so the combined output is a weighted average of the source history, not an extrapolation beyond that window’s extrema.
After a long constant price of 100, let the current close jump to 110. Each stage’s current-input weight is 2/(N+1). For the default six lengths their product is about 0.000027485742, so the new final value is approximately 100.000274857. It can turn blue immediately after a flat state despite its very small movement. Six stages do not force every turn to wait a fixed two, three or four bars.
This synthetic response establishes neither useful entry timing nor support at that price. A moving average is a calculated reference; whether price subsequently respects it is an empirical question.
Blue is a state; the returned buy flag is an event
The plot is blue when the final value exceeds its predecessor, red when it is lower, and yellow on equality or when the comparison is unavailable. The returned buy flag tests a crossover of MAVW with MAVW[1]: the current slope must be positive and the preceding slope nonpositive. A flat-to-rising transition qualifies, not only falling-to-rising.
In a synthetic output sequence 100, 100, 101, 102, the last two points are blue but only the 101 point has the upward-turn event. A turn event is not the price crossing the line. The helper’s buy/sell variable names do not place orders or emit alerts.
The code has no bar-close gate. Its current value, color and turn flags can change on the open bar. If adding confirmation, decide whether to confirm the current turn, require a run of states, or wait for a price condition; those are different rules.
Scope of the copied example
The script fixes the seeds at 3 and 5 and uses close. It has no settings inputs, alert conditions, markers, strategy orders, volume filter or ATR stop. Only the final line is plotted. To compare another implementation, match all six lengths, WMA weighting, source and available history.
The complete example below preserves the existing calculation. For a trading experiment, separately define entry time, price-based stops, exits, transaction costs and evaluation data. A color change, a numeric buffer around the line, or a chosen ADX threshold does not establish a measured win rate by itself.
Pine Script v6: implementation and limits
The full example computes six WMA stages and returns two slope-turn booleans. It plots only the final line, with yellow for a tie or unavailable comparison. It has no alert conditions or strategy orders.
//@version=6
indicator(title="MavilimW", overlay=true, max_labels_count=500)
// Core MavilimW calculation — cascaded WMA with Fibonacci-length progression
p_ta_mavw(simple int fmal, simple int smal) =>
tmal = fmal + smal
Fmal = smal + tmal
Ftmal = tmal + Fmal
Smal = Fmal + Ftmal
M1 = ta.wma(close, fmal)
M2 = ta.wma(M1, smal)
M3 = ta.wma(M2, tmal)
M4 = ta.wma(M3, Fmal)
M5 = ta.wma(M4, Ftmal)
MAVW = ta.wma(M5, Smal)
buySignal = ta.crossover(MAVW, MAVW[1])
sellSignal = ta.crossunder(MAVW, MAVW[1])
[MAVW, buySignal, sellSignal]
[p_ind_1_mavw, p_ind_1_buySignal, p_ind_1_sellSignal] = p_ta_mavw(3, 5)
// MAVW plot — blue when rising, red when falling, width 2
plot(p_ind_1_mavw, "MAVW",
p_ind_1_mavw > p_ind_1_mavw[1] ? color.rgb(41, 98, 255, 0) :
p_ind_1_mavw < p_ind_1_mavw[1] ? color.rgb(242, 54, 69, 0) :
color.yellow, 2)
Specify a checkable adaptation in Pineify
Ask Pine Script AI Coding Agent for explicit positive-integer seed inputs and a confirmed-bar alert for a precisely defined slope turn. Have it report the resulting six lengths and required history. Review the source and compare a constant-history step before evaluating trading rules.
Open Pine Script AI Coding AgentEducational indicator analysis. No measured trading performance is established here. Syntax validation does not prove formula equivalence, execution assumptions or profitability.