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
+8 -6
View File
@@ -30,7 +30,7 @@ public sealed class Trima : AbstractBase, IDisposable
private readonly int _period;
private readonly Sma _sma1;
private readonly Sma _sma2;
private readonly Action<TValue> _updateHandler;
private readonly TValuePublishedHandler _handler;
private ITValuePublisher? _publisher;
public Trima(int period)
@@ -43,7 +43,7 @@ public sealed class Trima : AbstractBase, IDisposable
_sma1 = new Sma(p1);
_sma2 = new Sma(p2);
_updateHandler = (item) => Update(item);
_handler = Handle;
Name = $"Trima({period})";
WarmupPeriod = p1 + p2 - 1;
@@ -52,14 +52,14 @@ public sealed class Trima : AbstractBase, IDisposable
public Trima(ITValuePublisher source, int period) : this(period)
{
_publisher = source;
source.Pub += _updateHandler;
source.Pub += _handler;
}
public void Dispose()
{
if (_publisher != null)
{
_publisher.Pub -= _updateHandler;
_publisher.Pub -= _handler;
_publisher = null;
}
}
@@ -73,7 +73,7 @@ public sealed class Trima : AbstractBase, IDisposable
TValue v2 = _sma2.Update(v1, isNew);
Last = v2;
PubEvent(Last);
PubEvent(Last, isNew);
return Last;
}
@@ -99,6 +99,8 @@ public sealed class Trima : AbstractBase, IDisposable
return new TSeries(t, v);
}
private void Handle(object? sender, TValueEventArgs args) => Update(args.Value, args.IsNew);
public override void Prime(ReadOnlySpan<double> source)
{
_sma1.Reset();
@@ -138,7 +140,7 @@ public sealed class Trima : AbstractBase, IDisposable
public static void Batch(ReadOnlySpan<double> source, Span<double> output, int 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));
if (period <= 0)
throw new ArgumentException("Period must be greater than 0", nameof(period));