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:
@@ -88,7 +88,7 @@ public sealed class MedianValidationTests : IDisposable
|
||||
}
|
||||
}
|
||||
|
||||
private static double CalculateMedian(List<double> sortedWindow)
|
||||
private static double CalculateMedian(IReadOnlyList<double> sortedWindow)
|
||||
{
|
||||
int count = sortedWindow.Count;
|
||||
if (count == 0) return 0; // Or NaN
|
||||
|
||||
@@ -25,6 +25,7 @@ public sealed class Median : AbstractBase
|
||||
private readonly int _period;
|
||||
private readonly RingBuffer _buffer;
|
||||
private readonly double[] _sortedBuffer;
|
||||
private readonly TValuePublishedHandler _handler;
|
||||
|
||||
/// <summary>
|
||||
/// Creates a Median indicator with the specified period.
|
||||
@@ -40,11 +41,12 @@ public sealed class Median : AbstractBase
|
||||
_sortedBuffer = new double[period];
|
||||
Name = $"Median({period})";
|
||||
WarmupPeriod = period;
|
||||
_handler = Handle;
|
||||
}
|
||||
|
||||
public Median(ITValuePublisher source, int period) : this(period)
|
||||
{
|
||||
source.Pub += (item) => Update(item);
|
||||
source.Pub += _handler;
|
||||
}
|
||||
|
||||
public Median(TSeries source, int period) : this(period)
|
||||
@@ -54,7 +56,7 @@ public sealed class Median : AbstractBase
|
||||
{
|
||||
Last = new TValue(source.LastTime, Last.Value);
|
||||
}
|
||||
source.Pub += (item) => Update(item);
|
||||
source.Pub += _handler;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -79,6 +81,9 @@ public sealed class Median : AbstractBase
|
||||
}
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
private void Handle(object? sender, TValueEventArgs args) => Update(args.Value, args.IsNew);
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public override TValue Update(TValue input, bool isNew = true)
|
||||
{
|
||||
@@ -123,7 +128,7 @@ public sealed class Median : AbstractBase
|
||||
}
|
||||
|
||||
Last = new TValue(input.Time, median);
|
||||
PubEvent(Last);
|
||||
PubEvent(Last, isNew);
|
||||
return Last;
|
||||
}
|
||||
|
||||
@@ -202,7 +207,7 @@ public sealed class Median : AbstractBase
|
||||
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));
|
||||
|
||||
|
||||
Reference in New Issue
Block a user