rxjs-trading-signals
Version:
The user - defined operator helps users quickly output index values. base trading-signals library
40 lines • 2.02 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.StochasticOscillator = void 0;
const big_js_1 = __importDefault(require("big.js"));
const rxjs_1 = require("rxjs");
const SMA_1 = require("../SMA/SMA");
const StochasticOscillator = (n, m, p) => {
return (observable) => new rxjs_1.Observable((subscriber) => {
let stochK = new big_js_1.default(0);
const subscription = observable
.pipe((0, rxjs_1.bufferCount)(n, 1), (0, rxjs_1.filter)(x => x.length === n), (0, rxjs_1.concatMap)(candles => {
const candle = candles[candles.length - 1];
return (0, rxjs_1.zip)((0, rxjs_1.from)(candles).pipe((0, rxjs_1.map)(({ high }) => new big_js_1.default(high)), (0, rxjs_1.max)((a, b) => (a.lt(b) ? -1 : 1))), (0, rxjs_1.from)(candles).pipe((0, rxjs_1.map)(({ low }) => new big_js_1.default(low)), (0, rxjs_1.min)((a, b) => (a.lt(b) ? -1 : 1)))).pipe((0, rxjs_1.concatMap)(([highest, lowest]) => {
const divisor = new big_js_1.default(highest).minus(lowest);
let fastK = new big_js_1.default(100).times(new big_js_1.default(candle.close).minus(lowest));
fastK = fastK.div(divisor.eq(0) ? 1 : divisor);
return (0, rxjs_1.of)(fastK);
}));
}), (0, SMA_1.SMA)(m), (0, rxjs_1.tap)(x => (stochK = x)), (0, SMA_1.SMA)(p), (0, rxjs_1.map)(stochD => ({ stochD, stochK })))
.subscribe({
next(x) {
subscriber.next(x);
},
error(err) {
subscriber.error(err);
},
complete() {
subscriber.complete();
},
});
return () => {
subscription.unsubscribe();
};
});
};
exports.StochasticOscillator = StochasticOscillator;
//# sourceMappingURL=StochasticOscillator.js.map