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
+18 -4
View File
@@ -86,8 +86,15 @@ public class IndicatorExtensionsTests
// Test GetSmoothCurvePoints
var series = new LineSeries("Test", Color.Blue, 1, LineStyle.Solid);
for (int i = 0; i < 20; i++) series.AddValue();
for (int i = 0; i < 20; i++) series.SetValue(100 + i, i);
for (int i = 0; i < 20; i++)
{
series.AddValue();
}
for (int i = 0; i < 20; i++)
{
series.SetValue(100 + i, i);
}
var points = IndicatorExtensions.GetSmoothCurvePoints(indicator, converter, clientRect, series);
Assert.NotEmpty(points);
@@ -152,8 +159,15 @@ public class IndicatorExtensionsTests
foreach (LineStyle style in Enum.GetValues<LineStyle>())
{
var series = new LineSeries("Test", Color.Blue, 1, style);
for (int i = 0; i < 20; i++) series.AddValue();
for (int i = 0; i < 20; i++) series.SetValue(100 + i, i);
for (int i = 0; i < 20; i++)
{
series.AddValue();
}
for (int i = 0; i < 20; i++)
{
series.SetValue(100 + i, i);
}
// Test with warmup and cold values
indicator.PaintSmoothCurve(args, series, warmupPeriod: 5, showColdValues: true);
+19 -5
View File
@@ -97,7 +97,10 @@ public static class IndicatorExtensions
ArgumentNullException.ThrowIfNull(indicator);
ArgumentNullException.ThrowIfNull(converter);
var data = indicator.HistoricalData;
if (data == null) return Array.Empty<Point>();
if (data == null)
{
return Array.Empty<Point>();
}
var lastTime = data.Time(data.Count - 1);
var firstTime = data.Time(0);
@@ -113,7 +116,10 @@ public static class IndicatorExtensions
int rightIndex = (int)data.GetIndexByTime(rightTime.Ticks);
int count = leftIndex - rightIndex;
if (count <= 0) return Array.Empty<Point>();
if (count <= 0)
{
return Array.Empty<Point>();
}
var allPoints = new Point[count];
@@ -131,7 +137,9 @@ public static class IndicatorExtensions
public static void PaintSmoothCurve(this Indicator indicator, PaintChartEventArgs args, LineSeries series, int warmupPeriod, bool showColdValues = true, double tension = 0.5)
{
if (!series.Visible || indicator.CurrentChart == null)
{
return;
}
Graphics gr = args.Graphics;
gr.SmoothingMode = SmoothingMode.AntiAlias;
@@ -142,7 +150,10 @@ public static class IndicatorExtensions
gr.SetClip(clientRect);
var data = indicator.HistoricalData;
if (data == null) return;
if (data == null)
{
return;
}
var lastTime = data.Time(data.Count - 1);
var firstTime = data.Time(0);
@@ -158,7 +169,10 @@ public static class IndicatorExtensions
int rightIndex = (int)data.GetIndexByTime(rightTime.Ticks);
int count = leftIndex - rightIndex;
if (count <= 0) return;
if (count <= 0)
{
return;
}
// Use ArrayPool to avoid allocations
Point[] allPoints = System.Buffers.ArrayPool<Point>.Shared.Rent(count);
@@ -191,7 +205,7 @@ public static class IndicatorExtensions
if (showColdValues)
{
int coldStart = Math.Max(0, hotCount);
int coldSegments = count - coldStart - 1;
int coldSegments = count - coldStart - 1;
if (coldSegments > 0)
{
+12
View File
@@ -197,7 +197,9 @@ public class HistoricalData
for (int i = 0; i < _items.Count; i++)
{
if (_items[i].TicksLeft == ticks)
{
return Count - 1 - i;
}
}
return -1;
}
@@ -281,14 +283,18 @@ public class LineSeries(string name, Color color, int width, LineStyle style)
public double GetValue(int offset = 0, SeekOriginHistory origin = SeekOriginHistory.End)
{
if (_values.Count == 0)
{
return double.NaN;
}
int index = origin == SeekOriginHistory.End
? _values.Count - 1 - offset
: offset;
if (index < 0 || index >= _values.Count)
{
return double.NaN;
}
return _values[index];
}
@@ -307,7 +313,9 @@ public class LineSeries(string name, Color color, int width, LineStyle style)
EnsureMarkerCapacity(offset + 1);
int index = _markers.Count - 1 - offset;
if (index >= 0 && index < _markers.Count)
{
_markers[index] = color;
}
}
public void SetMarker(int offset, IndicatorLineMarker marker)
@@ -324,13 +332,17 @@ public class LineSeries(string name, Color color, int width, LineStyle style)
private void EnsureCapacity(int count)
{
while (_values.Count < count)
{
_values.Add(double.NaN);
}
}
private void EnsureMarkerCapacity(int count)
{
while (_markers.Count < count)
{
_markers.Add(Color.Transparent);
}
}
public int Count => _values.Count;
+1 -1
View File
@@ -274,4 +274,4 @@ public class SgmaIndicatorTests
Assert.True(double.IsFinite(indicatorLow.LinesSeries[0].GetValue(0)));
Assert.True(double.IsFinite(indicatorHigh.LinesSeries[0].GetValue(0)));
}
}
}
+5 -2
View File
@@ -49,7 +49,10 @@ public sealed class SgmaIndicator : Indicator, IWatchlistIndicator
protected override void OnUpdate(UpdateArgs args)
{
if (_sgma == null || _selector == null) return;
if (_sgma == null || _selector == null)
{
return;
}
var item = HistoricalData[0, SeekOriginHistory.End];
double value = _selector(item);
@@ -61,4 +64,4 @@ public sealed class SgmaIndicator : Indicator, IWatchlistIndicator
bool isHot = _sgma.IsHot;
LinesSeries[0].SetValue(result.Value, isHot, ShowColdValues);
}
}
}