style patterns

This commit is contained in:
Miha Kralj
2026-01-25 16:01:45 -08:00
parent 2836f253c4
commit e59665c8f0
399 changed files with 6892 additions and 1323 deletions
+17 -4
View File
@@ -47,7 +47,10 @@ public class AccelIndicator : Indicator, IWatchlistIndicator
protected override void OnUpdate(UpdateArgs args)
{
if (_accel == null || _selector == null) return;
if (_accel == null || _selector == null)
{
return;
}
var item = HistoricalData[0, SeekOriginHistory.End];
double value = _selector(item);
@@ -64,7 +67,9 @@ public class AccelIndicator : Indicator, IWatchlistIndicator
{
value = _accel!.Last.Value;
if (!double.IsFinite(value))
{
value = 0.0;
}
}
TValue input = new(time, value);
@@ -87,8 +92,16 @@ public class AccelIndicator : Indicator, IWatchlistIndicator
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static IndicatorLineMarker GetMarker(double value)
{
if (value > 0) return GreenMarker;
if (value < 0) return RedMarker;
if (value > 0)
{
return GreenMarker;
}
if (value < 0)
{
return RedMarker;
}
return GrayMarker;
}
}
}
+2 -2
View File
@@ -254,11 +254,11 @@ public class AccelTests
{
var source = new TSeries();
var accel = new Accel(source);
source.Add(new TValue(DateTime.UtcNow, 10));
source.Add(new TValue(DateTime.UtcNow, 20));
source.Add(new TValue(DateTime.UtcNow, 35));
Assert.True(accel.IsHot);
Assert.Equal(5, accel.Last.Value); // 35 - 2*20 + 10 = 5
}
+48 -10
View File
@@ -110,7 +110,10 @@ public sealed class Accel : AbstractBase
public override TSeries Update(TSeries source)
{
if (source.Count == 0) return [];
if (source.Count == 0)
{
return [];
}
int len = source.Count;
@@ -187,16 +190,28 @@ public sealed class Accel : AbstractBase
public static void Calculate(ReadOnlySpan<double> source, Span<double> output)
{
if (source.Length != output.Length)
{
throw new ArgumentException("Source and output must have the same length", nameof(output));
}
int len = source.Length;
if (len == 0) return;
if (len == 0)
{
return;
}
// First two elements have insufficient history
output[0] = 0.0;
if (len == 1) return;
if (len == 1)
{
return;
}
output[1] = 0.0;
if (len == 2) return;
if (len == 2)
{
return;
}
int i = 2;
@@ -279,9 +294,20 @@ public sealed class Accel : AbstractBase
// Handle NaN/Infinity by substitution (find first finite value)
double fallback = FindFinite(curr, p1, p2);
if (!double.IsFinite(curr)) curr = fallback;
if (!double.IsFinite(p1)) p1 = fallback;
if (!double.IsFinite(p2)) p2 = fallback;
if (!double.IsFinite(curr))
{
curr = fallback;
}
if (!double.IsFinite(p1))
{
p1 = fallback;
}
if (!double.IsFinite(p2))
{
p2 = fallback;
}
// accel = curr - 2*prev1 + prev2
output[i] = Math.FusedMultiplyAdd(-2.0, p1, curr + p2);
@@ -291,9 +317,21 @@ public sealed class Accel : AbstractBase
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static double FindFinite(double a, double b, double c)
{
if (double.IsFinite(a)) return a;
if (double.IsFinite(b)) return b;
if (double.IsFinite(c)) return c;
if (double.IsFinite(a))
{
return a;
}
if (double.IsFinite(b))
{
return b;
}
if (double.IsFinite(c))
{
return c;
}
return 0.0;
}
}