本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=6
indicator("RTH VWAP (Held Until Next Open)", shorttitle="RTH VWAP", overlay=true)
// Inputs
src = input.source(hlc3, "Price Source")
showBands = input.bool(false, "Show Std Dev Bands")
bandMult = input.float(1.0, "Band Multiplier", minval=0.1, step=0.1)
// Session logic (US Eastern, regular hours 9:30-16:00)
tod = hour(time, "America/New_York") * 60 + minute(time, "America/New_York")
isRegular = tod >= 570 and tod < 960
newSession = isRegular and not isRegular[1]
// Cumulative sums: reset at the open, accumulate in RTH, hold flat otherwise
var float cumVol = na
var float cumVP = na
var float cumVP2 = na
cumVol := newSession ? volume : isRegular ? nz(cumVol[1]) + volume : cumVol[1]
cumVP := newSession ? volume * src : isRegular ? nz(cumVP[1]) + volume * src : cumVP[1]
cumVP2 := newSession ? volume * src * src : isRegular ? nz(cumVP2[1]) + volume * src * src : cumVP2[1]
vwapCalc = cumVol > 0 ? cumVP / cumVol : na
variance = cumVol > 0 ? math.max(cumVP2 / cumVol - vwapCalc * vwapCalc, 0) : 0.0
sd = math.sqrt(variance)
// Only draw on intraday charts
okTF = timeframe.isintraday
// Regular session: live VWAP
plot(okTF and isRegular ? vwapCalc : na, "VWAP (RTH)", color=color.yellow, linewidth=2, style=plot.style_linebr)
// After hours + pre-market: dotted line at the closing VWAP until the next open
plot(okTF and not isRegular and cumVol > 0 ? vwapCalc : na, "VWAP (Closing, to Open)", color=color.new(color.yellow, 20), linewidth=2, style=plot.style_circles)
// Optional bands (regular session only)
upper = plot(okTF and showBands and isRegular ? vwapCalc + bandMult * sd : na, "Upper Band", color=color.gray, style=plot.style_linebr)
lower = plot(okTF and showBands and isRegular ? vwapCalc - bandMult * sd : na, "Lower Band", color=color.gray, style=plot.style_linebr)
fill(upper, lower, color=color.new(color.gray, 90))