Ichimoku [2022] [v5] [keivanipchihagh]
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © keivanipchihagh

//@version=5
indicator(title = "Ichimoku", overlay = true, timeframe = "", timeframe_gaps = true)

// Inputs
TK_show = input.bool(true, title = '', inline = '1')                                                                        // Show Tenkan sen
TK_length = input.int(9, title = "Tenkan sen", minval = 1, inline = '1', tooltip = 'Also known as Conversion Line')         // Tenkan sen

KJ_show = input.bool(true, title = '', inline = '2')                                                                        // Show Kijun sen
KJ_length = input.int(26, title = "Kijun sen", minval = 1, inline = '2', tooltip = 'Also known as Base Line')               // Kijun sen

CH_show = input.bool(true, title = '', inline = '5')                                                                        // Show Chikou span
CH_length = input.int(26, title = "Chikou span", minval = 1, inline = '5', tooltip = 'Also known as Lagging Span')          // Chikou span

SA_show = input.bool(true, title = '', inline = '3')                                                                        // Show Sankou span A
SA_length = input.int(26, title = "Senkou span A", minval = 1, inline = '3', tooltip = 'Also known as Leading Span A')      // Sankou span A

SB_show = input.bool(true, title = '', inline = '4')                                                                        // Show Sankou span B
SB_length = input.int(52, title = "Senkou span B", minval = 1, inline = '4', tooltip = 'Also known as Leading Span B')      // Sankou span B

TK_KJ_cross_show = input.bool(true, title = 'Tenkan-Sen & Kijun-Sen crossovers')          // Tenkan-Sen & Kijun-Sen crossovers
_104_show = input.bool(false, title = '104 line (x4 times the timeframe)')                // Show 104 line
_208_show = input.bool(false, title = '208 line (x8 times the timeframe)')                // Show 208 line
QL_show = input.bool(false, title = 'Quality line')                                       // Quality line


// Calculations
TK = TK_show ? math.avg(ta.lowest(TK_length), ta.highest(TK_length)) : na       // Tenkan sen
KJ = KJ_show ? math.avg(ta.lowest(KJ_length), ta.highest(KJ_length)) : na       // Kijun sen
SB = SB_show ? math.avg(ta.lowest(SB_length), ta.highest(SB_length)) : na       // Sankou span B
SA = SA_show ? math.avg(TK, KJ) : na                                            // Sankou span A
CH = CH_show ? close : na                                                       // Chikou span
_104 = _104_show ? math.avg(ta.lowest(104), ta.highest(104)) : na               // 104 line
_208 = _208_show ? math.avg(ta.lowest(208), ta.highest(208)) : na               // 208 line
TK_KJ_cross_up = TK_KJ_cross_show ? ta.crossover(TK, KJ) : na                   // Tenkan-Sen & Kijun-Sen upwards crossover
TK_KJ_cross_down = TK_KJ_cross_show ? ta.crossover(KJ, TK) : na                 // Tenkan-Sen & Kijun-Sen downwards crossover


// Colors
TK_color = color.new(#B22833, 0)        // Tanken sen
KJ_color = color.new(#1848CC, 0)        // Kijun sen
CH_color = color.new(#056656, 0)        // Chikous span
SA_color = color.new(#4CAF50, 50)       // Sankou span A
SB_color = color.new(#FF0000, 40)       // Sankou span B
GK_color = color.new(#4CAF50, 85)       // Green Komu cloud
RK_color = color.new(#FF0000, 85)       // Red Kumo cloud
_104_color = color.new(#9C27B0, 20)     // 104 line
_108_color = color.new(#9C27B0, 30)     // 208 line
QL_color = color.new(#FF9800, 0)        // Quality line


// Plots
plot(TK, linewidth = 2, color = TK_color, title = "Tenkan sen")                                                 // Tenkan sen
plot(KJ, linewidth = 2, color = KJ_color, title = "Kijun sen")                                                  // Kijun sen
plot(CH, offset = -CH_length, linewidth = 1, color = CH_color, title = "Chikou span")                           // Chikou span
SenkouA = plot(SA, offset = CH_length,  linewidth = 1, color = SA_color, title = "Senkou span A")               // Senkou span A
SenkouB = plot(SB, offset = CH_length, linewidth = 1, color = SB_color, title = "Senkou span B")                // Senkou span B
fill(SenkouA, SenkouB, title = 'Kumo cloud', color = SA > SB ? GK_color : RK_color)                             // Kumo cloud
plot(_104, linewidth = 2, color = _104_color, title = "104 line")                                               // 104 line
plot(_208, linewidth = 4, color = _108_color, title = "208 line")                                               // 208 line
plot(QL_show ? KJ : na, linewidth = 1, offset = 26, color = QL_color, title = "Quality line")                   // Quality line
bgcolor(TK_KJ_cross_up ? GK_color : na, offset = -1, title = 'Upwards Tenkan sen & Kijun sen crossover')        // Tenkan-Sen & Kijun-Sen upwards crossover
bgcolor(TK_KJ_cross_down ? RK_color : na, offset = -1, title = 'Downwards Tenkan sen & Kijun sen crossover')    // Tenkan-Sen & Kijun-Sen downwards crossover