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
+2 -2
View File
@@ -180,7 +180,7 @@ public sealed class Covariance : AbstractBase
public static TSeries Calculate(TSeries sourceX, TSeries sourceY, int period, bool isPopulation = false)
{
if (sourceX.Count != sourceY.Count)
throw new ArgumentException("Source series must have the same length");
throw new ArgumentException("Source series must have the same length", nameof(sourceY));
int len = sourceX.Count;
var t = new List<long>(len);
@@ -201,7 +201,7 @@ public sealed class Covariance : AbstractBase
public static void Batch(ReadOnlySpan<double> sourceX, ReadOnlySpan<double> sourceY, Span<double> output, int period, bool isPopulation = false)
{
if (sourceX.Length != sourceY.Length || sourceX.Length != output.Length)
throw new ArgumentException("All spans must have the same length");
throw new ArgumentException("All spans must have the same length", nameof(output));
if (period < 2)
throw new ArgumentException("Period must be greater than or equal to 2", nameof(period));
+9 -3
View File
@@ -36,9 +36,11 @@ public sealed class LinReg : AbstractBase
private readonly double _sum_x;
private readonly double _denominator;
[StructLayout(LayoutKind.Auto)]
private record struct State(double SumY, double SumXY, double SumY2, double LastVal, double LastValidValue);
private State _state;
private State _p_state;
private readonly TValuePublishedHandler _handler;
private int _tickCount;
private const int ResyncInterval = 1000;
@@ -81,6 +83,7 @@ public sealed class LinReg : AbstractBase
_buffer = new RingBuffer(period);
Name = $"LinReg({period})";
WarmupPeriod = period;
_handler = Handle;
// Precalculate constants
// sum_x = 0 + 1 + ... + (n-1) = n(n-1)/2
@@ -95,9 +98,12 @@ public sealed class LinReg : AbstractBase
public LinReg(ITValuePublisher source, int period, int offset = 0) : this(period, offset)
{
source.Pub += (item) => Update(item);
source.Pub += _handler;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private void Handle(object? sender, TValueEventArgs args) => Update(args.Value, args.IsNew);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private double GetValidValue(double input)
{
@@ -250,7 +256,7 @@ public sealed class LinReg : AbstractBase
}
Last = new TValue(input.Time, result);
PubEvent(Last);
PubEvent(Last, isNew);
return Last;
}
@@ -329,7 +335,7 @@ public sealed class LinReg : AbstractBase
public static void Calculate(ReadOnlySpan<double> source, Span<double> output, int period, int offset = 0, double initialLastValid = 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 (period <= 0)
throw new ArgumentException("Period must be greater than 0", nameof(period));
@@ -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
+9 -4
View File
@@ -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));
+1 -1
View File
@@ -205,7 +205,7 @@ public sealed class Skew : AbstractBase
public static void Batch(ReadOnlySpan<double> source, Span<double> output, int period, bool isPopulation = false)
{
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 < 3)
throw new ArgumentException("Period must be greater than or equal to 3", nameof(period));
+1 -1
View File
@@ -181,7 +181,7 @@ public sealed class Variance : AbstractBase
public static void Batch(ReadOnlySpan<double> source, Span<double> output, int period, bool isPopulation = false)
{
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 < 2)
throw new ArgumentException("Period must be greater than or equal to 2", nameof(period));