Ichimoku Support and Resistance 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 Support and Resistance", shorttitle="Ichimoku SR", overlay=true)

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

fromDay   = input.int(defval = 2,    title = "From Day")  
fromMonth = input.int(defval = 1,  title = "From Month")     
fromYear  = input.int(defval = 2019, title = "From Year")       
thruDay   = input.int(defval = 1,    title = "To Day")   
thruMonth = input.int(defval = 1,    title = "To Month")     
thruYear  = input.int(defval = 2112, title = "To Year")       
    

start     = timestamp(fromYear, fromMonth, fromDay, 00, 00)        // backtest start window
finish    = timestamp(thruYear, thruMonth, thruDay, 23, 59)        // backtest finish window
window()  => time >= start and time <= finish ? true : false       // create function "within window of time"

// 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_show = input.bool(true, title="Show Tenkan SR")
kijun_show = input.bool(true, title="Show Kijun SR")
senkou_a_show = input.bool(true, title="Show Senkou Span A SR")
senkou_b_show = input.bool(true, title="Show Senkou Span B SR")


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)

var sr = array.new_float(0)

array.push(sr, 0.0) // initialization


if tenkan_high == tenkan_high[1] and window() and not array.includes(sr, tenkan_high) and tenkan_show
    array.push(sr, tenkan_high)

if kijun_high == kijun_high[1] and window() and not array.includes(sr, kijun_high) and kijun_show
    array.push(sr, kijun_high)

if senkou_a_high == senkou_a_high[1] and window() and not array.includes(sr, senkou_a_high) and senkou_a_show
    array.push(sr, senkou_a_high)

if senkou_b_high == senkou_b_high[1] and window() and not array.includes(sr, senkou_b_high) and senkou_b_show
    array.push(sr, senkou_b_high)
    

if barstate.islast
    for i = 0 to array.size(sr)-1
        sr_lines = line.new(bar_index, array.get(sr, i), bar_index-1, array.get(sr, i), extend=extend.both)

