From ccc1945ee40743d79286b522ee1346720b7b9dfd Mon Sep 17 00:00:00 2001 From: darkeye Date: Sun, 12 Jan 2025 16:16:10 +0100 Subject: [PATCH] =?UTF-8?q?[BOT]=20Neue=20Klasse=20f=C3=BCr=20Indikator=20?= =?UTF-8?q?berechnungen=20eingef=C3=BChrt?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../util/AssetInstrumentCalculator.js | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 src/js/tradingbot/util/AssetInstrumentCalculator.js diff --git a/src/js/tradingbot/util/AssetInstrumentCalculator.js b/src/js/tradingbot/util/AssetInstrumentCalculator.js new file mode 100644 index 0000000..f5dbfee --- /dev/null +++ b/src/js/tradingbot/util/AssetInstrumentCalculator.js @@ -0,0 +1,33 @@ +import AggregatedDataPoint from "../data/asset/AggregatedDataPoint"; + +export default class AssetInstrumentCalculator{ + + /** + * Calculate simple moving average + * + * Formula: SMA = (A1 + A2 + ……….An) / n + * + * @param {AggregatedDataPoint[]} dataPoints + * @returns {number} + */ + static calculateSMA(dataPoints) { + return dataPoints.map(dp => dp.avgPrice).reduce((a, b) => a+b, 0) / dataPoints.length; + } + + /** + * Calculate exponential moving average + * + * Formula: EMA = [Closing Price – EMA (Previous Time Period)] x Multiplier + EMA (Previous Time Period) + * + * SMA is taken as EMA of previous period; Multiplier ist 2/(n+1) + * + * @param {AggregatedDataPoint[]} dataPoints + * @returns {number} + */ + static caclulateEMA(dataPoints) { + const sma = AssetInstrumentCalculator.calculateSMA(dataPoints); + const multiplier = 2 / (dataPoints.length + 1); + + return sma * (1 - multiplier) + dataPoints[dataPoints.length - 1] * multiplier; + } +} \ No newline at end of file