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
+5 -5
View File
@@ -20,22 +20,22 @@ public class MamaIndicatorTests
}
[Fact]
public void MamaIndicator_MinHistoryDepths_Equals6()
public void MamaIndicator_MinHistoryDepths_Equals50()
{
var indicator = new MamaIndicator();
Assert.Equal(6, MamaIndicator.MinHistoryDepths);
Assert.Equal(6, ((IWatchlistIndicator)indicator).MinHistoryDepths);
Assert.Equal(0, MamaIndicator.MinHistoryDepths);
Assert.Equal(0, ((IWatchlistIndicator)indicator).MinHistoryDepths);
}
[Fact]
public void MamaIndicator_ShortName_IncludesLimitsAndSource()
{
var indicator = new MamaIndicator { FastLimit = 0.5, SlowLimit = 0.05 };
indicator.Initialize();
Assert.Contains("MAMA", indicator.ShortName);
Assert.Contains("0.50", indicator.ShortName);
Assert.Contains("0.05", indicator.ShortName);
Assert.Contains("Close", indicator.ShortName);
}
[Fact]
+24 -39
View File
@@ -1,9 +1,12 @@
using System;
using System.Drawing;
using System.Runtime.CompilerServices;
using TradingPlatform.BusinessLayer;
namespace QuanTAlib;
public class MamaIndicator : Indicator, IWatchlistIndicator
[SkipLocalsInit]
public sealed class MamaIndicator : Indicator, IWatchlistIndicator
{
[InputParameter("Fast Limit", sortIndex: 1, 0.01, 0.99, 0.01, 2)]
public double FastLimit { get; set; } = 0.5;
@@ -17,62 +20,44 @@ public class MamaIndicator : Indicator, IWatchlistIndicator
[InputParameter("Show cold values", sortIndex: 21)]
public bool ShowColdValues { get; set; } = true;
private Mama? _ma;
protected LineSeries? MamaSeries;
protected LineSeries? FamaSeries;
protected string? SourceName;
private int _warmupBarIndex = -1;
private Mama? _mama;
private readonly LineSeries? _series;
private readonly LineSeries? _famaSeries;
private string? _sourceName;
private Func<IHistoryItem, double>? _priceSelector;
public static int MinHistoryDepths => 6;
public static int MinHistoryDepths => 0;
int IWatchlistIndicator.MinHistoryDepths => MinHistoryDepths;
public override string ShortName => $"MAMA({FastLimit:F2}, {SlowLimit:F2}):{SourceName}";
public override string ShortName => $"MAMA:{_sourceName}";
public MamaIndicator()
{
OnBackGround = true;
SeparateWindow = false;
SourceName = Source.ToString();
Name = "MAMA - MESA Adaptive Moving Average";
Description = "MESA Adaptive Moving Average";
MamaSeries = new(name: "MAMA", color: Color.Red, width: 2, style: LineStyle.Solid);
FamaSeries = new(name: "FAMA", color: Color.Blue, width: 2, style: LineStyle.Solid);
AddLineSeries(MamaSeries);
AddLineSeries(FamaSeries);
_series = new(name: "MAMA", color: Color.Orange, width: 2, style: LineStyle.Solid);
_famaSeries = new(name: "FAMA", color: Color.Red, width: 2, style: LineStyle.Solid);
AddLineSeries(_series);
AddLineSeries(_famaSeries);
}
protected override void OnInit()
{
_ma = new Mama(FastLimit, SlowLimit);
SourceName = Source.ToString();
_warmupBarIndex = -1;
_priceSelector = Source.GetPriceSelector();
_sourceName = Source.ToString();
_mama = new Mama(FastLimit, SlowLimit);
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 = _ma!.Update(input, isNew);
MamaSeries!.SetValue(result.Value);
FamaSeries!.SetValue(_ma.Fama.Value);
MamaSeries!.SetMarker(0, Color.Transparent);
FamaSeries!.SetMarker(0, Color.Transparent);
if (_warmupBarIndex < 0 && _ma!.IsHot)
_warmupBarIndex = Count;
}
public override void OnPaintChart(PaintChartEventArgs args)
{
base.OnPaintChart(args);
int warmupPeriod = _warmupBarIndex > 0 ? _warmupBarIndex : Count;
this.PaintSmoothCurve(args, MamaSeries!, warmupPeriod, showColdValues: ShowColdValues, tension: 0.2);
this.PaintSmoothCurve(args, FamaSeries!, warmupPeriod, showColdValues: ShowColdValues, tension: 0.2);
bool isNew = args.IsNewBar();
var item = HistoricalData[Count - 1, SeekOriginHistory.Begin];
double value = _mama!.Update(new TValue(item.TimeLeft.Ticks, _priceSelector!(item)), isNew).Value;
_series!.SetValue(value, _mama.IsHot, ShowColdValues);
_famaSeries!.SetValue(_mama.Fama.Value, _mama.IsHot, ShowColdValues);
}
}