UNPKG

pricehistory

Version:

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

73 lines 2.98 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); Object.defineProperty(exports, "__esModule", { value: true }); const utilN = __importStar(require("@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); } } exports.default = setCandleVwap; //# sourceMappingURL=candle.setVwap.js.map