Exciting Update: Version 1.0.0 is now available, introducing high-performance technical indicators and custom drawing tools. Read more
Version: 1.0.0

Exponential Moving Average (EMA)

Plot Exponential Moving Average (EMA) indicators prioritizing recent market price shifts.

Overview

The Exponential Moving Average (EMA) is a trend indicator that applies more weight to the most recent data points. It reacts faster to price changes than the Simple Moving Average (SMA).

EMA Calculation

The calculation uses a smoothing multiplier K = 2 / (N + 1):

// EMA calculation logic
function calculateEMA(bars, period) {
  const ema = new Array(bars.length).fill(null);
  if (bars.length < period) return ema;

  const k = 2 / (period + 1);
  let sum = 0;
  for (let j = 0; j < period; j++) {
    sum += bars[j].close;
  }
  let prev = sum / period;
  ema[period - 1] = prev;

  for (let i = period; i < bars.length; i++) {
    prev = bars[i].close * k + prev * (1 - k);
    ema[i] = prev;
  }
  return ema;
}

Registering EMA Indicator

Register the indicator using the Charting API:

window.ChartingAPI.registerIndicator('ema', {
  name: 'EMA',
  type: 'overlay',
  params: { period: 20 },
  defaultColor: '#e91e63',
  calculate: function(bars, params) {
    const period = params.period || 20;
    return calculateEMA(bars, period);
  },
  render: function(ctx, chart, values, bounds, color) {
    // Render EMA line path...
  }
});