#// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
#// © xdecow
#study("Ichimoku [xdecow]", overlay=true)
# Converted by Sam4Cok@Samer800 - 02/2024
input ShowPanel = yes;#(true, 'Show panel')
input ColoredBars = no;#(false, 'Colored bars')
input KumoThicknessAvgLength = 610;#, 'Kumo Thickness AVG Length', minval=2)
input KumoThicknessAvgRatio = 0.5; #, 'Kumo Thickness AVG Ratio')
input preset = {default "Custom", "9/26/52/26 - 6D Markets (Default)", "8/22/44/22 - 5D Markets", "9/30/60/30 - 24h/7D Markets (Crypto)", "20/60/120/60 - 24h/7D Markets (Slow Version)"}; # "Preset"
input customTenkanSenLength = 9; #, minval=2, title="Tenkan-sen Length", group="Custom", tooltip="Conversion Line")
input customKijunSenLength = 26; #, minval=2, title="Kijun-sen Length", group="Custom", tooltip="Base Line")
input customChikouSpanLength = 52; #, minval=2, title="Chikou Span Length", group="Custom", tooltip="Lagging Span")
input customOffset = 26; #, minval=0, title="Offset", group="Custom")
input KijunSenCross = yes; # 'Kijun-sen Cross'
input TenkanSenCross = yes; # 'TK Cross',
input ChikouSpanCross = yes; # 'Chikou Span Cross',
input KumoBreakout = yes; # 'Kumo Breakout',
input KumoTwistCross = yes; # 'Kumo Twist'
def na = Double.NaN;
def bar = BarNumber();
#// Barcolor
DefineGlobalColor("lagging", CreateColor(129, 199, 132));
DefineGlobalColor("conversion", CreateColor(0, 188, 212));
DefineGlobalColor("base", CreateColor(255, 82, 82));
DefineGlobalColor("lead2", Color.GRAY);
DefineGlobalColor("lead2flat", CreateColor(255,183,77));
DefineGlobalColor("cons", CreateColor(208,119,0));
DefineGlobalColor("up1", CreateColor(56,142,60));
DefineGlobalColor("up2", CreateColor(102,187,106));
DefineGlobalColor("up3", CreateColor(165,214,167));
DefineGlobalColor("dn1", CreateColor(211,47,47));
DefineGlobalColor("dn2", CreateColor(239,83,80));
DefineGlobalColor("dn3", CreateColor(239,154,154));
#// Funcs
script addSignal {
input enabled = yes;
input signal = 0;
def addSignal = if enabled then signal else Double.NaN;
plot out = addSignal;
}
script donchian {
input len = 9;
def hh = Highest(high, len);
def ll = Lowest(low, len);
def donchian = (ll + hh) / 2;
plot out = donchian;
}
script getCloudTrend {
input l1 = close;
input l2 = close;
def getCloudTrend = if l1 > l2 then 1 else -1;
plot out = getCloudTrend;
}
script getPositionStrength {
input pos = 0;
input uptrend = yes;
def getPositionStrength = if uptrend then if pos == 1 then 1 else if pos == 0 then 2 else 3 else
if pos == -1 then -1 else if pos == 0 then -2 else -3;
plot out = getPositionStrength;
}
def i_conversion_len;
def i_base_len;
def i_lagging_len;
def i_offset;
switch (preset) {
case "9/26/52/26 - 6D Markets (Default)" :
i_conversion_len = 9;
i_base_len = 26;
i_lagging_len = 52;
i_offset = 26;
case "8/22/44/22 - 5D Markets" :
i_conversion_len = 5;
i_base_len = 22;
i_lagging_len = 44;
i_offset = 22;
case "9/30/60/30 - 24h/7D Markets (Crypto)" :
i_conversion_len = 10;
i_base_len = 30;
i_lagging_len = 60;
i_offset = 30;
case "20/60/120/60 - 24h/7D Markets (Slow Version)" :
i_conversion_len = 20;
i_base_len = 60;
i_lagging_len = 120;
i_offset = 60;
default :
i_conversion_len = customTenkanSenLength;
i_base_len = customKijunSenLength;
i_lagging_len = customChikouSpanLength;
i_offset = customOffset;
}
def n = i_offset - 1;
#// Calcs
def i_conversion = donchian(i_conversion_len);
def i_base = donchian(i_base_len);
def i_lead1 = (i_conversion + i_base) / 2;
def i_lead2 = donchian(i_lagging_len);
def i_cloud_top2 = Max(i_lead1, i_lead2);
def i_cloud_bot2 = Min(i_lead1, i_lead2);
#// kumo thickness
def sma = Average((i_cloud_top2 - i_cloud_bot2) * KumoThicknessAvgRatio, KumoThicknessAvgLength);
def i_cloud_thick_limit = i_cloud_bot2 + sma;
#// leading line at candle position
def i_lead1_current = i_lead1[n];
def i_lead2_current = i_lead2[n];
def i_cloud_top = Max(i_lead1_current, i_lead2_current);
def i_cloud_bot = Min(i_lead1_current, i_lead2_current);
#// position relative to kumo cloud
def s_base_position = if i_base > i_cloud_top then 1 else if i_base < i_cloud_bot then -1 else 0;
def s_base_breakout = if close > i_base then getPositionStrength(s_base_position, yes) else
getPositionStrength(s_base_position, no);
def Sig1 = addSignal(KijunSenCross, s_base_breakout);
#// cloud 2
def s_cloud2_trend = getCloudTrend(i_conversion, i_base);
def s_cloud2_top = Max(i_base, i_conversion);
def s_cloud2_bot = Min(i_base, i_conversion);
#// position relative to kumo cloud
def s_cloud2_position = if s_cloud2_bot > i_cloud_top then 1 else if s_cloud2_top < i_cloud_bot then -1 else 0;
def s_cloud2_cross = if s_cloud2_trend == 1 then getPositionStrength(s_cloud2_position, yes) else
getPositionStrength(s_cloud2_position, no);
def sig2 = addSignal(TenkanSenCross, s_cloud2_cross);
#// lagging span
#// cloud at lagging span
def s_lagging_lead1 = i_lead1_current[n];
def s_lagging_lead2 = i_lead2_current[n];
def s_lagging_cloud_top = Max(s_lagging_lead1, s_lagging_lead2);
def s_lagging_cloud_bot = Min(s_lagging_lead1, s_lagging_lead2);
#// closing value of current lagging span bar
def s_lagging_high = high[i_offset - 1];
def s_lagging_low = low[i_offset - 1];
def s_lagging_trend = if close > s_lagging_high then 1 else if close < s_lagging_low then -1 else 0;
#// s_lagging_position = close > s_lagging_cloud_top ? 1 :
def s_lagging_position = if close > i_cloud_top then 1 else if close < i_cloud_bot then -1 else 0;
def s_lagging_cross = if s_lagging_trend == 1 then getPositionStrength(s_lagging_position, yes) else
if s_lagging_trend == -1 then getPositionStrength(s_lagging_position, no) else 0;
def sig3 = addSignal(ChikouSpanCross, s_lagging_cross);
#// cloud breakout
def s_cloud_breakout = if close > i_cloud_top then 1 else if close < i_cloud_bot then -1 else 0;
def sig4 = addSignal(KumoBreakout, s_cloud_breakout);
#// leading cross
def s_cloud_trend = getCloudTrend(i_lead1, i_lead2);
def s_lead_cross = if s_cloud_trend == 1 then getPositionStrength(s_cloud_breakout, yes) else
getPositionStrength(s_cloud_breakout, no);
def sig5 = addSignal(KumoTwistCross, s_lead_cross);
def sigUp1 = if isNaN(sig1) then 0 else sig1;
def sigUp2 = if isNaN(sig2) then 0 else sig2;
def sigUp3 = if isNaN(sig3) then 0 else sig3;
def sigUp4 = if isNaN(sig4) then 0 else sig4;
def sigUp5 = if isNaN(sig5) then 0 else sig5;
def sigdn1 = if isNaN(sig1) then 5 else sig1;
def sigdn2 = if isNaN(sig2) then 5 else sig2;
def sigdn3 = if isNaN(sig3) then 5 else sig3;
def sigdn4 = if isNaN(sig4) then 5 else sig4;
def sigdn5 = if isNaN(sig5) then 5 else sig5;
#/ final signal
def s_signal_min = min(sigdn1, min(sigdn2, min(sigdn3, min(sigdn4, sigdn5))));
def s_signal_max = max(sigUp1, max(sigUp2, max(sigUp3, max(sigUp4, sigUp5))));
def s_signal = if s_signal_min > 0 then s_signal_max else
if s_signal_max < 0 then s_signal_min else 0;
#// Plots
#// Cloud
def i_cloud_color = if s_cloud_trend == 1 then
if isNaN(i_cloud_thick_limit) or i_cloud_top2 > i_cloud_thick_limit then 2 else 1 else
if isNaN(i_cloud_thick_limit) or i_cloud_top2 > i_cloud_thick_limit then -2 else -1;
def cloudClo = i_cloud_color[n];
def i_cloud_p1 = i_lead1[n]; # color=c_lead1, title="Senkou Span A"
plot i_cloud_p2 = i_lead2[n]; # color=i_lead2 == i_lead2[1] ? c_lead2_flat : c_lead2, linewidth=2, title='Senkou Span B')
plot i_cloud2_p1 = i_conversion; #, color=c_conversion, title="Tenkan-sen", linewidth=2)
plot i_cloud2_p2 = i_base; #, color=c_base, title="Kijun-sen", linewidth=2)
plot ChikouLine = close[-n]; #, offset=-i_offset + 1, color=c_lagging, title="Chikou Span", linewidth=2)
i_cloud_p2.SetLineWeight(2);
i_cloud2_p1.SetLineWeight(2);
ChikouLine.SetLineWeight(2);
i_cloud_p2.AssignValueColor(if i_cloud_p2 == i_cloud_p2[1] then GlobalColor("lead2flat") else GlobalColor("lead2"));
i_cloud2_p1.SetDefaultColor(GlobalColor("conversion"));
i_cloud2_p2.SetDefaultColor(GlobalColor("base"));
ChikouLine.SetDefaultColor(GlobalColor("lagging"));
AddCloud(if cloudClo==2 or cloudClo[-1]==2 then i_cloud_p1 else na, i_cloud_p2, Color.LIGHT_GREEN);
AddCloud(if cloudClo==1 or cloudClo[-1]==1 then i_cloud_p1 else na, i_cloud_p2, Color.DARK_GREEN);
AddCloud(if cloudClo==-2 or cloudClo[-1]==-2 then i_cloud_p2 else na, i_cloud_p1, Color.LIGHT_RED);
AddCloud(if cloudClo==-1 or cloudClo[-1]==-1 then i_cloud_p2 else na, i_cloud_p1, Color.DARK_RED);
#// signals
AddLabel(ShowPanel and KijunSenCross, "Kijun-sen Cross (" +
if s_base_breakout == 1 then "Strong)" else
if s_base_breakout == 2 then "Neutral)" else
if s_base_breakout == 3 then "Weak)" else
if s_base_breakout ==-1 then "Strong)" else
if s_base_breakout ==-2 then "Neutral)" else
if s_base_breakout ==-3 then "Weak)" else "Consolidation)",
if s_base_breakout == 1 then GlobalColor("up1") else
if s_base_breakout == 2 then GlobalColor("up2") else
if s_base_breakout == 3 then GlobalColor("up3") else
if s_base_breakout ==-1 then GlobalColor("dn1") else
if s_base_breakout ==-2 then GlobalColor("dn2") else
if s_base_breakout ==-3 then GlobalColor("dn3") else GlobalColor("cons"));
AddLabel(ShowPanel and TenkanSenCross, "TK Cross (" +
if s_cloud2_cross == 1 then "Strong)" else
if s_cloud2_cross == 2 then "Neutral)" else
if s_cloud2_cross == 3 then "Weak)" else
if s_cloud2_cross ==-1 then "Strong)" else
if s_cloud2_cross ==-2 then "Neutral)" else
if s_cloud2_cross ==-3 then "Weak)" else "Consolidation)",
if s_cloud2_cross == 1 then GlobalColor("up1") else
if s_cloud2_cross == 2 then GlobalColor("up2") else
if s_cloud2_cross == 3 then GlobalColor("up3") else
if s_cloud2_cross ==-1 then GlobalColor("dn1") else
if s_cloud2_cross ==-2 then GlobalColor("dn2") else
if s_cloud2_cross ==-3 then GlobalColor("dn3") else GlobalColor("cons"));
AddLabel(ShowPanel and ChikouSpanCross, "Chikou Span Cross (" +
if s_lagging_cross == 1 then "Strong)" else
if s_lagging_cross == 2 then "Neutral)" else
if s_lagging_cross == 3 then "Weak)" else
if s_lagging_cross ==-1 then "Strong)" else
if s_lagging_cross ==-2 then "Neutral)" else
if s_lagging_cross ==-3 then "Weak)" else "Consolidation)",
if s_lagging_cross == 1 then GlobalColor("up1") else
if s_lagging_cross == 2 then GlobalColor("up2") else
if s_lagging_cross == 3 then GlobalColor("up3") else
if s_lagging_cross ==-1 then GlobalColor("dn1") else
if s_lagging_cross ==-2 then GlobalColor("dn2") else
if s_lagging_cross ==-3 then GlobalColor("dn3") else GlobalColor("cons"));
AddLabel(ShowPanel and KumoBreakout, "Kumo Breakout (" +
if s_cloud_breakout == 1 then "Strong)" else
if s_cloud_breakout == 2 then "Neutral)" else
if s_cloud_breakout == 3 then "Weak)" else
if s_cloud_breakout ==-1 then "Strong)" else
if s_cloud_breakout ==-2 then "Neutral)" else
if s_cloud_breakout ==-3 then "Weak)" else "Consolidation)",
if s_cloud_breakout == 1 then GlobalColor("up1") else
if s_cloud_breakout == 2 then GlobalColor("up2") else
if s_cloud_breakout == 3 then GlobalColor("up3") else
if s_cloud_breakout ==-1 then GlobalColor("dn1") else
if s_cloud_breakout ==-2 then GlobalColor("dn2") else
if s_cloud_breakout ==-3 then GlobalColor("dn3") else GlobalColor("cons"));
AddLabel(ShowPanel and KumoTwistCross, "Kumo Twist (" +
if s_lead_cross == 1 then "Strong)" else
if s_lead_cross == 2 then "Neutral)" else
if s_lead_cross == 3 then "Weak)" else
if s_lead_cross ==-1 then "Strong)" else
if s_lead_cross ==-2 then "Neutral)" else
if s_lead_cross ==-3 then "Weak)" else "Consolidation)",
if s_lead_cross == 1 then GlobalColor("up1") else
if s_lead_cross == 2 then GlobalColor("up2") else
if s_lead_cross == 3 then GlobalColor("up3") else
if s_lead_cross ==-1 then GlobalColor("dn1") else
if s_lead_cross ==-2 then GlobalColor("dn2") else
if s_lead_cross ==-3 then GlobalColor("dn3") else GlobalColor("cons"));
AddLabel(ShowPanel, "Signal (" +
if s_signal == 1 then "Strong)" else
if s_signal == 2 then "Neutral)" else
if s_signal == 3 then "Weak)" else
if s_signal ==-1 then "Strong)" else
if s_signal ==-2 then "Neutral)" else
if s_signal ==-3 then "Weak)" else "Consolidation)",
if s_signal == 1 then GlobalColor("up1") else
if s_signal == 2 then GlobalColor("up2") else
if s_signal == 3 then GlobalColor("up3") else
if s_signal ==-1 then GlobalColor("dn1") else
if s_signal ==-2 then GlobalColor("dn2") else
if s_signal ==-3 then GlobalColor("dn3") else GlobalColor("cons"));
# trend > 0 ? c_up : trend < 0 ? c_down : c_consolidation
def bCol = if s_signal > 0 then 1 else
if s_signal < 0 then -1 else 0;
AssignPriceColor(if !ColoredBars then Color.CURRENT else
if close > open then
if bCol > 0 then Color.GREEN else
if bCol < 0 then Color.DARK_RED else Color.DARK_GRAY else
if bCol > 0 then Color.DARK_GREEN else
if bCol < 0 then Color.RED else Color.GRAY);
#-- END of CODE