The Perfect Support & Resistance
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Yidu_A_S_O_T_L_J_C

//@version=5
indicator("The Perfect Support & Resistance", "The Perfect S&R", overlay = true)

// Identify structure and lookback points to the left
// 10 levels max required
// assign each level one after another. -- rsi > 70 -> resistance and rsi < 30 -> support

rsiG = "RSI Settings"
rsiLengthInput = input.int(14, minval=1, title="RSI Length", group=rsiG)
rsiSourceInput = input.source(close, "Source", group=rsiG)
overbought = input.int(70, minval=50, maxval = 100, title="Overbought Threshold", group=rsiG)
oversold = input.int(30, minval=0, maxval = 50, title="Oversold Threshold", group=rsiG)

lbG = "Lookback"
lookback_0 = input.int(200, "Lookback zone 1", group = lbG)
lookback_1 = input.int(100, "Lookback zone 2", group = lbG)
lookback_2 = input.int(50, "Lookback zone 3", group = lbG)
lookback_3 = input.int(20, "Lookback zone 4", group = lbG)
lookback_4 = input.int(10, "Lookback zone 5", group = lbG)

tfG = "HTF"
htf = input.timeframe("5", "Timeframe1", group=tfG)
htf2 = input.timeframe("30", "Timeframe2", group=tfG)

up = ta.rma(math.max(ta.change(rsiSourceInput), 0), rsiLengthInput)
down = ta.rma(-math.min(ta.change(rsiSourceInput), 0), rsiLengthInput)
rsi = down == 0 ? 100 : up == 0 ? 0 : 100 - (100 / (1 + up / down))

var float level0High_tf = na
var float level0Low_tf = na
var float level1High_tf = na
var float level1Low_tf = na
var float level2High_tf = na
var float level2Low_tf = na
var float level3High_tf = na
var float level3Low_tf = na
var float level4High_tf = na
var float level4Low_tf = na

var float level0High = na
var float level0Low = na
var float level1High = na
var float level1Low = na
var float level2High = na
var float level2Low = na
var float level3High = na
var float level3Low = na
var float level4High = na
var float level4Low = na

swings(int len) =>
    var int os = 0
    var float itop = na
    var float ibtm = na

    upper = ta.highest(rsi, len)
    lower = ta.lowest(rsi, len)

    os := rsi[len] > upper and rsi[len] > overbought ? 0 : rsi[len] < lower and rsi[len] < oversold ? 1 : os[1]

    if (os == 0 and os[1] != 0)
        itop := high[len]

    if (os == 1 and os[1] != 1)
        ibtm := low[len]

    [itop, ibtm]

[level0_high, level0_low] = request.security(syminfo.tickerid, htf, swings(lookback_0))
[level1_high, level1_low] = request.security(syminfo.tickerid, htf, swings(lookback_1))
[level2_high, level2_low] = request.security(syminfo.tickerid, htf, swings(lookback_2))
[level3_high, level3_low] = request.security(syminfo.tickerid, htf, swings(lookback_3))
[level4_high, level4_low] = request.security(syminfo.tickerid, htf, swings(lookback_4))

// HTF
[level0_high_tf, level0_low_tf] = request.security(syminfo.tickerid, htf2, swings(lookback_0))
[level1_high_tf, level1_low_tf] = request.security(syminfo.tickerid, htf2, swings(lookback_1))
[level2_high_tf, level2_low_tf] = request.security(syminfo.tickerid, htf2, swings(lookback_2))
[level3_high_tf, level3_low_tf] = request.security(syminfo.tickerid, htf2, swings(lookback_3))
[level4_high_tf, level4_low_tf] = request.security(syminfo.tickerid, htf2, swings(lookback_4))


plot(level0_high, "Level 0 Resistance", close > level0_high ? color.lime : color.rgb(255, 166, 0), style = plot.style_circles)
plot(level1_high, "Level 1 Resistance", close > level1_high ? color.lime : color.rgb(255, 166, 0), style = plot.style_circles)
plot(level2_high, "Level 2 Resistance", close > level2_high ? color.lime : color.rgb(255, 166, 0), style = plot.style_circles)
plot(level3_high, "Level 3 Resistance", close > level3_high ? color.lime : color.rgb(255, 166, 0), style = plot.style_circles)
plot(level4_high, "Level 4 Resistance", close > level4_high ? color.lime : color.rgb(255, 166, 0), style = plot.style_circles)

plot(level0_high_tf, "Level 0 Resistance TF", color.white, style = plot.style_circles)
plot(level1_high_tf, "Level 1 Resistance TF", color.white, style = plot.style_circles)
plot(level2_high_tf, "Level 2 Resistance TF", color.white, style = plot.style_circles)
plot(level3_high_tf, "Level 3 Resistance TF", color.white, style = plot.style_circles)
plot(level4_high_tf, "Level 4 Resistance TF", color.white, style = plot.style_circles)

plot(level0_low, "Level 0 Support", close > level0_low ? color.lime : color.rgb(255, 166, 0), style = plot.style_circles)
plot(level1_low, "Level 1 Support", close > level1_low ? color.lime : color.rgb(255, 166, 0), style = plot.style_circles)
plot(level2_low, "Level 2 Support", close > level2_low ? color.lime : color.rgb(255, 166, 0), style = plot.style_circles)
plot(level3_low, "Level 3 Support", close > level3_low ? color.lime : color.rgb(255, 166, 0), style = plot.style_circles)
plot(level4_low, "Level 4 Support", close > level4_low ? color.lime : color.rgb(255, 166, 0), style = plot.style_circles)

plot(level0_low_tf, "Level 0 Support TF", color.white, style = plot.style_circles)
plot(level1_low_tf, "Level 1 Support TF", color.white, style = plot.style_circles)
plot(level2_low_tf, "Level 2 Support TF", color.white, style = plot.style_circles)
plot(level3_low_tf, "Level 3 Support TF", color.white, style = plot.style_circles)
plot(level4_low_tf, "Level 4 Support TF", color.white, style = plot.style_circles)