Add Savitzky-Golay Moving Average (SGMA) Indicator Implementation

- Implemented SgmaIndicator class in C# with properties for Period, Degree, and Source.
- Added unit tests for SgmaIndicator covering constructor defaults, initialization, and various update scenarios.
- Created a new Quantower adapter for the SGMA indicator, including input parameters and line series setup.
- Removed legacy SGMA implementation and tests to streamline the codebase.
- Updated project files to include new indicator and tests in the build process.
- Generated a missing indicators report and outlined a plan for oscillator documentation rewrite.
This commit is contained in:
Miha Kralj
2026-02-13 21:44:45 -08:00
parent 951842acca
commit dfeb23bf3d
81 changed files with 13629 additions and 2041 deletions
+161
View File
@@ -0,0 +1,161 @@
using TradingPlatform.BusinessLayer;
namespace QuanTAlib.Tests;
public sealed class SgmaIndicatorTests
{
[Fact]
public void SgmaIndicator_Constructor_SetsDefaults()
{
var indicator = new SgmaIndicator();
Assert.Equal(9, indicator.Period);
Assert.Equal(2, indicator.Degree);
Assert.Equal(SourceType.Close, indicator.Source);
Assert.True(indicator.ShowColdValues);
Assert.Equal("SGMA - Savitzky-Golay Moving Average", indicator.Name);
Assert.False(indicator.SeparateWindow);
Assert.True(indicator.OnBackGround);
}
[Fact]
public void SgmaIndicator_MinHistoryDepths_EqualsZero()
{
var indicator = new SgmaIndicator { Period = 9, Degree = 2 };
Assert.Equal(0, SgmaIndicator.MinHistoryDepths);
Assert.Equal(0, ((IWatchlistIndicator)indicator).MinHistoryDepths);
}
[Fact]
public void SgmaIndicator_ShortName_IncludesPeriodAndDegree()
{
var indicator = new SgmaIndicator { Period = 15, Degree = 3 };
Assert.Contains("SGMA", indicator.ShortName, StringComparison.Ordinal);
Assert.Contains("15", indicator.ShortName, StringComparison.Ordinal);
Assert.Contains("3", indicator.ShortName, StringComparison.Ordinal);
}
[Fact]
public void SgmaIndicator_SourceCodeLink_IsValid()
{
var indicator = new SgmaIndicator();
Assert.Contains("github.com", indicator.SourceCodeLink, StringComparison.Ordinal);
Assert.Contains("Sgma", indicator.SourceCodeLink, StringComparison.Ordinal);
}
[Fact]
public void SgmaIndicator_Initialize_CreatesInternalSgma()
{
var indicator = new SgmaIndicator { Period = 9, Degree = 2 };
indicator.Initialize();
Assert.Single(indicator.LinesSeries);
}
[Fact]
public void SgmaIndicator_ProcessUpdate_HistoricalBar_ComputesValue()
{
var indicator = new SgmaIndicator { Period = 3, Degree = 2 };
indicator.Initialize();
var now = DateTime.UtcNow;
indicator.HistoricalData.AddBar(now, 100, 105, 95, 102);
var args = new UpdateArgs(UpdateReason.HistoricalBar);
indicator.ProcessUpdate(args);
Assert.Equal(1, indicator.LinesSeries[0].Count);
Assert.True(double.IsFinite(indicator.LinesSeries[0].GetValue(0)));
}
[Fact]
public void SgmaIndicator_ProcessUpdate_NewBar_ComputesValue()
{
var indicator = new SgmaIndicator { Period = 3, Degree = 2 };
indicator.Initialize();
var now = DateTime.UtcNow;
indicator.HistoricalData.AddBar(now, 100, 105, 95, 102);
indicator.HistoricalData.AddBar(now.AddMinutes(1), 102, 108, 100, 106);
indicator.ProcessUpdate(new UpdateArgs(UpdateReason.HistoricalBar));
indicator.ProcessUpdate(new UpdateArgs(UpdateReason.NewBar));
Assert.Equal(2, indicator.LinesSeries[0].Count);
}
[Fact]
public void SgmaIndicator_ProcessUpdate_NewTick_ProcessesWithoutError()
{
var indicator = new SgmaIndicator { Period = 3, Degree = 2 };
indicator.Initialize();
var now = DateTime.UtcNow;
indicator.HistoricalData.AddBar(now, 100, 105, 95, 102);
indicator.ProcessUpdate(new UpdateArgs(UpdateReason.HistoricalBar));
double firstValue = indicator.LinesSeries[0].GetValue(0);
indicator.ProcessUpdate(new UpdateArgs(UpdateReason.NewTick));
double secondValue = indicator.LinesSeries[0].GetValue(0);
Assert.True(double.IsFinite(firstValue));
Assert.True(double.IsFinite(secondValue));
}
[Fact]
public void SgmaIndicator_MultipleUpdates_ProducesCorrectSequence()
{
var indicator = new SgmaIndicator { Period = 3, Degree = 2 };
indicator.Initialize();
var now = DateTime.UtcNow;
double[] closes = { 100, 102, 104, 103, 105 };
foreach (var close in closes)
{
indicator.HistoricalData.AddBar(now, close, close + 2, close - 2, close);
indicator.ProcessUpdate(new UpdateArgs(UpdateReason.HistoricalBar));
now = now.AddMinutes(1);
}
for (int i = 0; i < closes.Length; i++)
{
Assert.True(double.IsFinite(indicator.LinesSeries[0].GetValue(closes.Length - 1 - i)));
}
}
[Fact]
public void SgmaIndicator_DifferentSourceTypes_Work()
{
var sources = new[] { SourceType.Open, SourceType.High, SourceType.Low, SourceType.Close, SourceType.HL2, SourceType.HLC3 };
foreach (var source in sources)
{
var indicator = new SgmaIndicator { Period = 3, Degree = 2, Source = source };
indicator.Initialize();
var now = DateTime.UtcNow;
indicator.HistoricalData.AddBar(now, 100, 110, 90, 105);
indicator.ProcessUpdate(new UpdateArgs(UpdateReason.HistoricalBar));
Assert.True(double.IsFinite(indicator.LinesSeries[0].GetValue(0)),
$"Source {source} should produce finite value");
}
}
[Fact]
public void SgmaIndicator_Period_CanBeChanged()
{
var indicator = new SgmaIndicator { Period = 5 };
Assert.Equal(5, indicator.Period);
indicator.Period = 21;
Assert.Equal(21, indicator.Period);
Assert.Equal(0, SgmaIndicator.MinHistoryDepths);
}
}
+58
View File
@@ -0,0 +1,58 @@
using System.Runtime.CompilerServices;
using TradingPlatform.BusinessLayer;
namespace QuanTAlib;
[SkipLocalsInit]
public sealed class SgmaIndicator : Indicator, IWatchlistIndicator
{
[InputParameter("Period", sortIndex: 1, 3, 2000, 1, 0)]
public int Period { get; set; } = 9;
[InputParameter("Degree", sortIndex: 2, 0, 4, 1, 0)]
public int Degree { get; set; } = 2;
[IndicatorExtensions.DataSourceInput]
public SourceType Source { get; set; } = SourceType.Close;
[InputParameter("Show cold values", sortIndex: 21)]
public bool ShowColdValues { get; set; } = true;
private Sgma _sgma = null!;
private readonly LineSeries _series;
private string _sourceName = null!;
private Func<IHistoryItem, double> _priceSelector = null!;
public static int MinHistoryDepths => 0;
int IWatchlistIndicator.MinHistoryDepths => MinHistoryDepths;
public override string ShortName => $"SGMA {Period},{Degree}:{_sourceName}";
public override string SourceCodeLink => "https://github.com/mihakralj/QuanTAlib/blob/main/lib/trends_FIR/sgma/Sgma.cs";
public SgmaIndicator()
{
OnBackGround = true;
SeparateWindow = false;
Name = "SGMA - Savitzky-Golay Moving Average";
Description = "Polynomial-fitting FIR filter preserving peaks and inflection points";
_series = new LineSeries(name: $"SGMA {Period}", color: IndicatorExtensions.Averages, width: 2, style: LineStyle.Solid);
AddLineSeries(_series);
}
protected override void OnInit()
{
_priceSelector = Source.GetPriceSelector();
_sourceName = Source.ToString();
_sgma = new Sgma(Period, Degree);
base.OnInit();
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
protected override void OnUpdate(UpdateArgs args)
{
bool isNew = args.IsNewBar();
var item = HistoricalData[Count - 1, SeekOriginHistory.Begin];
double value = _sgma.Update(new TValue(item.TimeLeft.Ticks, _priceSelector(item)), isNew).Value;
_series.SetValue(value, _sgma.IsHot, ShowColdValues);
}
}