Trend Friendly RSI
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © faytterro

//@version=5
indicator("Trend Friendly RSI", timeframe="", timeframe_gaps=true)
src=input(hlc3,title="source")
len=input.int(25,title="lenght", maxval=500)
cr(x, y) =>
    z = 0.0
    weight = 0.0
    for i = 0 to y-1
        z:=z + x[i]*((y-1)/2+1-math.abs(i-(y-1)/2))
    z/(((y+1)/2)*(y+1)/2)
cr= cr(src,2*len-1) 
width=input.int(3, title="linewidth", minval=1)

dizi = array.new_float(500)
var line=array.new_line()
//if barstate.islast
for i=0 to len*2
    array.set(dizi,i,(i*(i-1)*(cr-2*cr[1]+cr[2])/2+i*(cr[1]-cr[2])+cr[2]))
x=close-array.get(dizi,len)
y=x>0? math.sqrt(100*x/ta.highest(x,200)) : -math.sqrt(100*x/ta.lowest(x,200))
y:=y*.5+nz(y[1])*.5
plot(5*y+50, color=cr>cr[1]? #00fc08 : #fc0303, linewidth=2)
hline(50)
top=hline(70)
bot=hline(30)
fill(top,bot, color=color.blue, transp=90)

buy=ta.crossover(ta.change(5*y+50),0) and 5*y+50<30 and cr>cr[1]
sell=ta.crossunder(ta.change(5*y+50),0) and 5*y+50>70 and cr<cr[1]
alertcondition(buy or sell)
bgcolor(color= buy? #00fc08 : sell? #fc0303 : na, transp=50)
barcolor(color= buy? #00fc08 : sell? #fc0303 : na)