UNPKG

pricehistory

Version:

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

88 lines 3.62 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 setCandleRsi(opt, candle, ctx) { var _a, _b; if (opt.rsi === false || candle.priceClose === undefined || ctx.prevClose === undefined) { return; } const period = opt.rsi; const change = candle.priceClose - ctx.prevClose; const gain = change > 0 ? change : 0; const loss = change < 0 ? -change : 0; const gainWinKey = `rsi${period}Gain`; const lossWinKey = `rsi${period}Loss`; (_a = ctx.window)[gainWinKey] ?? (_a[gainWinKey] = []); (_b = ctx.window)[lossWinKey] ?? (_b[lossWinKey] = []); ctx.window[gainWinKey].push(gain); ctx.window[lossWinKey].push(loss); if (ctx.window[gainWinKey].length > period) ctx.window[gainWinKey].shift(); if (ctx.window[lossWinKey].length > period) ctx.window[lossWinKey].shift(); if (ctx.window[gainWinKey].length < period) return; if (ctx.rsi.initialized !== true) { const avgGain = utilN.math.mean(ctx.window[gainWinKey]); const avgLoss = utilN.math.mean(ctx.window[lossWinKey]); if (avgGain === undefined || avgLoss === undefined) return; candle.rsi = utilN.math.num(avgLoss === 0 ? 100 : 100 - 100 / (1 + avgGain / avgLoss)); candle.averageGain = avgGain; candle.averageLoss = avgLoss; ctx.rsi.initialized = true; ctx.rsi.prevAvgGain = avgGain; ctx.rsi.prevAvgLoss = avgLoss; delete ctx.window[gainWinKey]; delete ctx.window[lossWinKey]; return; } if (ctx.rsi.prevAvgGain === undefined || ctx.rsi.prevAvgLoss === undefined) { return; } const avgGain = (ctx.rsi.prevAvgGain * (period - 1) + gain) / period; const avgLoss = (ctx.rsi.prevAvgLoss * (period - 1) + loss) / period; const rsi = avgLoss === 0 ? 100 : 100 - 100 / (1 + avgGain / avgLoss); candle.rsi = utilN.math.num(rsi); candle.averageGain = utilN.math.num(avgGain); candle.averageLoss = utilN.math.num(avgLoss); ctx.rsi.prevAvgGain = avgGain; ctx.rsi.prevAvgLoss = avgLoss; } exports.default = setCandleRsi; //# sourceMappingURL=candle.setRsi.js.map