//@version=6
strategy("Ichimoku Strategy", overlay=true, commission_value=0.1, slippage=3)
// Ichimoku Settings
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)
cloudUpper = leadLine1 > leadLine2 ? leadLine1 : leadLine2
cloudLower = leadLine1 < leadLine2 ? leadLine1 : leadLine2
laggingSpan = close
// Entry Conditions
bearishCross = ta.crossunder(conversionLine, baseLine)
priceBelowCloud = close < cloudLower
downtrendSignal = leadLine1 < leadLine2
laggingBelowCloud = laggingSpan < cloudLower[displacement]
shortCondition = bearishCross and priceBelowCloud and downtrendSignal
bullishCross = ta.crossunder(conversionLine, baseLine)
priceAboveCloud = close > cloudUpper
uptrendSignal = leadLine1 > leadLine2
longCondition = bullishCross and priceAboveCloud and uptrendSignal
// Stop Loss and Take Profit Calculation
var float shortStopLoss = na
var float shortTakeProfit = na
if (shortCondition)
shortStopLoss := conversionLine
shortTakeProfit := close - 2 * (shortStopLoss - close)
var float stopLoss = na
var float takeProfit = na
if (longCondition)
stopLoss := conversionLine
takeProfit := close + 2 * (close - stopLoss)
// Exit Conditions
shortStopLossHit = close < shortStopLoss
shortProfitTargetHit = close <= shortTakeProfit
stopLossHit = close > stopLoss
profitTargetHit = close >= takeProfit
// Strategy Execution
if (shortCondition)
strategy.entry("Short", strategy.short)
if (shortStopLossHit or shortProfitTargetHit)
strategy.close("Short")
if (longCondition)
strategy.entry("Long", strategy.long)
if (stopLossHit or profitTargetHit)
strategy.close("Long")
// Plot Ichimoku Lines
plot(conversionLine, color=color.blue, title="Conversion Line")
plot(baseLine, color=color.red, title="Base Line")
plot(laggingSpan, offset=-25, color=color.green, title="Lagging Span")
p1 = plot(leadLine1, offset=displacement - 1, color=color.green, title="Leading Span A")
p2 = plot(leadLine2, offset=displacement - 1, color=color.red, title="Leading Span B")
fill(p1, p2, color=leadLine1 > leadLine2 ? color.new(color.green, 80) : color.new(color.red, 80))