mirror of
https://github.com/mihakralj/QuanTAlib.git
synced 2026-08-22 20:48:04 +00:00
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:
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
namespace QuanTAlib;
|
||||
|
||||
public class SMA
|
||||
{
|
||||
private CircularBuffer buffer = null!;
|
||||
private int period;
|
||||
private double sum;
|
||||
public TValue Value { get; private set; }
|
||||
public bool IsHot { get; private set; }
|
||||
|
||||
public SMA(int period)
|
||||
{
|
||||
Init(period);
|
||||
}
|
||||
|
||||
public void Init(int period)
|
||||
{
|
||||
this.period = period;
|
||||
this.buffer = new CircularBuffer(period);
|
||||
this.sum = 0;
|
||||
this.IsHot = false;
|
||||
this.Value = default;
|
||||
}
|
||||
|
||||
public TValue Update(TValue input, bool IsNew = true)
|
||||
{
|
||||
if (buffer.Count == 0 || isNew)
|
||||
{
|
||||
if (buffer.Count == period)
|
||||
{
|
||||
sum -= buffer[0];
|
||||
}
|
||||
buffer.Add(input);
|
||||
sum += input.Value;
|
||||
}
|
||||
else
|
||||
{
|
||||
sum -= buffer[buffer.Count - 1];
|
||||
sum += input.Value;
|
||||
buffer[buffer.Count - 1] = input;
|
||||
}
|
||||
|
||||
double sma = sum / buffer.Count;
|
||||
Value = new TValue(input.Time, sma, isNew, IsHot);
|
||||
return Value;
|
||||
}
|
||||
|
||||
double sma = buffer.Count > 0 ? sum / buffer.Count : double.NaN;
|
||||
IsHot = buffer.Count >= period;
|
||||
Value = new TValue(input.Time, sma, IsNew, IsHot);
|
||||
return Value;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
namespace QuanTAlib;
|
||||
|
||||
public class WMA
|
||||
{
|
||||
private CircularBuffer buffer = null!;
|
||||
private CircularBuffer weights = null!;
|
||||
private int period;
|
||||
public TValue Value { get; private set; }
|
||||
public bool IsHot { get; private set; }
|
||||
|
||||
public WMA(int period)
|
||||
{
|
||||
Init(period);
|
||||
}
|
||||
|
||||
public void Init(int period)
|
||||
{
|
||||
this.period = period;
|
||||
this.buffer = new CircularBuffer(period);
|
||||
this.weights = new CircularBuffer(period);
|
||||
CalculateWeights();
|
||||
this.IsHot = false;
|
||||
this.Value = default;
|
||||
}
|
||||
|
||||
public TValue Update(TValue input, bool IsNew = true)
|
||||
{
|
||||
if (IsNew)
|
||||
{
|
||||
buffer.Add(input);
|
||||
}
|
||||
else if (buffer.Count > 0)
|
||||
{
|
||||
buffer[buffer.Count - 1] = input;
|
||||
}
|
||||
else
|
||||
{
|
||||
buffer.Add(input);
|
||||
}
|
||||
|
||||
double wma = 0;
|
||||
double totalWeights = 0;
|
||||
|
||||
for (int i = 0; i < buffer.Count; i++)
|
||||
{
|
||||
wma += buffer[i] * weights[i];
|
||||
totalWeights += weights[i];
|
||||
}
|
||||
|
||||
wma /= totalWeights;
|
||||
|
||||
IsHot = buffer.Count >= period;
|
||||
Value = new TValue(input.Time, wma, IsNew, IsHot);
|
||||
return Value;
|
||||
}
|
||||
|
||||
private void CalculateWeights()
|
||||
{
|
||||
for (int i = 1; i <= period; i++)
|
||||
{
|
||||
weights.Add(i);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user