mirror of
https://github.com/mihakralj/QuanTAlib.git
synced 2026-08-15 00:58: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:
@@ -19,12 +19,14 @@ namespace QuanTAlib;
|
||||
[SkipLocalsInit]
|
||||
public sealed class Ssf : AbstractBase
|
||||
{
|
||||
[StructLayout(LayoutKind.Auto)]
|
||||
private record struct State(double Ssf1, double Ssf2, double PrevInput, double LastValidValue, int Count, bool IsHot)
|
||||
{
|
||||
public static State New() => new() { Ssf1 = 0, Ssf2 = 0, PrevInput = 0, LastValidValue = 0, Count = 0, IsHot = false };
|
||||
}
|
||||
|
||||
private readonly double _c1, _c2, _c3;
|
||||
private readonly TValuePublishedHandler _handler;
|
||||
private State _state = State.New();
|
||||
private State _p_state = State.New();
|
||||
|
||||
@@ -52,6 +54,7 @@ public sealed class Ssf : AbstractBase
|
||||
|
||||
Name = $"Ssf({period})";
|
||||
WarmupPeriod = period;
|
||||
_handler = Handle;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -61,7 +64,7 @@ public sealed class Ssf : AbstractBase
|
||||
/// <param name="period">Period for SSF calculation</param>
|
||||
public Ssf(ITValuePublisher source, int period) : this(period)
|
||||
{
|
||||
source.Pub += (item) => Update(item);
|
||||
source.Pub += _handler;
|
||||
}
|
||||
|
||||
public Ssf(TSeries source, int period) : this(period)
|
||||
@@ -71,9 +74,11 @@ public sealed class Ssf : AbstractBase
|
||||
{
|
||||
Last = new TValue(source.LastTime, Last.Value);
|
||||
}
|
||||
source.Pub += (item) => Update(item);
|
||||
source.Pub += _handler;
|
||||
}
|
||||
|
||||
private void Handle(object? sender, TValueEventArgs e) => Update(e.Value, e.IsNew);
|
||||
|
||||
public override bool IsHot => _state.IsHot;
|
||||
|
||||
public override void Prime(ReadOnlySpan<double> source)
|
||||
@@ -171,7 +176,7 @@ public sealed class Ssf : AbstractBase
|
||||
_state.IsHot = true;
|
||||
|
||||
Last = new TValue(input.Time, ssf);
|
||||
PubEvent(Last);
|
||||
PubEvent(Last, isNew);
|
||||
return Last;
|
||||
}
|
||||
|
||||
@@ -276,7 +281,7 @@ public sealed class Ssf : AbstractBase
|
||||
double c1 = 1.0 - c2 - c3;
|
||||
|
||||
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));
|
||||
|
||||
if (source.Length == 0) return;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user