mirror of
https://github.com/mihakralj/QuanTAlib.git
synced 2026-08-15 00:58:04 +00:00
style patterns
This commit is contained in:
@@ -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);
|
||||
|
||||
@@ -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)
|
||||
{
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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)));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user