trading-signals
Version:
Technical indicators to run technical analysis with JavaScript / TypeScript.
66 lines • 2.42 kB
JavaScript
import { SMA } from '../../trend/SMA/SMA.js';
import { WSMA } from '../../trend/WSMA/WSMA.js';
import { TradingSignal, TrendIndicatorSeries } from '../../base/Indicator.js';
import { Period } from '../../base/Period.js';
import { RSI } from '../RSI/RSI.js';
export class StochasticRSI extends TrendIndicatorSeries {
#period;
#rsi;
interval;
smoothing;
#overbought;
#oversold;
constructor(interval, SmoothingRSI = WSMA, smoothing = {
d: new SMA(3),
k: new SMA(3),
}, { overbought = 0.8, oversold = 0.2 } = {}) {
super();
this.interval = interval;
this.smoothing = smoothing;
this.#period = new Period(interval);
this.#rsi = new RSI(interval, SmoothingRSI);
this.#overbought = overbought;
this.#oversold = oversold;
}
getRequiredInputs() {
return this.#rsi.getRequiredInputs() + this.#period.getRequiredInputs();
}
update(price, replace) {
const rsiResult = this.#rsi.update(price, replace);
if (rsiResult) {
const periodResult = this.#period.update(rsiResult, replace);
if (periodResult) {
const min = periodResult.lowest;
const max = periodResult.highest;
const denominator = max - min;
if (denominator === 0) {
return this.setResult(100, replace);
}
const numerator = rsiResult - min;
const stochRSI = numerator / denominator;
const k = this.smoothing.k.update(stochRSI, replace);
if (k) {
this.smoothing.d.update(k, replace);
}
return this.setResult(stochRSI, replace);
}
}
return null;
}
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=StochasticRSI.js.map