Ema algorithm C # implementation

Source: Internet
Author: User

Ema represents the exponential smoothing moving average, and its function is defined as Y = EMA (x, n) Then y = [2 * x + (N-1) * y']/(n + 1), where y' indicates the value of Y in the previous cycle.

Calculate the n-day exponent smoothing moving average of X. The true formula is: the average value of the index on the current day = the smoothing coefficient * (the exponent value on the current day-the average value of the yesterday's index) + the average value of the yesterday's index; smoothing coefficient = 2/(unit of cycle + 1)

Ema reference functions can be easily implemented by using Recursive Algorithms on computers, but are not easy to understand. The following describes the EMA function.

X is a variable. The values of X vary from far to near ground. They are marked as x1, x2, X3 ,...., XN

When n = 1, EMA (x, 1) = [2 * X1 + (1-1) * y']/(1 + 1) = x1

When n = 2, EMA (x, 2) = [2 * X2 + (2-1) * y']/(2 + 1) = (2/3) * X 2 + (1/3) X 1

When n = 3, EMA (x, 3) = [2 * X3 + (3-1) * y']/(3 + 1) = [2 * X3 + 2 * (2/3) * X2 + (1/3) * X1)]/4 = (1/2) * X3 + (1/3) * X2 + (1/6) * X1

When N = 4, EMA (x, 4) = [2 * X4 + (4-1) * y']/(4 + 1) = 2/5 * X4 + 3/5 * (1/2) * X3 + (1/3) * X2 + (1/6) * X1) = 2/5 * X4 + 3/10 * X3 + 1/5 * X2 + 1/10 * X1

When n = 5, EMA (x, 5) = (1/3) * X5 + (4/15) * X4 + (3/15) * X3 + (2/15) * X2 + (1/15) * X1

When n = 6, EMA (x, 6) = (2/7) * X6 + (5/21) * X5 + (4/21) * X4 + (3/21) * X3 + (2/21) * X2 + (1/21) * X1

When n = 7, EMA (x, 7) = (2/8) * X7 + (6/28) * X6 + (5/28) * X5 + (4/28) * X4 + (3/28) * X3 + (2/28) * X2 + (1/28) * X1

When n = 8, EMA (x, 8) = (2/9) * X8 + (36/36) * x36 + (6/36) * X6 + (5/36) * X5 + (4/36) * X4 + (3/36) * X3 + (2/36) * X2 + (1/36) * X1

When n = 9, EMA (x, 9) = (2/10) * x9 + (8/45) * X8 + (45/45) * x45 + (6/45) * X6 + (5/45) * X5 + (4/45) * X4 + (3/45) * X3 + (2/45) * X2 + (1/45) * X1

When n = 10, EMA (x, 10) = (2/11) * x10 + (9/55) * x9 + (8/55) * X8 + (55/55) * x55 + (6/55) * X6 + (5/55) * X5 + (4/55) * X4 + (3/55) * X3 + (2/55) * X2 + (1/55) * X1

When n = 11, EMA (x, 11) = (2/12) * X11 + (10/66) * x10 + (9/66) * x9 + (8/66) * X8 + (7/66) * X7 + (6/66) * X6 + (5/66) * X5 + (4/66) * X4 + (3/66) * X3 + (2/66) * X2 + (1/66) * X1

When n = 12, EMA (x, 12) = (2/13) * X12 + (11/78) * X11 + (10/78) * x10 + (9/78) * x9 + (8/78) * X8 + (7/78) * X7 + (6/78) * X6 + (5/78) * X5 + (4/78) * X4 + (3/78) * X3 + (2/78) * X2 + (1/78) * X1

When n = 13, EMA (x, 13) = (2/14) * x13 + (12/91) * X12 + (11/91) * X11 + (10/91) * x10 + (9/91) * x9 + (8/91) * X8 + (7/91) * X7 + (6/91) * X6 + (5/91) * X5 + (4/91) * X4 + (3/91) * X3 + (2/91) * X2 + (1/91) * X1

Other functions are available cyclically.

The coefficient denominator is N * (n + 1)/2. And the sum of coefficients is always 1 at any time. When X is a constant and X values remain unchanged every day, EMA (x, n) = MA (x, n ).

The above formula can be directly used as the calculation formula.

From the above enumeration analysis, we can see that the X value closer to the time period has a higher weight, indicating that the EMA function has enhanced the weight ratio for the recent x value, more timely reflect the recent X-value fluctuations. Therefore, EMA has more reference value than Ma, and EMA is not prone to dead forks and golden forks, so it should be reflected immediately when it appears! The EMA becomes more stable after the weekly thread is processed.

Like EMA, it is defined as Y = [2 * x + (N-1) * y']/(n + 1) y' is the calculated value of the previous cycle. Such a function is a recursive function that constantly calls the values of the previous cycle for calculation. We did not use recursion to speed up the computation.

/// <Summary>
/// Contains calculation results for Ema indicator
/// </Summary>
Public class emaresult
{
Public list <double> values {Get; set ;}

/// <Summary>
/// Represents the index of input signal at which the indicator starts
/// </Summary>
Public int startindexoffset {Get; set ;}
}

Bytes -------------------------------------------------------------------------------------------------------------------------------

/// <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 ();

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
};

Return result;
}

The above implementation code comes from the http://technicalanalysis.codeplex.com/

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.