/**
 * Indicator Definition Object for EMA.
 */
const EMA_INDICATOR = {
    name: 'EMA',
    label: 'Exponential Moving Average',
    usesBaseData: false, // This simple indicator uses the chart's currently displayed data
    params: [
        { name: 'period', type: 'number', defaultValue: 20, min: 2 },
    ],
    calculateFull: calculateFullEMA,
};

function calculateFullEMA(data, params) {
    const period = params.period;
    if (!data || data.length < period) return [];
    let emaData = [];
    const multiplier = 2 / (period + 1);
    let sum = 0;
    for (let i = 0; i < period; i++) {
        sum += data[i].close;
    }
    let prevEma = sum / period;
    emaData.push({ time: data[period - 1].time, value: prevEma });
    for (let i = period; i < data.length; i++) {
        const close = data[i].close;
        const ema = (close - prevEma) * multiplier + prevEma;
        emaData.push({ time: data[i].time, value: ema });
        prevEma = ema;
    }
    return emaData;
}
