Ichimoku Analysis Tool by TheSocialCryptoClub
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © TheSocialCryptoClub

//@version=5
indicator("Ichimoku Analysis Tool", overlay=true)

// functions
donchian(len) => math.avg(ta.lowest(len), ta.highest(len))


// input declarations
tenkan_len = input.int(9, title="Tenkan Length")
kijun_len = input.int(26, title="Kijun Length")
senkou_b_len = input.int(52, title="Seknou Span B Lenght")

higher_timeframe = input.timeframe("W", "Higher Timeframe")


tenkan = donchian(tenkan_len)
kijun = donchian(kijun_len)
senkou_a = math.avg(tenkan, kijun)
senkou_b = donchian(senkou_b_len)
chikou = close

tenkan_high = request.security(syminfo.tickerid,higher_timeframe,donchian(tenkan_len))
kijun_high = request.security(syminfo.tickerid,higher_timeframe,donchian(kijun_len))
senkou_a_high = request.security(syminfo.tickerid,higher_timeframe,math.avg(tenkan_high, kijun_high))
senkou_b_high = request.security(syminfo.tickerid,higher_timeframe, donchian(senkou_b_len))
chikou_high = request.security(syminfo.tickerid,higher_timeframe,close)


plot(tenkan, color=color.blue, title="Tenkan Current Time Frame", linewidth=1)
plot(tenkan_high, color=color.aqua, title="Tenkan Highest Time Frame", linewidth=2)

plot(kijun, color=color.maroon, title="Kijun Current Time Frame", linewidth=1)
plot(kijun_high, color=color.red, title="Kijun Highest Time Frame", linewidth=2)

// senkou_a_high_plot = plot(senkou_a_high, offset = kijun_len - 1, color=#A5D6A7, title="Seknou Span A")
// senkou_b_high_plot = plot(senkou_b_high, offset = kijun_len - 1, color=#EF9A9A, title="Senkou Span B")

plot(chikou, offset=-kijun_len+1, color=#43A047, title="Chikou")

senkou_a_plot = plot(senkou_a, offset = kijun_len - 1, color=#A5D6A7, title="Seknou Span A")
senkou_b_plot = plot(senkou_b, offset = kijun_len - 1, color=#EF9A9A, title="Senkou Span B")

fill(senkou_a_plot, senkou_b_plot, color = senkou_a > senkou_b ? color.rgb(67, 160, 71, 90) : color.rgb(244, 67, 54, 90))


table summary_table = table.new(position.top_right, columns=2, rows=28,bgcolor=#0F0F0F,border_width=1,border_color=#000000)

table_bgcolor = #0F0F0F
table_bgcolor_content = #1A2421
table_text_color = #D3D3D3
table_text_color_content = #FFFFFF

table.cell(summary_table,0,0,"Symbol",  text_color=table_text_color, bgcolor=table_bgcolor)
table.cell(summary_table,1,0,syminfo.tickerid,  text_color=table_text_color, bgcolor=table_bgcolor_content)

table.cell(summary_table,0,1,"Timeframe",  text_color=#D3D3D3, bgcolor=table_bgcolor)
table.cell(summary_table,1,1,timeframe.period,  text_color=#D3D3D3, bgcolor=table_bgcolor_content)

kumo_direction_text = senkou_a > senkou_b ? "Uptrend" : "Downtrend"
kumo_direction_color = senkou_a > senkou_b ? color.lime : color.red
table.cell(summary_table,0,2,"Kumo Direction",  text_color=table_text_color, bgcolor=table_bgcolor)
table.cell(summary_table,1,2, kumo_direction_text,  text_color=table_text_color_content, bgcolor=kumo_direction_color)

kumo_twist_bars = ta.barssince(ta.cross(senkou_a, senkou_b))
table.cell(summary_table,0,3,"Kumo Twist Bars",  text_color=table_text_color, bgcolor=table_bgcolor)
table.cell(summary_table,1,3, str.tostring(kumo_twist_bars),  text_color=table_text_color_content, bgcolor=table_bgcolor_content)

kumo_with = math.abs(senkou_a - senkou_b) / close * 100
table.cell(summary_table,0,4,"Kumo Width",  text_color=table_text_color, bgcolor=table_bgcolor)
table.cell(summary_table,1,4, str.tostring(math.round(kumo_with,2)) + "%",  text_color=table_text_color_content, bgcolor=table_bgcolor_content)

senkou_b_direction = senkou_b > senkou_b[1] ? "Uptrend" : (senkou_b < senkou_b[1] ? "Downtrend" : "Flat")
senkou_b_color = senkou_b > senkou_b[1] ? color.lime : (senkou_b < senkou_b[1] ? color.red : color.gray)
table.cell(summary_table,0,5,"Senkou Span B",  text_color=table_text_color, bgcolor=table_bgcolor)
table.cell(summary_table,1,5, senkou_b_direction,  text_color=table_text_color_content, bgcolor=senkou_b_color)

senkou_a_direction = senkou_a > senkou_a[1] ? "Uptrend" : (senkou_a < senkou_a[1] ? "Downtrend" : "Flat")
senkou_a_color = senkou_a > senkou_a[1] ? color.lime : (senkou_a < senkou_a[1] ? color.red : color.gray)
table.cell(summary_table,0,6,"Senkou Span A",  text_color=table_text_color, bgcolor=table_bgcolor)
table.cell(summary_table,1,6, senkou_a_direction,  text_color=table_text_color_content, bgcolor=senkou_a_color)

price_vs_kumo = (close > senkou_b[tenkan_len-1]) and (close > senkou_a[tenkan_len-1]) ? "Uptrend" : ((close < senkou_b[tenkan_len-1]) and (close < senkou_a[tenkan_len-1]) ? "Downtrend" : "Flat")
price_vs_kumo_color = (close > senkou_b[tenkan_len-1]) and (close > senkou_a[tenkan_len-1]) ? color.lime : ((close < senkou_b[tenkan_len-1]) and (close < senkou_a[tenkan_len-1]) ? color.red : color.gray)
table.cell(summary_table,0,7,"Price vs Kumo",  text_color=table_text_color, bgcolor=table_bgcolor)
table.cell(summary_table,1,7, price_vs_kumo,  text_color=table_text_color_content, bgcolor=price_vs_kumo_color)

price_vs_future_kumo = (close > senkou_b) and (close > senkou_a) ? "Uptrend" : ((close < senkou_b) and (close < senkou_a) ? "Downtrend" : "Flat")
price_vs_future_kumo_color = (close > senkou_b) and (close > senkou_a) ? color.lime : ((close < senkou_b) and (close < senkou_a) ? color.red : color.gray)
table.cell(summary_table,0,8,"Price vs Future Kumo",  text_color=table_text_color, bgcolor=table_bgcolor)
table.cell(summary_table,1,8, price_vs_future_kumo,  text_color=table_text_color_content, bgcolor=price_vs_future_kumo_color)

price_vs_kijun = (close > kijun) ? "Uptrend" : ((close < kijun) ? "Downtrend" : "Flat")
price_vs_kijun_color = (close > kijun) ? color.lime : ((close < kijun) ? color.red : color.gray)
table.cell(summary_table,0,9,"Price vs Kijun",  text_color=table_text_color, bgcolor=table_bgcolor)
table.cell(summary_table,1,9, price_vs_kijun,  text_color=table_text_color_content, bgcolor=price_vs_kijun_color)

price_vs_chikou = (close > close[kijun_len]) ? "Uptrend" : ((close < close[kijun_len]) ? "Downtrend" : "Flat")
price_vs_chikou_color = (close > close[kijun_len]) ? color.lime : ((close < close[kijun_len]) ? color.red : color.gray)
table.cell(summary_table,0,10,"Price vs Chikou",  text_color=table_text_color, bgcolor=table_bgcolor)
table.cell(summary_table,1,10, price_vs_chikou,  text_color=table_text_color_content, bgcolor=price_vs_chikou_color)

kijun_direction  = (kijun > kijun[1]) ? "Uptrend" : ((kijun < kijun[1]) ? "Downtrend" : "Flat")
kijun_direction_color = (kijun > kijun[1]) ? color.lime : ((kijun < kijun[1]) ? color.red : color.gray)
table.cell(summary_table,0,11,"Kijun Direction",  text_color=table_text_color, bgcolor=table_bgcolor)
table.cell(summary_table,1,11, kijun_direction,  text_color=table_text_color_content, bgcolor=kijun_direction_color)

kijun_vs_kumo = (kijun > senkou_b[kijun_len-1]) and (kijun > senkou_a[kijun_len-1]) ? "Uptrend" : ((kijun < senkou_b[kijun_len-1]) and (kijun < senkou_a[kijun_len-1]) ? "Downtrend" : "Flat")
kijun_vs_kumo_color = (kijun > senkou_b[kijun_len-1]) and (kijun > senkou_a[kijun_len-1]) ? color.lime : ((kijun < senkou_b[kijun_len-1]) and (kijun < senkou_a[kijun_len-1]) ? color.red : color.gray)
table.cell(summary_table,0,12,"Kijun vs Kumo",  text_color=table_text_color, bgcolor=table_bgcolor)
table.cell(summary_table,1,12, kijun_vs_kumo,  text_color=table_text_color_content, bgcolor=kijun_vs_kumo_color)

tenkan_kijun_cross = ta.barssince(ta.cross(tenkan, kijun))
table.cell(summary_table,0,13,"Tenkan/Kijun Cross Bars",  text_color=table_text_color, bgcolor=table_bgcolor)
table.cell(summary_table,1,13, str.tostring(tenkan_kijun_cross),  text_color=table_text_color_content, bgcolor=table_bgcolor_content)

price_vs_tenkan = (close > tenkan) ? "Uptrend" : ((close < tenkan) ? "Downtrend" : "Flat")
price_vs_tenkan_color = (close > tenkan) ? color.lime : ((close < tenkan) ? color.red : color.gray)
table.cell(summary_table,0,14,"Price vs Tenkan",  text_color=table_text_color, bgcolor=table_bgcolor)
table.cell(summary_table,1,14, price_vs_tenkan,  text_color=table_text_color_content, bgcolor=price_vs_tenkan_color)

tenkan_direction  = (tenkan > tenkan[1]) ? "Uptrend" : ((tenkan < tenkan[1]) ? "Downtrend" : "Flat")
tenkan_direction_color = (tenkan > tenkan[1]) ? color.lime : ((tenkan < tenkan[1]) ? color.red : color.gray)

table.cell(summary_table,0,15,"Tenkan Direction",  text_color=table_text_color, bgcolor=table_bgcolor)
table.cell(summary_table,1,15, tenkan_direction,  text_color=table_text_color_content, bgcolor=tenkan_direction_color)

tenkan_vs_kumo = (tenkan > senkou_b[kijun_len-1]) and (tenkan > senkou_a[kijun_len-1]) ? "Uptrend" : ((tenkan < senkou_b[kijun_len-1]) and (tenkan < senkou_a[kijun_len-1]) ? "Downtrend" : "Flat")
tenkan_vs_kumo_color = (tenkan > senkou_b[kijun_len-1]) and (tenkan > senkou_a[kijun_len-1]) ? color.green : ((tenkan < senkou_b[kijun_len-1]) and (tenkan < senkou_a[kijun_len-1]) ? color.red : color.gray)
table.cell(summary_table,0,16,"Tenkan vs Kumo",  text_color=table_text_color, bgcolor=table_bgcolor)
table.cell(summary_table,1,16, tenkan_vs_kumo,  text_color=table_text_color_content, bgcolor=tenkan_vs_kumo_color)

tenkan_vs_future_kumo = (tenkan > senkou_b) and (tenkan > senkou_a) ? "Uptrend" : ((tenkan < senkou_b) and (tenkan < senkou_a) ? "Downtrend" : "Flat")
tenkan_vs_future_kumo_color = (tenkan > senkou_b) and (tenkan > senkou_a) ? color.lime : ((tenkan < senkou_b) and (tenkan < senkou_a) ? color.red : color.gray)
table.cell(summary_table,0,17,"Tenkan vs Future Kumo",  text_color=table_text_color, bgcolor=table_bgcolor)
table.cell(summary_table,1,17, tenkan_vs_future_kumo,  text_color=table_text_color_content, bgcolor=tenkan_vs_future_kumo_color)


tenkan_vs_kijun = tenkan > kijun ? "Uptrend" : "Downtrend"
tenkan_vs_kijun_color = tenkan > kijun ? color.lime : color.red
table.cell(summary_table,0,18,"Tenkan vs Kijun",  text_color=table_text_color, bgcolor=table_bgcolor)
table.cell(summary_table,1,18, tenkan_vs_kijun,  text_color=table_text_color_content, bgcolor=tenkan_vs_kijun_color)
