---
id: "T:cAlgo.API.Indicators.ExponentialMovingAverage"
kind: "interface"
namespace: "cAlgo.API.Indicators"
url: "https://help.ctrader.com/ctrader-algo/references/Indicators/ExponentialMovingAverage/"
---

> Documentation index for agents: https://help.ctrader.com/ctrader-algo/llms.txt · machine index: https://help.ctrader.com/ctrader-algo/api.json

# ExponentialMovingAverage (interface)

The exponential moving average of the price data source over a period of time.

Environments: Desktop supported · CLI supported · Cloud supported.

## Definition

```csharp
public interface ExponentialMovingAverage
```

## Remarks

The exponential moving average is similar to the simple moving average, but applies more weight to more recent data. The weighting for each older price data decreases exponentially. Therefore the exponential moving average reacts faster to latest price changes than the simple moving average.

## Examples

C#:

```csharp
public partial class EmaExample : Indicator
{
    private ExponentialMovingAverage _emaFast;
    private ExponentialMovingAverage _emaSlow;

    protected override void Initialize()
    {
        // initialize new instances of ExponentialMovingAverage Indicator class
        _emaFast = Indicators.ExponentialMovingAverage(Price, FastPeriods);
        // _emaSlow is the exponential moving average of the emaFast
        _emaSlow = Indicators.ExponentialMovingAverage(_emaFast.Result, SlowPeriods);
    }

    public override void Calculate(int index)
    {
        // If the index is less than SlowPeriods don't calculate
        if(index <= SlowPeriods)
        {
            return;
        }

        if(_emaFast.Result.HasCrossedAbove(_emaSlow.Result,0))
        {
            // Print the index at which the fast ema crossed the slow ema
            Print("Fast EMA Has Crossed Above at index = {0}", index);
        }
    }
}
```

Python:

```python
import clr
clr.AddReference("cAlgo.API")

from cAlgo.API import *

class Test():
    def initialize(self):
        self.emaFast = api.Indicators.ExponentialMovingAverage(api.Price, api.FastPeriods)
        # emaSlow is the exponential moving average of the emaFast
        self.emaSlow = api.Indicators.ExponentialMovingAverage(self.emaFast.Result, api.SlowPeriods)

    def calculate(self, index):
        # If the index is less than SlowPeriods don't calculate
        if index <= api.SlowPeriods:
            return

        if Functions.HasCrossedAbove(self.emaFast.Result, self.emaSlow.Result, 0):
            # Print the index at which the fast ema crossed the slow ema
            api.Print(f"Fast EMA Has Crossed Above at index = {index}")
```

config.json:

```json
{
  "Parameters": [
    {
      "Name": "Price",
      "DisplayName": "Data Source",
      "Type": "DataSeries"
    },
    {
      "Name": "SlowPeriods",
      "DisplayName": "Slow Periods",
      "Type": "Integer",
      "DefaultValue": 10
    },
    {
      "Name": "FastPeriods",
      "DisplayName": "Fast Periods",
      "Type": "Integer",
      "DefaultValue": 5
    }
  ]
}
```

C#:

```csharp
using cAlgo.API;
using cAlgo.API.Indicators;

namespace cAlgo.Robots
{
    // This sample cBot shows how to use the Exponential Moving Average indicator
    public partial class ExponentialMovingAverageSample : Robot
    {
        private double _volumeInUnits;

        private ExponentialMovingAverage _fastExponentialMovingAverage;

        private ExponentialMovingAverage _slowExponentialMovingAverage;

        public Position[] BotPositions
        {
            get
            {
                return Positions.FindAll(Label);
            }
        }

        protected override void OnStart()
        {
            _volumeInUnits = Symbol.QuantityToVolumeInUnits(VolumeInLots);

            _fastExponentialMovingAverage = Indicators.ExponentialMovingAverage(Bars.ClosePrices, 9);

            _slowExponentialMovingAverage = Indicators.ExponentialMovingAverage(Bars.ClosePrices, 20);
        }

        protected override void OnBar()
        {
            if (_fastExponentialMovingAverage.Result.Last(1) > _slowExponentialMovingAverage.Result.Last(1) && _fastExponentialMovingAverage.Result.Last(2) < _slowExponentialMovingAverage.Result.Last(2))
            {
                ClosePositions(TradeType.Sell);

                ExecuteMarketOrder(TradeType.Buy, SymbolName, _volumeInUnits, Label, StopLossInPips, TakeProfitInPips);
            }
            else if (_fastExponentialMovingAverage.Result.Last(1) < _slowExponentialMovingAverage.Result.Last(1) && _fastExponentialMovingAverage.Result.Last(2) > _slowExponentialMovingAverage.Result.Last(2))
            {
                ClosePositions(TradeType.Buy);

                ExecuteMarketOrder(TradeType.Sell, SymbolName, _volumeInUnits, Label, StopLossInPips, TakeProfitInPips);
            }
        }

        private void ClosePositions(TradeType tradeType)
        {
            foreach (var position in BotPositions)
            {
                if (position.TradeType != tradeType) continue;

                ClosePosition(position);
            }
        }
    }
}
```

Python:

```python
import clr

clr.AddReference("cAlgo.API")

# Import cAlgo API types
from cAlgo.API import *

# Import trading wrapper functions
from robot_wrapper import *

class ExponentialMovingAverageSample():
    def on_start(self):
        self.volumeInUnits = api.Symbol.QuantityToVolumeInUnits(api.VolumeInLots)
        self.fastExponentialMovingAverage = api.Indicators.ExponentialMovingAverage(api.Bars.ClosePrices, 9)
        self.slowExponentialMovingAverage = api.Indicators.ExponentialMovingAverage(api.Bars.ClosePrices, 20)

    def on_bar_closed(self):
        if self.fastExponentialMovingAverage.Result.Last(0) > self.slowExponentialMovingAverage.Result.Last(0) and self.fastExponentialMovingAverage.Result.Last(1) < self.slowExponentialMovingAverage.Result.Last(1):
            self.close_positions(TradeType.Sell)
            api.ExecuteMarketOrder(TradeType.Buy, api.SymbolName, self.volumeInUnits, api.Label)
        elif self.fastExponentialMovingAverage.Result.Last(0) < self.slowExponentialMovingAverage.Result.Last(0) and self.fastExponentialMovingAverage.Result.Last(1) > self.slowExponentialMovingAverage.Result.Last(1):
            self.close_positions(TradeType.Buy)
            api.ExecuteMarketOrder(TradeType.Sell, api.SymbolName, self.volumeInUnits, api.Label)

    def get_bot_positions(self):
        return api.Positions.FindAll(api.Label)

    def close_positions(self, tradeType):
        for position in self.get_bot_positions():
            if position.TradeType != tradeType:
                continue
            api.ClosePosition(position)
```

config.json:

```json
{
  "AccessRights": "None",
  "TimeZone": "UTC",
  "Parameters": [
    {
      "Name": "VolumeInLots",
      "DisplayName": "Volume (Lots)",
      "Type": "Double",
      "DefaultValue": 0.01
    },
    {
      "Name": "StopLossInPips",
      "DisplayName": "Stop Loss (Pips)",
      "Type": "Double",
      "DefaultValue": 10
    },
    {
      "Name": "TakeProfitInPips",
      "DisplayName": "Take Profit (Pips)",
      "Type": "Double",
      "DefaultValue": 10
    },
    {
      "Name": "Label",
      "Type": "String",
      "DefaultValue": "Sample"
    }
  ]
}
```

## Inherited members

- `IndicatorDataSeries Result {get;}` (from MovingAverage) [details](https://help.ctrader.com/ctrader-algo/references/Indicators/MovingAverage/Result/index.md)
- `ChartIndicator AddToChart()` (from StandardIndicator) [details](https://help.ctrader.com/ctrader-algo/references/General/StandardIndicator/AddToChart/index.md)
- `Symbol Symbol {get;}` (from StandardIndicator) [details](https://help.ctrader.com/ctrader-algo/references/General/StandardIndicator/Symbol/index.md)
- `TimeFrame TimeFrame {get;}` (from StandardIndicator) [details](https://help.ctrader.com/ctrader-algo/references/General/StandardIndicator/TimeFrame/index.md)
- `Bars Bars {get;}` (from StandardIndicator) [details](https://help.ctrader.com/ctrader-algo/references/General/StandardIndicator/Bars/index.md)

