Refactor IndicatorExtensions: Remove unused methods and optimize price retrieval

This commit is contained in:
Miha Kralj
2025-12-24 13:50:19 -08:00
parent c47b106597
commit 8917575994
101 changed files with 1311 additions and 450292 deletions
+4 -2
View File
@@ -11,9 +11,11 @@ public class HtitIndicatorTests
public void Indicator_Initializes_Correctly()
{
var indicator = new HtitIndicator();
indicator.Initialize();
Assert.Equal("HTIT - Ehlers Hilbert Transform Instantaneous Trend", indicator.Name);
Assert.Equal("HTIT:Close", indicator.ShortName);
Assert.Equal(50, HtitIndicator.MinHistoryDepths);
Assert.StartsWith("HTIT", indicator.ShortName);
Assert.Contains("Close", indicator.ShortName);
Assert.Equal(0, HtitIndicator.MinHistoryDepths);
Assert.Single(indicator.LinesSeries);
}
+18 -28
View File
@@ -1,13 +1,15 @@
using System;
using System.Drawing;
using System.Runtime.CompilerServices;
using TradingPlatform.BusinessLayer;
namespace QuanTAlib;
public class HtitIndicator : Indicator, IWatchlistIndicator
[SkipLocalsInit]
public sealed class HtitIndicator : Indicator, IWatchlistIndicator
{
[InputParameter("Period", sortIndex: 1, 1, 2000, 1, 0)]
public int Period { get; set; } = 50; // Not used in calculation but kept for consistency if needed
public int Period { get; set; } = 50; // Not used in calculation but kept for consistency
[IndicatorExtensions.DataSourceInput]
public SourceType Source { get; set; } = SourceType.Close;
@@ -16,51 +18,39 @@ public class HtitIndicator : Indicator, IWatchlistIndicator
public bool ShowColdValues { get; set; } = true;
private Htit? _htit;
protected LineSeries? Series;
protected string? SourceName;
private int _warmupBarIndex = -1;
private readonly LineSeries? _series;
private string? _sourceName;
private Func<IHistoryItem, double>? _priceSelector;
public static int MinHistoryDepths => 50;
public static int MinHistoryDepths => 0;
int IWatchlistIndicator.MinHistoryDepths => MinHistoryDepths;
public override string ShortName => $"HTIT:{SourceName}";
public override string ShortName => $"HTIT:{_sourceName}";
public HtitIndicator()
{
OnBackGround = true;
SeparateWindow = false;
SourceName = Source.ToString();
Name = "HTIT - Ehlers Hilbert Transform Instantaneous Trend";
Description = "Ehlers Hilbert Transform Instantaneous Trend";
Series = new(name: "HTIT", color: Color.Orange, width: 2, style: LineStyle.Solid);
AddLineSeries(Series);
_series = new(name: "HTIT", color: Color.Orange, width: 2, style: LineStyle.Solid);
AddLineSeries(_series);
}
protected override void OnInit()
{
_priceSelector = Source.GetPriceSelector();
_sourceName = Source.ToString();
_htit = new Htit();
SourceName = Source.ToString();
_warmupBarIndex = -1;
base.OnInit();
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
protected override void OnUpdate(UpdateArgs args)
{
TValue input = this.GetInputValue(args, Source);
bool isNew = args.Reason == UpdateReason.NewBar || args.Reason == UpdateReason.HistoricalBar;
TValue result = _htit!.Update(input, isNew);
Series!.SetValue(result.Value);
Series!.SetMarker(0, Color.Transparent);
if (_warmupBarIndex < 0 && _htit.IsHot)
_warmupBarIndex = Count;
}
public override void OnPaintChart(PaintChartEventArgs args)
{
base.OnPaintChart(args);
int warmupPeriod = _warmupBarIndex > 0 ? _warmupBarIndex : Count;
this.PaintSmoothCurve(args, Series!, warmupPeriod, showColdValues: ShowColdValues, tension: 0.2);
bool isNew = args.IsNewBar();
var item = HistoricalData[Count - 1, SeekOriginHistory.Begin];
double value = _htit!.Update(new TValue(item.TimeLeft.Ticks, _priceSelector!(item)), isNew).Value;
_series!.SetValue(value, _htit.IsHot, ShowColdValues);
}
}