Pine Script handles Ichimoku’s displacement gracefully with the offset
parameter on plot, so building the full system — cloud and all — is cleaner than
you’d expect. See the explainer for what each line is.
What you’ll need
- A TradingView account (free tier is fine)
- The Pine Editor
The source
//@version=5
indicator("AlgoGen Ichimoku", shorttitle="AG Ichimoku", overlay=true)
convLen = input.int(9, "Tenkan")
baseLen = input.int(26, "Kijun")
spanBLen = input.int(52, "Senkou B")
disp = input.int(26, "Displacement")
midline(len) => math.avg(ta.highest(high, len), ta.lowest(low, len))
tenkan = midline(convLen)
kijun = midline(baseLen)
senkouA = math.avg(tenkan, kijun)
senkouB = midline(spanBLen)
plot(tenkan, "Tenkan", color=#eb6834, linewidth=2)
plot(kijun, "Kijun", color=#4a3aa7, linewidth=2)
// Chikou: current close shifted BACK.
plot(close, "Chikou", color=#1baf7a, offset=-disp)
// Senkou spans shifted FORWARD; fill between them for the cloud.
pA = plot(senkouA, "Senkou A", color=color.new(#0ca30c, 60), offset=disp)
pB = plot(senkouB, "Senkou B", color=color.new(#d03b3b, 60), offset=disp)
fill(pA, pB, color = senkouA > senkouB ? color.new(#0ca30c, 85) : color.new(#d03b3b, 85))
math.avg(ta.highest(high, len), ta.lowest(low, len)) is the midpoint every line
shares. The magic is offset: +disp pushes the Senkou spans forward into the
future (creating the projected cloud), and -disp pulls the Chikou backward.
The fill colours the cloud green or red depending on which span is on top.
Detecting a Kumo twist
A “twist” — where the future cloud changes colour because Senkou A crosses Senkou B — is one of Ichimoku’s forward-looking cues. Because the spans are computed on the current bar (before displacement), you can detect it directly:
twistBull = ta.crossover(senkouA, senkouB)
twistBear = ta.crossunder(senkouA, senkouB)
bgcolor(twistBull ? color.new(#0ca30c, 90) : na, offset=disp, title="Bull twist")
bgcolor(twistBear ? color.new(#d03b3b, 90) : na, offset=disp, title="Bear twist")
The offset=disp on bgcolor places the highlight where the twist will actually
appear in the projected cloud — 26 bars ahead — so it lines up with the visual.
Gotchas
offset, not[], for plotting displacement. Useoffset=dispon theplotcall to draw a line shifted in time. UsingsenkouA[disp]instead shifts the data the wrong way and misplaces the cloud.- Chikou offset is negative.
offset=-dispdraws the close 26 bars back. A positive offset here is a classic mistake. math.avgfor the midpoint. It’s just(highest + lowest) / 2;math.avgreads cleanly and avoids a stray operator-precedence bug.
A cloud-position signal
cloudTop = math.max(senkouA, senkouB)
longOK = close > cloudTop[disp] // price above the cloud that's under it now
alertcondition(ta.crossover(tenkan, kijun) and longOK, "Ichimoku bull", "TK cross above cloud")
Same indicator elsewhere: Python, MQL5, EasyLanguage, NinjaScript. Then test an Ichimoku rule in AlgoGen.
This post is educational, not financial advice. Indicators describe the past; they don’t predict the future. Backtest anything before you risk real money on it.
Sources and further reading
- Ichimoku: At a Glance (CMT Association)
- Pine Script built-ins (TradingView)
