rxjs-trading-signals
Version:
The user - defined operator helps users quickly output index values. base trading-signals library
62 lines • 2.52 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.RSI = void 0;
const big_js_1 = __importDefault(require("big.js"));
const rxjs_1 = require("rxjs");
const WSMA_1 = require("../WSMA/WSMA");
const RSI = (interval, SmoothingIndicator = WSMA_1.WSMA) => {
return (observable) => new rxjs_1.Observable((subscriber) => {
let prevPrice;
let maxValue = new big_js_1.default(100);
const source$ = observable.pipe((0, rxjs_1.concatMap)((price, index) => {
if (!index) {
prevPrice = new big_js_1.default(price);
return (0, rxjs_1.of)();
}
const currentPrice = new big_js_1.default(price);
const previousPrice = new big_js_1.default(prevPrice);
prevPrice = new big_js_1.default(price);
if (currentPrice.gt(previousPrice)) {
return (0, rxjs_1.of)({
avgLoss: new big_js_1.default(0),
avgGain: currentPrice.minus(previousPrice),
});
}
else {
return (0, rxjs_1.of)({
avgLoss: previousPrice.minus(currentPrice),
avgGain: new big_js_1.default(0),
});
}
}), (0, rxjs_1.share)());
const subscription = (0, rxjs_1.zip)(source$.pipe((0, rxjs_1.map)(({ avgLoss }) => avgLoss), SmoothingIndicator(interval)), source$.pipe((0, rxjs_1.map)(({ avgGain }) => avgGain), SmoothingIndicator(interval)))
.pipe((0, rxjs_1.concatMap)(([avgLoss, avgGain]) => {
if (new big_js_1.default(avgLoss).eq(0)) {
return (0, rxjs_1.of)(new big_js_1.default(100));
}
const relativeStrength = new big_js_1.default(avgGain).div(avgLoss);
return (0, rxjs_1.of)(maxValue.minus(maxValue.div(relativeStrength.plus(1))));
}))
.subscribe({
next(x) {
subscriber.next(x);
},
error(err) {
subscriber.error(err);
},
complete() {
subscriber.complete();
},
});
return () => {
subscription.unsubscribe();
prevPrice = null;
maxValue = null;
};
});
};
exports.RSI = RSI;
//# sourceMappingURL=RSI.js.map