Originally a foreign source (technicalanalysisengine src 1.25) internal calculation of the EMA is:
var copyinputvalues = input. ToList ();
for (int i = period; I < Copyinputvalues.count; i++)
{
var resultvalue = (Copyinputvalues[i]-returnvalues.last ()) * multiplier + returnvalues.last ();
Returnvalues.add (Resultvalue);
}
var result = new Emaresult ()
{
Values = Returnvalues,
Startindexoffset = period-1
};
It is evident that such calculations are inconsistent with our tradition and may not even produce results. After studying the EMA algorithm in the main software, such as the domestic Tongda letter, the EMA calculation can be realized by using the method. Paste the implementation method of C #.
Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Text;
Using System.Threading.Tasks;
Namespace Myema
{
public class Myema
{
static void Main (string[] args)
{
Double[] arr;
arr = new double[5]{2077,2077,2078,2083,2082};
List<double> dd=new list<double> () {2077,2077,2077,2078,2083,2082};
Emaresult du=;
var result=ema (dd,5);
Console.WriteLine ("{0} result value", result.) Values.count);
for (int i=0;i<result. values.count;i++) {
Console.WriteLine ("{0} of Ema={1}", I,result. Values[i]);
}
Console.WriteLine ("Emar={0}", result. EmaR);
}
<summary>
Contains calculation results for EMA indicator
</summary>
public class Emaresult
{
Public list<double> Values {get; set;}
public int Startindexoffset {get; set;}
Public double EmaR {get; set;}
}
//------------------------------------------------------------------------------------------------------------- ------------------
<summary>
Calculates exponential moving Average (EMA) indicator
</summary>
<param name= "Input" >input signal</param>
<param name= "period" >number of periods</param>
<returns>object containing Operation Results</returns>
public static Emaresult EMA (ienumerable<double> input, int period)
{
var returnvalues = new list<double> ();
Double multiplier = (2.0/(period + 1));
Double Initialsma = input. Take (period). Average ();
Returnvalues.add (INITIALSMA);
var copyinputvalues = input. ToList ();
int j=0;
for (int i = copyinputvalues.count-period; i < copyinputvalues.count; i++)
{
if (j<1)
{
var resultvalue =copyinputvalues[i];
Returnvalues.add (Resultvalue);
}
Else
{
var resultvalue = (copyinputvalues[i]*multiplier) + (1-multiplier) * Returnvalues.last ();
Returnvalues.add (Resultvalue);
}
j + +;
}
var result = new Emaresult ()
{
Emar=returnvalues.last (),
Values = Returnvalues,
Startindexoffset = period-1
};
return result;
}
}
}
C # Implementation of EMA calculation (C # exponential moving Average (EMA) indicator)