mirror of
https://github.com/mihakralj/QuanTAlib.git
synced 2026-08-12 23: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:
@@ -139,7 +139,8 @@ public class BlmaTests
|
||||
var input = new double[] { 1, 2, 3, 4, 5 };
|
||||
var timestamps = new List<DateTime>();
|
||||
|
||||
blma.Pub += (item) => timestamps.Add(item.AsDateTime);
|
||||
blma.Pub += (object? sender, TValueEventArgs args) => timestamps.Add(args.Value.AsDateTime);
|
||||
|
||||
|
||||
blma.Prime(input);
|
||||
|
||||
@@ -164,7 +165,8 @@ public class BlmaTests
|
||||
];
|
||||
var timestamps = new List<DateTime>();
|
||||
|
||||
blma.Pub += (item) => timestamps.Add(item.AsDateTime);
|
||||
blma.Pub += (object? sender, TValueEventArgs args) => timestamps.Add(args.Value.AsDateTime);
|
||||
|
||||
|
||||
blma.Prime(input);
|
||||
|
||||
|
||||
@@ -42,7 +42,7 @@ public class BlmaValidationTests
|
||||
}
|
||||
}
|
||||
|
||||
private static List<double> CalculateReference(TSeries source, int period)
|
||||
private static IReadOnlyList<double> CalculateReference(TSeries source, int period)
|
||||
{
|
||||
var result = new List<double>();
|
||||
var buffer = new List<double>();
|
||||
|
||||
@@ -11,6 +11,7 @@ public sealed class Blma : AbstractBase, IDisposable
|
||||
private readonly RingBuffer _buffer;
|
||||
private readonly double[] _weights;
|
||||
private readonly double _weightSum;
|
||||
private readonly TValuePublishedHandler _handler;
|
||||
private ITValuePublisher? _publisher;
|
||||
private bool _hasLast;
|
||||
|
||||
@@ -31,26 +32,27 @@ public sealed class Blma : AbstractBase, IDisposable
|
||||
|
||||
// Pre-calculate weights for the full period
|
||||
_weightSum = CalculateWeights(period, _weights);
|
||||
_handler = Handle;
|
||||
}
|
||||
|
||||
public Blma(ITValuePublisher source, int period) : this(period)
|
||||
{
|
||||
_publisher = source;
|
||||
source.Pub += Handle;
|
||||
source.Pub += _handler;
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
if (_publisher != null)
|
||||
{
|
||||
_publisher.Pub -= Handle;
|
||||
_publisher.Pub -= _handler;
|
||||
_publisher = null;
|
||||
}
|
||||
}
|
||||
|
||||
private void Handle(TValue value)
|
||||
private void Handle(object? sender, TValueEventArgs args)
|
||||
{
|
||||
Update(value);
|
||||
Update(args.Value, args.IsNew);
|
||||
}
|
||||
|
||||
public override void Reset()
|
||||
@@ -118,7 +120,7 @@ public sealed class Blma : AbstractBase, IDisposable
|
||||
var tValue = new TValue(input.Time, result);
|
||||
Last = tValue;
|
||||
_hasLast = true;
|
||||
PubEvent(tValue);
|
||||
PubEvent(tValue, isNew);
|
||||
return tValue;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user