feat: Refactor TBar and TValue to record structs; improve readability and performance

fix: Update condition checks in Htit and Mgdi for better numerical stability
refactor: Simplify parameter struct in T3 and enhance Vidya state management
This commit is contained in:
Miha Kralj
2025-12-17 07:27:45 -08:00
parent d277e08056
commit 8cec2fec8d
6 changed files with 20 additions and 100 deletions
+2 -2
View File
@@ -220,7 +220,7 @@ public sealed class Htit : AbstractBase
private double CalculatePeriod(double prevPeriod)
{
double period = 0;
if (_state.Im != 0 && _state.Re != 0)
if (Math.Abs(_state.Im) > 1e-9 && Math.Abs(_state.Re) > 1e-9)
{
period = 2 * Math.PI / Math.Atan(_state.Im / _state.Re);
}
@@ -372,7 +372,7 @@ public sealed class Htit : AbstractBase
// 7. Calculate Period
double period = 0;
if (im != 0 && re != 0)
if (Math.Abs(im) > 1e-9 && Math.Abs(re) > 1e-9)
{
period = 2 * Math.PI / Math.Atan(im / re);
}
+10 -5
View File
@@ -33,7 +33,7 @@ public sealed class Mgdi : AbstractBase
public Mgdi(int period = 14, double k = 0.6)
{
if (period < 1) throw new ArgumentOutOfRangeException(nameof(period));
ArgumentOutOfRangeException.ThrowIfLessThan(period, 1);
if (double.IsNaN(k) || double.IsInfinity(k) || k <= 0) throw new ArgumentOutOfRangeException(nameof(k), "k must be a finite value greater than 0");
_period = period;
_k = k;
@@ -57,10 +57,15 @@ public sealed class Mgdi : AbstractBase
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public override TValue Update(TValue input, bool isNew = true)
{
if (isNew) _p_state = _state;
else _state = _p_state;
if (isNew) _state.Count++;
if (isNew)
{
_p_state = _state;
_state.Count++;
}
else
{
_state = _p_state;
}
double price = input.Value;
if (!double.IsFinite(price))
+1 -29
View File
@@ -31,35 +31,7 @@ public sealed class T3 : AbstractBase
public static State New() => new() { IsInitialized = false };
}
private readonly struct Parameters : IEquatable<Parameters>
{
public readonly double Alpha;
public readonly double C1, C2, C3, C4;
public Parameters(double alpha, double c1, double c2, double c3, double c4)
{
Alpha = alpha;
C1 = c1;
C2 = c2;
C3 = c3;
C4 = c4;
}
public override bool Equals(object? obj) => obj is Parameters other && Equals(other);
#pragma warning disable S1244 // Do not check floating point equality with exact values
public bool Equals(Parameters other) =>
Alpha == other.Alpha &&
C1 == other.C1 && C2 == other.C2 &&
C3 == other.C3 && C4 == other.C4;
#pragma warning restore S1244 // Do not check floating point equality with exact values
public override int GetHashCode() => HashCode.Combine(Alpha, C1, C2, C3, C4);
public static bool operator ==(Parameters left, Parameters right) => left.Equals(right);
public static bool operator !=(Parameters left, Parameters right) => !left.Equals(right);
}
private readonly record struct Parameters(double Alpha, double C1, double C2, double C3, double C4);
private readonly Parameters _params;
private State _state = State.New();
+1 -2
View File
@@ -67,14 +67,13 @@ public sealed class Vidya : AbstractBase
if (isNew)
{
_p_state = _state;
_state.BarCount++;
}
else
{
_state = _p_state;
}
if (isNew) _state.BarCount++;
if (_state.IsInitialized)
{
_state.PrevClose = _state.CurrentClose;