Moving Average Cloud: Two SMAs and Pine Script | Pineify
The Moving Average Cloud shades the space between SMA 5 and SMA 10 of close. Green means SMA 5 is strictly higher; the fallback is red, including equality. The fill makes the relationship visible without adding information to the two averages. Cloud width is a price difference, not a measured probability or an automatic position-size rule.
Inspect the Pine ScriptThe calculation and its exact visual contract
SMA1 is the average of the latest five closes; SMA2 is the average of the latest ten. On a complete ten-bar window, let R be the average of its recent five closes and P the average of its preceding five. Then SMA1 − SMA2 = (R − P)/2.
This default cloud therefore compares two adjacent five-bar averages. A green state says the recent block average exceeds the prior block average. It does not require the current close, either SMA slope, or every recent bar to be rising.
The plotted SMA1 line is opaque blue and SMA2 is opaque red. In Pine color.new(), transparency 0 is fully opaque; transparency 67 gives approximately 33% opacity. The original code uses 67 for the fill and 0 for the lines, so the lines are visible.
At equality, the red branch is selected, but the two lines coincide and the fill has zero vertical width. A green-to-red color choice can occur at equality without a strict crossunder; a later move below can be a crossunder while the chosen color remains red.
Reference: Pine transparency and conditional colors.
Worked step: thinning without a falling price
Start with at least ten closes of 100. Price then moves to 110 and remains there. This is a synthetic calculation, not a backtest.
| Bar after step | Close | SMA 5 | SMA 10 | Signed gap |
|---|---|---|---|---|
| 0 | 100 | 100 | 100 | 0 |
| 1 | 110 | 102 | 101 | 1 |
| 2 | 110 | 104 | 102 | 2 |
| 5 | 110 | 110 | 105 | 5 |
| 6 | 110 | 110 | 106 | 4 |
| 9 | 110 | 110 | 109 | 1 |
| 10 | 110 | 110 | 110 | 0 |
The gap grows for the first five bars, then shrinks to zero even though price stays at 110. A thinning cloud does not prove selling pressure or a price reversal. Equality at bar 10 selects red internally, but there is no negative gap and no visible cloud thickness.
For a price rising exactly one unit every bar, a complete SMA N equals the current price minus (N−1)/2. SMA 5 and SMA 10 therefore stay 2.5 units apart. A sustained green cloud with constant width can represent a constant rate of increase, not acceleration.
Lengths, scale, and initialization
The displayed call fixes lengths at 5 and 10. There are no input functions for changing them in the settings panel. The helper can accept other positive lengths when the code is deliberately adapted.
Equal lengths on the same source produce identical averages and zero thickness. A one-period difference can already produce nonzero thickness: on the one-unit linear rise, SMA 5 and SMA 6 differ by 0.5. No minimum two-period difference or 1:1.5 ratio is mathematically required.
The ratio alone does not specify behavior. On that same linear rise, lengths 5/10 produce gap 2.5 while 10/20 produce gap 5, despite both having ratio 1:2. Absolute window sizes and the observed source sequence matter. The pair 8/13 has ratio 1:1.625, not 1:2.
With continuous valid closes, SMA 5 first appears on the fifth observation and SMA 10 on the tenth. Before both exist, there is no fully defined cloud. SMA 10 includes the current bar through nine bars ago, not the bar ten bars ago.
Raw width scales with the price units. If normalizing, distinguish absolute width from signed spread, specify the denominator, and handle zero or negative values explicitly. None of these transformations turns width into a calibrated measure of trade success.
Live cloud versus a confirmed event
On an open bar, the latest close can change both averages and their ordering. This script has no close-confirmation gate, alert condition, crossover marker, or order logic. Its color is a continuously evaluated state.
If adapting it to alerts, define whether a green state, a strict crossover, or several successive closed bars should trigger an event. Waiting three closes changes the time at which the rule becomes known. A backtest must not count the first bar as an already confirmed entry.
Adding a stop or sizing rule requires its own definition. A cloud flip does not cap loss, and a wider cloud alone does not justify increasing position size. The indicator contains no measured risk model.
A fill is not an Ichimoku forecast
A two-SMA cloud is the same pair of numerical averages as an ordinary two-SMA overlay, with an added fill. No supported development lineage is asserted here.
Ichimoku uses high-low range midpoints and multiple components, including spans drawn forward. A forward drawing offset places currently calculated information on later chart coordinates; it is not knowledge of future prices. Its parameters are configurable, and counting its lines does not establish which method responds first on a given sequence.
This example uses only two close-based SMAs, has no forward offset, and makes no future-support prediction. Compare formulas and observation times instead of assuming all indicators with the word “cloud” share the same calculation.
Official reference: TradingView Ichimoku Cloud.
Pine Script v6: implementation and limits
The full source uses a fixed 5/10 pair of close-based SMAs, two visible lines, and a conditional fill. There are no settings inputs or confirmation rules. The existing preview illustrates appearance, not performance or current market behavior.
//@version=6
indicator(title="Moving Average Cloud", overlay=true, max_labels_count=500)
// Two SMAs with colored cloud fill between them
p_ta_moving_average_cloud(simple int sma1Length, simple int sma2Length) =>
sma1 = ta.sma(close, sma1Length)
sma2 = ta.sma(close, sma2Length)
[sma1, sma2]
// Default: 5-period and 10-period SMAs
[sma1, sma2] = p_ta_moving_average_cloud(5, 10)
// Plot the two SMAs
sma1Plot = plot(sma1, "SMA1", color=color.new(#2196F3, 0), linewidth=2)
sma2Plot = plot(sma2, "SMA2", color=color.new(#F44336, 0), linewidth=2)
// Cloud fill: green for bullish, red for bearish
cloudColor = sma1 > sma2 ? color.new(#4CAF50, 67) : color.new(#F44336, 67)
fill(sma1Plot, sma2Plot, title="MA Cloud", color=cloudColor)Specify a checkable adaptation in Pineify
Specify both lengths, source, equality behavior, transparency, and whether the requested result is a state or a new confirmed event. Ask the Pine Script AI Coding Agent to preserve those choices explicitly. Verify the step-response table and live-bar behavior before evaluating a strategy.
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.