C # Implementation of EMA calculation (C # exponential moving Average (EMA) indicator)

Source: Internet
Author: User

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)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.