UNPKG

@4ex/indicators

Version:

Technical indicators for ohlc charts written in TypeScript

73 lines (72 loc) 2.59 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.BB = void 0; const fp_1 = require("lodash/fp"); const technicalindicators_1 = require("technicalindicators"); const types_1 = require("../types"); const defaultInput = { period: 20, stdDev: 2, source: types_1.OHLCEnum.CLOSE, }; /** * Bollinger Bands® are a type of chart indicator for technical analysis * and have become widely used by traders in many markets, including stocks, * futures, and currencies. Created by John Bollinger in the 1980s, the bands * offer unique insights into price and volatility. In fact, there are a * number of uses for Bollinger Bands®, such as determining overbought and * oversold levels, as a trend following tool, and for monitoring for breakouts. */ class BB { /** * @param {OHLC[]} series candles series * @param {OHLCEnum} source candles price source * @param {number} period period length * @param {number} stdDev standard deviation */ constructor(series = [], source = defaultInput.source, period = defaultInput.period, stdDev = defaultInput.stdDev) { // super(); const values = fp_1.map(source)(series); this.indicator = new technicalindicators_1.BollingerBands({ period, values, stdDev }); this.source = source; } /** * Retrieve BB values for instance * @return {BollingerBandsOutput[]} values for instance data */ getResults() { return this.indicator.getResult(); } /** * Calculate BB to next tick * @param {OHLC} candle new candle to add to series * @return {BollingerBandsOutput | undefined} next BB value or undefined if * period is greater than actual series length */ next(candle) { return this.indicator.nextValue(candle[this.source]); } /** * Create instance from data * @param {BollingerBandsInput} input input data * @return {BB} BB instance */ static generator({ series, period, stdDev, source, }) { return new BB(series, source, period, stdDev); } /** * Get BB values from input * @param {BollingerBandsInput} input input data * @return {BollingerBandsOutput[]} BB values */ static calculate(input) { const { series, period, source, stdDev } = Object.assign(Object.assign({}, defaultInput), input); const values = fp_1.map(source)(series); return technicalindicators_1.BollingerBands.calculate({ values, period, stdDev, }); } } exports.BB = BB;