ExponentialMovingAverage


Summary

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

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.

Syntax

public interface ExponentialMovingAverage : MovingAverage, IIndicator

Members

NameTypeSummary

Example 1

	    [Indicator]
	    public class EmaExample : Indicator
	    {
	        private ExponentialMovingAverage _emaFast;
	        private ExponentialMovingAverage _emaSlow;
	        [Parameter("Data Source")]
	        public DataSeries Price { get; set; }
	        [Parameter("Slow Periods", DefaultValue = 10)]
	        public int SlowPeriods { get; set; }
	        [Parameter("Fast Periods", DefaultValue = 5)]
	        public int FastPeriods { get; set; }
	        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);
	            }
	        }
     }

Example 2

using cAlgo.API;
using cAlgo.API.Indicators;
namespace cAlgo.Robots
{
    // This sample cBot shows how to use the Exponential Moving Average indicator
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class ExponentialMovingAverageSample : Robot
    {
        private double _volumeInUnits;
        private ExponentialMovingAverage _fastExponentialMovingAverage;
        private ExponentialMovingAverage _slowExponentialMovingAverage;
        [Parameter("Volume (Lots)", DefaultValue = 0.01)]
        public double VolumeInLots { get; set; }
        [Parameter("Stop Loss (Pips)", DefaultValue = 10)]
        public double StopLossInPips { get; set; }
        [Parameter("Take Profit (Pips)", DefaultValue = 10)]
        public double TakeProfitInPips { get; set; }
        [Parameter("Label", DefaultValue = "Sample")]
        public string Label { get; set; }
        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);
            }
        }
    }
}
Reference