mirror of
https://github.com/mihakralj/QuanTAlib.git
synced 2026-08-19 19:18: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:
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user