Building Trading Strategies Indicators Exponential Moving Average

Exponential Moving Average

The ExponentialMovingAverage (Ema) is a widely-used technical indicator that gives more weight to recent price data, making it more responsive to new information compared to a Simple Moving Average (SMA). The Ema decreases the lag by applying more weight to recent prices relative to older prices.

Market Signals

The Ema can provide key market signals. A common interpretation is that when the Ema crosses above the price, it's a buy signal, and when it crosses below, it's a sell signal.

Calculation

The Ema is calculated with the formula:

EMA_i = EMA_i-1 + K * (Price - EMA_i-1)

where K is the smoothing factor calculated by 2 / (Period + 1).

This formula allows the Ema to be continuously updated as new price data becomes available.

    import { Bar, InstrumentProcessorBase } from '@botmain/common';
    import { ExponentialMovingAverage } from '@botmain/indicators';

    export class InstrumentProcessor extends InstrumentProcessorBase {
        ema = new ExponentialMovingAverage(28);

        ...

        onBarClose(bar: Bar) {
            //Add bar close to calculate new exponential moving average
            this.ema.add(bar.close);

            ...

            if(bar.open < this.ema.last && bar.close > this.ema.last) {
                //price crossed simple moving average. Place trade..
            }
        }
    }