UNPKG

@4ex/indicators

Version:

Technical indicators for ohlc charts written in TypeScript

90 lines (89 loc) 3.18 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Stoch = void 0; const fp_1 = require("lodash/fp"); const technicalindicators_1 = require("technicalindicators"); const types_1 = require("../types"); const defaultInput = { period: 14, signalPeriod: 3, }; /** * A stochastic oscillator is a momentum indicator comparing a particular * closing price of a security to a range of its prices over a certain * period of time. The sensitivity of the oscillator to market movements * is reducible by adjusting that time period or by taking a moving average * of the result. It is used to generate overbought and oversold trading * signals, utilizing a 0–100 bounded range of values. */ class Stoch { /** * @param {OHLC[]} series candles series * @param {number} period period for indicator * @param {number} signalPeriod period for indicator */ constructor(series, period = defaultInput.period, signalPeriod = defaultInput.signalPeriod) { // super(); const high = fp_1.map(types_1.OHLCEnum.HIGH)(series); const low = fp_1.map(types_1.OHLCEnum.LOW)(series); const close = fp_1.map(types_1.OHLCEnum.CLOSE)(series); this.indicator = new technicalindicators_1.Stochastic({ high, low, close, period, signalPeriod, }); this.period = period; this.signalPeriod = period; } /** * Retrieve Stoch values for instance * @return {StochasticOscillatorOutput[]} values for instance data */ getResults() { return this.indicator.getResult(); } /** * Calculate Stoch to next tick * @param {OHLC} candle new candle to add to series * @return {StochasticOscillatorOutput | undefined} next Stoch value or * undefined if period is greater than actual series length */ next(candle) { return this.indicator.nextValue({ high: [candle[types_1.OHLCEnum.HIGH]], low: [candle[types_1.OHLCEnum.LOW]], close: [candle[types_1.OHLCEnum.CLOSE]], period: this.period, signalPeriod: this.signalPeriod, }); } /** * Create instance from data * @param {StochasticOscillatorInput} input input data * @return {Stoch} Stoch instance */ static generator({ series, period, signalPeriod, }) { return new Stoch(series, period, signalPeriod); } /** * Get Stoch values from input * @param {StochasticOscillatorInput} input input data * @return {StochasticOscillatorOutput[]} Stoch values */ static calculate(input) { const { series, period, signalPeriod } = Object.assign(Object.assign({}, defaultInput), input); const high = fp_1.map(types_1.OHLCEnum.HIGH)(series); const low = fp_1.map(types_1.OHLCEnum.LOW)(series); const close = fp_1.map(types_1.OHLCEnum.CLOSE)(series); return technicalindicators_1.Stochastic.calculate({ high, low, close, period, signalPeriod, }); } } exports.Stoch = Stoch;