Skip to content

Commit

Permalink
add MAD, MSE, MAPE to SMA (#45)
Browse files Browse the repository at this point in the history
* add MAD, MSE, MAPE to SMA
  • Loading branch information
DaveSkender authored Jun 28, 2020
1 parent dde05ef commit dd1f2bb
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 7 deletions.
3 changes: 3 additions & 0 deletions Indicators/Sma/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ The first `N-1` periods will have `null` values since there's not enough data to
| `Index` | int | Sequence of dates
| `Date` | DateTime | Date
| `Sma` | decimal | Simple moving average for `N` lookback periods
| `Mad` | decimal | Mean absolute deviation
| `Mse` | decimal | Mean square error
| `Mape` | decimal | Mean absolute percentage error

## Example

Expand Down
5 changes: 4 additions & 1 deletion Indicators/Sma/Sma.Models.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@

public class SmaResult : ResultBase
{
public decimal? Sma { get; set; }
public decimal? Sma { get; set; } // simple moving average
public decimal? Mad { get; set; } // mean absolute deviation
public decimal? Mse { get; set; } // mean square error
public decimal? Mape { get; set; } // mean absolute percentage error
}

}
31 changes: 25 additions & 6 deletions Indicators/Sma/Sma.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.Linq;

namespace Skender.Stock.Indicators
Expand Down Expand Up @@ -30,11 +31,29 @@ public static IEnumerable<SmaResult> GetSma(IEnumerable<Quote> history, int look

if (h.Index >= lookbackPeriod)
{
IEnumerable<decimal> period = history
.Where(x => x.Index <= h.Index && x.Index > (h.Index - lookbackPeriod))
.Select(x => x.Close);

result.Sma = period.Average();
IEnumerable<Quote> period = history
.Where(x => x.Index <= h.Index && x.Index > (h.Index - lookbackPeriod));

// simple moving average
result.Sma = period
.Select(x => x.Close)
.Average();

// mean absolute deviation
result.Mad = period
.Select(x => Math.Abs(x.Close - (decimal)result.Sma))
.Average();

// mean squared error
result.Mse = period
.Select(x => (x.Close - (decimal)result.Sma) * (x.Close - (decimal)result.Sma))
.Average();

// mean absolute percent error
result.Mape = period
.Where(x => x.Close != 0)
.Select(x => Math.Abs(x.Close - (decimal)result.Sma) / x.Close)
.Average();
}

results.Add(result);
Expand Down
3 changes: 3 additions & 0 deletions IndicatorsTests/Test.Sma.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ public void GetSmaTest()
// sample value
SmaResult sma = results.Where(x => x.Date == DateTime.Parse("12/31/2018")).FirstOrDefault();
Assert.AreEqual((decimal)251.86, sma.Sma);
Assert.AreEqual((decimal)9.45, sma.Mad);
Assert.AreEqual((double)119.2510, Math.Round((double)sma.Mse, 4));
Assert.AreEqual((double)0.037637, Math.Round((double)sma.Mape, 6));
}


Expand Down

0 comments on commit dd1f2bb

Please sign in to comment.