rxjs-trading-signals
Version:
The user - defined operator helps users quickly output index values. base trading-signals library
46 lines • 1.6 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.EMA = void 0;
const big_js_1 = __importDefault(require("big.js"));
const rxjs_1 = require("rxjs");
const isZero_1 = require("../utils/isZero");
const EMA = (interval, isOutput = false) => {
return (observable) => new rxjs_1.Observable((subscriber) => {
let result = new big_js_1.default(0);
let weightFactor = 2 / (interval + 1);
let count = 0;
const subscription = observable
.pipe((0, rxjs_1.concatMap)(_price => {
count++;
const price = new big_js_1.default(_price);
if ((0, isZero_1.isZero)(result)) {
result = price;
}
const w = new big_js_1.default(result).times(1 - weightFactor);
result = price.times(weightFactor).add(w);
return (0, rxjs_1.of)(result).pipe((0, rxjs_1.filter)(() => isOutput || count >= interval));
}))
.subscribe({
next(x) {
subscriber.next(x);
},
error(err) {
subscriber.error(err);
},
complete() {
subscriber.complete();
},
});
return () => {
subscription.unsubscribe();
result = null;
weightFactor = null;
count = null;
};
});
};
exports.EMA = EMA;
//# sourceMappingURL=EMA.js.map