RSI Divergence
// 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("RSI Divergence", overlay = false, max_lines_count = 500)//, overlay=true)//, scale = scale.none)
rsilen=input.int(14, title="rsi length")
rsisrc=input(close, title="source")
x=ta.rsi(rsisrc,rsilen)
len=input.int(25, title="RSI Divergence length", maxval=500)
src=close
extrapolation=0
//zoom=input.int(0, title="zoom", maxval=27, minval=-27)
//hline(300-zoom*10, color=color.rgb(54, 58, 69, 100))
//hline(10, color=color.rgb(54, 58, 69, 100))
// for ax+b
xo=0.0
yo=0.0
xyo=0.0
xxo=0.0
for i=0 to len-1
    xo:= xo + i/(len)
    yo:= yo + x[len-1-i]/(len)
    xyo:= xyo + i*x[len-1-i]/(len)
    xxo:= xxo + i*i/(len)
dnm=ta.lowest(low,200)
dizi=array.new_float(len*2+1+extrapolation)
linedizi=array.new_line()
a=(xo*yo-xyo)/(xo*xo-xxo)
b=yo-a*xo
for i=0 to len-1+extrapolation
    array.set(dizi,i,a*i+b)

//// for src
// for ax+b
xo2=0.0
yo2=0.0
xyo2=0.0
xxo2=0.0
for i=0 to len-1
    xo2:= xo2 + i/(len)
    yo2:= yo2 + src[len-1-i]/(len)
    xyo2:= xyo2 + i*src[len-1-i]/(len)
    xxo2:= xxo2 + i*i/(len)

dizi2=array.new_float(len*2+1+extrapolation)
linedizi2=array.new_line()
a2=(xo2*yo2-xyo2)/(xo2*xo2-xxo2)
b2=yo2-a*xo2
for i=0 to len-1+extrapolation
    array.set(dizi2,i,a2*i+b2)
ttk=((array.get(dizi,0)<array.get(dizi,1)) and (array.get(dizi2,0)>array.get(dizi2,1)))? 1 : 
 ((array.get(dizi,0)>array.get(dizi,1)) and (array.get(dizi2,0)<array.get(dizi2,1)))? -1 : 0
cg=((array.get(dizi,0)<array.get(dizi,1)) and (array.get(dizi2,0)>array.get(dizi2,1)))// and ta.highest(ttk[1],len/2)<1)
cr=((array.get(dizi,0)>array.get(dizi,1)) and (array.get(dizi2,0)<array.get(dizi2,1)))// and ta.lowest(ttk[1],len/2)>-1)
bgcolor(color=(cg and ta.highest(ttk[1],len/2)<1)? color.rgb(76, 175, 79, 50) : 
 (cr and ta.lowest(ttk[1],len/2)>-1)? color.rgb(255, 82, 82, 50) : na, offset=0, display=display.none)
plot(x, color = color.rgb(200, 87, 194))

// for ax+b
xo3=0.0
yo3=0.0
xyo3=0.0
xxo3=0.0
for i=0 to len-1
    xo3:= xo3 + i/(len)
    yo3:= yo3 + x[len-1-i+(ta.barssince(cg))]/(len)
    xyo3:= xyo3 + i*x[len-1-i+(ta.barssince(cg))]/(len)
    xxo3:= xxo3 + i*i/(len)

dizi3=array.new_float(len*2+1+extrapolation)
linedizi3=array.new_line()
a3=(xo3*yo3-xyo3)/(xo3*xo3-xxo3)
b3=yo3-a3*xo3
for i=0 to len-1+extrapolation
    array.set(dizi3,i,a3*i+b3)

// for ax+b
xo4=0.0
yo4=0.0
xyo4=0.0
xxo4=0.0
for i=0 to len-1
    xo4:= xo4 + i/(len)
    yo4:= yo4 + x[len-1-i+(ta.barssince(cr))]/(len)
    xyo4:= xyo4 + i*x[len-1-i+(ta.barssince(cr))]/(len)
    xxo4:= xxo4 + i*i/(len)

dizi4=array.new_float(len*2+1+extrapolation)
linedizi4=array.new_line()
a4=(xo4*yo4-xyo4)/(xo4*xo4-xxo4)
b4=yo4-a4*xo4
for i=0 to len-1+extrapolation
    array.set(dizi4,i,a4*i+b4)
prp = input.int(0, "previous positive divergences", minval = 0)
prn = input.int(0, "previous negative divergences", minval = 0)
line=line.new((ta.valuewhen(cg, bar_index,prp)-len),
 ta.valuewhen(cg,array.get(dizi3,0),prp), 
 ta.valuewhen(cg, bar_index,prp),
 ta.valuewhen(cg,array.get(dizi3,len-1),prp), color=color.rgb(0,255,0), width=2)
line2=line.new((ta.valuewhen(cr, bar_index,prn)-len),
 ta.valuewhen(cr,array.get(dizi4,0),prn), 
 ta.valuewhen(cr, bar_index,prn),
 ta.valuewhen(cr,array.get(dizi4,len-1),prn), color=color.rgb(255, 0, 0, 0), width=2)
line.delete(line[1])
line.delete(line2[1])


alertcondition(cg, title = "RSI Buy")
alertcondition(cr, title = "RSI Sell")
hline(50, color = color.new(#787B86, 50))
rs=hline(30)
rss=hline(70)
fill(rs, rss, color=color.rgb(126, 87, 194, 90), title="RSI Background Fill")

