UNPKG

rxjs-trading-signals

Version:

The user - defined operator helps users quickly output index values. base trading-signals library

42 lines 2.16 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.BollingerBands = void 0; const big_js_1 = __importDefault(require("big.js")); const rxjs_1 = require("rxjs"); const BollingerBands = (interval, deviationMultiplier = 2) => { return (observable) => new rxjs_1.Observable((subscriber) => { const subscription = observable .pipe((0, rxjs_1.bufferCount)(interval, 1), (0, rxjs_1.filter)(items => items.length === interval), (0, rxjs_1.concatMap)(items => { const middle = (0, rxjs_1.from)(items).pipe((0, rxjs_1.scan)((curr, next) => curr.plus(next), new big_js_1.default(0)), (0, rxjs_1.last)(), (0, rxjs_1.map)(x => x.div(items.length))); return (0, rxjs_1.zip)((0, rxjs_1.of)(items), middle).pipe((0, rxjs_1.concatMap)(([items, middle]) => { const squaredDifferences = items.map((x) => new big_js_1.default(x).sub(middle).pow(2)); return (0, rxjs_1.zip)((0, rxjs_1.of)(middle), (0, rxjs_1.from)(squaredDifferences).pipe((0, rxjs_1.scan)((curr, next) => curr.plus(next), new big_js_1.default(0)), (0, rxjs_1.last)(), (0, rxjs_1.map)(x => x.div(squaredDifferences.length || 1).sqrt()))).pipe((0, rxjs_1.concatMap)(([middle, standardDeviation]) => { return (0, rxjs_1.of)({ lower: middle.sub(standardDeviation.times(deviationMultiplier)), middle, upper: middle.add(standardDeviation.times(deviationMultiplier)), }); })); })); })) .subscribe({ next(x) { subscriber.next(x); }, error(err) { subscriber.error(err); }, complete() { subscriber.complete(); }, }); return () => { subscription.unsubscribe(); }; }); }; exports.BollingerBands = BollingerBands; //# sourceMappingURL=BollingerBands.js.map