RSI Supply / Demand Zones
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © GioLucaas

//@version=5
indicator("RSI Zones", overlay=true)

length = input(14, "Length")
overbought = input(70, "Overbought Threshold")
oversold = input(30, "Oversold Threshold")

var maxA = 0.0
var minA = 0.0
var maxB = 0.0
var minB = 0.0

_rsi = ta.rsi(close, length)

if (_rsi[1] >= overbought)
    if (_rsi[2] < overbought)
        maxA := high[1]
        minA := low[1]
    else
        if (high[1] > maxA[1])
            maxA := high[1]
        else 
            maxA := maxA[1]
        if (low[1] < minA)
            minA := low[1]
        else 
            minA := minA[1]
else
    maxA := maxA[1]
    minA := minA[1]
  
if (_rsi[1] <= oversold)
    if (_rsi[2] > oversold)
        maxB := high[1]
        minB := low[1]
    else 
        if (high[1] > maxB[1])
            maxB := high[1]
        else 
            maxB := maxB[1]
        if (low[1] < minB[1])
            minB := low[1]
        else
            minB := minB[1]
else
    maxB := maxB[1]
    minB := minB[1]

color1 = maxA != maxA[1] ? na : #87f163
color2 = minA != minA[1] ? na : #6fc950
color3 = maxB != maxB[1] ? na : #f16387
color4 = minB != minB[1] ? na : #c9506f

plot(maxA, title='Overbought Top', color=color1)
plot(minA, title='Overbought Bottom', color=color2, style=plot.style_cross)
plot(maxB, title='Oversold Top', color=color3, style=plot.style_cross)
plot(minB, title='Oversold Bottom', color=color4)