mirror of
https://github.com/mihakralj/QuanTAlib.git
synced 2026-08-16 09:38:05 +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:
@@ -57,7 +57,7 @@ public class MamaTests
|
||||
|
||||
// Manually chain for test
|
||||
bool eventFired = false;
|
||||
mama.Pub += (item) => eventFired = true;
|
||||
mama.Pub += (object? sender, TValueEventArgs args) => eventFired = true;
|
||||
|
||||
mama.Update(new TValue(DateTime.UtcNow, 100.0));
|
||||
|
||||
|
||||
@@ -18,7 +18,9 @@ public sealed class Mama : AbstractBase
|
||||
private readonly double _fastLimit;
|
||||
private readonly double _slowLimit;
|
||||
private readonly double _scaledFastLimit;
|
||||
private readonly TValuePublishedHandler _handler;
|
||||
|
||||
[StructLayout(LayoutKind.Auto)]
|
||||
private record struct State(
|
||||
double Period, double Phase, double Mama, double Fama, double SumPr,
|
||||
double I2, double Q2, double Re, double Im, double LastValidPrice, int Index
|
||||
@@ -55,7 +57,7 @@ public sealed class Mama : AbstractBase
|
||||
{
|
||||
if (fastLimit <= slowLimit || fastLimit <= 0 || slowLimit <= 0)
|
||||
{
|
||||
throw new ArgumentException("FastLimit must be > SlowLimit and > 0");
|
||||
throw new ArgumentException("FastLimit must be > SlowLimit and > 0", nameof(fastLimit));
|
||||
}
|
||||
_fastLimit = fastLimit;
|
||||
_slowLimit = slowLimit;
|
||||
@@ -69,14 +71,17 @@ public sealed class Mama : AbstractBase
|
||||
|
||||
Name = $"Mama({fastLimit:F2},{slowLimit:F2})";
|
||||
WarmupPeriod = 50;
|
||||
_handler = Handle;
|
||||
Init();
|
||||
}
|
||||
|
||||
public Mama(ITValuePublisher source, double fastLimit = 0.5, double slowLimit = 0.05) : this(fastLimit, slowLimit)
|
||||
{
|
||||
source.Pub += (item) => Update(item);
|
||||
source.Pub += _handler;
|
||||
}
|
||||
|
||||
private void Handle(object? sender, TValueEventArgs e) => Update(e.Value, e.IsNew);
|
||||
|
||||
private void Init()
|
||||
{
|
||||
Reset();
|
||||
@@ -229,7 +234,7 @@ public sealed class Mama : AbstractBase
|
||||
double mama = Step(input.Value, isNew);
|
||||
Last = new TValue(input.Time, mama);
|
||||
Fama = new TValue(input.Time, _state.Fama);
|
||||
PubEvent(Last);
|
||||
PubEvent(Last, isNew);
|
||||
return Last;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user