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:
@@ -20,12 +20,14 @@ namespace QuanTAlib;
|
||||
[SkipLocalsInit]
|
||||
public sealed class Usf : AbstractBase
|
||||
{
|
||||
[StructLayout(LayoutKind.Auto)]
|
||||
private record struct State(double Usf1, double Usf2, double PrevInput1, double PrevInput2, double LastValidValue, int Count, bool IsHot)
|
||||
{
|
||||
public static State New() => new() { Usf1 = 0, Usf2 = 0, PrevInput1 = 0, PrevInput2 = 0, LastValidValue = double.NaN, 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();
|
||||
|
||||
@@ -48,6 +50,7 @@ public sealed class Usf : AbstractBase
|
||||
|
||||
Name = $"Usf({period})";
|
||||
WarmupPeriod = period;
|
||||
_handler = Handle;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -57,7 +60,7 @@ public sealed class Usf : AbstractBase
|
||||
/// <param name="period">Period for USF calculation</param>
|
||||
public Usf(ITValuePublisher source, int period) : this(period)
|
||||
{
|
||||
source.Pub += (item) => Update(item);
|
||||
source.Pub += _handler;
|
||||
}
|
||||
|
||||
public Usf(TSeries source, int period) : this(period)
|
||||
@@ -67,9 +70,11 @@ public sealed class Usf : 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 Usf : AbstractBase
|
||||
_state.IsHot = true;
|
||||
|
||||
Last = new TValue(input.Time, usf);
|
||||
PubEvent(Last);
|
||||
PubEvent(Last, isNew);
|
||||
return Last;
|
||||
}
|
||||
|
||||
@@ -278,7 +283,7 @@ public sealed class Usf : AbstractBase
|
||||
double c1 = (1.0 + c2 - c3) / 4.0;
|
||||
|
||||
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