mirror of
https://github.com/mihakralj/QuanTAlib.git
synced 2026-08-15 09:08:04 +00:00
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:
@@ -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
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user