Paste this indicator into Pine Editor and add it to a standard one-minute chart. Select a loaded bar opening as Window start and choose a window within a continuous trading period. The default is four minutes; the end is exclusive. Both calculations use close and update only when the chart bar is confirmed. The script plots cumulative values within the window, not a rolling average or an exchange-session VWAP.
Matched close samples with strict minute coverage and explicit volume validityPine Script v6
//@version=6
indicator("VWAP vs TWAP: matched window", overlay = true)
int startTime = input.time(timestamp("01 Oct 2026 13:30 +0000"), "Window start", confirm = true)
int windowMinutes = input.int(4, "Window minutes", minval = 1, maxval = 1440)
if not chart.is_standard or not timeframe.isminutes or timeframe.multiplier != 1
runtime.error("Use a standard one-minute chart.")
int endTime = startTime + windowMinutes * 60000
bool inWindow = time >= startTime and time < endTime
var float priceSum = 0.0
var float priceVolumeSum = 0.0
var float volumeSum = 0.0
var int sampleCount = 0
var bool priceCoverageValid = true
var bool volumeCoverageValid = true
if inWindow and barstate.isconfirmed
bool fullMinute = time_close - time == 60000
bool expectedOpen = time == startTime + sampleCount * 60000
priceCoverageValid := priceCoverageValid and fullMinute and expectedOpen and not na(close)
volumeCoverageValid := volumeCoverageValid and not na(volume) and volume >= 0
sampleCount += 1
if not na(close)
priceSum += close
if not na(volume) and volume >= 0
priceVolumeSum += close * volume
volumeSum += volume
bool visible = inWindow and barstate.isconfirmed
float twapProxy = priceCoverageValid and sampleCount > 0 ? priceSum / sampleCount : na
float vwapProxy = priceCoverageValid and volumeCoverageValid and volumeSum > 0 ? priceVolumeSum / volumeSum : na
plot(visible ? twapProxy : na, "TWAP close-sample proxy", color.blue, 2, plot.style_linebr)
plot(visible ? vwapProxy : na, "VWAP close-volume proxy", color.orange, 2, plot.style_linebr)
plot(visible ? sampleCount : na, "Observed minutes", display = display.data_window)
plot(visible ? volumeSum : na, "Known volume sum", display = display.data_window)
plot(visible ? (priceCoverageValid ? 1 : 0) : na, "Price coverage valid: 1=yes", display = display.data_window)
plot(visible ? (volumeCoverageValid ? 1 : 0) : na, "Volume coverage valid: 1=yes", display = display.data_window)
plot(visible ? (priceCoverageValid and sampleCount == windowMinutes ? 1 : 0) : na, "Full window complete: 1=yes", display = display.data_window)