refactor structs to records, add new classes for financial calculations, implement EMA and SMA with circular buffer. Generate random financial data using GBM model. Also, test the SMA calculation with sample data.

This commit is contained in:
Miha Kralj
2024-07-28 21:26:44 -07:00
parent ef534393db
commit 3455baaf6c
95 changed files with 8661 additions and 7012 deletions
+42
View File
@@ -0,0 +1,42 @@
namespace QuanTAlib;
public class EMA
{
private double lastEma, lastEmaCandidate, k;
private int period, i;
public TValue Value { get; private set; }
public bool IsHot { get; private set; }
public EMA(int period)
{
Init(period);
}
public void Init(int period)
{
this.period = period;
this.k = 2.0 / (period + 1);
this.lastEma = this.lastEmaCandidate = double.NaN;
this.i = 0;
}
public TValue Update(TValue input, bool IsNew = true)
{
double ema;
if (double.IsNaN(lastEma)) { lastEma = input.Value; }
if (IsNew)
{
lastEma = lastEmaCandidate;
i++;
}
double kk = (i < period) ? (2.0 / (i + 1)) : k;
ema = lastEma + kk * (input.Value - lastEma);
lastEmaCandidate = ema;
IsHot = i >= period;
Value = new TValue(input.Time, ema, IsNew, IsHot);
return Value;
}
}