本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=2
// Shows the Daily, Weekly, Monthly, Quarterly, and Yearly VWAP. Also
// shows the previous closing VWAP, which is usually very near the
// HLC3 standard pivot for the previous time frame. i.e. The previous
// daily VWAP closing price is usually near the current Daily Pivot.
study("Multi-Timeframe VWAP", overlay = true)
std1 = input(title = "STDEV 1", defval = 1, type = float, step = 0.1)
std2 = input(title = "STDEV 2", defval = 2, type = float, step = 0.1)
std3 = input(title = "STDEV 3", defval = 3, type = float, step = 0.1)
showStd = input(title = "Show STDEV Bands", defval = false, type = bool)
showPrev = input(title = "Show Previous VWAP", defval = false, type = bool)
bands = input(title = "STDEV Bands Timeframe (D, W, M, Q, Y)", defval = "D")
startD = security(tickerid, "D", time)
startW = security(tickerid, "W", time)
startM = security(tickerid, "M", time)
startQ = security(tickerid, "3M", time)
startY = security(tickerid, "12M", time)
newSessionD = iff(change(startD), 1, 0)
newSessionW = iff(change(startW), 1, 0)
newSessionM = iff(change(startM), 1, 0)
newSessionQ = iff(change(startQ), 1, 0)
newSessionY = iff(change(startY), 1, 0)
getVWAP(newSession) =>
p = iff(newSession, hlc3 * volume, p[1] + hlc3 * volume)
vol = iff(newSession, volume, vol[1] + volume)
v = p / vol
// Incremental weighted standard deviation (rolling)
// http://people.ds.cam.ac.uk/fanf2/hermes/doc/antiforgery/stats.pdf (part 5)
// x[i] = hlc3[i], w[i] = volume[i], u[i] - v[i]
Sn = iff(newSession, 0, Sn[1] + volume * (hlc3 - v[1]) * (hlc3 - v))
std = sqrt(Sn / vol)
[v, std]
[vD, stdevD] = getVWAP(newSessionD)
[vW, stdevW] = getVWAP(newSessionW)
[vM, stdevM] = getVWAP(newSessionM)
[vQ, stdevQ] = getVWAP(newSessionQ)
[vY, stdevY] = getVWAP(newSessionY)
vDplot = plot(vD, title = "VWAP - Daily", color = #00FF00DD, style = circles, transp = 10, linewidth = 1)
vWplot = plot(vW, title = "VWAP - Weekly", color = #40E200DD, style = circles, transp = 10, linewidth = 2)
vMplot = plot(vM, title = "VWAP - Monthly", color = #80C600DD, style = circles, transp = 10, linewidth = 3)
vQplot = plot(vQ, title = "VWAP - Quarterly", color = #BFA900DD, style = circles, transp = 10, linewidth = 4)
vYplot = plot(vY, title = "VWAP - Yearly", color = #FF8C00DD, style = circles, transp = 10, linewidth = 4)
v = iff(bands == "W", vW, iff(bands == "M", vM, iff(bands == "Q", vQ, iff(bands == "Y", vY, vD))))
s = iff(bands == "W", stdevW, iff(bands == "M", stdevM, iff(bands == "Q", stdevQ, iff(bands == "Y", stdevY, stdevD))))
vPlot = plot(v, title = "VWAP - Selected (transparent, not used)", color = #00000000)
s1up = plot(showStd ? v + std1 * s : na, title = "VWAP - STDEV 1", color = #22FFFFDD, style = line, linewidth = 1)
s1dn = plot(showStd ? v - std1 * s : na, title = "VWAP - STDEV 1", color = #22FFFFDD, style = line, linewidth = 1)
s2up = plot(showStd ? v + std2 * s : na, title = "VWAP - STDEV 2", color = #44FFFFDD, style = circles, linewidth = 1)
s2dn = plot(showStd ? v - std2 * s : na, title = "VWAP - STDEV 2", color = #44FFFFDD, style = circles, linewidth = 1)
s3up = plot(showStd ? v + std3 * s : na, title = "VWAP - STDEV 3", color = #66FFFFDD, style = circles, linewidth = 1)
s3dn = plot(showStd ? v - std3 * s : na, title = "VWAP - STDEV 3", color = #66FFFFDD, style = circles, linewidth = 1)
fill(vPlot, s1up, title = "VWAP - S1 Fill (Up)", color = #11111122)
fill(vPlot, s1dn, title = "VWAP - S1 Fill (Down)", color = #11111122)
fill(s1up, s2up, title = "S1 - S2 Fill (Up)", color = #33333322)
fill(s1dn, s2dn, title = "S1 - S2 Fill (Down)", color = #33333322)
fill(s2up, s3up, title = "S2 - S3 Fill (Up)", color = #55555522)
fill(s2dn, s3dn, title = "S2 - S3 Fill (Down)", color = #55555522)
pvD = valuewhen(newSessionD, vD[1], 0)
pvW = valuewhen(newSessionW, vW[1], 0)
pvM = valuewhen(newSessionM, vM[1], 0)
pvQ = valuewhen(newSessionQ, vQ[1], 0)
pvY = valuewhen(newSessionY, vY[1], 0)
plot(showPrev ? pvD : na, title = "Previous VWAP - Daily", color = #00FF0099, style = cross, transp = 10, linewidth = 1)
plot(showPrev ? pvW : na, title = "Previous VWAP - Weekly", color = #40E20099, style = cross, transp = 10, linewidth = 2)
plot(showPrev ? pvM : na, title = "Previous VWAP - Monthly", color = #80C60099, style = cross, transp = 10, linewidth = 3)
plot(showPrev ? pvQ : na, title = "Previous VWAP - Quarterly", color = #BFA90099, style = cross, transp = 10, linewidth = 4)
plot(showPrev ? pvY : na, title = "Previous VWAP - Yearly", color = #FF8C0099, style = cross, transp = 10, linewidth = 4)