trading-signals
Version:
Technical indicators to run technical analysis with JavaScript / TypeScript.
50 lines • 1.63 kB
JavaScript
import { TradingSignal, TrendIndicatorSeries } from '../../base/Indicator.js';
import { SMA } from '../../trend/SMA/SMA.js';
import { pushUpdate } from '../../util/pushUpdate.js';
export class EMV extends TrendIndicatorSeries {
#candles = [];
#sma;
#scale;
interval;
constructor(interval, scale = 100_000_000) {
super();
this.interval = interval;
this.#sma = new SMA(interval);
this.#scale = scale;
}
getRequiredInputs() {
return this.interval + 1;
}
update(candle, replace) {
pushUpdate(this.#candles, replace, candle, 2);
if (this.#candles.length < 2) {
return null;
}
const prev = this.#candles[0];
const distanceMoved = (candle.high + candle.low) / 2 - (prev.high + prev.low) / 2;
const highLowDiff = candle.high - candle.low;
let emv1 = 0;
if (highLowDiff !== 0 && candle.volume !== 0) {
const boxRatio = candle.volume / this.#scale / highLowDiff;
emv1 = distanceMoved / boxRatio;
}
const smaResult = this.#sma.update(emv1, replace);
if (smaResult === null) {
return null;
}
return this.setResult(smaResult, replace);
}
calculateSignalState(result) {
if (result === null || result === undefined) {
return TradingSignal.UNKNOWN;
}
if (result > 0) {
return TradingSignal.BULLISH;
}
if (result < 0) {
return TradingSignal.BEARISH;
}
return TradingSignal.SIDEWAYS;
}
}
//# sourceMappingURL=EMV.js.map