Three Simple Moving Averages: Windows, Stacks and Timing
The copied script calculates SMA 50, SMA 100 and SMA 200 from close, on the active chart timeframe. It plots orange, teal and purple lines. Its helper accepts three length/source pairs, but the example fixes all six arguments in code and declares no settings-panel inputs. Three windows on one timeframe are not a multi-timeframe data request.
Inspect the Pine ScriptEach line averages its own source window
With complete data, SMA(N) is the sum of the current source and its previous N − 1 values, divided by N. The three calculations are separate calls, not three smoothing stages applied to one another. This also distinguishes the indicator from TEMA.
The Pine reference documents that ta.sma ignores unavailable source values. A literal fixed-bar sum and division describes the complete-data case; do not replace missing observations with zero and expect the same result. Match sources and usable history when comparing lines.
Different lengths on the same source do not respond identically. Keeping all sources on close is a valid way to isolate the effect of length. Different sources introduce another distinction: if one line averages high and another low, their ordering may reflect candle range rather than a trend across horizons. HLC3 is a different per-bar input, not a guaranteed reduction in false signals.
A moving average responds on the current update
For a full, uninterrupted N-bar window, SMA[t] − SMA[t−1] = (source[t] − source[t−N]) / N. The latest observation replaces the departing one immediately. A 50-bar average does not wait 25 bars before reacting.
On a steady linear ramp with slope b per bar, the average lies b × (N − 1)/2 behind the current value. That is a property of the ramp and equal weights, not a universal delay before detecting any price change, crossover or reversal.
For a synthetic series held at 100 long enough to fill every window, a step to 120 immediately raises SMA 50 to 100.4, SMA 100 to 100.2 and SMA 200 to 100.1. All three respond on the same bar, by different amounts. A long window can still change sharply if the incoming and departing prices differ greatly.
Ordering, slope and current price are separate checks
To compare short, medium and long averages, first establish ascending lengths and comparable source units. Preserve their identities when testing order; sorting the three values would erase the very relationship being measured. An ascending value stack alone does not mean all slopes are positive or that the latest close is above them.
Use hypothetical closes 100, 110, 120, 300, 90 and lengths 2, 3 and 4. At the fourth close, the averages are 210, 176.666667 and 157.5. At the fifth close they are 195, 170 and 155. Short remains above medium above long, while every average falls and the current close 90 is below every line.
A rule requiring positive slope in addition to ordering is stricter than ordering alone. Neither rule is implemented by the supplied script: it plots values without classifying stacks or generating signals. Requiring an ordered state on several bars also differs from a one-time transition into that state.
Widening gaps do not prove accelerating prices
Take a fully initialized series at 100, then change it to 120 and leave it flat. Use lengths 2, 4 and 6 to inspect the update directly.
| Update | Close | SMA 2 | SMA 4 | SMA 6 | Short–medium / medium–long gaps |
|---|---|---|---|---|---|
| 1 | 120 | 110 | 105 | 103.333333 | 5 / 1.666667 |
| 2 | 120 | 120 | 110 | 106.666667 | 10 / 3.333333 |
| 3 | 120 | 120 | 115 | 110 | 5 / 5 |
Between updates 1 and 2 both gaps widen while price does not change. The windows are replacing old observations at different rates. A width such as maximum average minus minimum average measures dispersion among smoothed levels; it is not the volatility of the underlying returns.
For a percentage separation, state the denominator and handle zero or negative references explicitly. A threshold has no universal statistical meaning. Equal periods and sources produce identical lines, and closely spaced lengths remain valid comparisons rather than automatically invalid configurations.
Match the code and product controls
The fixed call uses 50/100/200 and close for all sources. Pineify’s separate Visual Editor configuration exposes three length and three source fields, but those controls are not declarations in this copied script. Edit the arguments or add explicit positive-length inputs for an adjustable copy.
No relationship among the three lengths is enforced by the helper. A 20/50/200 selection has ratios 2.5 and 10 relative to 20, not 2.5 and 4. A 10/30/100 selection has ratios 3 and 10. These arithmetic facts do not establish a preferred setting.
The indicator has no alerts, confirmed-bar signals, orders, ATR stops, RSI filter or higher-timeframe request. With close as source, live lines update before bar close. If adding events, specify whether they wait for confirmation and when orders can subsequently execute.
All three windows overlap heavily on the same data. They are not independent evidence of future performance. Compare settings on the same feed, sessions and price adjustments, keep a separate evaluation sample, and record fees and execution assumptions if constructing a strategy.
Pine Script v6: implementation and limits
This complete example retains its three fixed SMA calls and plot colors. There are no crossover booleans, buy/sell markers or orders. The function name does not imply an independently tested trading system.
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// (c) Pineify
//@version=6
indicator(title="Three Simple Moving Averages", overlay=true, max_labels_count=500)
p_ta_three_simple_moving_averages(simple int length1, series float source1, simple int length2, series float source2, simple int length3, series float source3) =>
sma1 = ta.sma(source1, length1)
sma2 = ta.sma(source2, length2)
sma3 = ta.sma(source3, length3)
[sma1, sma2, sma3]
[p_ind_1_sma1, p_ind_1_sma2, p_ind_1_sma3] = p_ta_three_simple_moving_averages(50, close, 100, close, 200, close)
plot(p_ind_1_sma1, "TSMA - SMA 1", color.rgb(255, 152, 0, 0), 1)
plot(p_ind_1_sma2, "TSMA - SMA 2", color.rgb(8, 153, 129, 0), 1)
plot(p_ind_1_sma3, "TSMA - SMA 3", color.rgb(156, 39, 176, 0), 1)
Specify a checkable adaptation in Pineify
Ask the Pine Script AI Coding Agent to expose three positive lengths and independent sources, then specify whether you need line ordering, slopes, price position or a transition event. Request confirmation rules explicitly. Compare the supplied numerical cases and final chart values before adding trade execution logic.
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.