JavaScript · Indicator Code
VWAP - Volume-Weighted Average Price in JavaScript
The running average price weighted by traded volume, accumulated from the session start.
Verified. Python and JavaScript implementations agree to
0.00e+00 on a 60-bar reference OHLCV series (Python vs JavaScript, comparable positions).JavaScript
/**
* Session VWAP.
*
* Running cumulative (typicalPrice * volume) / cumulative volume,
* accumulated from the first bar. Typical price = (high + low + close) / 3.
*
* Bars with a missing high/low/close or zero/falsy volume do not advance
* the accumulator; they carry forward the prior VWAP (or null until any
* volume has accumulated).
*
* @param {Array<{high:number,low:number,close:number,volume:number}>} candles
* @returns {Array<number|null>} VWAP per bar (null before any volume accumulates)
*/
export function vwap(candles) {
const len = candles.length;
const result = new Array(len).fill(null);
if (!len) return result;
let cumTPV = 0; // cumulative typical-price * volume
let cumVol = 0; // cumulative volume
for (let i = 0; i < len; i++) {
const { high, low, close, volume } = candles[i];
// Skip incomplete bars: carry forward the running VWAP.
if (high == null || low == null || close == null || !volume) {
result[i] = cumVol > 0 ? cumTPV / cumVol : null;
continue;
}
const tp = (high + low + close) / 3; // typical price
cumTPV += tp * volume;
cumVol += volume;
result[i] = cumVol > 0 ? cumTPV / cumVol : null;
}
return result;
}
Other platforms: Python
← VWAP - Volume-Weighted Average Price (all platforms) · All indicators · Glossary concept
← VWAP - Volume-Weighted Average Price (all platforms) · All indicators · Glossary concept
Improve Your Craft Every Morning
Daily commentary from Dr. Ken Long — what he's seeing in markets, how he's framing trades, and what's worth practicing today. Free.
Your email:
Tue–Fri mornings. Unsubscribe anytime. No spam, no hype.