mirror of
https://github.com/mihakralj/QuanTAlib.git
synced 2026-08-16 09:38:05 +00:00
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:
@@ -0,0 +1,142 @@
|
||||
using TradingPlatform.BusinessLayer;
|
||||
|
||||
namespace QuanTAlib.Tests;
|
||||
|
||||
public sealed class AmatIndicatorTests
|
||||
{
|
||||
[Fact]
|
||||
public void AmatIndicator_Constructor_SetsDefaults()
|
||||
{
|
||||
var indicator = new AmatIndicator();
|
||||
|
||||
Assert.Equal(10, indicator.FastPeriod);
|
||||
Assert.Equal(50, indicator.SlowPeriod);
|
||||
Assert.Equal(SourceType.Close, indicator.Source);
|
||||
Assert.True(indicator.ShowColdValues);
|
||||
Assert.Equal("AMAT - Archer Moving Averages Trends", indicator.Name);
|
||||
Assert.True(indicator.SeparateWindow);
|
||||
Assert.True(indicator.OnBackGround);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void AmatIndicator_MinHistoryDepths_EqualsZero()
|
||||
{
|
||||
var indicator = new AmatIndicator { FastPeriod = 10, SlowPeriod = 50 };
|
||||
|
||||
Assert.Equal(0, AmatIndicator.MinHistoryDepths);
|
||||
Assert.Equal(0, ((IWatchlistIndicator)indicator).MinHistoryDepths);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void AmatIndicator_ShortName_IncludesParameters()
|
||||
{
|
||||
var indicator = new AmatIndicator { FastPeriod = 8, SlowPeriod = 40 };
|
||||
|
||||
Assert.Contains("AMAT", indicator.ShortName, StringComparison.Ordinal);
|
||||
Assert.Contains("8", indicator.ShortName, StringComparison.Ordinal);
|
||||
Assert.Contains("40", indicator.ShortName, StringComparison.Ordinal);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void AmatIndicator_SourceCodeLink_IsValid()
|
||||
{
|
||||
var indicator = new AmatIndicator();
|
||||
|
||||
Assert.Contains("github.com", indicator.SourceCodeLink, StringComparison.Ordinal);
|
||||
Assert.Contains("Amat", indicator.SourceCodeLink, StringComparison.Ordinal);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void AmatIndicator_Initialize_CreatesInternalAmat()
|
||||
{
|
||||
var indicator = new AmatIndicator { FastPeriod = 10, SlowPeriod = 50 };
|
||||
|
||||
indicator.Initialize();
|
||||
|
||||
// Trend + Strength = 2 line series
|
||||
Assert.Equal(2, indicator.LinesSeries.Count);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void AmatIndicator_ProcessUpdate_HistoricalBar_ComputesValue()
|
||||
{
|
||||
var indicator = new AmatIndicator { FastPeriod = 5, SlowPeriod = 10 };
|
||||
indicator.Initialize();
|
||||
|
||||
var now = DateTime.UtcNow;
|
||||
for (int i = 0; i < 20; i++)
|
||||
{
|
||||
indicator.HistoricalData.AddBar(now.AddMinutes(i), 100 + i, 110 + i, 90 + i, 105 + i);
|
||||
|
||||
var args = new UpdateArgs(UpdateReason.HistoricalBar);
|
||||
indicator.ProcessUpdate(args);
|
||||
}
|
||||
|
||||
double trend = indicator.LinesSeries[0].GetValue(0);
|
||||
double strength = indicator.LinesSeries[1].GetValue(0);
|
||||
|
||||
Assert.True(double.IsFinite(trend));
|
||||
Assert.True(double.IsFinite(strength));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void AmatIndicator_ProcessUpdate_NewBar_ComputesValue()
|
||||
{
|
||||
var indicator = new AmatIndicator { FastPeriod = 5, SlowPeriod = 10 };
|
||||
indicator.Initialize();
|
||||
|
||||
var now = DateTime.UtcNow;
|
||||
for (int i = 0; i < 15; i++)
|
||||
{
|
||||
indicator.HistoricalData.AddBar(now.AddMinutes(i), 100 + i, 110 + i, 90 + i, 105 + i);
|
||||
var args = new UpdateArgs(UpdateReason.HistoricalBar);
|
||||
indicator.ProcessUpdate(args);
|
||||
}
|
||||
|
||||
// Simulate a new bar
|
||||
indicator.HistoricalData.AddBar(now.AddMinutes(15), 115, 125, 105, 120);
|
||||
var newArgs = new UpdateArgs(UpdateReason.NewBar);
|
||||
indicator.ProcessUpdate(newArgs);
|
||||
|
||||
double trend = indicator.LinesSeries[0].GetValue(0);
|
||||
double strength = indicator.LinesSeries[1].GetValue(0);
|
||||
|
||||
Assert.True(double.IsFinite(trend));
|
||||
Assert.True(double.IsFinite(strength));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void AmatIndicator_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 AmatIndicator { FastPeriod = 3, SlowPeriod = 8, 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 trend value");
|
||||
Assert.True(double.IsFinite(indicator.LinesSeries[1].GetValue(0)),
|
||||
$"Source {source} should produce finite strength value");
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void AmatIndicator_Periods_CanBeChanged()
|
||||
{
|
||||
var indicator = new AmatIndicator { FastPeriod = 5, SlowPeriod = 20 };
|
||||
Assert.Equal(5, indicator.FastPeriod);
|
||||
Assert.Equal(20, indicator.SlowPeriod);
|
||||
|
||||
indicator.FastPeriod = 15;
|
||||
indicator.SlowPeriod = 60;
|
||||
Assert.Equal(15, indicator.FastPeriod);
|
||||
Assert.Equal(60, indicator.SlowPeriod);
|
||||
Assert.Equal(0, AmatIndicator.MinHistoryDepths);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,66 @@
|
||||
using System.Drawing;
|
||||
using System.Runtime.CompilerServices;
|
||||
using TradingPlatform.BusinessLayer;
|
||||
|
||||
namespace QuanTAlib;
|
||||
|
||||
[SkipLocalsInit]
|
||||
public sealed class AmatIndicator : Indicator, IWatchlistIndicator
|
||||
{
|
||||
[InputParameter("Fast Period", sortIndex: 1, 1, 500, 1, 0)]
|
||||
public int FastPeriod { get; set; } = 10;
|
||||
|
||||
[InputParameter("Slow Period", sortIndex: 2, 1, 500, 1, 0)]
|
||||
public int SlowPeriod { get; set; } = 50;
|
||||
|
||||
[IndicatorExtensions.DataSourceInput]
|
||||
public SourceType Source { get; set; } = SourceType.Close;
|
||||
|
||||
[InputParameter("Show cold values", sortIndex: 21)]
|
||||
public bool ShowColdValues { get; set; } = true;
|
||||
|
||||
private Amat _amat = null!;
|
||||
private readonly LineSeries _trendSeries;
|
||||
private readonly LineSeries _strengthSeries;
|
||||
private string _sourceName = null!;
|
||||
private Func<IHistoryItem, double> _priceSelector = null!;
|
||||
|
||||
public static int MinHistoryDepths => 0;
|
||||
int IWatchlistIndicator.MinHistoryDepths => MinHistoryDepths;
|
||||
|
||||
public override string ShortName => $"AMAT {FastPeriod},{SlowPeriod}:{_sourceName}";
|
||||
public override string SourceCodeLink => "https://github.com/mihakralj/QuanTAlib/blob/main/lib/dynamics/amat/Amat.cs";
|
||||
|
||||
public AmatIndicator()
|
||||
{
|
||||
OnBackGround = true;
|
||||
SeparateWindow = true;
|
||||
Name = "AMAT - Archer Moving Averages Trends";
|
||||
Description = "Trend system using fast/slow EMA alignment for directional signals";
|
||||
|
||||
_trendSeries = new LineSeries(name: "Trend", color: Color.Green, width: 2, style: LineStyle.Solid);
|
||||
_strengthSeries = new LineSeries(name: "Strength", color: Color.Orange, width: 1, style: LineStyle.Solid);
|
||||
|
||||
AddLineSeries(_trendSeries);
|
||||
AddLineSeries(_strengthSeries);
|
||||
}
|
||||
|
||||
protected override void OnInit()
|
||||
{
|
||||
_priceSelector = Source.GetPriceSelector();
|
||||
_sourceName = Source.ToString();
|
||||
_amat = new Amat(FastPeriod, SlowPeriod);
|
||||
base.OnInit();
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
protected override void OnUpdate(UpdateArgs args)
|
||||
{
|
||||
bool isNew = args.IsNewBar();
|
||||
var item = HistoricalData[Count - 1, SeekOriginHistory.Begin];
|
||||
_ = _amat.Update(new TValue(item.TimeLeft.Ticks, _priceSelector(item)), isNew);
|
||||
|
||||
_trendSeries.SetValue(_amat.Last.Value, _amat.IsHot, ShowColdValues);
|
||||
_strengthSeries.SetValue(_amat.Strength.Value, _amat.IsHot, ShowColdValues);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user