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
@@ -57,7 +57,7 @@ public sealed class Adx : ITValuePublisher
/// </summary>
public string Name { get; }
public event Action<TValue>? Pub;
public event TValuePublishedHandler? Pub;
/// <summary>
/// Current ADX value.
@@ -265,7 +265,7 @@ public sealed class Adx : ITValuePublisher
DiMinus = new TValue(input.Time, diMinus);
Last = new TValue(input.Time, _adx);
Pub?.Invoke(Last);
Pub?.Invoke(this, new TValueEventArgs { Value = Last, IsNew = isNew });
return Last;
}
+3 -2
View File
@@ -30,7 +30,7 @@ public sealed class Adxr : ITValuePublisher
/// </summary>
public string Name { get; }
public event Action<TValue>? Pub;
public event TValuePublishedHandler? Pub;
/// <summary>
/// Current ADXR value.
@@ -130,7 +130,7 @@ public sealed class Adxr : ITValuePublisher
}
Last = new TValue(input.Time, adxr);
Pub?.Invoke(Last);
Pub?.Invoke(this, new TValueEventArgs { Value = Last, IsNew = true });
return Last;
}
@@ -233,3 +233,4 @@ public sealed class Adxr : ITValuePublisher
return new TSeries(tList, [.. v]);
}
}
+4 -4
View File
@@ -31,7 +31,7 @@ public sealed class Ao : ITValuePublisher
/// </summary>
public string Name { get; }
public event Action<TValue>? Pub;
public event TValuePublishedHandler? Pub;
/// <summary>
/// Current AO value.
@@ -99,7 +99,7 @@ public sealed class Ao : ITValuePublisher
double ao = sFast.Value - sSlow.Value;
Last = new TValue(input.Time, ao);
Pub?.Invoke(Last);
Pub?.Invoke(this, new TValueEventArgs { Value = Last, IsNew = true });
return Last;
}
@@ -117,7 +117,7 @@ public sealed class Ao : ITValuePublisher
double ao = sFast.Value - sSlow.Value;
Last = new TValue(input.Time, ao);
Pub?.Invoke(Last);
Pub?.Invoke(this, new TValueEventArgs { Value = Last, IsNew = true });
return Last;
}
@@ -167,7 +167,7 @@ public sealed class Ao : ITValuePublisher
public static void Calculate(ReadOnlySpan<double> high, ReadOnlySpan<double> low, Span<double> destination, int fastPeriod = 5, int slowPeriod = 34)
{
if (high.Length != low.Length || high.Length != destination.Length)
throw new ArgumentException("High, low, and destination spans must have the same length.");
throw new ArgumentException("High, low, and destination spans must have the same length.", nameof(destination));
int len = high.Length;
if (len == 0) return;
+11 -4
View File
@@ -26,13 +26,14 @@ public sealed class Apo : ITValuePublisher
{
private readonly Ema _emaFast;
private readonly Ema _emaSlow;
private readonly TValuePublishedHandler _handler;
/// <summary>
/// Display name for the indicator.
/// </summary>
public string Name { get; }
public event Action<TValue>? Pub;
public event TValuePublishedHandler? Pub;
/// <summary>
/// Current APO value.
@@ -65,6 +66,7 @@ public sealed class Apo : ITValuePublisher
_emaFast = new Ema(fastPeriod);
_emaSlow = new Ema(slowPeriod);
_handler = Handle;
WarmupPeriod = slowPeriod;
Name = $"Apo({fastPeriod},{slowPeriod})";
}
@@ -77,7 +79,7 @@ public sealed class Apo : ITValuePublisher
/// <param name="slowPeriod">Slow EMA period (default 26)</param>
public Apo(ITValuePublisher source, int fastPeriod = 12, int slowPeriod = 26) : this(fastPeriod, slowPeriod)
{
source.Pub += (item) => Update(item);
source.Pub += _handler;
}
/// <summary>
@@ -105,7 +107,7 @@ public sealed class Apo : ITValuePublisher
double apo = eFast.Value - eSlow.Value;
Last = new TValue(input.Time, apo);
Pub?.Invoke(Last);
Pub?.Invoke(this, new TValueEventArgs { Value = Last, IsNew = isNew });
return Last;
}
@@ -143,6 +145,11 @@ public sealed class Apo : ITValuePublisher
return new TSeries(t, v);
}
private void Handle(object? sender, TValueEventArgs args)
{
Update(args.Value, args.IsNew);
}
/// <summary>
/// Calculates APO for the entire series using a new instance.
/// </summary>
@@ -167,7 +174,7 @@ public sealed class Apo : ITValuePublisher
public static void Calculate(ReadOnlySpan<double> source, Span<double> output, int fastPeriod = 12, int slowPeriod = 26)
{
if (source.Length != output.Length)
throw new ArgumentException("Source and output spans must be of the same length.");
throw new ArgumentException("Source and output spans must be of the same length.", nameof(output));
Span<double> fastEma = source.Length <= 1024 ? stackalloc double[source.Length] : new double[source.Length];
Span<double> slowEma = source.Length <= 1024 ? stackalloc double[source.Length] : new double[source.Length];
+3 -2
View File
@@ -32,7 +32,7 @@ public sealed class Aroon : ITValuePublisher
/// </summary>
public string Name { get; }
public event Action<TValue>? Pub;
public event TValuePublishedHandler? Pub;
/// <summary>
/// Current Aroon Oscillator value (Up - Down).
@@ -150,7 +150,7 @@ public sealed class Aroon : ITValuePublisher
Down = new TValue(input.Time, down);
Last = new TValue(input.Time, osc);
Pub?.Invoke(Last);
Pub?.Invoke(this, new TValueEventArgs { Value = Last, IsNew = true });
return Last;
}
@@ -255,3 +255,4 @@ public sealed class Aroon : ITValuePublisher
return new TSeries(tList, [.. v]);
}
}
+3 -2
View File
@@ -32,7 +32,7 @@ public sealed class AroonOsc : ITValuePublisher
/// </summary>
public string Name { get; }
public event Action<TValue>? Pub;
public event TValuePublishedHandler? Pub;
/// <summary>
/// Current Aroon Oscillator value.
@@ -136,7 +136,7 @@ public sealed class AroonOsc : ITValuePublisher
Last = new TValue(input.Time, osc);
Pub?.Invoke(Last);
Pub?.Invoke(this, new TValueEventArgs { Value = Last, IsNew = true });
return Last;
}
@@ -241,3 +241,4 @@ public sealed class AroonOsc : ITValuePublisher
return new TSeries(tList, [.. v]);
}
}
+4 -3
View File
@@ -32,7 +32,7 @@ public sealed class Bop : ITValuePublisher
/// </summary>
public static string Name => "Bop";
public event Action<TValue>? Pub;
public event TValuePublishedHandler? Pub;
/// <summary>
/// Current BOP value.
@@ -76,7 +76,7 @@ public sealed class Bop : ITValuePublisher
}
Last = new TValue(input.Time, bop);
Pub?.Invoke(Last);
Pub?.Invoke(this, new TValueEventArgs { Value = Last, IsNew = true });
return Last;
}
@@ -92,7 +92,7 @@ public sealed class Bop : ITValuePublisher
// Or we could throw NotSupportedException.
// Given the interface contract, returning 0 is safer than crashing.
Last = new TValue(input.Time, 0);
Pub?.Invoke(Last);
Pub?.Invoke(this, new TValueEventArgs { Value = Last, IsNew = true });
return Last;
}
@@ -180,3 +180,4 @@ public sealed class Bop : ITValuePublisher
return new TSeries(t, v);
}
}
+9 -3
View File
@@ -37,12 +37,14 @@ public sealed class Cfb : ITValuePublisher
private readonly double[] _runningSums;
private readonly double[] _p_runningSums;
[StructLayout(LayoutKind.Auto)]
private record struct State(double PrevCfb, double LastPrice, double LastValidValue);
private State _state;
private State _p_state;
private readonly TValuePublishedHandler _handler;
public string Name { get; }
public event Action<TValue>? Pub;
public event TValuePublishedHandler? Pub;
public TValue Last { get; private set; }
public bool IsHot => _prices.IsFull;
public int WarmupPeriod { get; }
@@ -81,14 +83,18 @@ public sealed class Cfb : ITValuePublisher
_p_runningSums = new double[_lengths.Length];
Name = "Jurik Composite Fractal Behavior";
_handler = Handle;
_state.PrevCfb = 1.0;
}
public Cfb(ITValuePublisher source, int[]? lengths = null) : this(lengths)
{
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)]
public void Reset()
{
@@ -210,7 +216,7 @@ public sealed class Cfb : ITValuePublisher
_state.PrevCfb = cfb;
Last = new TValue(input.Time, cfb);
Pub?.Invoke(Last);
Pub?.Invoke(this, new TValueEventArgs { Value = Last, IsNew = isNew });
return Last;
}
+3 -3
View File
@@ -23,7 +23,7 @@ public sealed class Dmx : ITValuePublisher
private bool _isInitialized;
public string Name { get; }
public event Action<TValue>? Pub;
public event TValuePublishedHandler? Pub;
public TValue Last { get; private set; }
public int WarmupPeriod { get; }
@@ -114,7 +114,7 @@ public sealed class Dmx : ITValuePublisher
double dmxValue = diPlus - diMinus;
Last = new TValue(input.Time, dmxValue);
Pub?.Invoke(Last);
Pub?.Invoke(this, new TValueEventArgs { Value = Last, IsNew = true });
return Last;
}
@@ -159,7 +159,7 @@ public sealed class Dmx : ITValuePublisher
return;
if (low.Length != len || close.Length != len || destination.Length != len)
throw new ArgumentException("All input spans must have the same length");
throw new ArgumentException("All input spans must have the same length", nameof(destination));
if (period <= 0)
throw new ArgumentException("Period must be greater than zero.", nameof(period));
+11 -4
View File
@@ -23,6 +23,7 @@ public sealed class Macd : ITValuePublisher
private readonly Ema _fastEma;
private readonly Ema _slowEma;
private readonly Ema _signalEma;
private readonly TValuePublishedHandler _handler;
public string Name { get; }
public bool IsHot => _fastEma.IsHot && _slowEma.IsHot && _signalEma.IsHot;
@@ -32,13 +33,14 @@ public sealed class Macd : ITValuePublisher
public TValue Signal { get; private set; }
public TValue Histogram { get; private set; }
public event Action<TValue>? Pub;
public event TValuePublishedHandler? Pub;
public Macd(int fastPeriod = 12, int slowPeriod = 26, int signalPeriod = 9)
{
_fastEma = new Ema(fastPeriod);
_slowEma = new Ema(slowPeriod);
_signalEma = new Ema(signalPeriod);
_handler = Handle;
Name = $"Macd({fastPeriod},{slowPeriod},{signalPeriod})";
WarmupPeriod = Math.Max(fastPeriod, slowPeriod) + signalPeriod;
@@ -47,7 +49,7 @@ public sealed class Macd : ITValuePublisher
public Macd(ITValuePublisher source, int fastPeriod = 12, int slowPeriod = 26, int signalPeriod = 9)
: this(fastPeriod, slowPeriod, signalPeriod)
{
source.Pub += (item) => Update(item);
source.Pub += _handler;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
@@ -78,7 +80,7 @@ public sealed class Macd : ITValuePublisher
Signal = signal;
Histogram = new TValue(input.Time, histValue);
Pub?.Invoke(Last);
Pub?.Invoke(this, new TValueEventArgs { Value = Last, IsNew = isNew });
return Last;
}
@@ -100,6 +102,11 @@ public sealed class Macd : ITValuePublisher
return new TSeries(t, v);
}
private void Handle(object? sender, TValueEventArgs args)
{
Update(args.Value, args.IsNew);
}
/// <summary>
/// Calculates the MACD Line (Fast EMA - Slow EMA).
@@ -108,7 +115,7 @@ public sealed class Macd : ITValuePublisher
public static void Calculate(ReadOnlySpan<double> source, Span<double> destination, int fastPeriod = 12, int slowPeriod = 26)
{
if (source.Length != destination.Length)
throw new ArgumentException("Source and destination must be same length");
throw new ArgumentException("Source and destination must be same length", nameof(destination));
int len = source.Length;
double[] fastBuffer = ArrayPool<double>.Shared.Rent(len);
+10 -3
View File
@@ -26,6 +26,7 @@ public sealed class Rsi : AbstractBase
private readonly int _period;
private readonly Rma _avgGain;
private readonly Rma _avgLoss;
private readonly TValuePublishedHandler _handler;
private double _prevValue;
private double _p_prevValue;
@@ -39,6 +40,7 @@ public sealed class Rsi : AbstractBase
_period = period;
_avgGain = new Rma(period);
_avgLoss = new Rma(period);
_handler = Handle;
_prevValue = double.NaN;
_p_prevValue = double.NaN;
@@ -48,7 +50,7 @@ public sealed class Rsi : AbstractBase
public Rsi(ITValuePublisher source, int period = 14) : this(period)
{
source.Pub += (item) => Update(item);
source.Pub += _handler;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
@@ -110,7 +112,7 @@ public sealed class Rsi : AbstractBase
}
Last = new TValue(input.Time, rsi);
PubEvent(Last);
PubEvent(Last, isNew);
return Last;
}
@@ -145,6 +147,11 @@ public sealed class Rsi : AbstractBase
return new TSeries(t, v);
}
private void Handle(object? sender, TValueEventArgs args)
{
Update(args.Value, args.IsNew);
}
public override void Prime(ReadOnlySpan<double> source)
{
foreach (var value in source)
@@ -163,7 +170,7 @@ public sealed class Rsi : AbstractBase
public static void Calculate(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));
+9 -4
View File
@@ -27,6 +27,7 @@ public sealed class Rsx : ITValuePublisher
private readonly int _period;
private readonly double _alpha;
[StructLayout(LayoutKind.Auto)]
private record struct State
{
// Momentum filters (3 stages, 2 filters each)
@@ -46,13 +47,14 @@ public sealed class Rsx : ITValuePublisher
private State _state;
private State _p_state;
private readonly TValuePublishedHandler _handler;
/// <summary>
/// Display name for the indicator.
/// </summary>
public string Name { get; }
public event Action<TValue>? Pub;
public event TValuePublishedHandler? Pub;
/// <summary>
/// The number of bars required to warm up the indicator.
@@ -72,11 +74,12 @@ public sealed class Rsx : ITValuePublisher
WarmupPeriod = period;
_alpha = 3.0 / (period + 2.0);
Name = $"Rsx({period})";
_handler = Handle;
}
public Rsx(ITValuePublisher source, int period) : this(period)
{
source.Pub += (item) => Update(item);
source.Pub += _handler;
}
/// <summary>
@@ -90,6 +93,8 @@ public sealed class Rsx : ITValuePublisher
public bool IsHot => _state.IsInitialized;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private void Handle(object? sender, TValueEventArgs args) => Update(args.Value, args.IsNew);
public TValue Update(TValue input, bool isNew = true)
{
if (isNew)
@@ -177,7 +182,7 @@ public sealed class Rsx : ITValuePublisher
}
Last = new TValue(input.Time, rsx);
Pub?.Invoke(Last);
Pub?.Invoke(this, new TValueEventArgs { Value = Last, IsNew = isNew });
return Last;
}
@@ -219,7 +224,7 @@ public sealed class Rsx : ITValuePublisher
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));
+9 -4
View File
@@ -22,12 +22,13 @@ public sealed class Vel : ITValuePublisher
private readonly Pwma _pwma;
private readonly Wma _wma;
private readonly int _period;
private readonly TValuePublishedHandler _handler;
public string Name { get; }
public TValue Last { get; private set; }
public bool IsHot => _pwma.IsHot && _wma.IsHot;
public int WarmupPeriod { get; }
public event Action<TValue>? Pub;
public event TValuePublishedHandler? Pub;
public Vel(int period)
{
@@ -38,13 +39,17 @@ public sealed class Vel : ITValuePublisher
_period = period;
WarmupPeriod = period;
Name = $"Vel({period})";
_handler = Handle;
}
public Vel(ITValuePublisher source, int period) : this(period)
{
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)]
public TValue Update(TValue input, bool isNew = true)
{
@@ -52,7 +57,7 @@ public sealed class Vel : ITValuePublisher
var wma = _wma.Update(input, isNew);
Last = new TValue(input.Time, pwma.Value - wma.Value);
Pub?.Invoke(Last);
Pub?.Invoke(this, new TValueEventArgs { Value = Last, IsNew = isNew });
return Last;
}
@@ -110,7 +115,7 @@ public sealed class Vel : ITValuePublisher
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));
Span<double> pwma = source.Length <= 1024 ? stackalloc double[source.Length] : new double[source.Length];
Span<double> wma = source.Length <= 1024 ? stackalloc double[source.Length] : new double[source.Length];