[BOT] Neue Klasse für Indikator berechnungen eingeführt
This commit is contained in:
parent
0117f0a563
commit
ccc1945ee4
33
src/js/tradingbot/util/AssetInstrumentCalculator.js
Normal file
33
src/js/tradingbot/util/AssetInstrumentCalculator.js
Normal file
@ -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;
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user