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