trading-signals
Version:
Technical indicators to run technical analysis with JavaScript / TypeScript.
55 lines • 1.85 kB
JavaScript
import { TradingSignal, TrendIndicatorSeries } from '../../base/Indicator.js';
import { pushUpdate } from '../../util/pushUpdate.js';
export class CMO extends TrendIndicatorSeries {
#prices = [];
#overbought;
#oversold;
interval;
constructor(interval, { overbought = 50, oversold = -50 } = {}) {
super();
this.interval = interval;
this.#overbought = overbought;
this.#oversold = oversold;
}
getRequiredInputs() {
return this.interval + 1;
}
update(price, replace) {
pushUpdate(this.#prices, replace, price, this.getRequiredInputs());
if (this.#prices.length < this.getRequiredInputs()) {
return null;
}
let gains = 0;
let losses = 0;
for (let i = 1; i < this.#prices.length; i++) {
const change = this.#prices[i] - this.#prices[i - 1];
if (change > 0) {
gains += change;
}
else {
losses -= change;
}
}
const total = gains + losses;
if (total === 0) {
return this.setResult(0, replace);
}
return this.setResult((100 * (gains - losses)) / total, replace);
}
calculateSignalState(result) {
const hasResult = result !== null && result !== undefined;
const isOversold = hasResult && result <= this.#oversold;
const isOverbought = hasResult && result >= this.#overbought;
switch (true) {
case !hasResult:
return TradingSignal.UNKNOWN;
case isOversold:
return TradingSignal.BEARISH;
case isOverbought:
return TradingSignal.BULLISH;
default:
return TradingSignal.SIDEWAYS;
}
}
}
//# sourceMappingURL=CMO.js.map