SUPRES
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © shakibsharifian

//@version=5
indicator("SUPRES")

roundDec(val,dec)=>
    ret=math.pow(10,-dec)*(int(val*math.pow(10,dec)))
    
gaussianMEAN(src,length)=>
    float sum=0
    for int _i=0 to length-1
        sum:=sum+src[_i]
    _mean=sum/length
    sum:=0
    for int _i=0 to length-1
        sum:=sum+(math.pow((_mean-src[_i]),2))
    _variance=sum/length
    _std=math.sqrt(_variance)

    sum:=0
    float coefSum=0    
    for int _i=0 to length-1
        _coef=(1/(_std*math.sqrt(2*3.1415)))*math.exp(-.5*math.pow(((src[_i]-_mean)/_std),2))
        sum:=sum+(_coef*src[_i])
        coefSum:=coefSum+_coef
    _gaussMean=sum/coefSum
    _gaussMean

bcwsma(s,l,m) => _bcwsma=0.00, _bcwsma:= (m*s+(l-m)*nz(_bcwsma[1]))/l, _bcwsma

myKDJ(param,fast,ma1,ma2) =>
    h = ta.highest(high, fast)
    l = ta.lowest(low,fast)
    RSV = 100*((param-l)/(h-l))
    pK = bcwsma(RSV, ma1, 1)
    pD = bcwsma(pK, ma2, 1)
    pJ = 3 * pK-2 * pD

    [pD,pJ]
    
//plot(close,color=color.white,linewidth=2)

txtOpt1="Auto"
txtOpt2="Tiny"
txtOpt3="Small"
txtOpt4="Normal"
txtOpt5="Large"
bk = input.int(30, minval=1, title='BACKWARD',group='SUPPORT/RESISTANCE (SUPRES)',inline='a')
memory = input.int(2, minval=1, title='MEMORY',group='SUPPORT/RESISTANCE (SUPRES)',inline='a')
LONG = input.int(9, minval=1, title='LENGTH', group='SUPER TREND', inline='k')
ma1 = input.int(3, minval=1, title='MA', group='SUPER TREND', inline='k')
colTP = input.color(color.purple,title='RISING',group='SUPER TREND',inline='a')
colBT = input.color(color.yellow,title='FALLING',group='SUPER TREND',inline='a')
src = input.source(close, title='SOURCE',group='T.O',inline='a')
dec = input.int(2, title='DECIMAL T.O',group='T.O',inline='a')
colTO = input.color(color.new(#ed37db,0),title='',group='T.O',inline='a')
string listSize = input.string('Auto',title='TEXT SIZE',options=[txtOpt1,txtOpt2,txtOpt3,txtOpt4,txtOpt5],group='LIST',inline='a')

lstSz = switch listSize
	txtOpt1 =>        size.auto
	txtOpt2 =>        size.tiny
	txtOpt3 =>        size.small
	txtOpt4 =>        size.normal
	txtOpt5 =>        size.large

Lkdj = input.int(21, minval=1, title='LENGTH', group='KDJ', inline='k')
ma1kdj = input.int(8, minval=1, title='MA1', group='KDJ', inline='k')
ma2kdj = input.int(8, minval=1, title='MA2', group='KDJ', inline='k')

[_pD,_pJ]=myKDJ(src,Lkdj,ma1kdj,ma2kdj)

red_volArr=array.new<float>()
green_volArr=array.new<float>()
red_PrcArr=array.new<float>()
green_PrcArr=array.new<float>()
for _i=0 to bk
    if close[_i]>open[_i]
        array.push(green_volArr,volume[_i])
        array.push(green_PrcArr,math.min(open[_i],close[_i]))
    else
        array.push(red_volArr,volume[_i])
        array.push(red_PrcArr,math.max(open[_i],close[_i]))

redInd=array.sort_indices(red_volArr,order.descending)
greenInd=array.sort_indices(green_volArr,order.descending)

sliceSup=array.new<float>()
sliceRes=array.new<float>()

var ag = array.new_line()
var ar = array.new_line()
if array.size(redInd)>memory and array.size(greenInd)>memory
    for _k=0 to memory-1
        array.push(ag, line.new(bar_index - 1, array.get(green_PrcArr,array.get(greenInd,_k)), bar_index, array.get(green_PrcArr,array.get(greenInd,_k)),extend=extend.right,color=color.aqua))
        array.push(ar, line.new(bar_index - 1, array.get(red_PrcArr,array.get(redInd,_k)), bar_index, array.get(red_PrcArr,array.get(redInd,_k)),extend=extend.right,color=color.red))
        array.push(sliceSup,array.get(green_PrcArr,array.get(greenInd,_k)))
        array.push(sliceRes,array.get(red_PrcArr,array.get(redInd,_k)))

if array.size(ar) > memory
	ln = array.shift(ar)
	line.delete(ln)
if array.size(ag) > memory
	ln = array.shift(ag)
	line.delete(ln)



//SUPER TREND	
[supertrend, direction] = ta.supertrend(ma1, LONG)
stCol=direction <0 ? colTP:colBT
stTxtCol=direction <0 ? color.white:color.black
plot(direction < 0 ? supertrend : na, "Up direction", color = stCol, style=plot.style_linebr,linewidth=3)
plot(direction > 0 ? supertrend : na, "Down direction", color = stCol, style=plot.style_linebr,linewidth=3)

stLbl=label.new(bar_index,supertrend,text=str.substring(str.tostring(supertrend),0,8),style=label.style_label_left,size=size.normal,color=stCol,textcolor=stTxtCol)
label.delete(stLbl[1])



//T.O Line
ausw=ta.rma(gaussianMEAN(gaussianMEAN(src,bk),bk),bk)
plot(ausw,color=colTO,linewidth=4,title='T.O Line')



//TABLE
array.sort(sliceSup,order.ascending)
array.sort(sliceRes,order.ascending)


prcArr=array.from(src,roundDec(ausw,dec),roundDec(supertrend,dec))
array.concat(prcArr,sliceSup)
array.concat(prcArr,sliceRes)
colArr=array.from(color.white,colTO,stCol)

for int ii=0 to array.size(sliceSup)-1
    array.push(colArr,color.aqua)
for int ii=0 to array.size(sliceRes)-1
    array.push(colArr,color.red)
    
indSrt=array.sort_indices(prcArr,order.descending)

var t = table.new(position.middle_right, 2,(memory*2)+5, color.olive)
table.cell(t,1, 0,'PRICE',bgcolor=color.new(color.black,0),text_color=color.yellow,text_size=lstSz)
table.cell(t,0, 0,'KDJ',bgcolor=0.97*_pJ>_pD?color.new(color.green,0):color.new(color.red,0),text_color=color.black,text_size=lstSz,width=0.5)

for int _l=0 to array.size(prcArr)-1
    table.cell(t,1, _l+1,  str.tostring(array.get(prcArr,array.get(indSrt,_l))),bgcolor=color.new(color.black,0),text_color=array.get(colArr,array.get(indSrt,_l)),text_size=lstSz)
    table.cell(t,0, _l+1,  '',bgcolor=array.get(colArr,array.get(indSrt,_l)),text_color=color.black,text_size=lstSz)

array.clear(ar)
array.clear(ag)
array.clear(sliceSup)
array.clear(sliceRes)