Joshua
Joshua

Reputation: 3515

Calculate Exponential Moving Average on a Queue in C#

I have a simple class for calculating the moving average of values I add to it. I use it like this:

MovingAverage ma = new MovingAverage();
ma.push(value1);
ma.push(value2);
... 
Console.Writeline(average.Average);

//the class
public class MovingAverage
{
    public int Period = 5;
    private Queue<double> Quotes = new Queue<double>();

    public void Push(double quote)
    {
        if (Quotes.Count == Period)
            Quotes.Dequeue();
        Quotes.Enqueue(quote);

    }
    public void Clear()
    {
        Quotes.Clear();
    }
    public double Average { get { if (Quotes.Count == 0) return 0; return Quotes.Average(); } }
    public double ExponentialMovingAverage
    {
        get
        {
            ???
        }
    }
}

I would like to extend this class to also return the ExponentialMovingAverage. How would you write return the Exponential Average of the Queued items in Quotes?

I realize you will need to add an Alpha property to the class but I'm not sure how to complete the math for the calculation.

Upvotes: 8

Views: 20191

Answers (4)

CupcakeThief
CupcakeThief

Reputation: 21

I think there's a small tweak required to @Ani's answer. The initial value would get set to "alpha * nextQuote" instead of just "nextQuote". Easiest fix is to set the initial seed value to match the first record, then the first iteration becomes alpha * S1 + (1 - alpha) * S1:

return Quotes
  .DefaultIfEmpty()
  .Aggregate(Quotes.FirstOrDefault() ?? 0.0,
(ema, nextQuote) => alpha * nextQuote + (1 - alpha) * ema);

Upvotes: 0

Drew Noakes
Drew Noakes

Reputation: 311255

Here's a minimal version of @MattWolf's answer with a slightly different API, and using C# 7.

public sealed class FloatExponentialMovingAverageCalculator
{
    private readonly float _alpha;
    private float _lastAverage = float.NaN;

    public FloatExponentialMovingAverageCalculator(int lookBack) => _alpha = 2f / (lookBack + 1);

    public float NextValue(float value) => _lastAverage = float.IsNaN(_lastAverage)
        ? value
        : (value - _lastAverage)*_alpha + _lastAverage;
}

Upvotes: 7

Matt
Matt

Reputation: 7254

Do do not need a queue for an Exponential Moving Average because you only need to keep track of the previous EMA.

public class ExponentialMovingAverageIndicator
{
    private bool _isInitialized;
    private readonly int _lookback;
    private readonly double _weightingMultiplier;
    private double _previousAverage;

    public double Average { get; private set; }
    public double Slope { get; private set; }

    public ExponentialMovingAverageIndicator(int lookback)
    {
        _lookback = lookback;
        _weightingMultiplier = 2.0/(lookback + 1);
    }

    public void AddDataPoint(double dataPoint)
    {
        if (!_isInitialized)
        {
            Average = dataPoint;
            Slope = 0;
            _previousAverage = Average;
            _isInitialized = true;
            return;
        }

        Average = ((dataPoint - _previousAverage)*_weightingMultiplier) + _previousAverage;
        Slope = Average - _previousAverage;

        //update previous average
        _previousAverage = Average;
    }
}

Upvotes: 8

Ani
Ani

Reputation: 113452

How about with LINQ:

return Quotes.DefaultIfEmpty()
             .Aggregate((ema, nextQuote) => alpha * nextQuote + (1 - alpha) * ema);

I would point out that for real-time financial data, this is highly inefficient. A much better way would be to cache the previous EMA value and update it on a new quote with the above (constant-time) recurrence-formula.

Upvotes: 19

Related Questions