chore: Update documentation links and improve code readability in averages and tests

This commit is contained in:
Miha Kralj
2025-12-07 20:44:24 -08:00
parent 03df45003e
commit 488de7ea1e
5 changed files with 61 additions and 45 deletions
+1 -1
View File
@@ -51,7 +51,7 @@
| SINEMA | Sine-weighted MA | | SINEMA | Sine-weighted MA |
| [SMA](sma/Sma.cs) | Simple MA | | [SMA](sma/Sma.cs) | Simple MA |
| SSF | Ehlers Super Smooth Filter | | SSF | Ehlers Super Smooth Filter |
| T3 | Tillson T3 MA | | [T3](t3/T3.cs) | Tillson T3 MA |
| [TEMA](tema/Tema.cs) | Triple Exponential MA | | [TEMA](tema/Tema.cs) | Triple Exponential MA |
| [TRIMA](trima/Trima.cs) | Triangular MA | | [TRIMA](trima/Trima.cs) | Triangular MA |
| USF | Ehlers Ultrasmooth Filter | | USF | Ehlers Ultrasmooth Filter |
+1 -1
View File
@@ -75,7 +75,7 @@ public sealed class Hma : ITValuePublisher
public TSeries Update(TSeries source) public TSeries Update(TSeries source)
{ {
if (source.Count == 0) return new TSeries(new List<long>(), new List<double>()); if (source.Count == 0) return new TSeries([], new List<double>());
int len = source.Count; int len = source.Count;
var t = new List<long>(len); var t = new List<long>(len);
+4
View File
@@ -35,10 +35,12 @@ public sealed class T3 : ITValuePublisher
public override bool Equals(object? obj) => obj is State other && Equals(other); public override bool Equals(object? obj) => obj is State other && Equals(other);
#pragma warning disable S1244 // Do not check floating point equality with exact values
public bool Equals(State other) => public bool Equals(State other) =>
E1 == other.E1 && E2 == other.E2 && E3 == other.E3 && E1 == other.E1 && E2 == other.E2 && E3 == other.E3 &&
E4 == other.E4 && E5 == other.E5 && E6 == other.E6 && E4 == other.E4 && E5 == other.E5 && E6 == other.E6 &&
IsInitialized == other.IsInitialized; IsInitialized == other.IsInitialized;
#pragma warning restore S1244 // Do not check floating point equality with exact values
public override int GetHashCode() => HashCode.Combine(E1, E2, E3, E4, E5, E6, IsInitialized); public override int GetHashCode() => HashCode.Combine(E1, E2, E3, E4, E5, E6, IsInitialized);
@@ -63,10 +65,12 @@ public sealed class T3 : ITValuePublisher
public override bool Equals(object? obj) => obj is Parameters other && Equals(other); public override bool Equals(object? obj) => obj is Parameters other && Equals(other);
#pragma warning disable S1244 // Do not check floating point equality with exact values
public bool Equals(Parameters other) => public bool Equals(Parameters other) =>
Alpha == other.Alpha && Alpha == other.Alpha &&
C1 == other.C1 && C2 == other.C2 && C1 == other.C1 && C2 == other.C2 &&
C3 == other.C3 && C4 == other.C4; C3 == other.C3 && C4 == other.C4;
#pragma warning restore S1244 // Do not check floating point equality with exact values
public override int GetHashCode() => HashCode.Combine(Alpha, C1, C2, C3, C4); public override int GetHashCode() => HashCode.Combine(Alpha, C1, C2, C3, C4);
+22 -9
View File
@@ -111,8 +111,10 @@ public class IndicatorExtensionsTests
[Fact] [Fact]
public void LogicMethods_CalculateCorrectly() public void LogicMethods_CalculateCorrectly()
{ {
var indicator = new TestIndicator(); var indicator = new TestIndicator
indicator.CurrentChart = new MockChart(); {
CurrentChart = new MockChart()
};
// Add some data // Add some data
var now = new DateTime(2024, 1, 1, 12, 0, 0, DateTimeKind.Utc); var now = new DateTime(2024, 1, 1, 12, 0, 0, DateTimeKind.Utc);
@@ -159,7 +161,7 @@ public class IndicatorExtensionsTests
// Check value at offset 9 (i=9 in setup loop) // Check value at offset 9 (i=9 in setup loop)
// i=9 is odd -> -10.0 (Negative) // i=9 is odd -> -10.0 (Negative)
// Color should be Red (150, 255, 0, 0) // Color should be Red (150, 255, 0, 0)
var first = rects.First(); var first = rects[0];
Assert.Equal(Color.FromArgb(150, 255, 0, 0), first.Color); Assert.Equal(Color.FromArgb(150, 255, 0, 0), first.Color);
// Verify geometry // Verify geometry
@@ -168,7 +170,7 @@ public class IndicatorExtensionsTests
// Height = Abs(0 - (-10)) = 10. // Height = Abs(0 - (-10)) = 10.
// Y = 0 (since negative bars start at 0 and go down? No, GDI+ coords usually go down. // Y = 0 (since negative bars start at 0 and go down? No, GDI+ coords usually go down.
// But here we are testing the logic in GetHistogramRectangles: // But here we are testing the logic in GetHistogramRectangles:
// else { new Rectangle(barX, barY0, ...) } -> Y = barY0 = 0. // else new Rectangle(barX, barY0, ...) -> Y = barY0 = 0.
Assert.Equal(0, first.Rect.Y); Assert.Equal(0, first.Rect.Y);
Assert.Equal(10, first.Rect.Height); Assert.Equal(10, first.Rect.Height);
} }
@@ -189,16 +191,27 @@ public class IndicatorExtensionsTests
RunPaintTests(graphics); RunPaintTests(graphics);
Assert.True(true); // Assertion to satisfy SonarCloud RSPEC-2699 Assert.True(true); // Assertion to satisfy SonarCloud RSPEC-2699
} }
catch (TypeInitializationException) { return; } // System.Drawing.Common not supported catch (TypeInitializationException)
catch (PlatformNotSupportedException) { return; } // GDI+ not available {
catch (DllNotFoundException) { return; } // libgdiplus not found // System.Drawing.Common not supported on this platform
}
catch (PlatformNotSupportedException)
{
// GDI+ not available on this platform
}
catch (DllNotFoundException)
{
// libgdiplus not found on this platform
}
} }
[System.Runtime.Versioning.SupportedOSPlatform("windows")] [System.Runtime.Versioning.SupportedOSPlatform("windows")]
private void RunPaintTests(Graphics graphics) private void RunPaintTests(Graphics graphics)
{ {
var indicator = new TestIndicator(); var indicator = new TestIndicator
indicator.CurrentChart = new MockChart(); {
CurrentChart = new MockChart()
};
// Add some data // Add some data
var now = new DateTime(2024, 1, 1, 12, 0, 0, DateTimeKind.Utc); var now = new DateTime(2024, 1, 1, 12, 0, 0, DateTimeKind.Utc);
+15 -16
View File
@@ -170,24 +170,23 @@ public static class IndicatorExtensions
DateTime rightTime = new[] { converter.GetTime(clientRect.Right), indicator.HistoricalData.Time(0) }.Min(); DateTime rightTime = new[] { converter.GetTime(clientRect.Right), indicator.HistoricalData.Time(0) }.Min();
int rightIndex = (int)indicator.HistoricalData.GetIndexByTime(rightTime.Ticks); int rightIndex = (int)indicator.HistoricalData.GetIndexByTime(rightTime.Ticks);
using (Pen defaultPen = new(series.Color, series.Width) { DashStyle = ConvertLineStyleToDashStyle(series.Style) }) using Pen defaultPen = new(series.Color, series.Width) { DashStyle = ConvertLineStyleToDashStyle(series.Style) };
using (Pen coldPen = new(series.Color, series.Width) { DashStyle = DashStyle.Dot }) using Pen coldPen = new(series.Color, series.Width) { DashStyle = DashStyle.Dot };
int hotCount = indicator.Count - warmupPeriod - rightIndex;
// Draw the hot part
if (hotCount > 0)
{ {
int hotCount = indicator.Count - warmupPeriod - rightIndex; var hotPoints = allPoints.Take(Math.Min(hotCount + 1, allPoints.Count)).ToArray();
gr.DrawCurve(defaultPen, hotPoints, 0, hotPoints.Length - 1, (float)tension);
}
// Draw the hot part // Draw the cold part
if (hotCount > 0) if (showColdValues && hotCount < allPoints.Count)
{ {
var hotPoints = allPoints.Take(Math.Min(hotCount + 1, allPoints.Count)).ToArray(); var coldPoints = allPoints.Skip(Math.Max(0, hotCount)).ToArray();
gr.DrawCurve(defaultPen, hotPoints, 0, hotPoints.Length - 1, (float)tension); gr.DrawCurve(coldPen, coldPoints, 0, coldPoints.Length - 1, (float)tension);
}
// Draw the cold part
if (showColdValues && hotCount < allPoints.Count)
{
var coldPoints = allPoints.Skip(Math.Max(0, hotCount)).ToArray();
gr.DrawCurve(coldPen, coldPoints, 0, coldPoints.Length - 1, (float)tension);
}
} }
} }
} }