trading-signals
Version:
Technical indicators to run technical analysis with JavaScript / TypeScript.
84 lines • 2.87 kB
JavaScript
import { TradingSignal, TrendIndicatorSeries } from '../../base/Indicator.js';
export class REI extends TrendIndicatorSeries {
#highs = [];
#lows = [];
#closes = [];
interval;
#overbought;
#oversold;
constructor(interval, { overbought = 60, oversold = -60 } = {}) {
super();
this.interval = interval;
this.#overbought = overbought;
this.#oversold = oversold;
}
calculateSignalState(result) {
const hasResult = result !== null && result !== undefined;
const isOverbought = hasResult && result > this.#overbought;
const isOversold = hasResult && result < this.#oversold;
switch (true) {
case !hasResult:
return TradingSignal.UNKNOWN;
case isOverbought:
return TradingSignal.BULLISH;
case isOversold:
return TradingSignal.BEARISH;
default:
return TradingSignal.SIDEWAYS;
}
}
getRequiredInputs() {
return this.interval + 8;
}
#calculateN(j) {
if (this.#highs[j - 2] < this.#closes[j - 7] &&
this.#highs[j - 2] < this.#closes[j - 8] &&
this.#highs[j] < this.#highs[j - 5] &&
this.#highs[j] < this.#highs[j - 6]) {
return 0;
}
return 1;
}
#calculateM(j) {
if (this.#lows[j - 2] > this.#closes[j - 7] &&
this.#lows[j - 2] > this.#closes[j - 8] &&
this.#lows[j] > this.#lows[j - 5] &&
this.#lows[j] > this.#lows[j - 6]) {
return 0;
}
return 1;
}
update(candle, replace) {
if (replace) {
this.#highs.pop();
this.#lows.pop();
this.#closes.pop();
}
this.#highs.push(candle.high);
this.#lows.push(candle.low);
this.#closes.push(candle.close);
if (this.#highs.length < this.getRequiredInputs()) {
return null;
}
let subValueSum = 0;
let absValueSum = 0;
const limitIndex = this.#highs.length - 1;
for (let j = limitIndex; j > this.interval; j--) {
const diffHighs = this.#highs[j] - this.#highs[j - 2];
const diffLows = this.#lows[j] - this.#lows[j - 2];
const n = this.#calculateN(j);
const m = this.#calculateM(j);
const s = diffHighs + diffLows;
const subValue = n * m * s;
const absDailyValue = Math.abs(diffHighs) + Math.abs(diffLows);
subValueSum += subValue;
absValueSum += absDailyValue;
}
if (absValueSum === 0) {
return this.setResult(0, replace);
}
const rei = (subValueSum / absValueSum) * 100;
return this.setResult(rei, replace);
}
}
//# sourceMappingURL=REI.js.map