TradingView indicator guides

Ichimoku Cloud in Pine Script: Formulas and Plot Alignment

Ichimoku combines range midpoints with displaced plots. The source below calculates the familiar 9, 26 and 52-bar components, then displays the cloud ahead and the close behind. Those plot positions do not create future data. Correct interpretation depends on separating when a value was calculated from where it is drawn.

Inspect the Pine Script

Five series, three range windows

For a complete valid window, midpoint(N) = (highest high over N bars + lowest low over N bars) / 2. Tenkan-sen, or the conversion line, uses N = 9. Kijun-sen, or the base line, uses N = 26. Senkou Span A averages those two lines. Senkou Span B uses midpoint(52). Chikou, the lagging plot, uses the current close.

These midpoints average two extremes, not all closes or all highs and lows. The base line can remain flat while individual prices move, provided its window’s highest high and lowest low remain unchanged. An old extreme leaving the window can also move a midpoint without a matching new extreme on the current bar.

TradingView’s Ichimoku overview describes the five components and their usual displacement convention. For the copyable implementation here, inspect the actual offsets below rather than substituting a prose period label.

This source uses offsets of +25 and −25

The leading plots set offset = 26 − 1, which is +25 chart-index positions. The lagging plot sets offset = −26 + 1, which is −25 positions. A span calculated at index 100 is drawn at index 125; the close calculated at index 100 is drawn at index 75. The conversion and base lines remain at index 100.

The reference’s fourth helper argument is named lagging_span but is unused inside the function. Its literal value does not control the plots. To change displacement, update the plot offsets consistently or refactor the example to use one explicit displacement setting. Changing only that helper argument leaves the picture unchanged.

TradingView’s plot documentation distinguishes display offset from the calculated series. A positive offset moves the drawing to the right. It does not make the series predict the value at that future timestamp, and it does not shift the underlying variable used in a condition.

Compare price with the cloud at the same timestamp

Let A[t] and B[t] be the spans calculated now. With +25 plot offsets, the cloud drawn underneath the current price at index t comes from A[t−25] and B[t−25]. Its upper boundary is max(A[t−25], B[t−25]); its lower boundary is min(A[t−25], B[t−25]). Comparing close with A[t] and B[t] instead compares price with the newly calculated cloud that is drawn ahead.

Hypothetical example: the current 9-bar high and low are 112 and 104, giving conversion 108. The current 26-bar extremes are 114 and 98, giving base 106. Span A is 107. The 52-bar extremes are 120 and 90, giving Span B 105. Those current spans appear 25 positions ahead.

Suppose the spans calculated 25 bars earlier were A = 109 and B = 111, while the current close is 108. Price is above both newly calculated spans, 107 and 105, but below the cloud currently drawn at its timestamp, 109 to 111. These statements can both be true. An alert labeled “above the cloud” must specify which comparison it uses.

The cloud width is the absolute difference between its two aligned spans. Extending the Span B lookback does not guarantee a wider cloud: both the highest high and lowest low can change, and the midpoint’s distance from Span A can rise or fall. Width alone is not a probability that a price level will hold.

The lagging plot must not become a backdated signal

The close at index 100 is known at index 100 and appears at index 75 in this example. It was not known at index 75. A historical chart can therefore display this later observation over earlier price action; a backtest must not use that placement as an entry available on the earlier bar.

A present-time comparison of close with close[25] is observable now. A comparison with the full earlier candle needs its high and low, rather than its close alone. Define the intended comparison explicitly. The lagging plot’s visual position does not decide which historical price field to use.

Current high, low and close can develop before the bar closes. Conversion, base, newly calculated spans and the back-displayed close can consequently change on that open bar. The already calculated spans from 25 completed bars earlier are a different timing reference. The snippet adds no bar-close confirmation or data-revision protection.

Read the actual colors and features

Conversion is blue, base is red, lagging close is green, Span A is pale green and Span B is pale red. The cloud fill is always blue with transparency 90. It does not switch between green and red based on which span is higher. A crossing of the span series changes their ordering, not this fill color.

The displayed source has no input() calls. Its 9, 26, 52 and literal offsets are fixed in code; plot appearance can be handled through the chart’s available style controls. It draws no crossover markers, alert conditions, positions or orders. It does not enforce a full-history readiness rule beyond the built-in calculations.

A conversion/base crossover is one relation between rolling midpoints. Price position relative to the aligned cloud is another. Neither has a measured reliability rating here, and combining them does not by itself demonstrate an edge. A cloud twist refers to a change in span ordering, not simply price sitting inside a flat cloud.

Build a testable rule without a forecast claim

First choose the cloud alignment, source chart, windows and confirmation timing. Define whether “above cloud” is a state or the first transition into that state, how equality is treated, and whether a conversion/base crossing must occur on the same bar. Record these choices before evaluating results.

For a strategy, separately specify order timing, stops, exits, fees, slippage and position sizing. Using the cloud or an ATR multiple as a candidate stop does not guarantee the fill price or maximum loss. No best timeframe, win rate, signal frequency or benefit from an RSI, volume or EMA filter is established by this indicator example.

For comparison, Bollinger Bands use price dispersion around an average, while ADX measures a directional-movement construction without providing a signed trend direction on its own. Their outputs answer different questions; their names do not establish a ranking in trading reliability.

Pine Script v6: implementation and limits

This reference preserves the fixed 9/26/52 calculation and the literal ±25 plot offsets. The helper’s fourth argument is unused. It has no editable calculation inputs, dynamic cloud colors, alerts or orders. Check both the calculation timestamp and plot position in TradingView.

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="Ichimoku Cloud", overlay=true, max_labels_count=500)

p_custom_sources() =>
    [open, high, low, close, volume]

p_ta_donchian(len) => math.avg(ta.lowest(len), ta.highest(len))
p_ta_ichimoku(simple int conversion_length, simple int base_length, simple int span_b_length, simple int lagging_span) =>
    conversionLine = p_ta_donchian(conversion_length)
    baseLine = p_ta_donchian(base_length)
    leadLine1 = math.avg(conversionLine, baseLine)
    leadLine2 = p_ta_donchian(span_b_length)
    [conversionLine, baseLine, leadLine1, leadLine2, close]

[p_ind_1_conversion, p_ind_1_base, p_ind_1_spanA, p_ind_1_spanB, p_ind_1_close] = p_ta_ichimoku(9, 26, 52, 26)

plot(p_ind_1_conversion, color=color.rgb(41, 98, 255, 0), linewidth=1, title="Ichimoku - Conversion Line")
plot(p_ind_1_base, color=color.rgb(183, 28, 28, 0), linewidth=1, title="Ichimoku - Base Line")
plot(p_ind_1_close, offset = -26 + 1, color=color.rgb(67, 160, 71, 0), linewidth=1, title="Ichimoku - Lagging Span")
p_ind_1_spanA_p = plot(p_ind_1_spanA, color=color.rgb(165, 214, 167, 0), linewidth=1, title="Ichimoku - Leading Span A", offset = 26 - 1)
p_ind_1_spanB_p = plot(p_ind_1_spanB, color=color.rgb(239, 154, 154, 0), linewidth=1, title="Ichimoku - Leading Span B", offset = 26 - 1)
fill(p_ind_1_spanA_p, p_ind_1_spanB_p, color=color.rgb(33, 150, 243, 90), title="Ichimoku - Cloud")
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 three lookbacks and the exact displacement convention. For a cloud condition, state whether it refers to the cloud drawn at the current price or the newly calculated spans. Ask for explicit inputs, aligned series, missing-history guards and confirmed alerts if required. Inspect and syntax-check the generated source, then reconcile a known bar 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. Current and past price data produce the spans. A positive plot offset only places those values to the right on the chart. The source contains no statistical forecast or measured prediction result.

The actual plot expressions are 26 − 1 and −26 + 1. They evaluate to +25 and −25. Keep the literal chart-index convention explicit when matching another implementation or writing conditions.

If the intention is to match the cloud visually underneath the current candle, use spans calculated 25 bars earlier in this example. Current unshifted span variables describe the cloud drawn ahead. A rule can choose either comparison, but they are different.

Not in the displayed helper. That parameter is unused, and the plot offsets are separate literals. Update all relevant offsets together or request an explicit shared setting.

No. It uses a constant translucent blue fill. The two boundary lines have different fixed colors. Their order can change without changing the fill color.

Not as though it was known then. The value is the later current close, drawn 25 positions into the past. Any decision must use its real calculation time, otherwise the rule introduces future information.