UNPKG

pricehistory

Version:

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

53 lines 2.17 kB
import * as utilN from "@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; } export default setCandleRsi; //# sourceMappingURL=candle.setRsi.js.map