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
+2 -1
View File
@@ -173,7 +173,7 @@ public class T3Tests
private class TestPublisher : ITValuePublisher
{
public event Action<TValue>? Pub;
public event TValuePublishedHandler? Pub;
public int SubscriberCount => Pub?.GetInvocationList().Length ?? 0;
}
@@ -222,3 +222,4 @@ public class T3Tests
Assert.Null(exception);
}
}
+8 -4
View File
@@ -27,11 +27,13 @@ namespace QuanTAlib;
[SkipLocalsInit]
public sealed class T3 : AbstractBase, IDisposable
{
[StructLayout(LayoutKind.Auto)]
private record struct State(double E1, double E2, double E3, double E4, double E5, double E6, bool IsInitialized)
{
public static State New() => new() { IsInitialized = false };
}
[StructLayout(LayoutKind.Auto)]
private readonly record struct Parameters(double Alpha, double C1, double C2, double C3, double C4);
private readonly Parameters _params;
@@ -40,7 +42,7 @@ public sealed class T3 : AbstractBase, IDisposable
private double _lastValidValue;
private double _p_lastValidValue;
private ITValuePublisher? _publisher;
private Action<TValue>? _handler;
private TValuePublishedHandler? _handler;
/// <summary>
/// Creates T3 with specified period and volume factor.
@@ -80,7 +82,7 @@ public sealed class T3 : AbstractBase, IDisposable
public T3(ITValuePublisher source, int period, double vfactor = 0.7) : this(period, vfactor)
{
_publisher = source;
_handler = (item) => Update(item);
_handler = Handle;
source.Pub += _handler;
}
@@ -98,10 +100,12 @@ public sealed class T3 : AbstractBase, IDisposable
{
Last = new TValue(source.LastTime, Last.Value);
}
_handler = (item) => Update(item);
_handler = Handle;
_publisher.Pub += _handler;
}
private void Handle(object? sender, TValueEventArgs e) => Update(e.Value, e.IsNew);
/// <summary>
/// True if the T3 has been initialized (received at least one value).
/// </summary>
@@ -285,7 +289,7 @@ public sealed class T3 : AbstractBase, IDisposable
if (period <= 0)
throw new ArgumentException("Period must be greater than 0", nameof(period));
if (source.Length != output.Length)
throw new ArgumentException("Source and output must have the same length");
throw new ArgumentException("Source and output must have the same length", nameof(output));
double alpha = 2.0 / (period + 1);
double v = vfactor;