mirror of
https://github.com/mihakralj/QuanTAlib.git
synced 2026-08-21 12:08:05 +00:00
code reviews
This commit is contained in:
@@ -23,6 +23,11 @@ public class ChangeIndicator : Indicator, IWatchlistIndicator
|
||||
private Change? _change;
|
||||
private Func<IHistoryItem, double>? _selector;
|
||||
|
||||
// Cached markers to avoid per-update allocations
|
||||
private static readonly IndicatorLineMarker GreenMarker = new(Color.Green);
|
||||
private static readonly IndicatorLineMarker RedMarker = new(Color.Red);
|
||||
private static readonly IndicatorLineMarker GrayMarker = new(Color.Gray);
|
||||
|
||||
public int MinHistoryDepths => Period + 1;
|
||||
public override string ShortName => $"CHANGE({Period})";
|
||||
|
||||
@@ -55,21 +60,25 @@ public class ChangeIndicator : Indicator, IWatchlistIndicator
|
||||
_change.Update(input, isNew);
|
||||
|
||||
bool isHot = _change.IsHot;
|
||||
double changeValue = _change.Last.Value; // Cache to avoid repeated property access
|
||||
|
||||
LinesSeries[0].SetValue(_change.Last.Value, isHot, ShowColdValues);
|
||||
LinesSeries[0].SetValue(changeValue, isHot, ShowColdValues);
|
||||
LinesSeries[1].SetValue(0);
|
||||
|
||||
if (isHot || ShowColdValues)
|
||||
{
|
||||
double change = _change.Last.Value;
|
||||
Color color;
|
||||
if (change > 0)
|
||||
color = Color.Green;
|
||||
else if (change < 0)
|
||||
color = Color.Red;
|
||||
else
|
||||
color = Color.Gray;
|
||||
LinesSeries[0].SetMarker(0, new IndicatorLineMarker(color));
|
||||
// Use cached markers to avoid per-update allocations
|
||||
IndicatorLineMarker marker = GetMarker(changeValue);
|
||||
LinesSeries[0].SetMarker(0, marker);
|
||||
}
|
||||
}
|
||||
|
||||
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
|
||||
private static IndicatorLineMarker GetMarker(double value)
|
||||
{
|
||||
if (!double.IsFinite(value)) return GrayMarker;
|
||||
if (value > 0) return GreenMarker;
|
||||
if (value < 0) return RedMarker;
|
||||
return GrayMarker;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -162,10 +162,16 @@ public class ChangeTests
|
||||
indicator.Update(_source[i]);
|
||||
}
|
||||
|
||||
// Compare last 10 values
|
||||
// Compare last 10 values between batch and streaming
|
||||
var streamResult = new TSeries();
|
||||
for (int j = 0; j < _source.Count; j++)
|
||||
{
|
||||
streamResult.Add(indicator.Update(_source[j]), true);
|
||||
}
|
||||
|
||||
for (int i = Math.Max(0, _source.Count - 10); i < _source.Count; i++)
|
||||
{
|
||||
Assert.Equal(batchResult[i].Value, batchResult[i].Value, 1e-10);
|
||||
Assert.Equal(batchResult[i].Value, streamResult[i].Value, 1e-10);
|
||||
}
|
||||
|
||||
// Ensure final values match
|
||||
@@ -214,4 +220,4 @@ public class ChangeTests
|
||||
Assert.True(change.IsHot);
|
||||
Assert.NotEqual(0.0, change.Last.Value);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user