UNPKG

pricehistory

Version:

Transforms raw OHLCV series data into enriched candles with technical indicators, pattern recognition, and trend analysis.

38 lines 1.53 kB
import * as utilN from "@nameer/utils"; function setCandleVwap(opt, candle, ctx, period) { var _a, _b; if (opt.vwap !== true || candle.volume === undefined) return; const typicalPrice = utilN.math.mean(candle.priceHigh, candle.priceLow, candle.priceClose) ?? candle.priceClose; if (typicalPrice === undefined) return; if (period !== undefined) { const priceWinKey = `vwap${period}TypicalPrice`; const volWinKey = `vwap${period}Volume`; (_a = ctx.window)[priceWinKey] ?? (_a[priceWinKey] = []); (_b = ctx.window)[volWinKey] ?? (_b[volWinKey] = []); ctx.window[priceWinKey].push(typicalPrice); ctx.window[volWinKey].push(candle.volume); if (ctx.window[priceWinKey].length > period) { ctx.window[priceWinKey].shift(); } if (ctx.window[volWinKey].length > period) { ctx.window[volWinKey].shift(); } let vwapPV = 0; let vwapVolume = 0; for (let i = 0; i < ctx.window[priceWinKey].length; i++) { vwapPV += ctx.window[priceWinKey][i] * ctx.window[volWinKey][i]; vwapVolume += ctx.window[volWinKey][i]; } candle[`sma${period}Vwap`] = utilN.math.num(vwapPV / vwapVolume); } else { ctx.vwapPV += typicalPrice * candle.volume; ctx.vwapVolume += candle.volume; candle.vwap = utilN.math.num(ctx.vwapPV / ctx.vwapVolume); } } export default setCandleVwap; //# sourceMappingURL=candle.setVwap.js.map