Faytterro Oscillator
// 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("Faytterro Oscillator", overlay=false, max_lines_count=500, max_bars_back = 500)
src=input(hlc3,title="source")
len=input.int(50,title="lenght", maxval=500)
c = input.color(color.rgb(0, 217, 255))
w = input.int(1, minval = 1)
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) 
hline(50)
width= 1 //input.int(1,"width", minval=1)

//plot(cr, color= #cf0202, linewidth=width,offset=-len+1)
h1=hline(70)
h2=hline(30)
fill(h1,h2, color = color.rgb(203, 21, 235, 90))
mult = 2//input.float(2.0, minval=0.001, maxval=50, title="StdDev")
dev = mult * ta.stdev(src, len)
upper = cr + cr(dev, 2*len-1)
lower = cr - cr(dev, 2*len-1)
v = 100*(src[len]-lower)/(upper-lower)
fd(x) =>
    100-100/(math.pow(1+0.03,x-50)+1)
fdv = fd(v)
//plot(lower, color=  color.rgb(137, 255, 97), offset=1-len, linewidth=2)
//plot(upper, color=  color.rgb(255, 137, 137), offset=1-len, linewidth=2)
//plot(v, offset=-len, linewidth=width, color = color.rgb(0, 217, 255), editable = false)
plot(fdv, offset=-len, linewidth=w, color = c, editable = false, display = display.pane)
// extrapolation
diz = array.new_float(500)
var lin=array.new_line()
diz2 = array.new_float(500)
diz3 = array.new_float(500)
var lin2=array.new_line(0)
var lin3=array.new_line(0)
if barstate.islast
    for k=0 to len-1
        sum=0.0
        dv=0.0
        for i=0 to 2*len-2-k
            sum +=(len-math.abs(len-1-k-i))*src[i]/(len*len-k*(k+1)/2) 
            dv +=(len-math.abs(len-1-k-i))*dev[i]/(len*len-k*(k+1)/2)
        array.set(diz,k, sum + dv)
        array.set(diz2,k, sum - dv)
    for i=0 to (len-1)
        array.push(lin3, line.new(na, na, na, na))
        line.set_xy1(array.get(lin3,i), bar_index[len]+i, fd(100*(src[len-i]-array.get(diz2,math.max(i-1,0)))/(array.get(diz,math.max(i-1,0))-array.get(diz2,math.max(i-1,0)))))
        line.set_xy2(array.get(lin3,i), bar_index[len]+i+1, fd(100*(src[len-i-1]-array.get(diz2,i))/(array.get(diz,i)-array.get(diz2,i))))
        line.set_color(array.get(lin3,i), color.new(c,i*80/(len-1) ))
        line.set_width(array.get(lin3,i),w)

buy= fd(100*(src-array.get(diz2,len-1))/(array.get(diz,len-1)-array.get(diz2,len-1)))>30 and fd(100*(src[1]-array.get(diz2,len-2))/(array.get(diz,len-2)-array.get(diz2,len-2)))<30
sell= fd(100*(src-array.get(diz2,len-1))/(array.get(diz,len-1)-array.get(diz2,len-1)))<70 and fd(100*(src[1]-array.get(diz2,len-2))/(array.get(diz,len-2)-array.get(diz2,len-2)))>70
//plot(fd(100*(src-array.get(diz2,len-1))/(array.get(diz,len-1)-array.get(diz2,len-1))))
//plot(fd(100*(src[1]-array.get(diz2,len-2))/(array.get(diz,len-2)-array.get(diz2,len-2))))
alertcondition(buy, "oversold", "this is just an early warning. In the next bars, the indicator will confirm or reject this signal. please take this into account when assessing your risk.")
alertcondition(sell, "overbought", "this is just an early warning. In the next bars, the indicator will confirm or reject this signal. please take this into account when assessing your risk.")
//barcolor(color =  buy? color.green : sell? color.red : na)//fdv>70? color.rgb(255, 20, 20) : fdv<30? color.rgb(20, 255, 20) : na, offset = -len)
bgcolor(color =  buy? color.green : sell? color.red : na)
//plotshape(fdv>70, "sell", shape.triangledown, location.abovebar, color.red, -len, "", color.rgb(0,0,0,100), true, size = size.tiny)
//plotshape(fdv<30, "buy", shape.triangleup, location.belowbar, color.green, -len, "", color.rgb(0,0,0,100), true, size = size.tiny)
ttk= input.bool(false ,title = "close warning")
var testTable = table.new(position = position.middle_right, columns = 1, rows = 1, bgcolor = color.rgb(255, 235, 59, 100), border_width = 1)
if barstate.islast
    table.cell(table_id = testTable, column = 0, row = 0, text = "WARNİNG\n Transparent lines are repainted to a limited extent. \nThe lower the transparency, \nthe lower the chance of repainting. \nAfter the number of bars in the 'length' section have passed, \nthe repaint disappears completely. ", text_color = color.rgb(255,150,150,ttk? 1000 : 0))
plot(fd(100*(src-array.get(diz2,len-1))/(array.get(diz,len-1)-array.get(diz2,len-1))), color = color.new(c,100))