Tracking the Ichimoku Base Line in Pine Script

This lesson demonstrates how you can track the Ichimoku Cloud's Base Line to use as a support & resistance condition for detecting candlestick patterns around it when it's going horizontal.

Check out my other free lessons!

Source Code

// PineScriptMastery.com
// @version=5
indicator(title="Ichimoku Cloud", shorttitle="Ichimoku", overlay=true)

// Get user input
conversionPeriods       = input.int(9, minval=1, title="Conversion Line Length")
basePeriods             = input.int(26, minval=1, title="Base Line Length")
laggingSpan2Periods     = input.int(52, minval=1, title="Leading Span B Length")
displacement            = input.int(26, minval=1, title="Lagging Span")
donchian(len)           => math.avg(ta.lowest(len), ta.highest(len))
conversionLine          = donchian(conversionPeriods)
baseLine                = donchian(basePeriods)
leadLine1               = math.avg(conversionLine, baseLine)
leadLine2               = donchian(laggingSpan2Periods)

// Draw cloud
plot(conversionLine, color=#2962FF, title="Conversion Line", display=display.none)
plot(baseLine, color=#B71C1C, title="Base Line", display=display.none)
plot(close, offset = -displacement + 1, color=#43A047, title="Lagging Span", display=display.none)
p1 = plot(leadLine1, offset = displacement - 1, color=#A5D6A7, title="Leading Span A", display=display.none)
p2 = plot(leadLine2, offset = displacement - 1, color=#EF9A9A, title="Leading Span B", display=display.none)
fill(p1, p2, color = leadLine1 > leadLine2 ? color.rgb(67, 160, 71, 90) : color.rgb(244, 67, 54, 90), display=display.none)

// Track horizontal baseline
var baseLineSaved = baseLine
if baseLine != baseLine[1]
    baseLineSaved := na
else
    baseLineSaved := baseLine

// Draw baseline
plot(baseLineSaved, color=color.purple, style=plot.style_linebr, title="Base Line Saved")

// Track price trading above/below baseline
isPriceAboveBaseLine = close > baseLineSaved
priceTradingAboveBL = isPriceAboveBaseLine and not na(baseLineSaved)
priceTradingBelowBL = not isPriceAboveBaseLine and not na(baseLineSaved)

// Draw condition
bgcolor(priceTradingAboveBL ? color.new(color.green,80) : na)
bgcolor(priceTradingBelowBL ? color.new(color.red,80) : na)

// Track candle pattern tests of baseline (example purposes - needs more conditions to be actually useful!)
candlePatternBull = priceTradingAboveBL and priceTradingAboveBL[1] and low[1] < baseLineSaved and close > baseLineSaved
candlePatternBear = priceTradingBelowBL and priceTradingBelowBL[1] and high[1] > baseLineSaved and close < baseLineSaved
plotshape(candlePatternBull, style=shape.triangleup, color=color.green, location=location.belowbar)
plotshape(candlePatternBear, style=shape.triangledown, color=color.red)