Refactor event handling and improve argument validation across indicators

- Updated event handler signatures to use TValueEventArgs for consistency in Mama, Mgdi, Pwma, Rma, Sma, Ssf, Super, T3, Tema, Trima, Usf, Vidya, Wma, and Atr classes.
- Enhanced argument validation by specifying parameter names in exceptions for clarity.
- Adjusted tests to align with new event handler signatures.
- Improved code readability and maintainability by using structured records and lambda expressions.
This commit is contained in:
Miha Kralj
2025-12-27 15:46:28 -08:00
parent 4750c2b1e8
commit d7dbd7078a
73 changed files with 502 additions and 300 deletions
@@ -111,7 +111,7 @@ public sealed class BilateralValidationTests : IDisposable
_output.WriteLine("Bilateral Span validated successfully against Reference");
}
private List<double> GetReferenceData(int period, double sigmaSRatio, double sigmaRMult)
private IReadOnlyList<double> GetReferenceData(int period, double sigmaSRatio, double sigmaRMult)
{
var reference = new BilateralReference(period, sigmaSRatio, sigmaRMult);
var results = new List<double>();
@@ -180,7 +180,7 @@ public sealed class BilateralValidationTests : IDisposable
return sumWeights < 1e-10 ? centerVal : sumWeightedSrc / sumWeights;
}
private static double CalculateStDev(List<double> values)
private static double CalculateStDev(IReadOnlyList<double> values)
{
if (values.Count < 2) return 0;
+8 -2
View File
@@ -28,9 +28,11 @@ public sealed class Bilateral : AbstractBase
private readonly RingBuffer _buffer;
private readonly double[] _spatialWeights;
[StructLayout(LayoutKind.Auto)]
private record struct State(double SumSq, double LastValidValue);
private State _state;
private State _p_state;
private readonly TValuePublishedHandler _handler;
/// <summary>
/// Creates a Bilateral Filter with specified parameters.
@@ -49,6 +51,7 @@ public sealed class Bilateral : AbstractBase
_buffer = new RingBuffer(period);
Name = $"Bilateral({period}, {sigmaSRatio:F2}, {sigmaRMult:F2})";
WarmupPeriod = period;
_handler = Handle;
_spatialWeights = new double[period];
PrecalculateSpatialWeights();
@@ -57,7 +60,7 @@ public sealed class Bilateral : AbstractBase
public Bilateral(ITValuePublisher source, int period, double sigmaSRatio = 0.5, double sigmaRMult = 1.0)
: this(period, sigmaSRatio, sigmaRMult)
{
source.Pub += (item) => Update(item);
source.Pub += _handler;
}
public override bool IsHot => _buffer.IsFull;
@@ -113,6 +116,9 @@ public sealed class Bilateral : AbstractBase
_p_state = _state;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private void Handle(object? sender, TValueEventArgs args) => Update(args.Value, args.IsNew);
public override TSeries Update(TSeries source)
{
if (source.Count == 0) return [];
@@ -171,7 +177,7 @@ public sealed class Bilateral : AbstractBase
double result = CalculateBilateral();
Last = new TValue(input.Time, result);
PubEvent(Last);
PubEvent(Last, isNew);
return Last;
}