clean code fixes

This commit is contained in:
Miha Kralj
2024-10-06 17:16:47 -07:00
parent bcac34bf09
commit b7b5a4a1bf
52 changed files with 787 additions and 643 deletions
+145 -156
View File
@@ -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;
}
}
+1 -1
View File
@@ -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)
{
+2 -2
View File
@@ -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)
{
+1 -1
View File
@@ -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
View File
@@ -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
View File
@@ -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;
}
}
+4 -6
View File
@@ -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;
}
}
+1 -1
View File
@@ -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
View File
@@ -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 -3
View File
@@ -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)
{
+1 -1
View File
@@ -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.");
+3 -2
View File
@@ -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;
+3 -4
View File
@@ -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})";
+1 -1
View File
@@ -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
View File
@@ -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;
}
}
+1 -1
View File
@@ -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 )
{
+1 -1
View File
@@ -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
View File
@@ -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
View File
@@ -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)
{
+1 -1
View File
@@ -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)
{
+2 -2
View File
@@ -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;
+1 -8
View File
@@ -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];
}
+4 -4
View File
@@ -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;