greatwolf

ICHIMOKU LAG LINE STRATEGY

Strategy Test using Ichimoku Cloud and lag line.
Remove from Favorite Scripts Add to Favorite Scripts
//@version=2
strategy(title = "Chikou Cloud Crossover", initial_capital = 200000, overlay = false)

takelong   = input(title = "Take Long Positions",  type = bool, defval = true)
takeshort  = input(title = "Take Short Positions", type = bool, defval = true)
waitcandle = input(title = "Enter on opposite candle", type = bool, defval = false)
usehtf     = input(title = "Check Higher Timeframe Kumo", type = bool, defval = false)
useFF      = input(title = "Use Fixed Fractional Size", defval = false, type = bool)
riskEQ     = input(title = "Equity Risk%", defval = 0.5, minval = 0, maxval = 100, type = float)
startyear  = input(title = "Start Year",  defval = 2000, minval = 1970, type = float)
startmonth = input(title = "Start Month", defval = 1,    minval = 1, maxval = 12, type = float)
startday   = input(title = "Start Day",   defval = 1,    minval = 1, maxval = 30, type = float)


// Plot equity curve
PLCurve = (strategy.initial_capital + strategy.netprofit) / strategy.initial_capital * 100
plot(PLCurve > 100 ? na : PLCurve, title = "-Equity Curve", style = areabr, linewidth = 2, color = #EA9999)
plot(PLCurve < 100 ? na : PLCurve, title = "+Equity Curve", style = areabr, linewidth = 2, color = lime)
hline(100, linestyle = dashed, linewidth = 1, color = silver)


// Ichimoku Components
conversionPeriods   = 9
basePeriods         = 26
kumoSpan2Periods    = 52
displacement        = 26

donchian(len) => avg(lowest(len), highest(len))
conversionLine = donchian(conversionPeriods)
baseLine       = donchian(basePeriods)
spanA          = offset(avg(conversionLine, baseLine), displacement)
spanB          = offset(donchian(kumoSpan2Periods), displacement)
lagLine(A, B) =>
    threshold = 2
    upper = offset(max(A, B), displacement)
    lower = offset(min(A, B), displacement)
    sum(close < lower, threshold) == threshold ? -1
   : sum(upper < close, threshold) == threshold ? 1
   : 0

htfconversionLine = donchian(conversionPeriods * 4)
htfbaseLine       = donchian(basePeriods * 4)
htfspanA          = offset(avg(htfconversionLine, htfbaseLine), displacement * 4)
htfspanB          = offset(donchian(kumoSpan2Periods * 4), displacement * 4)


// Trade entry/exit signals
upperSpan = max(spanA, spanB)
lowerSpan = min(spanA, spanB)
longStop  = min(baseLine, lowest(low, displacement * 4))
shortStop = max(baseLine, highest(high, displacement * 4))
bullish =  1
bearish = -1
trade_signal() =>
    (lagLine(spanA, spanB) == bullish and lagLine(conversionLine, baseLine) == bullish and conversionLine > baseLine and low > upperSpan and (usehtf ? close > htfspanB : true)) ? bullish
   : (lagLine(spanA, spanB) == bearish and lagLine(conversionLine, baseLine) == bearish and conversionLine < baseLine and high < lowerSpan and (usehtf ? close < htfspanB : true)) ? bearish
   : 0
open_signal(sig) => trade_signal() == sig
close_signal(sig) =>
    (sig == bullish and lagLine(spanA, spanB) == bearish) ? true
  : (sig == bearish and lagLine(spanA, spanB) == bullish)


// Trade execution
compute_position(risk, entry, stop) =>
    pricestop = max(entry, stop) - min(entry, stop)
    pos_size = risk / (pricestop * 1.5)
    nz(pos_size)
bar_filter() =>
    startingpoint = year > startyear or (year == startyear and (month > startmonth or (month == startmonth and dayofmonth >= startday)))

if (close_signal(bullish) and (takeshort ? not open_signal(bearish) : true))
    strategy.cancel("IchiLE")
    strategy.close("IchiLE")
if (close_signal(bearish) and (takelong ? not open_signal(bullish) : true))
    strategy.cancel("IchiSE")
    strategy.close("IchiSE")

riskamount = riskEQ  / 100 * (strategy.initial_capital + (useFF ? strategy.netprofit : 0))
strategy.entry("IchiLE", strategy.long,  compute_position(riskamount, highest(9), longStop), when = takelong and bar_filter() and open_signal(bullish))
strategy.entry("IchiSE", strategy.short, compute_position(riskamount, lowest(9), shortStop), when = takeshort and bar_filter() and open_signal(bearish))
greatwolf
4 months ago
This script implements the typical way of trading off Ichimoku Clouds. The rules are quite simple:

If the lag line(Chikou Span) is above the cloud take a long position and close any short positions. If lag line is below the cloud open a short position and close any longs etc.

As extra confirmation, the script also checks that the price action is also on appropriate side of the cloud and the trigger line and base line is oriented correctly, eg. trigger line above base line for longs etc.

This script does correct position sizing based on account equity using your standard fix fractional sizing formula you can find around the internet.
+2 Reply
sjcwealth
2 months ago
Does it work for 1 minute time frame?
Reply
greatwolf sjcwealth
a month ago
Likely not. This is mainly because Ichimoku is a trend following system and like any trend following strategy it tends to be weak in consolidating markets or when price action tends to whip-saw around. So I find it doesn't work as well at lower timeframes.
Reply
sjcwealth greatwolf
a month ago
I found that too.
Reply
Ideas Scripts Chart
United States
United Kingdom
India
España
Italia
Brasil
Россия
Türkiye
日本
한국
Home Stock Screener Economic Calendar How It Works Chart Features House Rules Moderators For the WEB Widgets Stock Charting Library Priority Support Feature Request Blog & News FAQ Help & Wiki Twitter
Private Messages Chat Ideas Published Followers Following Priority Support Public Profile Profile Settings Billing Sign Out