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.
[Indicator]publicclassEmaExample:Indicator{privateExponentialMovingAverage_emaFast;privateExponentialMovingAverage_emaSlow;[Parameter("Data Source")]publicDataSeriesPrice{get;set;}[Parameter("Slow Periods", DefaultValue = 10)]publicintSlowPeriods{get;set;}[Parameter("Fast Periods", DefaultValue = 5)]publicintFastPeriods{get;set;}protectedoverridevoidInitialize(){// 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);}publicoverridevoidCalculate(intindex){// If the index is less than SlowPeriods don't calculateif(index<=SlowPeriods){return;}if(_emaFast.Result.HasCrossedAbove(_emaSlow.Result,0)){// Print the index at which the fast ema crossed the slow emaPrint("Fast EMA Has Crossed Above at index = {0}",index);}}}
usingcAlgo.API;usingcAlgo.API.Indicators;namespacecAlgo.Robots{// This sample cBot shows how to use the Exponential Moving Average indicator[Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]publicclassExponentialMovingAverageSample:Robot{privatedouble_volumeInUnits;privateExponentialMovingAverage_fastExponentialMovingAverage;privateExponentialMovingAverage_slowExponentialMovingAverage;[Parameter("Volume (Lots)", DefaultValue = 0.01)]publicdoubleVolumeInLots{get;set;}[Parameter("Stop Loss (Pips)", DefaultValue = 10)]publicdoubleStopLossInPips{get;set;}[Parameter("Take Profit (Pips)", DefaultValue = 10)]publicdoubleTakeProfitInPips{get;set;}[Parameter("Label", DefaultValue = "Sample")]publicstringLabel{get;set;}publicPosition[]BotPositions{get{returnPositions.FindAll(Label);}}protectedoverridevoidOnStart(){_volumeInUnits=Symbol.QuantityToVolumeInUnits(VolumeInLots);_fastExponentialMovingAverage=Indicators.ExponentialMovingAverage(Bars.ClosePrices,9);_slowExponentialMovingAverage=Indicators.ExponentialMovingAverage(Bars.ClosePrices,20);}protectedoverridevoidOnBar(){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);}elseif(_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);}}privatevoidClosePositions(TradeTypetradeType){foreach(varpositioninBotPositions){if(position.TradeType!=tradeType)continue;ClosePosition(position);}}}}
1 2 3 4 5 6 7 8 91011121314151617
importclrclr.AddReference("cAlgo.API")fromcAlgo.APIimport*classTest():definitialize(self):# Price and FastPeriods are parameters defined in indicator C# file# initialize new instances of ExponentialMovingAverage Indicator classself.emaFast=api.Indicators.ExponentialMovingAverage(api.Price,api.FastPeriods)# emaSlow is the exponential moving average of the emaFastself.emaSlow=api.Indicators.ExponentialMovingAverage(self.emaFast.Result,api.SlowPeriods)defcalculate(self,index):# If the index is less than SlowPeriods don't calculateifindex<=api.SlowPeriods:returnifFunctions.HasCrossedAbove(self.emaFast.Result,self.emaSlow.Result,0):# Print the index at which the fast ema crossed the slow emaapi.Print(f"Fast EMA Has Crossed Above at index = {index}")