import AggregatedDataPoint from "../data/asset/AggregatedDataPoint.js";

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 calculateEMA(dataPoints) {
        const sma = AssetInstrumentCalculator.calculateSMA(dataPoints);
        const multiplier = 2 / (dataPoints.length + 1);

        return sma * (1 - multiplier) + dataPoints[dataPoints.length - 1].avgPrice * multiplier;
    }
}