UNPKG

rxjs-trading-signals

Version:

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

50 lines 2.07 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.CG = void 0; const big_js_1 = __importDefault(require("big.js")); const rxjs_1 = require("rxjs"); const SMA_1 = require("../SMA/SMA"); const CG = (interval, signalInterval) => { return (observable) => new rxjs_1.Observable((subscriber) => { let prices = []; const source$ = observable.pipe((0, rxjs_1.concatMap)((price) => { prices.push(new big_js_1.default(price)); if (prices.length > interval) { prices.shift(); } let nominator = new big_js_1.default(0); let denominator = new big_js_1.default(0); return (0, rxjs_1.range)(0, prices.length).pipe((0, rxjs_1.concatMap)(i => { const price = new big_js_1.default(prices[i]); nominator = nominator.plus(price.times(i + 1)); denominator = denominator.plus(price); return (0, rxjs_1.of)({ nominator, denominator, }); }), (0, rxjs_1.last)(), (0, rxjs_1.map)(({ nominator, denominator }) => (denominator.gt(0) ? nominator.div(denominator) : new big_js_1.default(0)))); }), (0, rxjs_1.share)()); const subscription = (0, rxjs_1.zip)(source$.pipe((0, rxjs_1.skip)(signalInterval)), source$.pipe((0, SMA_1.SMA)(signalInterval))) .pipe((0, rxjs_1.concatMap)(([cg, signal]) => (0, rxjs_1.of)({ cg, signal }))) .subscribe({ next(x) { subscriber.next(x); }, error(err) { subscriber.error(err); }, complete() { subscriber.complete(); }, }); return () => { subscription.unsubscribe(); prices = null; }; }); }; exports.CG = CG; //# sourceMappingURL=CG.js.map