Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Math.Net Exponential Moving Average

Tags:

math.net

I'm using simple moving average in Math.Net, but now that I also need to calculate EMA (exponential moving average) or any kind of weighted moving average, I don't find it in the library.

I looked over all methods under MathNet.Numerics.Statistics and beyond, but didn't find anything similar.

Is it missing in library or I need to reference some additional package?

like image 810
Alex Michel Avatar asked Oct 19 '25 09:10

Alex Michel


2 Answers

I don't see any EMA in MathNet.Numerics, however it's trivial to program. The routine below is based on the definition at Investopedia.

public double[] EMA(double[] x, int N)
{
    // x is the input series            
    // N is the notional age of the data used
    // k is the smoothing constant

    double k = 2.0 / (N + 1);
    double[] y = new double[x.Length];
    y[0] = x[0];
    for (int i = 1; i < x.Length; i++) y[i] = k * x[i] + (1 - k) * y[i - 1];

    return y;
}
like image 112
phv3773 Avatar answered Oct 22 '25 03:10

phv3773


Occasionally I found this package: https://daveskender.github.io/Stock.Indicators/docs/INDICATORS.html It targets to the latest .NET framework and has very detailed documents.

like image 38
Mason Zhang Avatar answered Oct 22 '25 03:10

Mason Zhang