trading-signals
Version:
Technical indicators to run technical analysis with JavaScript / TypeScript.
57 lines • 2.16 kB
JavaScript
import { TradingSignal, TrendIndicatorSeries } from '../../base/Indicator.js';
import { getTypicalPrice } from '../../util/getTypicalPrice.js';
import { pushUpdate } from '../../util/pushUpdate.js';
export class MFI extends TrendIndicatorSeries {
#candles = [];
#overbought;
#oversold;
interval;
constructor(interval, { overbought = 80, oversold = 20 } = {}) {
super();
this.interval = interval;
this.#overbought = overbought;
this.#oversold = oversold;
}
getRequiredInputs() {
return this.interval + 1;
}
update(candle, replace) {
pushUpdate({ array: this.#candles, item: candle, maxLength: this.getRequiredInputs(), replace: replace });
if (this.#candles.length < this.getRequiredInputs()) {
return null;
}
const typicalPrices = this.#candles.map(getTypicalPrice);
let positiveFlow = 0;
let negativeFlow = 0;
for (let i = 1; i < this.#candles.length; i++) {
const rawMoneyFlow = typicalPrices[i] * this.#candles[i].volume;
if (typicalPrices[i] > typicalPrices[i - 1]) {
positiveFlow += rawMoneyFlow;
}
else if (typicalPrices[i] < typicalPrices[i - 1]) {
negativeFlow += rawMoneyFlow;
}
}
const totalFlow = positiveFlow + negativeFlow;
if (totalFlow === 0) {
return this.setResult(50, replace);
}
return this.setResult((100 * positiveFlow) / totalFlow, 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=MFI.js.map