mirror of
https://github.com/mihakralj/QuanTAlib.git
synced 2026-08-19 02:58:05 +00:00
clean code fixes
This commit is contained in:
+145
-156
@@ -1,162 +1,151 @@
|
||||
using System;
|
||||
namespace QuanTAlib;
|
||||
|
||||
namespace QuanTAlib
|
||||
public class Afirma : AbstractBase
|
||||
{
|
||||
|
||||
public class Afirma : AbstractBase
|
||||
public enum WindowType
|
||||
{
|
||||
|
||||
public enum WindowType
|
||||
{
|
||||
Rectangular,
|
||||
Hanning1,
|
||||
Hanning2,
|
||||
Blackman,
|
||||
BlackmanHarris
|
||||
}
|
||||
|
||||
private readonly int Periods;
|
||||
private readonly int Taps;
|
||||
private readonly WindowType Window;
|
||||
private readonly CircularBuffer _buffer;
|
||||
private readonly double[] _weights;
|
||||
private readonly double _wsum;
|
||||
private readonly double[] _armaBuffer;
|
||||
private readonly int _n;
|
||||
private readonly double _sx2, _sx3, _sx4, _sx5, _sx6, _den;
|
||||
|
||||
public Afirma(int periods, int taps, WindowType window)
|
||||
{
|
||||
if (periods < 1)
|
||||
{
|
||||
throw new ArgumentOutOfRangeException(nameof(periods), "Periods must be greater than or equal to 1.");
|
||||
}
|
||||
if (taps < 1)
|
||||
{
|
||||
throw new ArgumentOutOfRangeException(nameof(taps), "Taps must be greater than or equal to 1.");
|
||||
}
|
||||
Periods = periods;
|
||||
Taps = taps;
|
||||
Window = window;
|
||||
WarmupPeriod = taps;
|
||||
_buffer = new CircularBuffer(taps);
|
||||
_weights = new double[taps];
|
||||
_wsum = CalculateWeights();
|
||||
_armaBuffer = new double[taps];
|
||||
_n = (Taps - 1) / 2;
|
||||
|
||||
// Calculate least squares coefficients in the constructor
|
||||
_sx2 = (2 * _n + 1) / 3.0;
|
||||
_sx3 = _n * (_n + 1) / 2.0;
|
||||
_sx4 = _sx2 * (3 * _n * _n + 3 * _n - 1) / 5.0;
|
||||
_sx5 = _sx3 * (2 * _n * _n + 2 * _n - 1) / 3.0;
|
||||
_sx6 = _sx2 * (3 * Math.Pow(_n, 3) * (_n + 2) - 3 * _n + 1) / 7.0;
|
||||
_den = _sx6 * _sx4 / _sx5 - _sx5;
|
||||
|
||||
Name = "Afirma";
|
||||
Init();
|
||||
}
|
||||
|
||||
public Afirma(object source, int periods, int taps, WindowType window) : this(periods, taps, window)
|
||||
{
|
||||
var pubEvent = source.GetType().GetEvent("Pub");
|
||||
pubEvent?.AddEventHandler(source, new ValueSignal(Sub));
|
||||
}
|
||||
|
||||
protected override void ManageState(bool isNew)
|
||||
{
|
||||
if (isNew)
|
||||
{
|
||||
_lastValidValue = Input.Value;
|
||||
_index++;
|
||||
}
|
||||
}
|
||||
|
||||
protected override double Calculation()
|
||||
{
|
||||
ManageState(IsNew);
|
||||
_buffer.Add(Input.Value, Input.IsNew);
|
||||
|
||||
if (_index >= Taps)
|
||||
{
|
||||
double a0 = _buffer[_n];
|
||||
double a1 = _buffer[_n] - _buffer[_n + 1];
|
||||
double sx2y = 0.0;
|
||||
double sx3y = 0.0;
|
||||
|
||||
for (int i = 0; i <= _n; i++)
|
||||
{
|
||||
sx2y += i * i * _buffer[_n - i];
|
||||
sx3y += i * i * i * _buffer[_n - i];
|
||||
}
|
||||
|
||||
sx2y = 2.0 * sx2y / _n / (_n + 1);
|
||||
sx3y = 2.0 * sx3y / _n / (_n + 1);
|
||||
double p = sx2y - a0 * _sx2 - a1 * _sx3;
|
||||
double q = sx3y - a0 * _sx3 - a1 * _sx4;
|
||||
double a2 = (p * _sx6 / _sx5 - q) / _den;
|
||||
double a3 = (q * _sx4 / _sx5 - p) / _den;
|
||||
|
||||
for (int k = 0; k <= _n; k++)
|
||||
{
|
||||
_armaBuffer[_n - k] = a0 + k * a1 + k * k * a2 + k * k * k * a3;
|
||||
}
|
||||
}
|
||||
|
||||
double result = 0.0;
|
||||
for (int k = 0; k < Taps; k++)
|
||||
{
|
||||
result += _buffer[k] * _weights[k] / _wsum;
|
||||
}
|
||||
|
||||
IsHot = _index >= WarmupPeriod;
|
||||
return result;
|
||||
}
|
||||
|
||||
private double CalculateWeights()
|
||||
{
|
||||
double wsum = 0.0;
|
||||
double centerTap = (Taps - 1) / 2.0;
|
||||
for (int k = 0; k < Taps; k++)
|
||||
{
|
||||
double windowWeight;
|
||||
switch (Window)
|
||||
{
|
||||
case WindowType.Rectangular:
|
||||
windowWeight = 1.0;
|
||||
break;
|
||||
case WindowType.Hanning1:
|
||||
windowWeight = 0.50 - 0.50 * Math.Cos(2.0 * Math.PI * k / (Taps - 1));
|
||||
break;
|
||||
case WindowType.Hanning2:
|
||||
windowWeight = 0.54 - 0.46 * Math.Cos(2.0 * Math.PI * k / (Taps - 1));
|
||||
break;
|
||||
case WindowType.Blackman:
|
||||
windowWeight = 0.42 - 0.50 * Math.Cos(2.0 * Math.PI * k / (Taps - 1)) + 0.08 * Math.Cos(4.0 * Math.PI * k / (Taps - 1));
|
||||
break;
|
||||
case WindowType.BlackmanHarris:
|
||||
windowWeight = 0.35875 - 0.48829 * Math.Cos(2.0 * Math.PI * k / (Taps - 1)) + 0.14128 * Math.Cos(4.0 * Math.PI * k / (Taps - 1)) - 0.01168 * Math.Cos(6.0 * Math.PI * k / (Taps - 1));
|
||||
break;
|
||||
default:
|
||||
windowWeight = 1.0;
|
||||
break;
|
||||
}
|
||||
|
||||
double sincWeight;
|
||||
if (Math.Abs(k - centerTap) < 1e-10)
|
||||
{
|
||||
sincWeight = 1.0;
|
||||
}
|
||||
else
|
||||
{
|
||||
sincWeight = Math.Sin(Math.PI * (k - centerTap) / Periods) / (Math.PI * (k - centerTap) / Periods);
|
||||
}
|
||||
|
||||
_weights[k] = windowWeight * sincWeight;
|
||||
wsum += _weights[k];
|
||||
}
|
||||
return wsum;
|
||||
}
|
||||
|
||||
Rectangular,
|
||||
Hanning1,
|
||||
Hanning2,
|
||||
Blackman,
|
||||
BlackmanHarris
|
||||
}
|
||||
|
||||
private readonly int Periods;
|
||||
private readonly int Taps;
|
||||
private readonly WindowType Window;
|
||||
private readonly CircularBuffer _buffer;
|
||||
private readonly double[] _weights;
|
||||
private readonly double _wsum;
|
||||
private readonly double[] _armaBuffer;
|
||||
private readonly int _n;
|
||||
private readonly double _sx2, _sx3, _sx4, _sx5, _sx6, _den;
|
||||
|
||||
public Afirma(int periods, int taps, WindowType window)
|
||||
{
|
||||
if (periods < 1)
|
||||
{
|
||||
throw new ArgumentOutOfRangeException(nameof(periods), "Periods must be greater than or equal to 1.");
|
||||
}
|
||||
if (taps < 1)
|
||||
{
|
||||
throw new ArgumentOutOfRangeException(nameof(taps), "Taps must be greater than or equal to 1.");
|
||||
}
|
||||
Periods = periods;
|
||||
Taps = taps;
|
||||
Window = window;
|
||||
WarmupPeriod = taps;
|
||||
_buffer = new CircularBuffer(taps);
|
||||
_weights = new double[taps];
|
||||
_wsum = CalculateWeights();
|
||||
_armaBuffer = new double[taps];
|
||||
_n = (Taps - 1) / 2;
|
||||
|
||||
// Calculate least squares coefficients in the constructor
|
||||
_sx2 = (2 * _n + 1) / 3.0;
|
||||
_sx3 = _n * (_n + 1) / 2.0;
|
||||
_sx4 = _sx2 * (3 * _n * _n + 3 * _n - 1) / 5.0;
|
||||
_sx5 = _sx3 * (2 * _n * _n + 2 * _n - 1) / 3.0;
|
||||
_sx6 = _sx2 * (3 * Math.Pow(_n, 3) * (_n + 2) - 3 * _n + 1) / 7.0;
|
||||
_den = _sx6 * _sx4 / _sx5 - _sx5;
|
||||
|
||||
Name = "Afirma";
|
||||
Init();
|
||||
}
|
||||
|
||||
public Afirma(object source, int periods, int taps, WindowType window) : this(periods, taps, window)
|
||||
{
|
||||
var pubEvent = source.GetType().GetEvent("Pub");
|
||||
pubEvent?.AddEventHandler(source, new ValueSignal(Sub));
|
||||
}
|
||||
|
||||
protected override void ManageState(bool isNew)
|
||||
{
|
||||
if (isNew)
|
||||
{
|
||||
_lastValidValue = Input.Value;
|
||||
_index++;
|
||||
}
|
||||
}
|
||||
|
||||
protected override double Calculation()
|
||||
{
|
||||
ManageState(IsNew);
|
||||
_buffer.Add(Input.Value, Input.IsNew);
|
||||
|
||||
if (_index >= Taps)
|
||||
{
|
||||
double a0 = _buffer[_n];
|
||||
double a1 = _buffer[_n] - _buffer[_n + 1];
|
||||
double sx2y = 0.0;
|
||||
double sx3y = 0.0;
|
||||
|
||||
for (int i = 0; i <= _n; i++)
|
||||
{
|
||||
sx2y += i * i * _buffer[_n - i];
|
||||
sx3y += i * i * i * _buffer[_n - i];
|
||||
}
|
||||
|
||||
sx2y = 2.0 * sx2y / _n / (_n + 1);
|
||||
sx3y = 2.0 * sx3y / _n / (_n + 1);
|
||||
double p = sx2y - a0 * _sx2 - a1 * _sx3;
|
||||
double q = sx3y - a0 * _sx3 - a1 * _sx4;
|
||||
double a2 = (p * _sx6 / _sx5 - q) / _den;
|
||||
double a3 = (q * _sx4 / _sx5 - p) / _den;
|
||||
|
||||
for (int k = 0; k <= _n; k++)
|
||||
{
|
||||
_armaBuffer[_n - k] = a0 + k * a1 + k * k * a2 + k * k * k * a3;
|
||||
}
|
||||
}
|
||||
|
||||
double result = 0.0;
|
||||
for (int k = 0; k < Taps; k++)
|
||||
{
|
||||
result += _buffer[k] * _weights[k] / _wsum;
|
||||
}
|
||||
|
||||
IsHot = _index >= WarmupPeriod;
|
||||
return result;
|
||||
}
|
||||
|
||||
private double CalculateWeights()
|
||||
{
|
||||
double wsum = 0.0;
|
||||
double centerTap = (Taps - 1) / 2.0;
|
||||
for (int k = 0; k < Taps; k++)
|
||||
{
|
||||
double windowWeight;
|
||||
switch (Window)
|
||||
{
|
||||
case WindowType.Rectangular:
|
||||
windowWeight = 1.0;
|
||||
break;
|
||||
case WindowType.Hanning1:
|
||||
windowWeight = 0.50 - 0.50 * Math.Cos(2.0 * Math.PI * k / (Taps - 1));
|
||||
break;
|
||||
case WindowType.Hanning2:
|
||||
windowWeight = 0.54 - 0.46 * Math.Cos(2.0 * Math.PI * k / (Taps - 1));
|
||||
break;
|
||||
case WindowType.Blackman:
|
||||
windowWeight = 0.42 - 0.50 * Math.Cos(2.0 * Math.PI * k / (Taps - 1)) + 0.08 * Math.Cos(4.0 * Math.PI * k / (Taps - 1));
|
||||
break;
|
||||
case WindowType.BlackmanHarris:
|
||||
windowWeight = 0.35875 - 0.48829 * Math.Cos(2.0 * Math.PI * k / (Taps - 1)) + 0.14128 * Math.Cos(4.0 * Math.PI * k / (Taps - 1)) - 0.01168 * Math.Cos(6.0 * Math.PI * k / (Taps - 1));
|
||||
break;
|
||||
default:
|
||||
windowWeight = 1.0;
|
||||
break;
|
||||
}
|
||||
|
||||
double sincWeight;
|
||||
sincWeight = Math.Abs(k - centerTap) < 1e-10 ? 1.0 : Math.Sin(Math.PI * (k - centerTap) / Periods) / (Math.PI * (k - centerTap) / Periods);
|
||||
|
||||
_weights[k] = windowWeight * sincWeight;
|
||||
wsum += _weights[k];
|
||||
}
|
||||
return wsum;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -25,7 +25,7 @@ public class Alma : AbstractBase
|
||||
/// <param name="offset">Controls the smoothness and high-frequency filtering. Default is 0.85.</param>
|
||||
/// <param name="sigma">Controls the shape of the Gaussian distribution. Default is 6.</param>
|
||||
/// <exception cref="ArgumentException">Thrown when period is less than 1.</exception>
|
||||
public Alma(int period, double offset = 0.85, double sigma = 6) : base()
|
||||
public Alma(int period, double offset = 0.85, double sigma = 6)
|
||||
{
|
||||
if (period < 1)
|
||||
{
|
||||
|
||||
@@ -4,8 +4,8 @@ public class Convolution : AbstractBase
|
||||
{
|
||||
private readonly double[] _kernel;
|
||||
private readonly int _kernelSize;
|
||||
private CircularBuffer _buffer;
|
||||
private double[] _normalizedKernel;
|
||||
private readonly CircularBuffer _buffer;
|
||||
private readonly double[] _normalizedKernel;
|
||||
|
||||
public Convolution(double[] kernel)
|
||||
{
|
||||
|
||||
@@ -28,7 +28,7 @@ public class Dema : AbstractBase
|
||||
private double _lastEma2, _p_lastEma2;
|
||||
private double _k, _e, _p_e;
|
||||
|
||||
public Dema(int period) : base()
|
||||
public Dema(int period)
|
||||
{
|
||||
if (period < 1)
|
||||
{
|
||||
|
||||
+6
-4
@@ -27,10 +27,12 @@ public class Ema : AbstractBase
|
||||
private readonly int _period;
|
||||
private CircularBuffer _sma;
|
||||
private double _lastEma, _p_lastEma;
|
||||
private double _k, _e, _p_e;
|
||||
private bool _isInit, _p_isInit, _useSma;
|
||||
private double _e, _p_e;
|
||||
private readonly double _k;
|
||||
private bool _isInit, _p_isInit;
|
||||
private readonly bool _useSma;
|
||||
|
||||
public Ema(int period, bool useSma = true) : base()
|
||||
public Ema(int period, bool useSma = true)
|
||||
{
|
||||
if (period < 1)
|
||||
{
|
||||
@@ -45,7 +47,7 @@ public class Ema : AbstractBase
|
||||
Init();
|
||||
}
|
||||
|
||||
public Ema(double alpha) : base()
|
||||
public Ema(double alpha)
|
||||
{
|
||||
_k = alpha;
|
||||
_useSma = false;
|
||||
|
||||
+75
-76
@@ -1,99 +1,98 @@
|
||||
using System;
|
||||
|
||||
namespace QuanTAlib
|
||||
namespace QuanTAlib;
|
||||
|
||||
public class Frama : AbstractBase
|
||||
{
|
||||
public class Frama : AbstractBase
|
||||
private readonly int _period;
|
||||
private readonly double _fc;
|
||||
private readonly CircularBuffer _buffer;
|
||||
private double _lastFrama;
|
||||
private double _prevLastFrama;
|
||||
|
||||
public Frama(int period, double fc = 0.5)
|
||||
{
|
||||
private readonly int _period;
|
||||
private readonly double _fc;
|
||||
private CircularBuffer _buffer;
|
||||
private double _lastFrama;
|
||||
private double _prevLastFrama;
|
||||
if (period < 2)
|
||||
throw new ArgumentException("Period must be at least 2", nameof(period));
|
||||
|
||||
public Frama(int period, double fc = 0.5) : base()
|
||||
_period = period;
|
||||
_fc = fc;
|
||||
_buffer = new CircularBuffer(period);
|
||||
WarmupPeriod = period;
|
||||
}
|
||||
|
||||
public override void Init()
|
||||
{
|
||||
base.Init();
|
||||
_buffer.Clear();
|
||||
_lastFrama = 0;
|
||||
_prevLastFrama = 0;
|
||||
}
|
||||
|
||||
protected override void ManageState(bool isNew)
|
||||
{
|
||||
if (isNew)
|
||||
{
|
||||
if (period < 2)
|
||||
throw new ArgumentException("Period must be at least 2", nameof(period));
|
||||
_prevLastFrama = _lastFrama;
|
||||
_index++;
|
||||
}
|
||||
else
|
||||
{
|
||||
_lastFrama = _prevLastFrama;
|
||||
}
|
||||
}
|
||||
|
||||
_period = period;
|
||||
_fc = fc;
|
||||
_buffer = new CircularBuffer(period);
|
||||
WarmupPeriod = period;
|
||||
protected override double Calculation()
|
||||
{
|
||||
ManageState(Input.IsNew);
|
||||
|
||||
_buffer.Add(Input.Value, Input.IsNew);
|
||||
|
||||
if (_buffer.Count < _period)
|
||||
{
|
||||
_lastFrama = _buffer.Average();
|
||||
return _lastFrama;
|
||||
}
|
||||
|
||||
public override void Init()
|
||||
{
|
||||
base.Init();
|
||||
_buffer.Clear();
|
||||
_lastFrama = 0;
|
||||
_prevLastFrama = 0;
|
||||
}
|
||||
int half = _period / 2;
|
||||
double hh = double.MinValue, ll = double.MaxValue;
|
||||
double hh1 = double.MinValue, ll1 = double.MaxValue;
|
||||
double hh2 = double.MinValue, ll2 = double.MaxValue;
|
||||
|
||||
protected override void ManageState(bool isNew)
|
||||
for (int i = 0; i < _period; i++)
|
||||
{
|
||||
if (isNew)
|
||||
double price = _buffer[i];
|
||||
hh = Math.Max(hh, price);
|
||||
ll = Math.Min(ll, price);
|
||||
|
||||
if (i < half)
|
||||
{
|
||||
_prevLastFrama = _lastFrama;
|
||||
_index++;
|
||||
hh1 = Math.Max(hh1, price);
|
||||
ll1 = Math.Min(ll1, price);
|
||||
}
|
||||
else
|
||||
{
|
||||
_lastFrama = _prevLastFrama;
|
||||
hh2 = Math.Max(hh2, price);
|
||||
ll2 = Math.Min(ll2, price);
|
||||
}
|
||||
}
|
||||
|
||||
protected override double Calculation()
|
||||
{
|
||||
ManageState(Input.IsNew);
|
||||
|
||||
_buffer.Add(Input.Value, Input.IsNew);
|
||||
double n1 = (hh - ll) / _period;
|
||||
double n2 = (hh1 - ll1 + hh2 - ll2) / (_period / 2);
|
||||
|
||||
if (_buffer.Count < _period)
|
||||
{
|
||||
_lastFrama = _buffer.Average();
|
||||
return _lastFrama;
|
||||
}
|
||||
double d = (Math.Log(n2 + double.Epsilon) - Math.Log(n1 + double.Epsilon)) / Math.Log(2);
|
||||
|
||||
int half = _period / 2;
|
||||
double hh = double.MinValue, ll = double.MaxValue;
|
||||
double hh1 = double.MinValue, ll1 = double.MaxValue;
|
||||
double hh2 = double.MinValue, ll2 = double.MaxValue;
|
||||
double alpha = Math.Exp(-4.6 * (d - 1));
|
||||
alpha = Math.Max(Math.Min(alpha, 1), 0.01); // Ensure alpha is between 0.01 and 1
|
||||
|
||||
for (int i = 0; i < _period; i++)
|
||||
{
|
||||
double price = _buffer[i];
|
||||
hh = Math.Max(hh, price);
|
||||
ll = Math.Min(ll, price);
|
||||
_lastFrama = alpha * (Input.Value - _lastFrama) + _lastFrama;
|
||||
|
||||
if (i < half)
|
||||
{
|
||||
hh1 = Math.Max(hh1, price);
|
||||
ll1 = Math.Min(ll1, price);
|
||||
}
|
||||
else
|
||||
{
|
||||
hh2 = Math.Max(hh2, price);
|
||||
ll2 = Math.Min(ll2, price);
|
||||
}
|
||||
}
|
||||
|
||||
double n1 = (hh - ll) / _period;
|
||||
double n2 = (hh1 - ll1 + hh2 - ll2) / (_period / 2);
|
||||
|
||||
double d = (Math.Log(n2 + double.Epsilon) - Math.Log(n1 + double.Epsilon)) / Math.Log(2);
|
||||
|
||||
double alpha = Math.Exp(-4.6 * (d - 1));
|
||||
alpha = Math.Max(Math.Min(alpha, 1), 0.01); // Ensure alpha is between 0.01 and 1
|
||||
|
||||
_lastFrama = alpha * (Input.Value - _lastFrama) + _lastFrama;
|
||||
|
||||
IsHot = _index >= WarmupPeriod;
|
||||
return _lastFrama;
|
||||
}
|
||||
|
||||
protected override double GetLastValid()
|
||||
{
|
||||
return _lastFrama;
|
||||
}
|
||||
IsHot = _index >= WarmupPeriod;
|
||||
return _lastFrama;
|
||||
}
|
||||
}
|
||||
|
||||
protected override double GetLastValid()
|
||||
{
|
||||
return _lastFrama;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
//not working yet
|
||||
//TODO consistency test
|
||||
|
||||
using QuanTAlib;
|
||||
namespace QuanTAlib;
|
||||
|
||||
public class Htit : AbstractBase
|
||||
{
|
||||
@@ -21,7 +21,7 @@ public class Htit : AbstractBase
|
||||
private double _lastPd = 0;
|
||||
private double _p_lastPd = 0;
|
||||
|
||||
public Htit() : base()
|
||||
public Htit()
|
||||
{
|
||||
Name = "Htit";
|
||||
WarmupPeriod = 12;
|
||||
@@ -138,9 +138,7 @@ public class Htit : AbstractBase
|
||||
{
|
||||
return ((4 * _itBuffer[0]) + (3 * _itBuffer[1]) + (2 * _itBuffer[2]) + _itBuffer[3]) / 10;
|
||||
}
|
||||
else
|
||||
{
|
||||
return pr;
|
||||
}
|
||||
|
||||
return pr;
|
||||
}
|
||||
}
|
||||
@@ -15,7 +15,7 @@ public class Hwma : AbstractBase
|
||||
{
|
||||
}
|
||||
|
||||
public Hwma(int period, double nA, double nB, double nC) : base()
|
||||
public Hwma(int period, double nA, double nB, double nC)
|
||||
{
|
||||
if (period < 1)
|
||||
{
|
||||
|
||||
+6
-6
@@ -1,20 +1,20 @@
|
||||
using QuanTAlib;
|
||||
namespace QuanTAlib;
|
||||
//TODO consistency test
|
||||
public class Jma : AbstractBase
|
||||
{
|
||||
public readonly int Period;
|
||||
private readonly double _phase;
|
||||
private readonly int _vshort, _vlong;
|
||||
private CircularBuffer _values;
|
||||
private CircularBuffer _voltyShort;
|
||||
private CircularBuffer _vsumBuff;
|
||||
private CircularBuffer _avoltyBuff;
|
||||
private readonly CircularBuffer _values;
|
||||
private readonly CircularBuffer _voltyShort;
|
||||
private readonly CircularBuffer _vsumBuff;
|
||||
private readonly CircularBuffer _avoltyBuff;
|
||||
|
||||
private double _beta, _len1, _pow1;
|
||||
private double _upperBand, _lowerBand, _prevMa1, _prevDet0, _prevDet1, _prevJma;
|
||||
private double _p_UpperBand, _p_LowerBand, _p_prevMa1, _p_prevDet0, _p_prevDet1, _p_prevJma;
|
||||
|
||||
public Jma(int period, double phase = 0, int vshort = 10) : base()
|
||||
public Jma(int period, double phase = 0, int vshort = 10)
|
||||
{
|
||||
if (period < 1)
|
||||
{
|
||||
|
||||
@@ -1,5 +1,3 @@
|
||||
using System;
|
||||
|
||||
namespace QuanTAlib;
|
||||
|
||||
public class Kama : AbstractBase
|
||||
@@ -9,7 +7,7 @@ public class Kama : AbstractBase
|
||||
private CircularBuffer? _buffer;
|
||||
private double _lastKama, _p_lastKama;
|
||||
|
||||
public Kama(int period, int fast = 2, int slow = 30) : base()
|
||||
public Kama(int period, int fast = 2, int slow = 30)
|
||||
{
|
||||
if (period < 1)
|
||||
{
|
||||
|
||||
@@ -10,7 +10,7 @@ public class Ltma : AbstractBase
|
||||
|
||||
public double Gamma => _gamma;
|
||||
|
||||
public Ltma(double gamma = 0.1) : base()
|
||||
public Ltma(double gamma = 0.1)
|
||||
{
|
||||
if (gamma < 0 || gamma > 1)
|
||||
throw new ArgumentOutOfRangeException(nameof(gamma), "Gamma must be between 0 and 1.");
|
||||
|
||||
@@ -8,12 +8,13 @@ public class Maaf : AbstractBase
|
||||
{
|
||||
private readonly CircularBuffer _priceBuffer;
|
||||
private readonly CircularBuffer _smoothBuffer;
|
||||
private double _prevFilter, _prevValue2, _threshold;
|
||||
private double _prevFilter, _prevValue2;
|
||||
private readonly double _threshold;
|
||||
private double _p_prevFilter, _p_prevValue2;
|
||||
|
||||
private readonly int _period;
|
||||
|
||||
public Maaf(int Period = 39, double Threshold = 0.002) : base()
|
||||
public Maaf(int Period = 39, double Threshold = 0.002)
|
||||
{
|
||||
_period = Period;
|
||||
_threshold = Threshold;
|
||||
|
||||
@@ -1,17 +1,16 @@
|
||||
using QuanTAlib;
|
||||
using System;
|
||||
namespace QuanTAlib;
|
||||
|
||||
public class Mama : AbstractBase
|
||||
{
|
||||
private readonly double _fastLimit, _slowLimit;
|
||||
private CircularBuffer _pr, _sm, _dt, _i1, _q1, _i2, _q2, _re, _im, _pd, _ph;
|
||||
private readonly CircularBuffer _pr, _sm, _dt, _i1, _q1, _i2, _q2, _re, _im, _pd, _ph;
|
||||
private double _mama, _fama;
|
||||
private double _prevMama, _prevFama, _sumPr;
|
||||
private double _p_prevMama, _p_prevFama, _p_sumPr;
|
||||
|
||||
public TValue Fama { get; private set; }
|
||||
|
||||
public Mama(double fastLimit = 0.5, double slowLimit = 0.05) : base()
|
||||
public Mama(double fastLimit = 0.5, double slowLimit = 0.05)
|
||||
{
|
||||
Fama = new TValue();
|
||||
Name = $"Mama({_fastLimit:F2}, {_slowLimit:F2})";
|
||||
|
||||
@@ -5,7 +5,7 @@ public class Mgdi : AbstractBase
|
||||
private readonly int _period;
|
||||
private readonly double _kFactor;
|
||||
private double _prevMd, _p_prevMd;
|
||||
public Mgdi(int period, double kFactor = 0.6) : base()
|
||||
public Mgdi(int period, double kFactor = 0.6)
|
||||
{
|
||||
if (period <= 0)
|
||||
{
|
||||
|
||||
+65
-69
@@ -1,78 +1,74 @@
|
||||
using System;
|
||||
using System.Linq;
|
||||
namespace QuanTAlib;
|
||||
|
||||
namespace QuanTAlib
|
||||
public class Mma : AbstractBase
|
||||
{
|
||||
public class Mma : AbstractBase
|
||||
private readonly int _period;
|
||||
private readonly CircularBuffer _buffer;
|
||||
private double _lastMma;
|
||||
|
||||
public Mma(int period)
|
||||
{
|
||||
private readonly int _period;
|
||||
private readonly CircularBuffer _buffer;
|
||||
private double _lastMma;
|
||||
|
||||
public Mma(int period) : base()
|
||||
if (period < 2)
|
||||
{
|
||||
if (period < 2)
|
||||
{
|
||||
throw new ArgumentOutOfRangeException(nameof(period), "Period must be greater than or equal to 2.");
|
||||
}
|
||||
_period = period;
|
||||
_buffer = new CircularBuffer(period);
|
||||
Name = "Mma";
|
||||
WarmupPeriod = period;
|
||||
Init();
|
||||
throw new ArgumentOutOfRangeException(nameof(period), "Period must be greater than or equal to 2.");
|
||||
}
|
||||
_period = period;
|
||||
_buffer = new CircularBuffer(period);
|
||||
Name = "Mma";
|
||||
WarmupPeriod = period;
|
||||
Init();
|
||||
}
|
||||
|
||||
public Mma(object source, int period) : this(period)
|
||||
public Mma(object source, int period) : this(period)
|
||||
{
|
||||
var pubEvent = source.GetType().GetEvent("Pub");
|
||||
pubEvent?.AddEventHandler(source, new ValueSignal(Sub));
|
||||
}
|
||||
|
||||
public override void Init()
|
||||
{
|
||||
base.Init();
|
||||
_lastMma = 0;
|
||||
_buffer.Clear();
|
||||
}
|
||||
|
||||
protected override void ManageState(bool isNew)
|
||||
{
|
||||
if (isNew)
|
||||
{
|
||||
var pubEvent = source.GetType().GetEvent("Pub");
|
||||
pubEvent?.AddEventHandler(source, new ValueSignal(Sub));
|
||||
}
|
||||
|
||||
public override void Init()
|
||||
{
|
||||
base.Init();
|
||||
_lastMma = 0;
|
||||
_buffer.Clear();
|
||||
}
|
||||
|
||||
protected override void ManageState(bool isNew)
|
||||
{
|
||||
if (isNew)
|
||||
{
|
||||
_index++;
|
||||
}
|
||||
}
|
||||
|
||||
protected override double Calculation()
|
||||
{
|
||||
ManageState(Input.IsNew);
|
||||
_buffer.Add(Input.Value, Input.IsNew);
|
||||
|
||||
if (_index >= _period)
|
||||
{
|
||||
double T = _buffer.Sum();
|
||||
double S = CalculateWeightedSum();
|
||||
_lastMma = (T / _period) + (6 * S) / ((_period + 1) * _period);
|
||||
}
|
||||
else
|
||||
{
|
||||
// Use simple average until we have enough data points
|
||||
_lastMma = _buffer.Average();
|
||||
}
|
||||
|
||||
IsHot = _index >= _period;
|
||||
return _lastMma;
|
||||
}
|
||||
|
||||
private double CalculateWeightedSum()
|
||||
{
|
||||
double sum = 0;
|
||||
for (int i = 0; i < _period; i++)
|
||||
{
|
||||
double weight = (_period - (2 * i + 1)) / 2.0;
|
||||
sum += weight * _buffer[^(i + 1)];
|
||||
}
|
||||
return sum;
|
||||
_index++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected override double Calculation()
|
||||
{
|
||||
ManageState(Input.IsNew);
|
||||
_buffer.Add(Input.Value, Input.IsNew);
|
||||
|
||||
if (_index >= _period)
|
||||
{
|
||||
double T = _buffer.Sum();
|
||||
double S = CalculateWeightedSum();
|
||||
_lastMma = (T / _period) + (6 * S) / ((_period + 1) * _period);
|
||||
}
|
||||
else
|
||||
{
|
||||
// Use simple average until we have enough data points
|
||||
_lastMma = _buffer.Average();
|
||||
}
|
||||
|
||||
IsHot = _index >= _period;
|
||||
return _lastMma;
|
||||
}
|
||||
|
||||
private double CalculateWeightedSum()
|
||||
{
|
||||
double sum = 0;
|
||||
for (int i = 0; i < _period; i++)
|
||||
{
|
||||
double weight = (_period - (2 * i + 1)) / 2.0;
|
||||
sum += weight * _buffer[^(i + 1)];
|
||||
}
|
||||
return sum;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,7 +6,7 @@ public class Qema : AbstractBase
|
||||
private readonly Ema _ema1, _ema2, _ema3, _ema4;
|
||||
private double _lastQema, _p_lastQema;
|
||||
|
||||
public Qema(double k1=0.2, double k2=0.2, double k3=0.2, double k4=0.2) : base()
|
||||
public Qema(double k1=0.2, double k2=0.2, double k3=0.2, double k4=0.2)
|
||||
{
|
||||
if (k1 <= 0 || k2 <= 0 || k3 <= 0 || k4 <= 0 )
|
||||
{
|
||||
|
||||
@@ -12,7 +12,7 @@ public class Rema : AbstractBase
|
||||
public int Period => _period;
|
||||
public double Lambda => _lambda;
|
||||
|
||||
public Rema(int period, double lambda = 0.5) : base()
|
||||
public Rema(int period, double lambda = 0.5)
|
||||
{
|
||||
if (period < 1)
|
||||
throw new ArgumentOutOfRangeException(nameof(period), "Period must be greater than or equal to 1.");
|
||||
|
||||
+30
-15
@@ -1,16 +1,20 @@
|
||||
using System;
|
||||
using System.Runtime.CompilerServices;
|
||||
|
||||
namespace QuanTAlib {
|
||||
namespace QuanTAlib;
|
||||
|
||||
public class Rma : AbstractBase {
|
||||
|
||||
public class Rma : AbstractBase
|
||||
{
|
||||
private readonly int _period;
|
||||
private double _alpha;
|
||||
private readonly double _alpha;
|
||||
private double _lastRMA;
|
||||
private double _savedLastRMA;
|
||||
|
||||
public Rma(int period) : base() {
|
||||
if (period < 1) {
|
||||
public Rma(int period)
|
||||
{
|
||||
if (period < 1)
|
||||
{
|
||||
throw new ArgumentException("Period must be greater than or equal to 1.", nameof(period));
|
||||
}
|
||||
_period = period;
|
||||
@@ -20,38 +24,50 @@ public class Rma : AbstractBase {
|
||||
Init();
|
||||
}
|
||||
|
||||
public Rma(object source, int period) : this(period) {
|
||||
public Rma(object source, int period) : this(period)
|
||||
{
|
||||
var pubEvent = source.GetType().GetEvent("Pub");
|
||||
pubEvent?.AddEventHandler(source, new ValueSignal(Sub));
|
||||
}
|
||||
|
||||
public override void Init() {
|
||||
public override void Init()
|
||||
{
|
||||
base.Init();
|
||||
_lastRMA = 0;
|
||||
_savedLastRMA = 0;
|
||||
}
|
||||
|
||||
protected override void ManageState(bool isNew) {
|
||||
if (isNew) {
|
||||
protected override void ManageState(bool isNew)
|
||||
{
|
||||
if (isNew)
|
||||
{
|
||||
_savedLastRMA = _lastRMA;
|
||||
_lastValidValue = Input.Value;
|
||||
_index++;
|
||||
} else {
|
||||
}
|
||||
else
|
||||
{
|
||||
_lastRMA = _savedLastRMA;
|
||||
}
|
||||
}
|
||||
|
||||
protected override double Calculation() {
|
||||
protected override double Calculation()
|
||||
{
|
||||
ManageState(Input.IsNew);
|
||||
|
||||
double rma;
|
||||
|
||||
if (_index == 1) {
|
||||
if (_index == 1)
|
||||
{
|
||||
rma = Input.Value;
|
||||
} else if (_index <= _period) {
|
||||
}
|
||||
else if (_index <= _period)
|
||||
{
|
||||
// Simple average during initial period
|
||||
rma = (_lastRMA * (_index - 1) + Input.Value) / _index;
|
||||
} else {
|
||||
}
|
||||
else
|
||||
{
|
||||
// Wilder's smoothing method
|
||||
rma = _alpha * (Input.Value - _lastRMA) + _lastRMA;
|
||||
}
|
||||
@@ -62,4 +78,3 @@ public class Rma : AbstractBase {
|
||||
return rma;
|
||||
}
|
||||
}
|
||||
}
|
||||
+1
-1
@@ -6,7 +6,7 @@ public class Sma : AbstractBase
|
||||
// inherited _value
|
||||
private readonly CircularBuffer _buffer;
|
||||
|
||||
public Sma(int period) : base()
|
||||
public Sma(int period)
|
||||
{
|
||||
if (period < 1)
|
||||
{
|
||||
|
||||
@@ -8,7 +8,7 @@ public class Smma : AbstractBase
|
||||
private CircularBuffer? _buffer;
|
||||
private double _lastSmma, _p_lastSmma;
|
||||
|
||||
public Smma(int period) : base()
|
||||
public Smma(int period)
|
||||
{
|
||||
if (period < 1)
|
||||
{
|
||||
|
||||
@@ -8,7 +8,7 @@ public class Tema : AbstractBase
|
||||
private double _lastEma3, _p_lastEma3;
|
||||
private double _k, _e, _p_e;
|
||||
|
||||
public Tema(int period) : base()
|
||||
public Tema(int period)
|
||||
{
|
||||
if (period < 1)
|
||||
{
|
||||
@@ -58,7 +58,7 @@ public class Tema : AbstractBase
|
||||
{
|
||||
double result, _ema1, _ema2, _ema3;
|
||||
ManageState(Input.IsNew);
|
||||
|
||||
|
||||
_e = (_e > 1e-10) ? (1 - _k) * _e : 0;
|
||||
double _invE = (_e > 1e-10) ? 1 / (1 - _e) : 1;
|
||||
|
||||
|
||||
@@ -30,14 +30,7 @@ public class Trima : AbstractBase
|
||||
|
||||
for (int i = 0; i < period; i++)
|
||||
{
|
||||
if (i < halfPeriod)
|
||||
{
|
||||
kernel[i] = i + 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
kernel[i] = period - i;
|
||||
}
|
||||
kernel[i] = i < halfPeriod ? i + 1 : period - i;
|
||||
weightSum += kernel[i];
|
||||
}
|
||||
|
||||
|
||||
@@ -7,11 +7,11 @@ public class Zlema : AbstractBase
|
||||
{
|
||||
private readonly int _period;
|
||||
private CircularBuffer? _buffer;
|
||||
private double _alpha;
|
||||
private int _lag;
|
||||
private readonly double _alpha;
|
||||
private readonly int _lag;
|
||||
private double _lastZLEMA, _p_lastZLEMA;
|
||||
|
||||
public Zlema(int period) : base()
|
||||
public Zlema(int period)
|
||||
{
|
||||
if (period < 1)
|
||||
{
|
||||
@@ -55,7 +55,7 @@ public class Zlema : AbstractBase
|
||||
protected override double Calculation()
|
||||
{
|
||||
ManageState(Input.IsNew);
|
||||
|
||||
|
||||
_buffer!.Add(Input.Value, Input.IsNew);
|
||||
|
||||
int lag = Math.Max(Math.Min((int)((_period - 1) * 0.5), _buffer.Count - 1), 0) + 1;
|
||||
|
||||
@@ -8,7 +8,7 @@ namespace QuanTAlib;
|
||||
/// and methods used by inheriting indicator types. It handles the basic flow of
|
||||
/// receiving bar data, performing calculations, and publishing results.
|
||||
/// </remarks>
|
||||
public abstract class AbstractBarBase : iTValue {
|
||||
public abstract class AbstractBarBase : ITValue {
|
||||
public DateTime Time { get; set; }
|
||||
public double Value { get; set; }
|
||||
public bool IsNew { get; set; }
|
||||
|
||||
@@ -8,7 +8,7 @@ namespace QuanTAlib;
|
||||
/// and methods used by inheriting indicator types. It handles the basic flow of
|
||||
/// receiving data, performing calculations, and publishing results.
|
||||
/// </remarks>
|
||||
public abstract class AbstractBase : iTValue
|
||||
public abstract class AbstractBase : ITValue
|
||||
{
|
||||
public DateTime Time { get; set; }
|
||||
public double Value { get; set; }
|
||||
|
||||
+95
-47
@@ -12,7 +12,8 @@ namespace QuanTAlib;
|
||||
/// a fixed-size buffer of double values. It uses SIMD operations for improved performance
|
||||
/// on supported hardware.
|
||||
/// </remarks>
|
||||
public class CircularBuffer : IEnumerable<double> {
|
||||
public class CircularBuffer : IEnumerable<double>
|
||||
{
|
||||
private readonly double[] _buffer;
|
||||
private int _start = 0;
|
||||
private int _size = 0;
|
||||
@@ -31,7 +32,8 @@ public class CircularBuffer : IEnumerable<double> {
|
||||
/// Initializes a new instance of the CircularBuffer class with the specified capacity.
|
||||
/// </summary>
|
||||
/// <param name="capacity">The maximum number of elements the buffer can hold.</param>
|
||||
public CircularBuffer(int capacity) {
|
||||
public CircularBuffer(int capacity)
|
||||
{
|
||||
Capacity = capacity;
|
||||
_buffer = GC.AllocateArray<double>(capacity, pinned: true);
|
||||
}
|
||||
@@ -42,16 +44,23 @@ public class CircularBuffer : IEnumerable<double> {
|
||||
/// <param name="item">The item to add to the buffer.</param>
|
||||
/// <param name="isNew">Indicates whether the item is a new value or an update to the last added value.</param>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public void Add(double item, bool isNew = true) {
|
||||
if (_size == 0 || isNew) {
|
||||
if (_size < Capacity) {
|
||||
public void Add(double item, bool isNew = true)
|
||||
{
|
||||
if (_size == 0 || isNew)
|
||||
{
|
||||
if (_size < Capacity)
|
||||
{
|
||||
_buffer[(_start + _size) % Capacity] = item;
|
||||
_size++;
|
||||
} else {
|
||||
}
|
||||
else
|
||||
{
|
||||
_buffer[_start] = item;
|
||||
_start = (_start + 1) % Capacity;
|
||||
}
|
||||
} else {
|
||||
}
|
||||
else
|
||||
{
|
||||
_buffer[(_start + _size - 1) % Capacity] = item;
|
||||
}
|
||||
}
|
||||
@@ -61,15 +70,18 @@ public class CircularBuffer : IEnumerable<double> {
|
||||
/// </summary>
|
||||
/// <param name="index">The zero-based index of the element to get or set.</param>
|
||||
/// <returns>The element at the specified index.</returns>
|
||||
public double this[Index index] {
|
||||
public double this[Index index]
|
||||
{
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
get {
|
||||
get
|
||||
{
|
||||
int actualIndex = index.IsFromEnd ? _size - index.Value : index.Value;
|
||||
actualIndex = Math.Clamp(actualIndex, 0, _size - 1);
|
||||
return _buffer[(_start + actualIndex) % Capacity];
|
||||
}
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
set {
|
||||
set
|
||||
{
|
||||
int actualIndex = index.IsFromEnd ? _size - index.Value : index.Value;
|
||||
actualIndex = Math.Clamp(actualIndex, 0, _size - 1);
|
||||
_buffer[(_start + actualIndex) % Capacity] = value;
|
||||
@@ -77,7 +89,8 @@ public class CircularBuffer : IEnumerable<double> {
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.NoInlining)]
|
||||
private static void ThrowArgumentOutOfRangeException() {
|
||||
private static void ThrowArgumentOutOfRangeException()
|
||||
{
|
||||
throw new ArgumentOutOfRangeException("index", "Index is out of range.");
|
||||
}
|
||||
|
||||
@@ -86,7 +99,8 @@ public class CircularBuffer : IEnumerable<double> {
|
||||
/// </summary>
|
||||
/// <returns>The newest element in the buffer.</returns>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public double Newest() {
|
||||
public double Newest()
|
||||
{
|
||||
if (_size == 0)
|
||||
return 0;
|
||||
return _buffer[(_start + _size - 1) % Capacity];
|
||||
@@ -97,14 +111,16 @@ public class CircularBuffer : IEnumerable<double> {
|
||||
/// </summary>
|
||||
/// <returns>The oldest element in the buffer.</returns>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public double Oldest() {
|
||||
public double Oldest()
|
||||
{
|
||||
if (_size == 0)
|
||||
ThrowInvalidOperationException();
|
||||
return _buffer[_start];
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.NoInlining)]
|
||||
private static void ThrowInvalidOperationException() {
|
||||
private static void ThrowInvalidOperationException()
|
||||
{
|
||||
throw new InvalidOperationException("Buffer is empty.");
|
||||
}
|
||||
|
||||
@@ -119,13 +135,15 @@ public class CircularBuffer : IEnumerable<double> {
|
||||
/// <summary>
|
||||
/// Represents an enumerator for the CircularBuffer.
|
||||
/// </summary>
|
||||
public struct Enumerator : IEnumerator<double> {
|
||||
public struct Enumerator : IEnumerator<double>
|
||||
{
|
||||
private readonly CircularBuffer _buffer;
|
||||
private int _index;
|
||||
private double _current;
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
internal Enumerator(CircularBuffer buffer) {
|
||||
internal Enumerator(CircularBuffer buffer)
|
||||
{
|
||||
_buffer = buffer;
|
||||
_index = -1;
|
||||
_current = default;
|
||||
@@ -136,7 +154,8 @@ public class CircularBuffer : IEnumerable<double> {
|
||||
/// </summary>
|
||||
/// <returns>true if the enumerator was successfully advanced to the next element; false if the enumerator has passed the end of the collection.</returns>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public bool MoveNext() {
|
||||
public bool MoveNext()
|
||||
{
|
||||
if (_index + 1 >= _buffer._size)
|
||||
return false;
|
||||
|
||||
@@ -154,7 +173,8 @@ public class CircularBuffer : IEnumerable<double> {
|
||||
/// <summary>
|
||||
/// Sets the enumerator to its initial position, which is before the first element in the buffer.
|
||||
/// </summary>
|
||||
public void Reset() {
|
||||
public void Reset()
|
||||
{
|
||||
_index = -1;
|
||||
_current = default;
|
||||
}
|
||||
@@ -171,13 +191,17 @@ public class CircularBuffer : IEnumerable<double> {
|
||||
/// <param name="destination">The one-dimensional array that is the destination of the elements copied from the buffer.</param>
|
||||
/// <param name="destinationIndex">The zero-based index in array at which copying begins.</param>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public void CopyTo(double[] destination, int destinationIndex) {
|
||||
public void CopyTo(double[] destination, int destinationIndex)
|
||||
{
|
||||
if (_size == 0)
|
||||
return;
|
||||
|
||||
if (_start + _size <= Capacity) {
|
||||
if (_start + _size <= Capacity)
|
||||
{
|
||||
Array.Copy(_buffer, _start, destination, destinationIndex, _size);
|
||||
} else {
|
||||
}
|
||||
else
|
||||
{
|
||||
int firstPartLength = Capacity - _start;
|
||||
Array.Copy(_buffer, _start, destination, destinationIndex, firstPartLength);
|
||||
Array.Copy(_buffer, 0, destination, destinationIndex + firstPartLength, _size - firstPartLength);
|
||||
@@ -189,15 +213,17 @@ public class CircularBuffer : IEnumerable<double> {
|
||||
/// </summary>
|
||||
/// <returns>A read-only span over the buffer contents.</returns>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public ReadOnlySpan<double> GetSpan() {
|
||||
public ReadOnlySpan<double> GetSpan()
|
||||
{
|
||||
if (_size == 0)
|
||||
return ReadOnlySpan<double>.Empty;
|
||||
|
||||
if (_start + _size <= Capacity) {
|
||||
if (_start + _size <= Capacity)
|
||||
{
|
||||
return new ReadOnlySpan<double>(_buffer, _start, _size);
|
||||
} else {
|
||||
return new ReadOnlySpan<double>(ToArray());
|
||||
}
|
||||
|
||||
return new ReadOnlySpan<double>(ToArray());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -216,7 +242,8 @@ public class CircularBuffer : IEnumerable<double> {
|
||||
/// Removes all elements from the buffer.
|
||||
/// </summary>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public void Clear() {
|
||||
public void Clear()
|
||||
{
|
||||
Array.Clear(_buffer, 0, _buffer.Length);
|
||||
_start = 0;
|
||||
_size = 0;
|
||||
@@ -227,7 +254,8 @@ public class CircularBuffer : IEnumerable<double> {
|
||||
/// </summary>
|
||||
/// <returns>The maximum value in the buffer.</returns>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public double Max() {
|
||||
public double Max()
|
||||
{
|
||||
if (_size == 0)
|
||||
ThrowInvalidOperationException();
|
||||
|
||||
@@ -239,7 +267,8 @@ public class CircularBuffer : IEnumerable<double> {
|
||||
/// </summary>
|
||||
/// <returns>The minimum value in the buffer.</returns>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public double Min() {
|
||||
public double Min()
|
||||
{
|
||||
if (_size == 0)
|
||||
ThrowInvalidOperationException();
|
||||
|
||||
@@ -251,7 +280,8 @@ public class CircularBuffer : IEnumerable<double> {
|
||||
/// </summary>
|
||||
/// <returns>The sum of all values in the buffer.</returns>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public double Sum() {
|
||||
public double Sum()
|
||||
{
|
||||
return SumSimd();
|
||||
}
|
||||
|
||||
@@ -260,7 +290,8 @@ public class CircularBuffer : IEnumerable<double> {
|
||||
/// </summary>
|
||||
/// <returns>The average of all values in the buffer.</returns>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public double Average() {
|
||||
public double Average()
|
||||
{
|
||||
if (_size == 0)
|
||||
ThrowInvalidOperationException();
|
||||
|
||||
@@ -268,22 +299,26 @@ public class CircularBuffer : IEnumerable<double> {
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
private double MaxSimd() {
|
||||
private double MaxSimd()
|
||||
{
|
||||
var span = GetSpan();
|
||||
var vectorSize = Vector<double>.Count;
|
||||
var maxVector = new Vector<double>(double.MinValue);
|
||||
|
||||
int i = 0;
|
||||
for (; i <= span.Length - vectorSize; i += vectorSize) {
|
||||
for (; i <= span.Length - vectorSize; i += vectorSize)
|
||||
{
|
||||
maxVector = Vector.Max(maxVector, new Vector<double>(span.Slice(i, vectorSize)));
|
||||
}
|
||||
|
||||
double max = double.MinValue;
|
||||
for (int j = 0; j < vectorSize; j++) {
|
||||
for (int j = 0; j < vectorSize; j++)
|
||||
{
|
||||
max = Math.Max(max, maxVector[j]);
|
||||
}
|
||||
|
||||
for (; i < span.Length; i++) {
|
||||
for (; i < span.Length; i++)
|
||||
{
|
||||
max = Math.Max(max, span[i]);
|
||||
}
|
||||
|
||||
@@ -291,22 +326,26 @@ public class CircularBuffer : IEnumerable<double> {
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
private double MinSimd() {
|
||||
private double MinSimd()
|
||||
{
|
||||
var span = GetSpan();
|
||||
var vectorSize = Vector<double>.Count;
|
||||
var minVector = new Vector<double>(double.MaxValue);
|
||||
|
||||
int i = 0;
|
||||
for (; i <= span.Length - vectorSize; i += vectorSize) {
|
||||
for (; i <= span.Length - vectorSize; i += vectorSize)
|
||||
{
|
||||
minVector = Vector.Min(minVector, new Vector<double>(span.Slice(i, vectorSize)));
|
||||
}
|
||||
|
||||
double min = double.MaxValue;
|
||||
for (int j = 0; j < vectorSize; j++) {
|
||||
for (int j = 0; j < vectorSize; j++)
|
||||
{
|
||||
min = Math.Min(min, minVector[j]);
|
||||
}
|
||||
|
||||
for (; i < span.Length; i++) {
|
||||
for (; i < span.Length; i++)
|
||||
{
|
||||
min = Math.Min(min, span[i]);
|
||||
}
|
||||
|
||||
@@ -314,22 +353,26 @@ public class CircularBuffer : IEnumerable<double> {
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
private double SumSimd() {
|
||||
private double SumSimd()
|
||||
{
|
||||
var span = GetSpan();
|
||||
var vectorSize = Vector<double>.Count;
|
||||
var sumVector = Vector<double>.Zero;
|
||||
|
||||
int i = 0;
|
||||
for (; i <= span.Length - vectorSize; i += vectorSize) {
|
||||
for (; i <= span.Length - vectorSize; i += vectorSize)
|
||||
{
|
||||
sumVector += new Vector<double>(span.Slice(i, vectorSize));
|
||||
}
|
||||
|
||||
double sum = 0;
|
||||
for (int j = 0; j < vectorSize; j++) {
|
||||
for (int j = 0; j < vectorSize; j++)
|
||||
{
|
||||
sum += sumVector[j];
|
||||
}
|
||||
|
||||
for (; i < span.Length; i++) {
|
||||
for (; i < span.Length; i++)
|
||||
{
|
||||
sum += span[i];
|
||||
}
|
||||
|
||||
@@ -340,7 +383,8 @@ public class CircularBuffer : IEnumerable<double> {
|
||||
/// Copies the buffer elements to a new array.
|
||||
/// </summary>
|
||||
/// <returns>An array containing copies of the buffer elements.</returns>
|
||||
public double[] ToArray() {
|
||||
public double[] ToArray()
|
||||
{
|
||||
double[] array = new double[_size];
|
||||
CopyTo(array, 0);
|
||||
return array;
|
||||
@@ -350,10 +394,12 @@ public class CircularBuffer : IEnumerable<double> {
|
||||
/// Performs a parallel operation on the buffer elements.
|
||||
/// </summary>
|
||||
/// <param name="operation">The operation to perform on each partition of the buffer.</param>
|
||||
public void ParallelOperation(Func<double[], int, int, double> operation) {
|
||||
public void ParallelOperation(Func<double[], int, int, double> operation)
|
||||
{
|
||||
const int MinimumPartitionSize = 1024;
|
||||
|
||||
if (_size < MinimumPartitionSize) {
|
||||
if (_size < MinimumPartitionSize)
|
||||
{
|
||||
var span = GetSpan();
|
||||
var array = span.ToArray();
|
||||
operation(array, 0, array.Length);
|
||||
@@ -363,7 +409,8 @@ public class CircularBuffer : IEnumerable<double> {
|
||||
int partitionCount = Environment.ProcessorCount;
|
||||
int partitionSize = _size / partitionCount;
|
||||
|
||||
if (partitionSize < MinimumPartitionSize) {
|
||||
if (partitionSize < MinimumPartitionSize)
|
||||
{
|
||||
partitionCount = Math.Max(1, _size / MinimumPartitionSize);
|
||||
partitionSize = _size / partitionCount;
|
||||
}
|
||||
@@ -371,7 +418,8 @@ public class CircularBuffer : IEnumerable<double> {
|
||||
var buffer = ToArray();
|
||||
var results = new double[partitionCount];
|
||||
|
||||
Parallel.For(0, partitionCount, i => {
|
||||
Parallel.For(0, partitionCount, i =>
|
||||
{
|
||||
int start = i * partitionSize;
|
||||
int length = (i == partitionCount - 1) ? _size - start : partitionSize;
|
||||
results[i] = operation(buffer, start, length);
|
||||
|
||||
@@ -9,7 +9,7 @@ public static class Formatters
|
||||
const string pad = "18";
|
||||
public static void Initialize()
|
||||
{
|
||||
Formatter.Register<iTValue>((tick, writer) =>
|
||||
Formatter.Register<ITValue>((tick, writer) =>
|
||||
{
|
||||
var sb = new StringBuilder();
|
||||
sb.Append("<table style='border-collapse: collapse; text-align: left;'><tr>");
|
||||
|
||||
+2
-2
@@ -1,6 +1,6 @@
|
||||
namespace QuanTAlib;
|
||||
|
||||
public interface iTBar
|
||||
public interface ITBar
|
||||
{
|
||||
DateTime Time { get; }
|
||||
double Open { get; }
|
||||
@@ -11,7 +11,7 @@ public interface iTBar
|
||||
bool IsNew { get; }
|
||||
}
|
||||
|
||||
public readonly record struct TBar(DateTime Time, double Open, double High, double Low, double Close, double Volume, bool IsNew = true) : iTBar
|
||||
public readonly record struct TBar(DateTime Time, double Open, double High, double Low, double Close, double Volume, bool IsNew = true) : ITBar
|
||||
{
|
||||
public DateTime Time { get; init; } = Time;
|
||||
public double Open { get; init; } = Open;
|
||||
|
||||
+2
-2
@@ -1,6 +1,6 @@
|
||||
namespace QuanTAlib;
|
||||
|
||||
public interface iTValue
|
||||
public interface ITValue
|
||||
{
|
||||
DateTime Time { get; }
|
||||
double Value { get; }
|
||||
@@ -8,7 +8,7 @@ public interface iTValue
|
||||
bool IsHot { get; }
|
||||
}
|
||||
|
||||
public readonly record struct TValue(DateTime Time, double Value, bool IsNew = true, bool IsHot = true) : iTValue
|
||||
public readonly record struct TValue(DateTime Time, double Value, bool IsNew = true, bool IsHot = true) : ITValue
|
||||
{
|
||||
public DateTime Time { get; init; } = Time;
|
||||
public double Value { get; init; } = Value;
|
||||
|
||||
@@ -8,7 +8,7 @@ public class GbmFeed : TBarSeries
|
||||
private readonly Random _random;
|
||||
private double _lastClose, _lastHigh, _lastLow;
|
||||
|
||||
public GbmFeed(double initialPrice = 100.0, double mu = 0.05, double sigma = 0.2) : base()
|
||||
public GbmFeed(double initialPrice = 100.0, double mu = 0.05, double sigma = 0.2)
|
||||
{
|
||||
_lastClose = _lastHigh = _lastLow = initialPrice;
|
||||
_mu = mu;
|
||||
|
||||
@@ -16,7 +16,7 @@ public class Entropy : AbstractBase
|
||||
/// <exception cref="ArgumentOutOfRangeException">
|
||||
/// Thrown when the period is less than 2.
|
||||
/// </exception>
|
||||
public Entropy(int period) : base()
|
||||
public Entropy(int period)
|
||||
{
|
||||
if (period < 2)
|
||||
{
|
||||
|
||||
@@ -16,7 +16,7 @@ public class Kurtosis : AbstractBase
|
||||
/// <exception cref="ArgumentOutOfRangeException">
|
||||
/// Thrown when the period is less than 4.
|
||||
/// </exception>
|
||||
public Kurtosis(int period) : base()
|
||||
public Kurtosis(int period)
|
||||
{
|
||||
if (period < 4)
|
||||
{
|
||||
|
||||
@@ -20,7 +20,7 @@ public class Max : AbstractBase
|
||||
/// <exception cref="ArgumentOutOfRangeException">
|
||||
/// Thrown when the period is less than 1 or decay is negative.
|
||||
/// </exception>
|
||||
public Max(int period, double decay = 0) : base()
|
||||
public Max(int period, double decay = 0)
|
||||
{
|
||||
if (period < 1)
|
||||
{
|
||||
@@ -105,7 +105,7 @@ public class Max : AbstractBase
|
||||
}
|
||||
|
||||
double decayRate = 1 - Math.Exp(-_halfLife * _timeSinceNewMax / Period);
|
||||
_currentMax = _currentMax - decayRate * (_currentMax - _buffer.Average());
|
||||
_currentMax -= decayRate * (_currentMax - _buffer.Average());
|
||||
_currentMax = Math.Min(_currentMax, _buffer.Max());
|
||||
|
||||
IsHot = true;
|
||||
|
||||
@@ -16,7 +16,7 @@ public class Median : AbstractBase
|
||||
/// <exception cref="ArgumentOutOfRangeException">
|
||||
/// Thrown when the period is less than 1.
|
||||
/// </exception>
|
||||
public Median(int period) : base()
|
||||
public Median(int period)
|
||||
{
|
||||
if (period < 1)
|
||||
{
|
||||
@@ -76,16 +76,7 @@ public class Median : AbstractBase
|
||||
Array.Sort(sortedValues);
|
||||
int middleIndex = sortedValues.Length / 2;
|
||||
|
||||
if (sortedValues.Length % 2 == 0)
|
||||
{
|
||||
// Even number of values: average of two middle values
|
||||
median = (sortedValues[middleIndex - 1] + sortedValues[middleIndex]) / 2.0;
|
||||
}
|
||||
else
|
||||
{
|
||||
// Odd number of values: middle value
|
||||
median = sortedValues[middleIndex];
|
||||
}
|
||||
median = (sortedValues.Length % 2 == 0) ? (sortedValues[middleIndex - 1] + sortedValues[middleIndex]) / 2.0 : sortedValues[middleIndex];
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
+24
-12
@@ -10,7 +10,8 @@ namespace QuanTAlib;
|
||||
/// efficiently. It also implements a decay mechanism to adjust the minimum value over
|
||||
/// time, allowing for a more responsive indicator in changing market conditions.
|
||||
/// </remarks>
|
||||
public class Min : AbstractBase {
|
||||
public class Min : AbstractBase
|
||||
{
|
||||
private readonly int Period;
|
||||
private readonly CircularBuffer _buffer;
|
||||
private readonly double _halfLife;
|
||||
@@ -25,11 +26,14 @@ public class Min : AbstractBase {
|
||||
/// <exception cref="ArgumentOutOfRangeException">
|
||||
/// Thrown when period is less than 1 or decay is negative.
|
||||
/// </exception>
|
||||
public Min(int period, double decay = 0) : base() {
|
||||
if (period < 1) {
|
||||
public Min(int period, double decay = 0)
|
||||
{
|
||||
if (period < 1)
|
||||
{
|
||||
throw new ArgumentOutOfRangeException(nameof(period), "Period must be greater than or equal to 1.");
|
||||
}
|
||||
if (decay < 0) {
|
||||
if (decay < 0)
|
||||
{
|
||||
throw new ArgumentOutOfRangeException(nameof(decay), "Half-life must be non-negative.");
|
||||
}
|
||||
Period = period;
|
||||
@@ -46,7 +50,8 @@ public class Min : AbstractBase {
|
||||
/// <param name="source">The source object to subscribe to for value updates.</param>
|
||||
/// <param name="period">The period over which to calculate the minimum value.</param>
|
||||
/// <param name="decay">The decay factor to apply to older values (default is 0).</param>
|
||||
public Min(object source, int period, double decay = 0) : this(period, decay) {
|
||||
public Min(object source, int period, double decay = 0) : this(period, decay)
|
||||
{
|
||||
var pubEvent = source.GetType().GetEvent("Pub");
|
||||
pubEvent?.AddEventHandler(source, new ValueSignal(Sub));
|
||||
}
|
||||
@@ -54,7 +59,8 @@ public class Min : AbstractBase {
|
||||
/// <summary>
|
||||
/// Initializes the Min instance by setting initial values.
|
||||
/// </summary>
|
||||
public override void Init() {
|
||||
public override void Init()
|
||||
{
|
||||
base.Init();
|
||||
_currentMin = double.MaxValue;
|
||||
_timeSinceNewMin = 0;
|
||||
@@ -64,14 +70,18 @@ public class Min : AbstractBase {
|
||||
/// Manages the state of the Min instance based on whether a new value is being processed.
|
||||
/// </summary>
|
||||
/// <param name="isNew">Indicates whether the current input is a new value.</param>
|
||||
protected override void ManageState(bool isNew) {
|
||||
if (isNew) {
|
||||
protected override void ManageState(bool isNew)
|
||||
{
|
||||
if (isNew)
|
||||
{
|
||||
_p_currentMin = _currentMin;
|
||||
_lastValidValue = Input.Value;
|
||||
_index++;
|
||||
_timeSinceNewMin++;
|
||||
_p_timeSinceNewMin = _timeSinceNewMin;
|
||||
} else {
|
||||
}
|
||||
else
|
||||
{
|
||||
_currentMin = _p_currentMin;
|
||||
_timeSinceNewMin = _p_timeSinceNewMin;
|
||||
}
|
||||
@@ -87,17 +97,19 @@ public class Min : AbstractBase {
|
||||
/// The decay rate is calculated using an exponential function based on the time since
|
||||
/// the last new minimum and the specified half-life.
|
||||
/// </remarks>
|
||||
protected override double Calculation() {
|
||||
protected override double Calculation()
|
||||
{
|
||||
ManageState(Input.IsNew);
|
||||
_buffer.Add(Input.Value, Input.IsNew);
|
||||
|
||||
if (Input.Value <= _currentMin) {
|
||||
if (Input.Value <= _currentMin)
|
||||
{
|
||||
_currentMin = Input.Value;
|
||||
_timeSinceNewMin = 0;
|
||||
}
|
||||
|
||||
double decayRate = 1 - Math.Exp(-_halfLife * _timeSinceNewMin / Period);
|
||||
_currentMin = _currentMin + decayRate * (_buffer.Average() - _currentMin);
|
||||
_currentMin += decayRate * (_buffer.Average() - _currentMin);
|
||||
_currentMin = Math.Max(_currentMin, _buffer.Min());
|
||||
|
||||
IsHot = true;
|
||||
|
||||
+19
-9
@@ -9,7 +9,8 @@ namespace QuanTAlib;
|
||||
/// efficiently. Before the specified period is reached, it returns the average of
|
||||
/// the available values as an approximation.
|
||||
/// </remarks>
|
||||
public class Mode : AbstractBase {
|
||||
public class Mode : AbstractBase
|
||||
{
|
||||
private readonly int Period;
|
||||
private readonly CircularBuffer _buffer;
|
||||
|
||||
@@ -20,8 +21,10 @@ public class Mode : AbstractBase {
|
||||
/// <exception cref="ArgumentOutOfRangeException">
|
||||
/// Thrown when period is less than 1.
|
||||
/// </exception>
|
||||
public Mode(int period) : base() {
|
||||
if (period < 1) {
|
||||
public Mode(int period)
|
||||
{
|
||||
if (period < 1)
|
||||
{
|
||||
throw new ArgumentOutOfRangeException(nameof(period), "Period must be greater than or equal to 1.");
|
||||
}
|
||||
Period = period;
|
||||
@@ -36,7 +39,8 @@ public class Mode : AbstractBase {
|
||||
/// </summary>
|
||||
/// <param name="source">The source object to subscribe to for value updates.</param>
|
||||
/// <param name="period">The period over which to calculate the mode.</param>
|
||||
public Mode(object source, int period) : this(period) {
|
||||
public Mode(object source, int period) : this(period)
|
||||
{
|
||||
var pubEvent = source.GetType().GetEvent("Pub");
|
||||
pubEvent?.AddEventHandler(source, new ValueSignal(Sub));
|
||||
}
|
||||
@@ -45,8 +49,10 @@ public class Mode : AbstractBase {
|
||||
/// Manages the state of the Mode instance based on whether a new value is being processed.
|
||||
/// </summary>
|
||||
/// <param name="isNew">Indicates whether the current input is a new value.</param>
|
||||
protected override void ManageState(bool isNew) {
|
||||
if (isNew) {
|
||||
protected override void ManageState(bool isNew)
|
||||
{
|
||||
if (isNew)
|
||||
{
|
||||
_lastValidValue = Input.Value;
|
||||
_index++;
|
||||
}
|
||||
@@ -64,12 +70,14 @@ public class Mode : AbstractBase {
|
||||
/// the available values as an approximation of the mode. Once the period is
|
||||
/// reached, it calculates the true mode by grouping and counting the values.
|
||||
/// </remarks>
|
||||
protected override double Calculation() {
|
||||
protected override double Calculation()
|
||||
{
|
||||
ManageState(Input.IsNew);
|
||||
_buffer.Add(Input.Value, Input.IsNew);
|
||||
|
||||
double mode;
|
||||
if (_index >= Period) {
|
||||
if (_index >= Period)
|
||||
{
|
||||
var values = _buffer.GetSpan().ToArray();
|
||||
var groupedValues = values.GroupBy(v => v)
|
||||
.OrderByDescending(g => g.Count())
|
||||
@@ -82,7 +90,9 @@ public class Mode : AbstractBase {
|
||||
.ToList();
|
||||
|
||||
mode = modes.Average(); // If there are multiple modes, we return their average
|
||||
} else {
|
||||
}
|
||||
else
|
||||
{
|
||||
mode = _buffer.Average(); // Use average until we have enough data points
|
||||
}
|
||||
|
||||
|
||||
@@ -10,7 +10,8 @@ namespace QuanTAlib;
|
||||
/// between two data points. Before the specified period is reached, it returns the
|
||||
/// average of the available values as an approximation.
|
||||
/// </remarks>
|
||||
public class Percentile : AbstractBase {
|
||||
public class Percentile : AbstractBase
|
||||
{
|
||||
private readonly int Period;
|
||||
private readonly double Percent;
|
||||
private readonly CircularBuffer _buffer;
|
||||
@@ -23,11 +24,14 @@ public class Percentile : AbstractBase {
|
||||
/// <exception cref="ArgumentOutOfRangeException">
|
||||
/// Thrown when period is less than 2 or percent is not between 0 and 100.
|
||||
/// </exception>
|
||||
public Percentile(int period, double percent) : base() {
|
||||
if (period < 2) {
|
||||
public Percentile(int period, double percent)
|
||||
{
|
||||
if (period < 2)
|
||||
{
|
||||
throw new ArgumentOutOfRangeException(nameof(period), "Period must be greater than or equal to 2 for percentile calculation.");
|
||||
}
|
||||
if (percent < 0 || percent > 100) {
|
||||
if (percent < 0 || percent > 100)
|
||||
{
|
||||
throw new ArgumentOutOfRangeException(nameof(percent), "Percent must be between 0 and 100.");
|
||||
}
|
||||
Period = period;
|
||||
@@ -44,7 +48,8 @@ public class Percentile : AbstractBase {
|
||||
/// <param name="source">The source object to subscribe to for value updates.</param>
|
||||
/// <param name="period">The period over which to calculate the percentile.</param>
|
||||
/// <param name="percent">The percentile to calculate (between 0 and 100).</param>
|
||||
public Percentile(object source, int period, double percent) : this(period, percent) {
|
||||
public Percentile(object source, int period, double percent) : this(period, percent)
|
||||
{
|
||||
var pubEvent = source.GetType().GetEvent("Pub");
|
||||
pubEvent?.AddEventHandler(source, new ValueSignal(Sub));
|
||||
}
|
||||
@@ -52,7 +57,8 @@ public class Percentile : AbstractBase {
|
||||
/// <summary>
|
||||
/// Initializes the Percentile instance by clearing the buffer.
|
||||
/// </summary>
|
||||
public override void Init() {
|
||||
public override void Init()
|
||||
{
|
||||
base.Init();
|
||||
_buffer.Clear();
|
||||
}
|
||||
@@ -61,8 +67,10 @@ public class Percentile : AbstractBase {
|
||||
/// Manages the state of the Percentile instance based on whether a new value is being processed.
|
||||
/// </summary>
|
||||
/// <param name="isNew">Indicates whether the current input is a new value.</param>
|
||||
protected override void ManageState(bool isNew) {
|
||||
if (isNew) {
|
||||
protected override void ManageState(bool isNew)
|
||||
{
|
||||
if (isNew)
|
||||
{
|
||||
_lastValidValue = Input.Value;
|
||||
_index++;
|
||||
}
|
||||
@@ -80,12 +88,14 @@ public class Percentile : AbstractBase {
|
||||
/// as an approximation. Once the period is reached, it calculates the true percentile by
|
||||
/// sorting the values and interpolating as necessary.
|
||||
/// </remarks>
|
||||
protected override double Calculation() {
|
||||
protected override double Calculation()
|
||||
{
|
||||
ManageState(Input.IsNew);
|
||||
_buffer.Add(Input.Value, Input.IsNew);
|
||||
|
||||
double result;
|
||||
if (_buffer.Count >= Period) {
|
||||
if (_buffer.Count >= Period)
|
||||
{
|
||||
var values = _buffer.GetSpan().ToArray();
|
||||
Array.Sort(values);
|
||||
|
||||
@@ -93,16 +103,21 @@ public class Percentile : AbstractBase {
|
||||
int lowerIndex = (int)Math.Floor(position);
|
||||
int upperIndex = (int)Math.Ceiling(position);
|
||||
|
||||
if (lowerIndex == upperIndex) {
|
||||
if (lowerIndex == upperIndex)
|
||||
{
|
||||
result = values[lowerIndex];
|
||||
} else {
|
||||
}
|
||||
else
|
||||
{
|
||||
// Interpolate between the two nearest values
|
||||
double lowerValue = values[lowerIndex];
|
||||
double upperValue = values[upperIndex];
|
||||
double fraction = position - lowerIndex;
|
||||
result = lowerValue + (upperValue - lowerValue) * fraction;
|
||||
}
|
||||
} else {
|
||||
}
|
||||
else
|
||||
{
|
||||
// Use average for insufficient data, like the Median class
|
||||
result = _buffer.Average();
|
||||
}
|
||||
|
||||
+22
-11
@@ -10,7 +10,8 @@ namespace QuanTAlib;
|
||||
/// for sample skewness calculation. A minimum of 3 data points is required for the
|
||||
/// calculation.
|
||||
/// </remarks>
|
||||
public class Skew : AbstractBase {
|
||||
public class Skew : AbstractBase
|
||||
{
|
||||
private readonly int Period;
|
||||
private readonly CircularBuffer _buffer;
|
||||
|
||||
@@ -21,8 +22,10 @@ public class Skew : AbstractBase {
|
||||
/// <exception cref="ArgumentOutOfRangeException">
|
||||
/// Thrown when period is less than 3.
|
||||
/// </exception>
|
||||
public Skew(int period) : base() {
|
||||
if (period < 3) {
|
||||
public Skew(int period)
|
||||
{
|
||||
if (period < 3)
|
||||
{
|
||||
throw new ArgumentOutOfRangeException(nameof(period), "Period must be greater than or equal to 3 for skewness calculation.");
|
||||
}
|
||||
Period = period;
|
||||
@@ -37,7 +40,8 @@ public class Skew : AbstractBase {
|
||||
/// </summary>
|
||||
/// <param name="source">The source object to subscribe to for value updates.</param>
|
||||
/// <param name="period">The period over which to calculate the skewness.</param>
|
||||
public Skew(object source, int period) : this(period) {
|
||||
public Skew(object source, int period) : this(period)
|
||||
{
|
||||
var pubEvent = source.GetType().GetEvent("Pub");
|
||||
pubEvent?.AddEventHandler(source, new ValueSignal(Sub));
|
||||
}
|
||||
@@ -45,7 +49,8 @@ public class Skew : AbstractBase {
|
||||
/// <summary>
|
||||
/// Initializes the Skew instance by clearing the buffer.
|
||||
/// </summary>
|
||||
public override void Init() {
|
||||
public override void Init()
|
||||
{
|
||||
base.Init();
|
||||
_buffer.Clear();
|
||||
}
|
||||
@@ -54,8 +59,10 @@ public class Skew : AbstractBase {
|
||||
/// Manages the state of the Skew instance based on whether a new value is being processed.
|
||||
/// </summary>
|
||||
/// <param name="isNew">Indicates whether the current input is a new value.</param>
|
||||
protected override void ManageState(bool isNew) {
|
||||
if (isNew) {
|
||||
protected override void ManageState(bool isNew)
|
||||
{
|
||||
if (isNew)
|
||||
{
|
||||
_lastValidValue = Input.Value;
|
||||
_index++;
|
||||
}
|
||||
@@ -73,13 +80,15 @@ public class Skew : AbstractBase {
|
||||
/// calculation. If there are fewer than 3 data points, or if the standard
|
||||
/// deviation is zero, the method returns 0.
|
||||
/// </remarks>
|
||||
protected override double Calculation() {
|
||||
protected override double Calculation()
|
||||
{
|
||||
ManageState(Input.IsNew);
|
||||
|
||||
_buffer.Add(Input.Value, Input.IsNew);
|
||||
|
||||
double skew = 0;
|
||||
if (_buffer.Count >= 3) { // We need at least 3 data points for skewness
|
||||
if (_buffer.Count >= 3)
|
||||
{ // We need at least 3 data points for skewness
|
||||
var values = _buffer.GetSpan().ToArray();
|
||||
double mean = values.Average();
|
||||
double n = values.Length;
|
||||
@@ -87,7 +96,8 @@ public class Skew : AbstractBase {
|
||||
double sumCubedDeviations = 0;
|
||||
double sumSquaredDeviations = 0;
|
||||
|
||||
foreach (var value in values) {
|
||||
foreach (var value in values)
|
||||
{
|
||||
double deviation = value - mean;
|
||||
sumCubedDeviations += Math.Pow(deviation, 3);
|
||||
sumSquaredDeviations += Math.Pow(deviation, 2);
|
||||
@@ -98,7 +108,8 @@ public class Skew : AbstractBase {
|
||||
double m2 = sumSquaredDeviations / n;
|
||||
double s3 = Math.Pow(m2, 1.5);
|
||||
|
||||
if (s3 != 0) { // Avoid division by zero
|
||||
if (s3 != 0)
|
||||
{ // Avoid division by zero
|
||||
skew = (Math.Sqrt(n * (n - 1)) / (n - 2)) * (m3 / s3);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,7 +9,8 @@ namespace QuanTAlib;
|
||||
/// standard deviation based on the isPopulation parameter. It uses a circular buffer
|
||||
/// to efficiently manage the data points within the specified period.
|
||||
/// </remarks>
|
||||
public class Stddev : AbstractBase {
|
||||
public class Stddev : AbstractBase
|
||||
{
|
||||
private readonly int Period;
|
||||
private readonly bool IsPopulation;
|
||||
private readonly CircularBuffer _buffer;
|
||||
@@ -25,8 +26,10 @@ public class Stddev : AbstractBase {
|
||||
/// <exception cref="ArgumentOutOfRangeException">
|
||||
/// Thrown when period is less than 2.
|
||||
/// </exception>
|
||||
public Stddev(int period, bool isPopulation = false) : base() {
|
||||
if (period < 2) {
|
||||
public Stddev(int period, bool isPopulation = false)
|
||||
{
|
||||
if (period < 2)
|
||||
{
|
||||
throw new ArgumentOutOfRangeException(nameof(period), "Period must be greater than or equal to 2.");
|
||||
}
|
||||
Period = period;
|
||||
@@ -46,7 +49,8 @@ public class Stddev : AbstractBase {
|
||||
/// <param name="isPopulation">
|
||||
/// A flag indicating whether to calculate population (true) or sample (false) standard deviation.
|
||||
/// </param>
|
||||
public Stddev(object source, int period, bool isPopulation = false) : this(period, isPopulation) {
|
||||
public Stddev(object source, int period, bool isPopulation = false) : this(period, isPopulation)
|
||||
{
|
||||
var pubEvent = source.GetType().GetEvent("Pub");
|
||||
pubEvent?.AddEventHandler(source, new ValueSignal(Sub));
|
||||
}
|
||||
@@ -54,7 +58,8 @@ public class Stddev : AbstractBase {
|
||||
/// <summary>
|
||||
/// Initializes the Stddev instance by clearing the buffer.
|
||||
/// </summary>
|
||||
public override void Init() {
|
||||
public override void Init()
|
||||
{
|
||||
base.Init();
|
||||
_buffer.Clear();
|
||||
}
|
||||
@@ -63,8 +68,10 @@ public class Stddev : AbstractBase {
|
||||
/// Manages the state of the Stddev instance based on whether a new value is being processed.
|
||||
/// </summary>
|
||||
/// <param name="isNew">Indicates whether the current input is a new value.</param>
|
||||
protected override void ManageState(bool isNew) {
|
||||
if (isNew) {
|
||||
protected override void ManageState(bool isNew)
|
||||
{
|
||||
if (isNew)
|
||||
{
|
||||
_lastValidValue = Input.Value;
|
||||
_index++;
|
||||
}
|
||||
@@ -83,13 +90,15 @@ public class Stddev : AbstractBase {
|
||||
/// where x is each value, mean is the average of all values, and n is the number of values.
|
||||
/// If there's only one value in the buffer, the method returns 0.
|
||||
/// </remarks>
|
||||
protected override double Calculation() {
|
||||
protected override double Calculation()
|
||||
{
|
||||
ManageState(Input.IsNew);
|
||||
|
||||
_buffer.Add(Input.Value, Input.IsNew);
|
||||
|
||||
double stddev = 0;
|
||||
if (_buffer.Count > 1) {
|
||||
if (_buffer.Count > 1)
|
||||
{
|
||||
var values = _buffer.GetSpan().ToArray();
|
||||
double mean = values.Average();
|
||||
double sumOfSquaredDifferences = values.Sum(x => Math.Pow(x - mean, 2));
|
||||
|
||||
@@ -9,7 +9,8 @@ namespace QuanTAlib;
|
||||
/// variance based on the isPopulation parameter. It uses a circular buffer
|
||||
/// to efficiently manage the data points within the specified period.
|
||||
/// </remarks>
|
||||
public class Variance : AbstractBase {
|
||||
public class Variance : AbstractBase
|
||||
{
|
||||
private readonly int Period;
|
||||
private readonly bool IsPopulation;
|
||||
private readonly CircularBuffer _buffer;
|
||||
@@ -25,8 +26,10 @@ public class Variance : AbstractBase {
|
||||
/// <exception cref="ArgumentOutOfRangeException">
|
||||
/// Thrown when period is less than 2.
|
||||
/// </exception>
|
||||
public Variance(int period, bool isPopulation = false) : base() {
|
||||
if (period < 2) {
|
||||
public Variance(int period, bool isPopulation = false)
|
||||
{
|
||||
if (period < 2)
|
||||
{
|
||||
throw new ArgumentOutOfRangeException(nameof(period), "Period must be greater than or equal to 2.");
|
||||
}
|
||||
Period = period;
|
||||
@@ -46,7 +49,8 @@ public class Variance : AbstractBase {
|
||||
/// <param name="isPopulation">
|
||||
/// A flag indicating whether to calculate population (true) or sample (false) variance.
|
||||
/// </param>
|
||||
public Variance(object source, int period, bool isPopulation = false) : this(period, isPopulation) {
|
||||
public Variance(object source, int period, bool isPopulation = false) : this(period, isPopulation)
|
||||
{
|
||||
var pubEvent = source.GetType().GetEvent("Pub");
|
||||
pubEvent?.AddEventHandler(source, new ValueSignal(Sub));
|
||||
}
|
||||
@@ -54,7 +58,8 @@ public class Variance : AbstractBase {
|
||||
/// <summary>
|
||||
/// Initializes the Variance instance by clearing the buffer.
|
||||
/// </summary>
|
||||
public override void Init() {
|
||||
public override void Init()
|
||||
{
|
||||
base.Init();
|
||||
_buffer.Clear();
|
||||
}
|
||||
@@ -63,8 +68,10 @@ public class Variance : AbstractBase {
|
||||
/// Manages the state of the Variance instance based on whether a new value is being processed.
|
||||
/// </summary>
|
||||
/// <param name="isNew">Indicates whether the current input is a new value.</param>
|
||||
protected override void ManageState(bool isNew) {
|
||||
if (isNew) {
|
||||
protected override void ManageState(bool isNew)
|
||||
{
|
||||
if (isNew)
|
||||
{
|
||||
_lastValidValue = Input.Value;
|
||||
_index++;
|
||||
}
|
||||
@@ -83,13 +90,15 @@ public class Variance : AbstractBase {
|
||||
/// where x is each value, mean is the average of all values, and n is the number of values.
|
||||
/// If there's only one value in the buffer, the method returns 0.
|
||||
/// </remarks>
|
||||
protected override double Calculation() {
|
||||
protected override double Calculation()
|
||||
{
|
||||
ManageState(Input.IsNew);
|
||||
|
||||
_buffer.Add(Input.Value, Input.IsNew);
|
||||
|
||||
double variance = 0;
|
||||
if (_buffer.Count > 1) {
|
||||
if (_buffer.Count > 1)
|
||||
{
|
||||
var values = _buffer.GetSpan().ToArray();
|
||||
double mean = values.Average();
|
||||
double sumOfSquaredDifferences = values.Sum(x => Math.Pow(x - mean, 2));
|
||||
|
||||
+20
-10
@@ -9,7 +9,8 @@ namespace QuanTAlib;
|
||||
/// the most recent value in a given period. It uses a circular buffer to
|
||||
/// efficiently manage the data points within the specified period.
|
||||
/// </remarks>
|
||||
public class Zscore : AbstractBase {
|
||||
public class Zscore : AbstractBase
|
||||
{
|
||||
private readonly int Period;
|
||||
private readonly CircularBuffer _buffer;
|
||||
|
||||
@@ -20,8 +21,10 @@ public class Zscore : AbstractBase {
|
||||
/// <exception cref="ArgumentOutOfRangeException">
|
||||
/// Thrown when period is less than 2.
|
||||
/// </exception>
|
||||
public Zscore(int period) : base() {
|
||||
if (period < 2) {
|
||||
public Zscore(int period)
|
||||
{
|
||||
if (period < 2)
|
||||
{
|
||||
throw new ArgumentOutOfRangeException(nameof(period), "Period must be greater than or equal to 2 for Z-score calculation.");
|
||||
}
|
||||
Period = period;
|
||||
@@ -36,7 +39,8 @@ public class Zscore : AbstractBase {
|
||||
/// </summary>
|
||||
/// <param name="source">The source object to subscribe to for value updates.</param>
|
||||
/// <param name="period">The period over which to calculate the Z-score.</param>
|
||||
public Zscore(object source, int period) : this(period) {
|
||||
public Zscore(object source, int period) : this(period)
|
||||
{
|
||||
var pubEvent = source.GetType().GetEvent("Pub");
|
||||
pubEvent?.AddEventHandler(source, new ValueSignal(Sub));
|
||||
}
|
||||
@@ -44,7 +48,8 @@ public class Zscore : AbstractBase {
|
||||
/// <summary>
|
||||
/// Initializes the Zscore instance by clearing the buffer.
|
||||
/// </summary>
|
||||
public override void Init() {
|
||||
public override void Init()
|
||||
{
|
||||
base.Init();
|
||||
_buffer.Clear();
|
||||
}
|
||||
@@ -53,8 +58,10 @@ public class Zscore : AbstractBase {
|
||||
/// Manages the state of the Zscore instance based on whether a new value is being processed.
|
||||
/// </summary>
|
||||
/// <param name="isNew">Indicates whether the current input is a new value.</param>
|
||||
protected override void ManageState(bool isNew) {
|
||||
if (isNew) {
|
||||
protected override void ManageState(bool isNew)
|
||||
{
|
||||
if (isNew)
|
||||
{
|
||||
_lastValidValue = Input.Value;
|
||||
_index++;
|
||||
}
|
||||
@@ -72,13 +79,15 @@ public class Zscore : AbstractBase {
|
||||
/// where x is the input value, μ is the mean of the period, and σ is the sample standard deviation.
|
||||
/// If there are fewer than 2 data points or if the standard deviation is 0, the method returns 0.
|
||||
/// </remarks>
|
||||
protected override double Calculation() {
|
||||
protected override double Calculation()
|
||||
{
|
||||
ManageState(Input.IsNew);
|
||||
|
||||
_buffer.Add(Input.Value, Input.IsNew);
|
||||
|
||||
double zScore = 0;
|
||||
if (_buffer.Count >= 2) { // We need at least 2 data points for Z-score
|
||||
if (_buffer.Count >= 2)
|
||||
{ // We need at least 2 data points for Z-score
|
||||
var values = _buffer.GetSpan().ToArray();
|
||||
double mean = values.Average();
|
||||
double n = values.Length;
|
||||
@@ -86,7 +95,8 @@ public class Zscore : AbstractBase {
|
||||
double sumSquaredDeviations = values.Sum(x => Math.Pow(x - mean, 2));
|
||||
double standardDeviation = Math.Sqrt(sumSquaredDeviations / (n - 1)); // Sample standard deviation
|
||||
|
||||
if (standardDeviation != 0) { // Avoid division by zero
|
||||
if (standardDeviation != 0)
|
||||
{ // Avoid division by zero
|
||||
zScore = (Input.Value - mean) / standardDeviation;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,7 +9,8 @@ namespace QuanTAlib;
|
||||
/// both annualized and non-annualized volatility measures. The calculation uses a sample
|
||||
/// standard deviation formula and assumes 252 trading days in a year for annualization.
|
||||
/// </remarks>
|
||||
public class Historical : AbstractBase {
|
||||
public class Historical : AbstractBase
|
||||
{
|
||||
private readonly int Period;
|
||||
private readonly bool IsAnnualized;
|
||||
private readonly CircularBuffer _buffer;
|
||||
@@ -24,8 +25,10 @@ public class Historical : AbstractBase {
|
||||
/// <exception cref="ArgumentOutOfRangeException">
|
||||
/// Thrown when period is less than 2.
|
||||
/// </exception>
|
||||
public Historical(int period, bool isAnnualized = true) : base() {
|
||||
if (period < 2) {
|
||||
public Historical(int period, bool isAnnualized = true)
|
||||
{
|
||||
if (period < 2)
|
||||
{
|
||||
throw new ArgumentOutOfRangeException(nameof(period), "Period must be greater than or equal to 2.");
|
||||
}
|
||||
Period = period;
|
||||
@@ -43,7 +46,8 @@ public class Historical : AbstractBase {
|
||||
/// <param name="source">The source object to subscribe to for value updates.</param>
|
||||
/// <param name="period">The period over which to calculate historical volatility.</param>
|
||||
/// <param name="isAnnualized">Whether to annualize the volatility (default is true).</param>
|
||||
public Historical(object source, int period, bool isAnnualized = true) : this(period, isAnnualized) {
|
||||
public Historical(object source, int period, bool isAnnualized = true) : this(period, isAnnualized)
|
||||
{
|
||||
var pubEvent = source.GetType().GetEvent("Pub");
|
||||
pubEvent?.AddEventHandler(source, new ValueSignal(Sub));
|
||||
}
|
||||
@@ -51,7 +55,8 @@ public class Historical : AbstractBase {
|
||||
/// <summary>
|
||||
/// Initializes the Historical instance by clearing buffers and resetting the previous close value.
|
||||
/// </summary>
|
||||
public override void Init() {
|
||||
public override void Init()
|
||||
{
|
||||
base.Init();
|
||||
_buffer.Clear();
|
||||
_logReturns.Clear();
|
||||
@@ -62,8 +67,10 @@ public class Historical : AbstractBase {
|
||||
/// Manages the state of the Historical instance based on whether a new value is being processed.
|
||||
/// </summary>
|
||||
/// <param name="isNew">Indicates whether the current input is a new value.</param>
|
||||
protected override void ManageState(bool isNew) {
|
||||
if (isNew) {
|
||||
protected override void ManageState(bool isNew)
|
||||
{
|
||||
if (isNew)
|
||||
{
|
||||
_lastValidValue = Input.Value;
|
||||
_index++;
|
||||
}
|
||||
@@ -82,19 +89,23 @@ public class Historical : AbstractBase {
|
||||
/// 3. If annualized, multiply by the square root of 252 (assumed trading days in a year).
|
||||
/// The method returns 0 until enough data points are available for the calculation.
|
||||
/// </remarks>
|
||||
protected override double Calculation() {
|
||||
protected override double Calculation()
|
||||
{
|
||||
ManageState(Input.IsNew);
|
||||
|
||||
_buffer.Add(Input.Value, Input.IsNew);
|
||||
|
||||
double volatility = 0;
|
||||
if (_buffer.Count > 1) {
|
||||
if (_previousClose != 0) {
|
||||
if (_buffer.Count > 1)
|
||||
{
|
||||
if (_previousClose != 0)
|
||||
{
|
||||
double logReturn = Math.Log(Input.Value / _previousClose);
|
||||
_logReturns.Add(logReturn, Input.IsNew);
|
||||
}
|
||||
|
||||
if (_logReturns.Count == Period) {
|
||||
if (_logReturns.Count == Period)
|
||||
{
|
||||
var returns = _logReturns.GetSpan().ToArray();
|
||||
double mean = returns.Average();
|
||||
double sumOfSquaredDifferences = returns.Sum(x => Math.Pow(x - mean, 2));
|
||||
@@ -102,7 +113,8 @@ public class Historical : AbstractBase {
|
||||
double variance = sumOfSquaredDifferences / (Period - 1); // Using sample standard deviation
|
||||
volatility = Math.Sqrt(variance);
|
||||
|
||||
if (IsAnnualized) {
|
||||
if (IsAnnualized)
|
||||
{
|
||||
// Assuming 252 trading days in a year. Adjust as needed.
|
||||
volatility *= Math.Sqrt(252);
|
||||
}
|
||||
|
||||
+22
-11
@@ -9,7 +9,8 @@ namespace QuanTAlib;
|
||||
/// both annualized and non-annualized volatility measures. The calculation uses a rolling
|
||||
/// sum of squared returns for efficiency and assumes 252 trading days in a year for annualization.
|
||||
/// </remarks>
|
||||
public class Realized : AbstractBase {
|
||||
public class Realized : AbstractBase
|
||||
{
|
||||
private readonly int Period;
|
||||
private readonly bool IsAnnualized;
|
||||
private readonly CircularBuffer _returns;
|
||||
@@ -24,8 +25,10 @@ public class Realized : AbstractBase {
|
||||
/// <exception cref="ArgumentOutOfRangeException">
|
||||
/// Thrown when period is less than 2.
|
||||
/// </exception>
|
||||
public Realized(int period, bool isAnnualized = true) : base() {
|
||||
if (period < 2) {
|
||||
public Realized(int period, bool isAnnualized = true)
|
||||
{
|
||||
if (period < 2)
|
||||
{
|
||||
throw new ArgumentOutOfRangeException(nameof(period), "Period must be greater than or equal to 2.");
|
||||
}
|
||||
Period = period;
|
||||
@@ -39,7 +42,8 @@ public class Realized : AbstractBase {
|
||||
/// <summary>
|
||||
/// Initializes the Realized instance by clearing buffers and resetting calculation variables.
|
||||
/// </summary>
|
||||
public override void Init() {
|
||||
public override void Init()
|
||||
{
|
||||
base.Init();
|
||||
_returns.Clear();
|
||||
_previousClose = 0;
|
||||
@@ -50,8 +54,10 @@ public class Realized : AbstractBase {
|
||||
/// Manages the state of the Realized instance based on whether a new value is being processed.
|
||||
/// </summary>
|
||||
/// <param name="isNew">Indicates whether the current input is a new value.</param>
|
||||
protected override void ManageState(bool isNew) {
|
||||
if (isNew) {
|
||||
protected override void ManageState(bool isNew)
|
||||
{
|
||||
if (isNew)
|
||||
{
|
||||
_lastValidValue = Input.Value;
|
||||
_index++;
|
||||
}
|
||||
@@ -72,14 +78,17 @@ public class Realized : AbstractBase {
|
||||
/// 5. If annualized, multiply by the square root of 252 (assumed trading days in a year).
|
||||
/// The method returns 0 until enough data points are available for the calculation.
|
||||
/// </remarks>
|
||||
protected override double Calculation() {
|
||||
protected override double Calculation()
|
||||
{
|
||||
ManageState(Input.IsNew);
|
||||
|
||||
double volatility = 0;
|
||||
if (_previousClose != 0) {
|
||||
if (_previousClose != 0)
|
||||
{
|
||||
double logReturn = Math.Log(Input.Value / _previousClose);
|
||||
|
||||
if (_returns.Count == Period) {
|
||||
if (_returns.Count == Period)
|
||||
{
|
||||
// Remove the oldest squared return from the sum
|
||||
_sumSquaredReturns -= Math.Pow(_returns[0], 2);
|
||||
}
|
||||
@@ -87,11 +96,13 @@ public class Realized : AbstractBase {
|
||||
_returns.Add(logReturn, Input.IsNew);
|
||||
_sumSquaredReturns += Math.Pow(logReturn, 2);
|
||||
|
||||
if (_returns.Count == Period) {
|
||||
if (_returns.Count == Period)
|
||||
{
|
||||
double variance = _sumSquaredReturns / Period;
|
||||
volatility = Math.Sqrt(variance);
|
||||
|
||||
if (IsAnnualized) {
|
||||
if (IsAnnualized)
|
||||
{
|
||||
// Assuming 252 trading days in a year. Adjust as needed.
|
||||
volatility *= Math.Sqrt(252);
|
||||
}
|
||||
|
||||
+19
-15
@@ -13,10 +13,11 @@ namespace QuanTAlib;
|
||||
/// This implementation uses a combination of Standard Deviation and Simple Moving Average
|
||||
/// calculations to compute the RVI.
|
||||
/// </remarks>
|
||||
public class Rvi : AbstractBase {
|
||||
public class Rvi : AbstractBase
|
||||
{
|
||||
private readonly int Period;
|
||||
private Stddev _upStdDev, _downStdDev;
|
||||
private Sma _upSma, _downSma;
|
||||
private readonly Stddev _upStdDev, _downStdDev;
|
||||
private readonly Sma _upSma, _downSma;
|
||||
private double _previousClose;
|
||||
|
||||
/// <summary>
|
||||
@@ -26,8 +27,10 @@ public class Rvi : AbstractBase {
|
||||
/// <exception cref="ArgumentOutOfRangeException">
|
||||
/// Thrown when period is less than 2.
|
||||
/// </exception>
|
||||
public Rvi(int period) : base() {
|
||||
if (period < 2) {
|
||||
public Rvi(int period)
|
||||
{
|
||||
if (period < 2)
|
||||
{
|
||||
throw new ArgumentOutOfRangeException(nameof(period), "Period must be greater than or equal to 2.");
|
||||
}
|
||||
Period = period;
|
||||
@@ -45,7 +48,8 @@ public class Rvi : AbstractBase {
|
||||
/// </summary>
|
||||
/// <param name="source">The source object to subscribe to for value updates.</param>
|
||||
/// <param name="period">The period over which to calculate the RVI.</param>
|
||||
public Rvi(object source, int period) : this(period) {
|
||||
public Rvi(object source, int period) : this(period)
|
||||
{
|
||||
var pubEvent = source.GetType().GetEvent("Pub");
|
||||
pubEvent?.AddEventHandler(source, new ValueSignal(Sub));
|
||||
}
|
||||
@@ -53,7 +57,8 @@ public class Rvi : AbstractBase {
|
||||
/// <summary>
|
||||
/// Initializes the Rvi instance by setting up the initial state.
|
||||
/// </summary>
|
||||
public override void Init() {
|
||||
public override void Init()
|
||||
{
|
||||
base.Init();
|
||||
_previousClose = 0;
|
||||
}
|
||||
@@ -62,8 +67,10 @@ public class Rvi : AbstractBase {
|
||||
/// Manages the state of the Rvi instance based on whether a new value is being processed.
|
||||
/// </summary>
|
||||
/// <param name="isNew">Indicates whether the current input is a new value.</param>
|
||||
protected override void ManageState(bool isNew) {
|
||||
if (isNew) {
|
||||
protected override void ManageState(bool isNew)
|
||||
{
|
||||
if (isNew)
|
||||
{
|
||||
_lastValidValue = Value;
|
||||
_index++;
|
||||
}
|
||||
@@ -84,7 +91,8 @@ public class Rvi : AbstractBase {
|
||||
/// 5. Compute the RVI as a percentage of up volatility to total volatility.
|
||||
/// The method returns 0 if the sum of up and down volatility is zero.
|
||||
/// </remarks>
|
||||
protected override double Calculation() {
|
||||
protected override double Calculation()
|
||||
{
|
||||
ManageState(Input.IsNew);
|
||||
|
||||
double close = Input.Value;
|
||||
@@ -97,11 +105,7 @@ public class Rvi : AbstractBase {
|
||||
_downSma.Calc(_downStdDev.Calc(new TValue(Input.Time, downMove, Input.IsNew)));
|
||||
|
||||
double rvi;
|
||||
if (_upSma.Value + _downSma.Value != 0) {
|
||||
rvi = 100 * _upSma.Value / (_upSma.Value + _downSma.Value);
|
||||
} else {
|
||||
rvi = 0;
|
||||
}
|
||||
rvi = (_upSma.Value + _downSma.Value != 0) ? 100 * _upSma.Value / (_upSma.Value + _downSma.Value) : 0;
|
||||
|
||||
_previousClose = close;
|
||||
IsHot = _index >= WarmupPeriod;
|
||||
|
||||
Reference in New Issue
Block a user