UNPKG

trading-utils

Version:

A collection of helpful trading utility functions.

44 lines 1.94 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.RsiIndicatorImpl = void 0; const lodash_1 = require("lodash"); const movingAverageService_1 = require("../common/movingAverageService"); class RsiIndicatorImpl { calculate(prices) { const dataPoints = this.calculateRSIIndicator(prices); return dataPoints[dataPoints.length - 1] > 50 ? 'LONG' : dataPoints[dataPoints.length - 1] === 50 ? 'NEUTRAL' : 'SHORT'; } calculateRSIIndicator = (prices) => { if (prices.length < 18) { throw new Error('Given parameter "prices" does not have the minimum length of 18'); } const upMoves = []; const downMoves = []; for (let i = 1; i < prices.length; i++) { const difference = prices[i] - prices[i - 1]; if (difference >= 0) { upMoves.push(difference); downMoves.push(0); } else { upMoves.push(0); downMoves.push(difference * -1); } } const gainSMA = movingAverageService_1.movingAverageService.calculateSMA(upMoves.slice(0, 14)); const lossSMA = movingAverageService_1.movingAverageService.calculateSMA(downMoves.slice(0, 14)); const avgGains = [gainSMA]; const avgLoss = [lossSMA]; for (let i = 14, a = 0; i < upMoves.length; i++, a++) { avgGains.push((13 * avgGains[a] + upMoves[i]) / 14); avgLoss.push((13 * avgLoss[a] + downMoves[i]) / 14); } const relativeStrengths = []; for (let i = 0; i < avgGains.length; i++) { relativeStrengths.push(avgGains[i] / avgLoss[i]); } return relativeStrengths.map((rs) => lodash_1.round(100 - 100 / (1 + rs), 2)); }; } exports.RsiIndicatorImpl = RsiIndicatorImpl; //# sourceMappingURL=rsiIndicator.js.map