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
+57
View File
@@ -0,0 +1,57 @@
namespace QuanTAlib;
public class CircularBuffer
{
private double[] _buffer = null!;
private int _start;
private int _size;
public CircularBuffer(int capacity)
{
_buffer = new double[capacity];
_start = 0;
_size = 0;
}
public int Capacity => _buffer.Length;
public int Count => _size;
public void Add(double item, bool isNew)
{
if (_size == 0 || isNew)
{
// If buffer is empty or isNew is true, add new item
if (_size < Capacity)
{
_buffer[(_start + _size) % Capacity] = item;
_size++;
}
else
{
_buffer[_start] = item;
_start = (_start + 1) % Capacity;
}
}
else
{
// If isNew is false, just update the last item
_buffer[(_start + _size - 1) % Capacity] = item;
}
}
public double this[int index]
{
get
{
if (index < 0 || index >= _size)
throw new IndexOutOfRangeException();
return _buffer[(_start + index) % Capacity];
}
set
{
if (index < 0 || index >= _size)
throw new IndexOutOfRangeException();
_buffer[(_start + index) % Capacity] = value;
}
}
}
+17
View File
@@ -0,0 +1,17 @@
namespace QuanTAlib;
public readonly record struct TBar(DateTime Time, double Open, double High, double Low, double Close, double Volume, bool IsNew = true)
{
public DateTime Time { get; init; } = Time;
public double Open { get; init; } = Open;
public double High { get; init; } = High;
public double Low { get; init; } = Low;
public double Close { get; init; } = Close;
public double Volume { get; init; } = Volume;
public bool IsNew { get; init; } = IsNew;
public TBar() : this(DateTime.UtcNow, 0, 0, 0, 0, 0) { }
public TBar(double open, double high, double low, double close, double volume) : this(DateTime.UtcNow, open, high, low, close, volume) { }
public TBar((DateTime time, double open, double high, double low, double close, double volume) tuple) : this(tuple.time, tuple.open, tuple.high, tuple.low, tuple.close, tuple.volume) { }
public override string ToString() => $"[{Time:yyyy-MM-dd HH:mm:ss}: O={Open:F2}, H={High:F2}, L={Low:F2}, C={Close:F2}, V={Volume:F2}]";
}
+20
View File
@@ -0,0 +1,20 @@
namespace QuanTAlib;
public readonly record struct TValue(DateTime Time, double Value, bool IsNew = true, bool IsHot = true)
{
public DateTime Time { get; init; } = Time;
public double Value { get; init; } = Value;
public bool IsNew { get; init; } = IsNew;
public bool IsHot { get; init; } = IsHot;
public TValue() : this(DateTime.UtcNow, 0) { }
public TValue(double value) : this(DateTime.UtcNow, value) { }
public TValue((DateTime time, double value) tuple) : this(tuple.time, tuple.value) { }
public static implicit operator double(TValue tv) => tv.Value;
public static implicit operator DateTime(TValue tv) => tv.Time;
public static implicit operator TValue(double value) => new TValue(DateTime.UtcNow, value);
public override string ToString() => $"[{Time:yyyy-MM-dd HH:mm:ss}: {Value:F2}]";
}