mirror of
https://github.com/mihakralj/QuanTAlib.git
synced 2026-08-21 03:58:04 +00:00
SIMD Refactor: Merge simd-dev into dev (#55)
Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com> Co-authored-by: aider (openrouter/anthropic/claude-sonnet-4) <aider@aider.chat> Co-authored-by: Warp <agent@warp.dev>
This commit is contained in:
co-authored by
Claude Opus 4.5
aider
Warp
parent
5bcdf8d614
commit
86fe32a682
@@ -0,0 +1,277 @@
|
||||
using Xunit;
|
||||
using TradingPlatform.BusinessLayer;
|
||||
|
||||
namespace QuanTAlib.Tests;
|
||||
|
||||
public 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.False(indicator.OnBackGround);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SgmaIndicator_MinHistoryDepths_EqualsPeriod()
|
||||
{
|
||||
var indicator = new SgmaIndicator { Period = 9, Degree = 2 };
|
||||
Assert.Equal(9, indicator.MinHistoryDepths);
|
||||
|
||||
indicator = new SgmaIndicator { Period = 21, Degree = 3 };
|
||||
Assert.Equal(21, indicator.MinHistoryDepths);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SgmaIndicator_ShortName_IncludesParameters()
|
||||
{
|
||||
var indicator = new SgmaIndicator { Period = 9, Degree = 2 };
|
||||
Assert.Equal("SGMA(9,2)", indicator.ShortName);
|
||||
|
||||
indicator = new SgmaIndicator { Period = 21, Degree = 4 };
|
||||
Assert.Equal("SGMA(21,4)", indicator.ShortName);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SgmaIndicator_Initialize_CreatesLineSeries()
|
||||
{
|
||||
var indicator = new SgmaIndicator { Period = 9, Degree = 2 };
|
||||
indicator.Initialize();
|
||||
|
||||
Assert.Single(indicator.LinesSeries);
|
||||
Assert.Equal("SGMA", indicator.LinesSeries[0].Name);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SgmaIndicator_ProcessUpdate_HistoricalBar_ComputesValue()
|
||||
{
|
||||
var indicator = new SgmaIndicator { Period = 5, 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);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SgmaIndicator_ProcessUpdate_NewBar_ComputesValue()
|
||||
{
|
||||
var indicator = new SgmaIndicator { Period = 5, 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 = 5, Degree = 2 };
|
||||
indicator.Initialize();
|
||||
|
||||
var now = DateTime.UtcNow;
|
||||
indicator.HistoricalData.AddBar(now, 100, 105, 95, 102);
|
||||
|
||||
indicator.ProcessUpdate(new UpdateArgs(UpdateReason.HistoricalBar));
|
||||
indicator.ProcessUpdate(new UpdateArgs(UpdateReason.NewTick));
|
||||
|
||||
// NewTick should update without crashing
|
||||
Assert.Equal(2, indicator.LinesSeries[0].Count);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SgmaIndicator_MultipleUpdates_ProducesCorrectSequence()
|
||||
{
|
||||
var indicator = new SgmaIndicator { Period = 5, Degree = 2 };
|
||||
indicator.Initialize();
|
||||
|
||||
var now = DateTime.UtcNow;
|
||||
|
||||
for (int i = 0; i < 20; i++)
|
||||
{
|
||||
indicator.HistoricalData.AddBar(
|
||||
now.AddMinutes(i),
|
||||
100 + i * 2,
|
||||
105 + i * 2,
|
||||
95 + i * 2,
|
||||
102 + i * 2);
|
||||
indicator.ProcessUpdate(new UpdateArgs(UpdateReason.HistoricalBar));
|
||||
}
|
||||
|
||||
Assert.Equal(20, indicator.LinesSeries[0].Count);
|
||||
|
||||
// Check that values are finite after warmup
|
||||
for (int i = 0; i < 20; i++)
|
||||
{
|
||||
Assert.True(double.IsFinite(indicator.LinesSeries[0].GetValue(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 = 5,
|
||||
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.Equal(1, indicator.LinesSeries[0].Count);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SgmaIndicator_Period_CanBeChanged()
|
||||
{
|
||||
var indicator = new SgmaIndicator();
|
||||
indicator.Period = 21;
|
||||
|
||||
Assert.Equal(21, indicator.Period);
|
||||
Assert.Equal(21, indicator.MinHistoryDepths);
|
||||
Assert.Equal("SGMA(21,2)", indicator.ShortName);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SgmaIndicator_Degree_CanBeChanged()
|
||||
{
|
||||
var indicator = new SgmaIndicator();
|
||||
indicator.Degree = 4;
|
||||
|
||||
Assert.Equal(4, indicator.Degree);
|
||||
Assert.Equal("SGMA(9,4)", indicator.ShortName);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SgmaIndicator_ShowColdValues_False_SetsNaN()
|
||||
{
|
||||
var indicator = new SgmaIndicator
|
||||
{
|
||||
Period = 21,
|
||||
Degree = 2,
|
||||
ShowColdValues = false
|
||||
};
|
||||
indicator.Initialize();
|
||||
|
||||
var now = DateTime.UtcNow;
|
||||
|
||||
// Add fewer bars than warmup
|
||||
for (int i = 0; i < 5; i++)
|
||||
{
|
||||
indicator.HistoricalData.AddBar(now.AddMinutes(i), 100, 105, 95, 102);
|
||||
indicator.ProcessUpdate(new UpdateArgs(UpdateReason.HistoricalBar));
|
||||
}
|
||||
|
||||
// With ShowColdValues = false, cold values should be NaN before warmup
|
||||
Assert.True(double.IsNaN(indicator.LinesSeries[0].GetValue(0)));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SgmaIndicator_ShowColdValues_True_ShowsValues()
|
||||
{
|
||||
var indicator = new SgmaIndicator
|
||||
{
|
||||
Period = 21,
|
||||
Degree = 2,
|
||||
ShowColdValues = true
|
||||
};
|
||||
indicator.Initialize();
|
||||
|
||||
var now = DateTime.UtcNow;
|
||||
|
||||
// Add fewer bars than warmup
|
||||
for (int i = 0; i < 5; i++)
|
||||
{
|
||||
indicator.HistoricalData.AddBar(now.AddMinutes(i), 100, 105, 95, 102);
|
||||
indicator.ProcessUpdate(new UpdateArgs(UpdateReason.HistoricalBar));
|
||||
}
|
||||
|
||||
// With ShowColdValues = true, values should be shown even before warmup
|
||||
Assert.True(double.IsFinite(indicator.LinesSeries[0].GetValue(0)));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SgmaIndicator_DegreeZero_ProducesUniformWeights()
|
||||
{
|
||||
// Degree 0 should behave like SMA (uniform weights)
|
||||
var indicator = new SgmaIndicator { Period = 5, Degree = 0 };
|
||||
indicator.Initialize();
|
||||
|
||||
var now = DateTime.UtcNow;
|
||||
|
||||
// Add 5 bars with known values
|
||||
double[] values = [10, 20, 30, 40, 50];
|
||||
for (int i = 0; i < 5; i++)
|
||||
{
|
||||
indicator.HistoricalData.AddBar(now.AddMinutes(i), values[i], values[i], values[i], values[i]);
|
||||
indicator.ProcessUpdate(new UpdateArgs(UpdateReason.HistoricalBar));
|
||||
}
|
||||
|
||||
// With degree 0 (uniform weights), result should be simple average
|
||||
double expected = values.Average();
|
||||
double actual = indicator.LinesSeries[0].GetValue(0);
|
||||
Assert.Equal(expected, actual, 6);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SgmaIndicator_HigherDegree_PreservesShape()
|
||||
{
|
||||
// Higher degree preserves peaks and valleys better
|
||||
var indicatorLow = new SgmaIndicator { Period = 5, Degree = 1 };
|
||||
var indicatorHigh = new SgmaIndicator { Period = 5, Degree = 4 };
|
||||
|
||||
indicatorLow.Initialize();
|
||||
indicatorHigh.Initialize();
|
||||
|
||||
var now = DateTime.UtcNow;
|
||||
|
||||
// Create data with a clear pattern
|
||||
double[] values = [100, 110, 150, 110, 100];
|
||||
for (int i = 0; i < 5; i++)
|
||||
{
|
||||
indicatorLow.HistoricalData.AddBar(now.AddMinutes(i), values[i], values[i], values[i], values[i]);
|
||||
indicatorLow.ProcessUpdate(new UpdateArgs(UpdateReason.HistoricalBar));
|
||||
|
||||
indicatorHigh.HistoricalData.AddBar(now.AddMinutes(i), values[i], values[i], values[i], values[i]);
|
||||
indicatorHigh.ProcessUpdate(new UpdateArgs(UpdateReason.HistoricalBar));
|
||||
}
|
||||
|
||||
// Both should produce finite values
|
||||
Assert.True(double.IsFinite(indicatorLow.LinesSeries[0].GetValue(0)));
|
||||
Assert.True(double.IsFinite(indicatorHigh.LinesSeries[0].GetValue(0)));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
// Sgma.Quantower.cs - Quantower adapter for Savitzky-Golay Moving Average
|
||||
|
||||
using System.Drawing;
|
||||
using TradingPlatform.BusinessLayer;
|
||||
using static QuanTAlib.IndicatorExtensions;
|
||||
|
||||
namespace QuanTAlib;
|
||||
|
||||
/// <summary>
|
||||
/// SGMA: Savitzky-Golay Moving Average - Quantower Indicator Adapter
|
||||
/// A FIR filter that uses polynomial fitting to smooth data while preserving
|
||||
/// higher moments (peaks, valleys, and inflection points).
|
||||
/// </summary>
|
||||
public sealed class SgmaIndicator : Indicator, IWatchlistIndicator
|
||||
{
|
||||
[InputParameter("Period", sortIndex: 10, minimum: 3, maximum: 500, increment: 2, decimalPlaces: 0)]
|
||||
public int Period { get; set; } = 9;
|
||||
|
||||
[InputParameter("Polynomial Degree", sortIndex: 11, minimum: 0, maximum: 4, increment: 1, decimalPlaces: 0)]
|
||||
public int Degree { get; set; } = 2;
|
||||
|
||||
[DataSourceInput]
|
||||
public SourceType Source { get; set; } = SourceType.Close;
|
||||
|
||||
[InputParameter("Show Cold Values", sortIndex: 100)]
|
||||
public bool ShowColdValues { get; set; } = true;
|
||||
|
||||
private Sgma? _sgma;
|
||||
private Func<IHistoryItem, double>? _selector;
|
||||
|
||||
public int MinHistoryDepths => Period;
|
||||
public override string ShortName => $"SGMA({Period},{Degree})";
|
||||
|
||||
public SgmaIndicator()
|
||||
{
|
||||
Name = "SGMA - Savitzky-Golay Moving Average";
|
||||
Description = "A FIR filter using polynomial fitting for smoothing with shape preservation.";
|
||||
SeparateWindow = false;
|
||||
OnBackGround = false;
|
||||
}
|
||||
|
||||
protected override void OnInit()
|
||||
{
|
||||
_sgma = new Sgma(Period, Degree);
|
||||
_selector = Source.GetPriceSelector();
|
||||
|
||||
AddLineSeries(new LineSeries("SGMA", Averages, 2, LineStyle.Solid));
|
||||
}
|
||||
|
||||
protected override void OnUpdate(UpdateArgs args)
|
||||
{
|
||||
if (_sgma == null || _selector == null) return;
|
||||
|
||||
var item = HistoricalData[0, SeekOriginHistory.End];
|
||||
double value = _selector(item);
|
||||
bool isNew = args.IsNewBar();
|
||||
|
||||
TValue input = new(item.TimeLeft, value);
|
||||
var result = _sgma.Update(input, isNew);
|
||||
|
||||
bool isHot = _sgma.IsHot;
|
||||
LinesSeries[0].SetValue(result.Value, isHot, ShowColdValues);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user