Chikou Filter for Ichimoku Cloud
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © MightyZinger

//@version=5
indicator(title = "Chikou Filter for Ichimoku Cloud", shorttitle = 'MZ Chikou Filter', overlay = true)

/////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////
/////                     Source Options                       //////
/////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////

// ─── Different Sources Options List ───► [
string SRC_Tv = 'Use traditional TradingView Sources '
string SRC_Wc = '(high + low + (2 * close)) / 4'
string SRC_Wo = 'close+high+low-2*open'
string SRC_Wi = '(close+high+low) / 3'
string SRC_Ex = 'close>open ? high : low'
string SRC_Hc = 'Heikin Ashi Close'
string src_grp = 'Source Parameters'

// ●───────── Inputs ─────────● {
diff_src = input.string(SRC_Wi, '→ Different Sources Options', options=[SRC_Tv, SRC_Wc, SRC_Wo, SRC_Wi, SRC_Ex, SRC_Hc], group=src_grp)
i_sourceSetup = input.source(close, 'Source Setup', group=src_grp)
uha_src = input.bool(true, title='Use Heikin Ashi Candles for Different Source Calculations', group=src_grp)
i_Symmetrical = input.bool(true, 'Apply Symmetrically Weighted Moving Average at the price source (May Result Repainting)', group=src_grp)

// Heikinashi Candles for calculations
h_close = uha_src ? ohlc4 : close
f_ha_open() =>
    haopen   = float(na)
    haopen := na(haopen[1]) ? (open + close) / 2 : (nz(haopen[1]) + nz(ohlc4[1])) / 2
    haopen
h_open  = uha_src ? f_ha_open() : open
h_high = high
h_low = low
// Get Source 
src_o = diff_src == SRC_Wc ? (h_high + h_low + 2 * h_close) / 4 :
     diff_src == SRC_Wo ? h_close + h_high + h_low - 2 * h_open :
     diff_src == SRC_Wi ? (h_close + h_high + h_low) / 3 : diff_src == SRC_Ex ? h_close > h_open ? h_high :
     h_low : diff_src == SRC_Hc ? ohlc4 :
     i_sourceSetup

src_f = i_Symmetrical ? ta.swma(src_o) : src_o  // Symmetrically Weighted Moving Average?

string grp1 = 'Chikou Parameters'
c_len = input.int(26, title='Chikou Period:', group=grp1)
p_lb = input.int(50, title='Chikou Filter Percentage Lookback:', group=grp1, maxval = 90)

/////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////

chikou_filter(src, len, perc, _high, _low, bull_col, bear_col, r_col)=>
    var _up = bool(na)
    var _dn = bool(na)
    var _re = bool(na)
    var color _clr = color.new(na, 0)
    var sig = int(na)
    
    lb_len = int(len*perc/100)      // Percentage Candels of total length to be considered for filter
    
    _up := src > ta.highest(_high, lb_len)[lb_len]
    _dn := src < ta.lowest(_low, lb_len)[lb_len]
    _re := src < ta.highest(_high, lb_len)[lb_len] and src > ta.lowest(_low, lb_len)[lb_len]
    
    _clr := _up ? bull_col : _dn ? bear_col : r_col
    
    sig := _up ? 1 : _dn ? -1 : 0     // provides Bullish and Bearish condition in Integer format that can be used as signals
    
    [_clr, sig]
/////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////


[c_col, _trend] = chikou_filter(src_f, c_len, p_lb, high, low, color.green, color.red, color.yellow) 

chikou_plot = plot(src_f, offset = -c_len+1 , color= c_col , title="Chikou-Span",  linewidth=4)

// Alerts

if _trend == 1 and not _trend[1] == 1
    alert('Buy : Chikou crossed resistance at ' + str.tostring(close), alert.freq_once_per_bar_close)
if _trend == -1 and not _trend[1] == -1
    alert('Sell : Chikou below support at ' + str.tostring(close), alert.freq_once_per_bar_close)

/////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////




