mirror of
https://github.com/mihakralj/QuanTAlib.git
synced 2026-08-09 06:27:45 +00:00
dependabot
This commit is contained in:
+153
-80
@@ -1,89 +1,162 @@
|
||||
namespace QuanTAlib;
|
||||
using System;
|
||||
|
||||
public class Afirma : AbstractBase
|
||||
namespace QuanTAlib
|
||||
{
|
||||
private readonly int Period;
|
||||
private readonly CircularBuffer _buffer;
|
||||
private readonly double _alpha; // Adaptive factor
|
||||
private double _lastAfirma, _p_lastAfirma;
|
||||
private double _lastError, _p_lastError;
|
||||
|
||||
public Afirma(int period, double alpha = 0.1)
|
||||
public class Afirma : AbstractBase
|
||||
{
|
||||
if (period < 1)
|
||||
|
||||
public enum WindowType
|
||||
{
|
||||
throw new ArgumentOutOfRangeException(nameof(period), "Period must be greater than or equal to 1.");
|
||||
Rectangular,
|
||||
Hanning1,
|
||||
Hanning2,
|
||||
Blackman,
|
||||
BlackmanHarris
|
||||
}
|
||||
if (alpha <= 0 || alpha >= 1)
|
||||
|
||||
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)
|
||||
{
|
||||
throw new ArgumentOutOfRangeException(nameof(alpha), "Alpha must be between 0 and 1 (exclusive).");
|
||||
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();
|
||||
}
|
||||
Period = period;
|
||||
WarmupPeriod = period;
|
||||
_buffer = new CircularBuffer(period);
|
||||
_alpha = alpha;
|
||||
Name = "Afirma";
|
||||
WarmupPeriod = period;
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public Afirma(object source, int period, double alpha = 0.1) : this(period: period, alpha: alpha)
|
||||
{
|
||||
var pubEvent = source.GetType().GetEvent("Pub");
|
||||
pubEvent?.AddEventHandler(source, new ValueSignal(Sub));
|
||||
}
|
||||
|
||||
public override void Init()
|
||||
{
|
||||
base.Init();
|
||||
_lastAfirma = 0;
|
||||
_lastError = 0;
|
||||
}
|
||||
|
||||
protected override void ManageState(bool isNew)
|
||||
{
|
||||
if (isNew)
|
||||
{
|
||||
_lastValidValue = Input.Value;
|
||||
_index++;
|
||||
_p_lastAfirma = _lastAfirma;
|
||||
_p_lastError = _lastError;
|
||||
}
|
||||
else
|
||||
{
|
||||
_lastAfirma = _p_lastAfirma;
|
||||
_lastError = _p_lastError;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Core AFIRMA calculation
|
||||
/// </summary>
|
||||
protected override double Calculation()
|
||||
{
|
||||
double result;
|
||||
ManageState(IsNew);
|
||||
_buffer.Add(Input.Value, Input.IsNew);
|
||||
|
||||
if (_index < Period)
|
||||
{
|
||||
// Use simple average during warmup period
|
||||
result = _buffer.Average();
|
||||
}
|
||||
else
|
||||
{
|
||||
// AFIRMA calculation
|
||||
double sma = _buffer.Average();
|
||||
double error = Input.Value - _lastAfirma;
|
||||
double denominator = Math.Abs(error) + Math.Abs(_lastError);
|
||||
double adaptiveFactor = denominator != 0 ? _alpha * Math.Abs(error) / denominator : _alpha;
|
||||
result = sma + adaptiveFactor * (Input.Value - sma);
|
||||
|
||||
_lastError = error;
|
||||
}
|
||||
|
||||
_lastAfirma = result;
|
||||
IsHot = _index >= WarmupPeriod;
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,89 @@
|
||||
namespace QuanTAlib;
|
||||
|
||||
public class Ama : AbstractBase
|
||||
{
|
||||
private readonly int Period;
|
||||
private readonly CircularBuffer _buffer;
|
||||
private readonly double _alpha; // Adaptive factor
|
||||
private double _lastAfirma, _p_lastAfirma;
|
||||
private double _lastError, _p_lastError;
|
||||
|
||||
public Ama(int period, double alpha = 0.1)
|
||||
{
|
||||
if (period < 1)
|
||||
{
|
||||
throw new ArgumentOutOfRangeException(nameof(period), "Period must be greater than or equal to 1.");
|
||||
}
|
||||
if (alpha <= 0 || alpha >= 1)
|
||||
{
|
||||
throw new ArgumentOutOfRangeException(nameof(alpha), "Alpha must be between 0 and 1 (exclusive).");
|
||||
}
|
||||
Period = period;
|
||||
WarmupPeriod = period;
|
||||
_buffer = new CircularBuffer(period);
|
||||
_alpha = alpha;
|
||||
Name = "Afirma";
|
||||
WarmupPeriod = period;
|
||||
Init();
|
||||
}
|
||||
|
||||
public Ama(object source, int period, double alpha = 0.1) : this(period: period, alpha: alpha)
|
||||
{
|
||||
var pubEvent = source.GetType().GetEvent("Pub");
|
||||
pubEvent?.AddEventHandler(source, new ValueSignal(Sub));
|
||||
}
|
||||
|
||||
public override void Init()
|
||||
{
|
||||
base.Init();
|
||||
_lastAfirma = 0;
|
||||
_lastError = 0;
|
||||
}
|
||||
|
||||
protected override void ManageState(bool isNew)
|
||||
{
|
||||
if (isNew)
|
||||
{
|
||||
_lastValidValue = Input.Value;
|
||||
_index++;
|
||||
_p_lastAfirma = _lastAfirma;
|
||||
_p_lastError = _lastError;
|
||||
}
|
||||
else
|
||||
{
|
||||
_lastAfirma = _p_lastAfirma;
|
||||
_lastError = _p_lastError;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Core AFIRMA calculation
|
||||
/// </summary>
|
||||
protected override double Calculation()
|
||||
{
|
||||
double result;
|
||||
ManageState(IsNew);
|
||||
_buffer.Add(Input.Value, Input.IsNew);
|
||||
|
||||
if (_index < Period)
|
||||
{
|
||||
// Use simple average during warmup period
|
||||
result = _buffer.Average();
|
||||
}
|
||||
else
|
||||
{
|
||||
// AFIRMA calculation
|
||||
double sma = _buffer.Average();
|
||||
double error = Input.Value - _lastAfirma;
|
||||
double denominator = Math.Abs(error) + Math.Abs(_lastError);
|
||||
double adaptiveFactor = denominator != 0 ? _alpha * Math.Abs(error) / denominator : _alpha;
|
||||
result = sma + adaptiveFactor * (Input.Value - sma);
|
||||
|
||||
_lastError = error;
|
||||
}
|
||||
|
||||
_lastAfirma = result;
|
||||
IsHot = _index >= WarmupPeriod;
|
||||
return result;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user