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;
}
}
}