VF-ST-EMA-CPR
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © deepphalke
//@version=5
indicator(title='VF-ST-EMA-CPR', overlay=true)

//supertend
factor = input.int(3, title="SuperTrend Factor")
atrP = input.int(10, title="SuperTrend atrPeriod")

[superTrend, direction] = ta.supertrend(factor, atrP)
sclr = direction > 0 ? color.red : color.green
plot(superTrend, title='SuperTrend', color=sclr)

//EMA

emaP = input.int(31, title="EMA length")
srcEma = input.source(high,"EMA source")
plot(ta.ema(srcEma,emaP))

//Volatality and Fibbonacci trade table

showTable=input(true, title="Show VF Table")
res = 'D'
test1=0
f_secureSecurity(_symbol, _res, _src) => request.security(_symbol, _res, _src, barmerge.gaps_off) // orignal code line
dc = f_secureSecurity(syminfo.tickerid, res, close[1+test1])
dc1 = f_secureSecurity(syminfo.tickerid, res, close[2+test1])
dc2 = f_secureSecurity(syminfo.tickerid, res, close[3+test1])
dc3 = f_secureSecurity(syminfo.tickerid, res, close[4+test1])
dc4 = f_secureSecurity(syminfo.tickerid, res, close[5+test1])
dc5 = f_secureSecurity(syminfo.tickerid, res, close[6+test1])
dc6 = f_secureSecurity(syminfo.tickerid, res, close[7+test1])
dc7 = f_secureSecurity(syminfo.tickerid, res, close[8+test1])
dc8 = f_secureSecurity(syminfo.tickerid, res, close[9+test1])
dc9 = f_secureSecurity(syminfo.tickerid, res, close[10+test1])
dc10 = f_secureSecurity(syminfo.tickerid, res, close[11+test1])
dop = f_secureSecurity(syminfo.tickerid, res, open)
logg00 =  math.log(dc/dc1)
logg11 = math.log(dc1/dc2)
logg22 = math.log(dc2/dc3)
logg33 = math.log(dc3/dc4)
logg44 = math.log(dc4/dc5)
logg55 = math.log(dc5/dc6)
logg66 = math.log(dc6/dc7)
logg77 = math.log(dc7/dc8)
logg88 = math.log(dc8/dc9)
logg99 = math.log(dc9/dc10)

squrr00 = logg00*logg00
squrr11 = logg11*logg11
squrr22 = logg22*logg22
squrr33 = logg33*logg33
squrr44 = logg44*logg44
squrr55 = logg55*logg55
squrr66 = logg66*logg66
squrr77 = logg77*logg77
squrr88 = logg88*logg88
squrr99 = logg99*logg99

avg_logg0 = (logg00+logg11+logg22+logg33+logg44+logg55+logg66+logg77+logg88+logg99)/10
avg_squrr0 = (squrr00+squrr11+squrr22+squrr33+squrr44+squrr55+squrr66+squrr77+squrr88+squrr99)/10

variancee0 = avg_squrr0 - (avg_logg0*avg_logg0)
volatility0 = math.sqrt(variancee0)

round_preci=input.int(title="VF Decimal Scale", options=[0,1,2,3], defval=0)
range11=math.round(dc*volatility0,round_preci)

doKdc= math.abs(dop-dc)>(0.382*range11)? dop : dc

//re-do calc

logg =  math.log(doKdc/dc1)
logg1 = math.log(dc1/dc2)
logg2 = math.log(dc2/dc3)
logg3 = math.log(dc3/dc4)
logg4 = math.log(dc4/dc5)
logg5 = math.log(dc5/dc6)
logg6 = math.log(dc6/dc7)
logg7 = math.log(dc7/dc8)
logg8 = math.log(dc8/dc9)
logg9 = math.log(dc9/dc10)

squrr = logg*logg
squrr1 = logg1*logg1
squrr2 = logg2*logg2
squrr3 = logg3*logg3
squrr4 = logg4*logg4
squrr5 = logg5*logg5
squrr6 = logg6*logg6
squrr7 = logg7*logg7
squrr8 = logg8*logg8
squrr9 = logg9*logg9

avg_logg = (logg+logg1+logg2+logg3+logg4+logg5+logg6+logg7+logg8+logg9)/10
avg_squrr = (squrr+squrr1+squrr2+squrr3+squrr4+squrr5+squrr6+squrr7+squrr8+squrr9)/10

variancee = avg_squrr - (avg_logg*avg_logg)
volatility = math.sqrt(variancee)


range1=math.round(doKdc*volatility,round_preci)


//doKdc=dc
buy_above=math.round(doKdc+(range1 * 0.236),round_preci)
buy_conf=math.round(doKdc+(range1 * 0.382),round_preci)
b_t1=math.round(doKdc+(range1 * 0.5),round_preci)
b_t2=math.round(doKdc+(range1 * 0.618),round_preci)
b_t3=math.round(doKdc+(range1 * 0.786),round_preci)
b_t4=math.round(doKdc+(range1 * 0.888),round_preci)
b_t5=math.round(doKdc+(range1 * 1.236),round_preci)
b_t6=math.round(doKdc+(range1 * 1.618),round_preci)

sell_below=math.round(doKdc-(range1 * 0.236),round_preci)
sell_conf=math.round(doKdc-(range1 * 0.382),round_preci)
s_t1=math.round(doKdc-(range1 * 0.5),round_preci)
s_t2=math.round(doKdc-(range1 * 0.618),round_preci)
s_t3=math.round(doKdc-(range1 * 0.786),round_preci)
s_t4=math.round(doKdc-(range1 * 0.888),round_preci)
s_t5=math.round(doKdc-(range1 * 1.236),round_preci)
s_t6=math.round(doKdc-(range1 * 1.618),round_preci)


sizeOption = input.string(title="VF Text Size", 
     options=["Normal", "Small"],
     defval="Small")
     
txtSize= (sizeOption == "Small") ? size.small :  size.normal

//BUY/Sell confirmation line
// Today's Session Start timestamp
y = year(timenow)
m = month(timenow)
d = dayofmonth(timenow)

// Start & End time for 
showLineOnSameDay=input(true, title="Show VF Lines on same Day")
dd=showLineOnSameDay?d:d+1
start = timestamp(y, m, dd, 09, 15)
end = start + 86400000

tom_start = start
tom_end = end
colorLabl=input.color(color.black,"VF Label Text color")

var lbl1 = label.new(na, na, "Buy Conf", color = color(na), style = label.style_label_left,size = txtSize, textcolor = colorLabl )
var lbl2 = label.new(na, na, "Sell Conf", color = color(na), style = label.style_label_left,size = txtSize, textcolor = colorLabl)
var lbl3 = label.new(na, na, "T1", color = color(na), style = label.style_label_left,size = txtSize, textcolor = colorLabl)
var lbl4 = label.new(na, na, "T2", color = color(na), style = label.style_label_left,size = txtSize, textcolor = colorLabl)
var lbl5 = label.new(na, na, "T3", color = color(na), style = label.style_label_left,size = txtSize, textcolor = colorLabl)
var lbl6 = label.new(na, na, "T4", color = color(na), style = label.style_label_left,size = txtSize, textcolor = colorLabl)
var lbl7 = label.new(na, na, "T5", color = color(na), style = label.style_label_left,size = txtSize, textcolor = colorLabl)
var lbl8 = label.new(na, na, "T6", color = color(na), style = label.style_label_left,size = txtSize, textcolor = colorLabl)
var lbl9 = label.new(na, na, "T1", color = color(na), style = label.style_label_left,size = txtSize, textcolor = colorLabl)
var lbl10 = label.new(na, na, "T2", color = color(na), style = label.style_label_left,size = txtSize, textcolor = colorLabl)
var lbl11 = label.new(na, na, "T3", color = color(na), style = label.style_label_left,size = txtSize, textcolor = colorLabl)
var lbl12 = label.new(na, na, "T4", color = color(na), style = label.style_label_left,size = txtSize, textcolor = colorLabl)
var lbl13 = label.new(na, na, "T5", color = color(na), style = label.style_label_left,size = txtSize, textcolor = colorLabl)
var lbl14 = label.new(na, na, "T6", color = color(na), style = label.style_label_left,size = txtSize, textcolor = colorLabl)
var lbl15 = label.new(na, na, "Buy Above", color = color(na), style = label.style_label_left,size = txtSize, textcolor = colorLabl)
var lbl16 = label.new(na, na, "Sell below", color = color(na), style = label.style_label_left,size = txtSize, textcolor = colorLabl)

colorBuy=input.color(color.green,"Buy Above Lines color")
colorSell=input.color(color.red,"Sell Below Lines color")

var lin1 = line.new(na, na, na, na, xloc = xloc.bar_time, style = line.style_solid,color=colorBuy, width = 2)
var lin2 = line.new(na, na, na, na, xloc = xloc.bar_time, style = line.style_solid,color=colorSell, width = 2)
var lin3 = line.new(na, na, na, na, xloc = xloc.bar_time, style = line.style_solid,color=colorBuy)
var lin4 = line.new(na, na, na, na, xloc = xloc.bar_time, style = line.style_solid,color=colorBuy)
var lin5 = line.new(na, na, na, na, xloc = xloc.bar_time, style = line.style_solid,color=colorBuy)
var lin6 = line.new(na, na, na, na, xloc = xloc.bar_time, style = line.style_solid,color=colorBuy)
var lin7 = line.new(na, na, na, na, xloc = xloc.bar_time, style = line.style_solid,color=colorBuy)
var lin8 = line.new(na, na, na, na, xloc = xloc.bar_time, style = line.style_solid,color=colorBuy)
var lin9 = line.new(na, na, na, na, xloc = xloc.bar_time, style = line.style_solid,color=colorSell)
var lin10 = line.new(na, na, na, na, xloc = xloc.bar_time, style = line.style_solid,color=colorSell)
var lin11 = line.new(na, na, na, na, xloc = xloc.bar_time, style = line.style_solid,color=colorSell)
var lin12 = line.new(na, na, na, na, xloc = xloc.bar_time, style = line.style_solid,color=colorSell)
var lin13 = line.new(na, na, na, na, xloc = xloc.bar_time, style = line.style_solid,color=colorSell)
var lin14 = line.new(na, na, na, na, xloc = xloc.bar_time, style = line.style_solid,color=colorSell)
var lin15 = line.new(na, na, na, na, xloc = xloc.bar_time, style = line.style_solid,color=colorBuy)
var lin16 = line.new(na, na, na, na, xloc = xloc.bar_time, style = line.style_solid,color=colorSell)

//Buy_Conf_lvl
line.set_xy1(lin1, tom_start, buy_conf)
line.set_xy2(lin1, tom_end,buy_conf)
label.set_xy(lbl1, bar_index, buy_conf)
//Sell_Conf_lvl
line.set_xy1(lin2, tom_start, sell_conf)
line.set_xy2(lin2, tom_end,sell_conf)	
label.set_xy(lbl2, bar_index, sell_conf)
//T1
line.set_xy1(lin3, tom_start, b_t1)
line.set_xy2(lin3, tom_end,b_t1)
label.set_xy(lbl3, bar_index, b_t1)
//T2
line.set_xy1(lin4, tom_start, b_t2)
line.set_xy2(lin4, tom_end,b_t2)
label.set_xy(lbl4, bar_index, b_t2)
//T3
line.set_xy1(lin5, tom_start, b_t3)
line.set_xy2(lin5, tom_end,b_t3)
label.set_xy(lbl5, bar_index, b_t3)
//T4
line.set_xy1(lin6, tom_start, b_t4)
line.set_xy2(lin6, tom_end,b_t4)
label.set_xy(lbl6, bar_index, b_t4)
//T5
line.set_xy1(lin7, tom_start, b_t5)
line.set_xy2(lin7, tom_end,b_t5)
label.set_xy(lbl7, bar_index, b_t5)
//T6
line.set_xy1(lin8, tom_start, b_t6)
line.set_xy2(lin8, tom_end,b_t6)
label.set_xy(lbl8, bar_index, b_t6)

//S1
line.set_xy1(lin9, tom_start, s_t1)
line.set_xy2(lin9, tom_end,s_t1)
label.set_xy(lbl9, bar_index, s_t1)
//S2
line.set_xy1(lin10, tom_start, s_t2)
line.set_xy2(lin10, tom_end,s_t2)
label.set_xy(lbl10, bar_index, s_t2)
//S3
line.set_xy1(lin11, tom_start, s_t3)
line.set_xy2(lin11, tom_end,s_t3)
label.set_xy(lbl11, bar_index, s_t3)
//S4
line.set_xy1(lin12, tom_start, s_t4)
line.set_xy2(lin12, tom_end,s_t4)
label.set_xy(lbl12, bar_index, s_t4)
//S5
line.set_xy1(lin13, tom_start, s_t5)
line.set_xy2(lin13, tom_end,s_t5)
label.set_xy(lbl13, bar_index, s_t5)
//S6
line.set_xy1(lin14, tom_start, s_t6)
line.set_xy2(lin14, tom_end,s_t6)
label.set_xy(lbl14, bar_index, s_t6)

//Buy Above
line.set_xy1(lin15, tom_start, buy_above)
line.set_xy2(lin15, tom_end,buy_above)
label.set_xy(lbl15, bar_index, buy_above)

//Sell Below
line.set_xy1(lin16, tom_start, sell_below)
line.set_xy2(lin16, tom_end,sell_below)
label.set_xy(lbl16, bar_index, sell_below)

color_g=#e6fce8
coor_r=#fce6ea
color_n=#bab8b8
color_rg=#ebe6ea
color_wh=color.white

txt_color_r=#f50a19
txt_color_g=#039458

plotVF = timeframe.period == 'D'  or timeframe.period == '120'or timeframe.period == '120' or timeframe.period == '60' or timeframe.period == '15' or timeframe.period == '30' 
     or timeframe.period == '5' or timeframe.period == '3' or timeframe.period == '180'or timeframe.period == '240'

trendingTimes="T4,T5,T6: On trending Times"
NormalTargets="T2,T3: Normal Targets"
tableToolTip="This table works well in a Trending Markets; in sideways market use it as Support and Resistance"
var testTable = table.new(position = position.bottom_right, columns = 9, rows = 3, bgcolor = color.yellow, border_width = 1, border_color=color.white)
if barstate.islast and plotVF and showTable
    table.cell(table_id = testTable, column = 1, row = 0, text = "^/v " ,text_size=txtSize,tooltip="Above/Below")
    table.cell(table_id = testTable, column = 2, row = 0, text = "Conf ", bgcolor=color.green,text_size=txtSize,tooltip="Confirmation")
    table.cell(table_id = testTable, column = 3, row = 0, text = "T1 ", bgcolor=color_wh,text_size=txtSize)
    table.cell(table_id = testTable, column = 4, row = 0, text = "T2 ", bgcolor=color.gray,text_size=txtSize,tooltip=NormalTargets)
    table.cell(table_id = testTable, column = 5, row = 0, text = "T3 ", bgcolor=color.gray,text_size=txtSize,tooltip=NormalTargets)
    table.cell(table_id = testTable, column = 6, row = 0, text = "T4 ", bgcolor=color.green,text_size=txtSize,tooltip=trendingTimes)
    table.cell(table_id = testTable, column = 7, row = 0, text = "T5 ", bgcolor=color.green,text_size=txtSize,tooltip=trendingTimes)
    table.cell(table_id = testTable, column = 8, row = 0, text = "T6 ", bgcolor=color.green,text_size=txtSize,tooltip=trendingTimes)
    
    table.cell(table_id = testTable, column = 0, row = 0, text = "VF Table", bgcolor=color.white, text_size=txtSize,tooltip=tableToolTip)
    table.cell(table_id = testTable, column = 0, row = 1, text = "For Buy Above ", bgcolor=color.green ,text_size=txtSize)
    table.cell(table_id = testTable, column = 0, row = 2, text = "For Sell Below ", bgcolor=color.orange ,text_size=txtSize)
    
    table.cell(table_id = testTable, column = 1, row = 1, text = str.tostring(buy_above), bgcolor=color_g ,text_size=txtSize, text_color=txt_color_r)
    table.cell(table_id = testTable, column = 2, row = 1, text = str.tostring(buy_conf), bgcolor=color_g,text_size=txtSize, text_color=txt_color_r)
    table.cell(table_id = testTable, column = 3, row = 1, text = str.tostring(b_t1), bgcolor=color_wh,text_size=txtSize, text_color=txt_color_r)
    table.cell(table_id = testTable, column = 4, row = 1, text = str.tostring(b_t2), bgcolor=color_n,text_size=txtSize, text_color=txt_color_r)
    table.cell(table_id = testTable, column = 5, row = 1, text = str.tostring(b_t3), bgcolor=color_n,text_size=txtSize, text_color=txt_color_r)
    table.cell(table_id = testTable, column = 6, row = 1, text = str.tostring(b_t4), bgcolor=color_g,text_size=txtSize, text_color=txt_color_r)
    table.cell(table_id = testTable, column = 7, row = 1, text = str.tostring(b_t5), bgcolor=color_g,text_size=txtSize, text_color=txt_color_r)
    table.cell(table_id = testTable, column = 8, row = 1, text = str.tostring(b_t6), bgcolor=color_g,text_size=txtSize, text_color=txt_color_r)
    
    table.cell(table_id = testTable, column = 1, row = 2, text = str.tostring(sell_below) , bgcolor=coor_r,text_size=txtSize,text_color=txt_color_g)
    table.cell(table_id = testTable, column = 2, row = 2, text = str.tostring(sell_conf), bgcolor=coor_r,text_size=txtSize,text_color=txt_color_g)
    table.cell(table_id = testTable, column = 3, row = 2, text = str.tostring(s_t1), bgcolor=color_wh,text_size=txtSize,text_color=txt_color_g)
    table.cell(table_id = testTable, column = 4, row = 2, text = str.tostring(s_t2), bgcolor=color_rg,text_size=txtSize,text_color=txt_color_g)
    table.cell(table_id = testTable, column = 5, row = 2, text = str.tostring(s_t3), bgcolor=color_rg,text_size=txtSize,text_color=txt_color_g)
    table.cell(table_id = testTable, column = 6, row = 2, text = str.tostring(s_t4), bgcolor=coor_r,text_size=txtSize,text_color=txt_color_g)
    table.cell(table_id = testTable, column = 7, row = 2, text = str.tostring(s_t5), bgcolor=coor_r,text_size=txtSize,text_color=txt_color_g)
    table.cell(table_id = testTable, column = 8, row = 2, text = str.tostring(s_t6), bgcolor=coor_r,text_size=txtSize,text_color=txt_color_g)
tf = timeframe.isintraday ? "D" : "W"
d_high = f_secureSecurity(syminfo.tickerid, tf, high[1+test1])
d_low = f_secureSecurity(syminfo.tickerid, tf, low[1+test1])
d_close =  f_secureSecurity(syminfo.tickerid, tf, close[1+test1])


pivot = (d_high + d_low + d_close)/3
BC = (d_high + d_low)/2
TC = (pivot - BC) + pivot
R1 = (pivot * 2) - d_low
R2 = pivot + (d_high-d_low)
S1 = (pivot * 2) - d_high
S2 = pivot - (d_high-d_low)

var testPTable = table.new(position = position.top_right, columns = 8, rows = 2, bgcolor = color.yellow, border_width = 1, border_color=color.white)
table.cell(table_id = testPTable, column = 0, row = 0, text = "S2 ",text_size=txtSize )
table.cell(table_id = testPTable, column = 1, row = 0, text = "S1 ",text_size=txtSize )
table.cell(table_id = testPTable, column = 2, row = 0, text = "Pivot" ,text_size=txtSize)
table.cell(table_id = testPTable, column = 3, row = 0, text = "R1 ",text_size=txtSize )
table.cell(table_id = testPTable, column = 4, row = 0, text = "R2",text_size=txtSize )
table.cell(table_id = testPTable, column = 5, row = 0, text = "BC",text_size=txtSize )
table.cell(table_id = testPTable, column = 6, row = 0, text = "TC",text_size=txtSize )
table.cell(table_id = testPTable, column = 7, row = 0, text = "Range",text_size=txtSize )
table.cell(table_id = testPTable, column = 0, row = 1, text = str.tostring(math.round(S2,round_preci)) ,bgcolor=color.green,text_size=txtSize)
table.cell(table_id = testPTable, column = 1, row = 1, text =  str.tostring(math.round(S1,round_preci)), bgcolor=color.green,text_size=txtSize )
table.cell(table_id = testPTable, column = 2, row = 1, text =  str.tostring(math.round(pivot,round_preci)) ,text_size=txtSize)
table.cell(table_id = testPTable, column = 3, row = 1, text =  str.tostring(math.round(R1,round_preci)) , bgcolor=color.orange,text_size=txtSize )
table.cell(table_id = testPTable, column = 4, row = 1, text =  str.tostring(math.round(R2,round_preci)) , bgcolor=color.orange,text_size=txtSize )
table.cell(table_id = testPTable, column = 5, row = 1, text =  str.tostring(math.round(BC,round_preci)) , bgcolor=color.green,text_size=txtSize )
table.cell(table_id = testPTable, column = 6, row = 1, text =  str.tostring(math.round(TC,round_preci)) , bgcolor=color.green,text_size=txtSize )
table.cell(table_id = testPTable, column = 7, row = 1, text =  str.tostring(math.round((TC-BC),round_preci+1)) , bgcolor=color.orange,text_size=txtSize )

