You are on page 1of 4

//////////////////////////////////

//// vwap Stdev


///////////////////////////////////

// ----------------------------------------
groupVWAP = "Volume Weighted Average Price"
// ----------------------------------------

computeVWAP(src, isNewPeriod) =>


var float sumSrcVol = na
var float sumVol = na
var float sumSrcSrcVol = na

sumSrcVol := isNewPeriod ? src * volume : src * volume + sumSrcVol[1]


sumVol := isNewPeriod ? volume : volume + sumVol[1]
// sumSrcSrcVol calculates the dividend of the equation that is later used to
calculate the standard deviation
sumSrcSrcVol := isNewPeriod ? volume * math.pow(src, 2) : volume *
math.pow(src, 2) + sumSrcSrcVol[1]

_vwap = sumSrcVol / sumVol


variance = sumSrcSrcVol / sumVol - math.pow(_vwap, 2)
variance := variance < 0 ? 0 : variance
stDev = math.sqrt(variance)

[_vwap, stDev]

// ----------------------------------------

compute_stDEV(_vwap, stDev, stDevMultiplier_1, stDevMultiplier_2,


stDevMultiplier_3, stDevMultiplier_4, stDevMultiplier_5, stDevMultiplier_6) =>
upperBand_6 = _vwap + stDev * stDevMultiplier_6
upperBand_5 = _vwap + stDev * stDevMultiplier_5
upperBand_4 = _vwap + stDev * stDevMultiplier_4
upperBand_3 = _vwap + stDev * stDevMultiplier_3
upperBand_2 = _vwap + stDev * stDevMultiplier_2
upperBand_1 = _vwap + stDev * stDevMultiplier_1
lowerBand_1 = _vwap - stDev * stDevMultiplier_1
lowerBand_2 = _vwap - stDev * stDevMultiplier_2
lowerBand_3 = _vwap - stDev * stDevMultiplier_3
lowerBand_4 = _vwap - stDev * stDevMultiplier_4
lowerBand_5 = _vwap - stDev * stDevMultiplier_5
lowerBand_6 = _vwap - stDev * stDevMultiplier_6

[upperBand_6, upperBand_5, upperBand_4, upperBand_3, upperBand_2, upperBand_1,


lowerBand_1, lowerBand_2, lowerBand_3, lowerBand_4, lowerBand_5, lowerBand_6]

// ----------------------------------------

f_drawLabel(_x, _y, _text, _textcolor, _style, _size) =>


var _label = label.new(
x = _x,
y = _y,
text = _text,
textcolor = _textcolor,
style = _style,
size = _size,
xloc = xloc.bar_time
)
label.set_xy(_label, _x, _y)

// ----------------------------------------

src = input(hlc3, title="VWAP Source", inline="V0", group=groupVWAP)

vD_color = input.color(color.new(#E12D7B, 0), title="" , inline="V1",


group=groupVWAP)
vW_color = input.color(color.new(#F67B52, 0), title="" , inline="V2",
group=groupVWAP)

plot_vD = input(true , title="Show Daily VWAP" , inline="V1",


group=groupVWAP)
plot_vW = input(true , title="Show Weekly VWAP" , inline="V2",
group=groupVWAP)

// ----------------------------------
groupSTD = "Standard Deviation Bands"
// ----------------------------------

Std_selection = input.string("Week", title="Timeframe", options = ["Day", "Week"],


inline="STD0", group=groupSTD)

showStd = input(true, title="Show VWAP Bands |", inline="STD1", group=groupSTD)

showL = input(true, title="Lines", inline="STD1", group=groupSTD)

showLC = showL ? display.all : display.none

stDevMultiplier_1 = input.float(0.382 , step=0.1, title="StDev 1:",


inline="StDev1", group=groupSTD)
stDevMultiplier_2 = input.float(0.618 , step=0.1, title="StDev 2:",
inline="StDev2", group=groupSTD)
stDevMultiplier_3 = input.float(1 , step=0.1, title="StDev 3:", inline="StDev3",
group=groupSTD)
stDevMultiplier_4 = input.float(1.618 , step=0.1, title="StDev 4:",
inline="StDev4", group=groupSTD)
stDevMultiplier_5 = input.float(1.786 , step=0.1, title="StDev 5:",
inline="StDev5", group=groupSTD)
stDevMultiplier_6 = input.float(2.0 , step=0.1, title="StDev 6:",
inline="StDev6", group=groupSTD)

// --------------
groupL = "Labels"
// --------------

show_labels = input(true, title="Show Labels |", inline="L1", group=groupL)


show_VWAPlabels = input(true, title="VWAP", inline="L1", group=groupL)
show_STDlabels = input(true, title="StDev", inline="L1", group=groupL)

off_mult = input(2, title="| Offset", inline="L2", group=groupL)


var DEFAULT_LABEL_SIZE = size.normal
var DEFAULT_LABEL_STYLE = label.style_none
ll_offset = timenow + math.round(ta.change(time) * off_mult)

// --------------

timeChange(period) =>
ta.change(time(period))

newSessionD = timeChange("D")
newSessionW = timeChange("W")

[vD, stdevD] = computeVWAP(src, newSessionD)


[vW, stdevW] = computeVWAP(src, newSessionW)

// --------------

Vstyle = input(false, title="Circles Lines", inline="V0", group=groupVWAP)


VstyleC = Vstyle ? plot.style_circles : plot.style_line

vDplot = plot(plot_vD ? vD : na, title="VWAP - Daily", color=vD_color,


style=VstyleC, linewidth=1)
f_drawLabel(ll_offset, show_labels and show_VWAPlabels and plot_vD ? vD : na, "vD",
color.white, DEFAULT_LABEL_STYLE, DEFAULT_LABEL_SIZE)

vWplot = plot(plot_vW ? vW : na, title="VWAP - Weekly", color=vW_color,


style=VstyleC, linewidth=3)
f_drawLabel(ll_offset, show_labels and show_VWAPlabels and plot_vW ? vW : na, "vW",
color.silver, DEFAULT_LABEL_STYLE, DEFAULT_LABEL_SIZE)

// --------------

stdev_sel = Std_selection == "Day" ? stdevD :


Std_selection == "Week" ? stdevW : na

vwap_sel = Std_selection == "Day" ? vD :


Std_selection == "Week" ? vW : na

prev_period = Std_selection == "Day" ? newSessionD :


Std_selection == "Week" ? newSessionW : na

[s6up, s5up, s4up, s3up, s2up, s1up, s1dn, s2dn, s3dn, s4dn, s5dn, s6dn] =
compute_stDEV(vwap_sel, stdev_sel, stDevMultiplier_1, stDevMultiplier_2,
stDevMultiplier_3, stDevMultiplier_4, stDevMultiplier_5, stDevMultiplier_6)

// --------------

A = plot(showStd ? s6up : na, title="VWAP - STDEV +6", color=color.new(#e45b5b, 0),


style=plot.style_linebr, linewidth=3, display=showLC)
f_drawLabel(ll_offset, show_labels and show_STDlabels and showStd ? s6up : na ,
"SD+2", color.silver, DEFAULT_LABEL_STYLE, DEFAULT_LABEL_SIZE)

B = plot(showStd ? s5up : na, title="VWAP - STDEV +5", color=color.rgb(190, 25,


25), style=plot.style_linebr, linewidth=3, display=showLC)
f_drawLabel(ll_offset, show_labels and show_STDlabels and showStd ? s5up : na ,
"SD+1.786", color.silver, DEFAULT_LABEL_STYLE, DEFAULT_LABEL_SIZE)

C = plot(showStd ? s4up : na, title="VWAP - STDEV +4", color=color.new(#fc0202, 0),


style=plot.style_linebr, linewidth=3, display=showLC)
f_drawLabel(ll_offset, show_labels and show_STDlabels and showStd ? s4up : na ,
"SD+1.618", color.silver, DEFAULT_LABEL_STYLE, DEFAULT_LABEL_SIZE)

D = plot(showStd ? s3up : na, title="VWAP - STDEV +3", color=color.new(#8de790, 0),


style=plot.style_linebr, linewidth=3, display=showLC)
f_drawLabel(ll_offset, show_labels and show_STDlabels and showStd ? s3up : na ,
"SD+1", color.silver, DEFAULT_LABEL_STYLE, DEFAULT_LABEL_SIZE)

E = plot(showStd ? s2up : na, title="VWAP - STDEV +2", color=color.new(#02f50b, 0),


style=plot.style_linebr, linewidth=3, display=showLC)
f_drawLabel(ll_offset, show_labels and show_STDlabels and showStd ? s2up : na ,
"SD+61.8", color.silver, DEFAULT_LABEL_STYLE, DEFAULT_LABEL_SIZE)

F = plot(showStd ? s1up : na, title="VWAP - STDEV +1", color=color.new(#3b943e, 0),


style=plot.style_linebr, linewidth=3, display=showLC)
f_drawLabel(ll_offset, show_labels and show_STDlabels and showStd ? s1up : na ,
"SD+38.2", color.silver, DEFAULT_LABEL_STYLE, DEFAULT_LABEL_SIZE)

G = plot(showStd ? s1dn : na, title="VWAP - STDEV -1", color=color.new(#3b943e, 0),


style=plot.style_linebr, linewidth=3, display=showLC)
f_drawLabel(ll_offset, show_labels and show_STDlabels and showStd ? s1dn : na ,
"SD-38.2", color.silver, DEFAULT_LABEL_STYLE, DEFAULT_LABEL_SIZE)

H = plot(showStd ? s2dn : na, title="VWAP - STDEV -2", color=color.new(#02fb0a, 0),


style=plot.style_linebr, linewidth=3, display=showLC)
f_drawLabel(ll_offset, show_labels and show_STDlabels and showStd ? s2dn : na ,
"SD-61.8", color.silver, DEFAULT_LABEL_STYLE, DEFAULT_LABEL_SIZE)

I = plot(showStd ? s3dn : na, title="VWAP - STDEV -3", color=color.new(#74e878, 0),


style=plot.style_linebr, linewidth=3, display=showLC)
f_drawLabel(ll_offset, show_labels and show_STDlabels and showStd ? s3dn : na ,
"SD-1", color.silver, DEFAULT_LABEL_STYLE, DEFAULT_LABEL_SIZE)

J = plot(showStd ? s4dn : na, title="VWAP - STDEV -4", color=color.new(#fc0303, 0),


style=plot.style_linebr, linewidth=3, display=showLC)
f_drawLabel(ll_offset, show_labels and show_STDlabels and showStd ? s4dn : na ,
"SD-1.618", color.silver, DEFAULT_LABEL_STYLE, DEFAULT_LABEL_SIZE)

K = plot(showStd ? s5dn : na, title="VWAP - STDEV -5", color=color.rgb(190, 25,


25), style=plot.style_linebr, linewidth=3, display=showLC)
f_drawLabel(ll_offset, show_labels and show_STDlabels and showStd ? s5dn : na ,
"SD-1.786", color.silver, DEFAULT_LABEL_STYLE, DEFAULT_LABEL_SIZE)

L = plot(showStd ? s6dn : na, title="VWAP - STDEV -6", color=color.new(#f37878, 0),


style=plot.style_linebr, linewidth=3, display=showLC)
f_drawLabel(ll_offset, show_labels and show_STDlabels and showStd ? s6dn : na ,
"SD-2", color.silver, DEFAULT_LABEL_STYLE, DEFAULT_LABEL_SIZE)

You might also like