Add TRAMA implementation and comprehensive tests

- Implemented the TRAMA (Trend Regularity Adaptive Moving Average) class with adaptive EMA logic.
- Added unit tests for TRAMA functionality, including constructor validation, basic calculations, state management, and robustness checks.
- Created validation tests to ensure consistency across different modes of operation (streaming, batch, and static calculations).
- Enhanced documentation for TRAMA, including performance profiles and quality metrics.
- Updated workspace configuration by removing unnecessary folder references.
This commit is contained in:
Miha Kralj
2026-02-21 20:45:38 -08:00
parent 90d5638008
commit 7253f61299
199 changed files with 29577 additions and 234 deletions
+146
View File
@@ -0,0 +1,146 @@
using TradingPlatform.BusinessLayer;
namespace QuanTAlib.Tests;
public class Sp15IndicatorTests
{
[Fact]
public void Sp15Indicator_Constructor_SetsDefaults()
{
var indicator = new Sp15Indicator();
Assert.Equal(SourceType.Close, indicator.Source);
Assert.True(indicator.ShowColdValues);
Assert.Equal("SP15 - Spencer 15-Point Moving Average", indicator.Name);
Assert.False(indicator.SeparateWindow);
Assert.True(indicator.OnBackGround);
}
[Fact]
public void Sp15Indicator_MinHistoryDepths_IsZero()
{
var indicator = new Sp15Indicator();
Assert.Equal(0, Sp15Indicator.MinHistoryDepths);
Assert.Equal(0, ((IWatchlistIndicator)indicator).MinHistoryDepths);
}
[Fact]
public void Sp15Indicator_ShortName_ContainsSP15()
{
var indicator = new Sp15Indicator();
Assert.Contains("SP15", indicator.ShortName, StringComparison.Ordinal);
}
[Fact]
public void Sp15Indicator_SourceCodeLink_IsValid()
{
var indicator = new Sp15Indicator();
Assert.Contains("github.com", indicator.SourceCodeLink, StringComparison.Ordinal);
Assert.Contains("Sp15.Quantower.cs", indicator.SourceCodeLink, StringComparison.Ordinal);
}
[Fact]
public void Sp15Indicator_Initialize_CreatesInternalSp15()
{
var indicator = new Sp15Indicator();
indicator.Initialize();
Assert.Single(indicator.LinesSeries);
}
[Fact]
public void Sp15Indicator_ProcessUpdate_HistoricalBar_ComputesValue()
{
var indicator = new Sp15Indicator();
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 Sp15Indicator_ProcessUpdate_NewBar_ComputesValue()
{
var indicator = new Sp15Indicator();
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 Sp15Indicator_ProcessUpdate_NewTick_ProcessesWithoutError()
{
var indicator = new Sp15Indicator();
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 Sp15Indicator_MultipleUpdates_ProducesCorrectSequence()
{
var indicator = new Sp15Indicator();
indicator.Initialize();
var now = DateTime.UtcNow;
double[] closes = { 100, 102, 104, 103, 105, 107, 106 };
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 Sp15Indicator_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 Sp15Indicator { 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");
}
}
}
+53
View File
@@ -0,0 +1,53 @@
using System.Drawing;
using System.Runtime.CompilerServices;
using TradingPlatform.BusinessLayer;
namespace QuanTAlib;
[SkipLocalsInit]
public sealed class Sp15Indicator : Indicator, IWatchlistIndicator
{
[IndicatorExtensions.DataSourceInput]
public SourceType Source { get; set; } = SourceType.Close;
[InputParameter("Show cold values", sortIndex: 21)]
public bool ShowColdValues { get; set; } = true;
private Sp15 _sp15 = 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 => $"SP15:{_sourceName}";
public override string SourceCodeLink => "https://github.com/mihakralj/QuanTAlib/blob/main/lib/trends_FIR/sp15/Sp15.Quantower.cs";
public Sp15Indicator()
{
OnBackGround = true;
SeparateWindow = false;
Name = "SP15 - Spencer 15-Point Moving Average";
Description = "Spencer 15-Point Moving Average";
_series = new LineSeries(name: "SP15", color: IndicatorExtensions.Averages, width: 2, style: LineStyle.Solid);
AddLineSeries(_series);
}
protected override void OnInit()
{
_priceSelector = Source.GetPriceSelector();
_sourceName = Source.ToString();
_sp15 = new Sp15();
base.OnInit();
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
protected override void OnUpdate(UpdateArgs args)
{
bool isNew = args.IsNewBar();
var item = HistoricalData[Count - 1, SeekOriginHistory.Begin];
double value = _sp15.Update(new TValue(item.TimeLeft.Ticks, _priceSelector(item)), isNew).Value;
_series.SetValue(value, _sp15.IsHot, ShowColdValues);
}
}
+533
View File
@@ -0,0 +1,533 @@
namespace QuanTAlib.Tests;
public class Sp15Tests
{
private static TSeries MakeSeries(int count = 500)
{
var source = new TSeries();
var gbm = new GBM(startPrice: 100, seed: 42);
for (int i = 0; i < count; i++)
{
var bar = gbm.Next();
source.Add(bar.C);
}
return source;
}
// ── A) Constructor validation ──────────────────────────────
[Fact]
public void Constructor_SetsName()
{
var sp15 = new Sp15();
Assert.Equal("Sp15", sp15.Name);
}
[Fact]
public void Constructor_SetsWarmupPeriod()
{
var sp15 = new Sp15();
Assert.Equal(15, sp15.WarmupPeriod);
}
[Fact]
public void Constructor_InitiallyNotHot()
{
var sp15 = new Sp15();
Assert.False(sp15.IsHot);
}
[Fact]
public void Constructor_IsNewDefaultTrue()
{
var sp15 = new Sp15();
Assert.True(sp15.IsNew);
}
// ── B) Basic calculation ───────────────────────────────────
[Fact]
public void Update_ReturnsFiniteValue()
{
var sp15 = new Sp15();
var result = sp15.Update(new TValue(DateTime.UtcNow.Ticks, 100.0));
Assert.True(double.IsFinite(result.Value));
}
[Fact]
public void Update_LastMatchesReturnValue()
{
var sp15 = new Sp15();
var result = sp15.Update(new TValue(DateTime.UtcNow.Ticks, 100.0));
Assert.Equal(result.Value, sp15.Last.Value);
}
[Fact]
public void Update_ConstantInput_ReturnsConstant()
{
// Spencer filter preserves constants (weights sum to 1.0)
var sp15 = new Sp15();
const double c = 42.0;
for (int i = 0; i < 20; i++)
{
sp15.Update(new TValue(DateTime.UtcNow.AddMinutes(i).Ticks, c));
}
Assert.Equal(c, sp15.Last.Value, 1e-10);
}
[Fact]
public void Update_LinearInput_PreservesLinear()
{
// Spencer filter preserves polynomial trends up to degree 3
var sp15 = new Sp15();
const double slope = 2.5;
const double intercept = 10.0;
const int n = 30;
for (int i = 0; i < n; i++)
{
double val = intercept + slope * i;
sp15.Update(new TValue(DateTime.UtcNow.AddMinutes(i).Ticks, val));
}
// Centered at lag 7: output at bar n-1 matches polynomial at bar (n-1)-7
int centerIdx = n - 1 - 7;
double expected = intercept + slope * centerIdx;
Assert.Equal(expected, sp15.Last.Value, 1e-6);
}
[Fact]
public void Update_QuadraticInput_PreservesQuadratic()
{
var sp15 = new Sp15();
const int n = 40;
for (int i = 0; i < n; i++)
{
double val = 0.1 * i * i + 2.0 * i + 5.0;
sp15.Update(new TValue(DateTime.UtcNow.AddMinutes(i).Ticks, val));
}
int k = n - 1 - 7;
double expected = 0.1 * k * k + 2.0 * k + 5.0;
Assert.Equal(expected, sp15.Last.Value, 1e-4);
}
[Fact]
public void Update_CubicInput_PreservesCubic()
{
var sp15 = new Sp15();
const int n = 40;
for (int i = 0; i < n; i++)
{
double val = 0.001 * i * i * i + 0.1 * i * i + 2.0 * i + 5.0;
sp15.Update(new TValue(DateTime.UtcNow.AddMinutes(i).Ticks, val));
}
int k = n - 1 - 7;
double expected = 0.001 * k * k * k + 0.1 * k * k + 2.0 * k + 5.0;
Assert.Equal(expected, sp15.Last.Value, 1e-2);
}
// ── C) State + bar correction ──────────────────────────────
[Fact]
public void IsNew_True_AdvancesState()
{
var sp15 = new Sp15();
for (int i = 0; i < 20; i++)
{
sp15.Update(new TValue(DateTime.UtcNow.AddSeconds(i).Ticks, 100.0 + i), isNew: true);
}
Assert.True(sp15.IsHot);
}
[Fact]
public void IsNew_False_RewritesLastBar()
{
var sp15 = new Sp15();
var series = MakeSeries(20);
for (int i = 0; i < 20; i++)
{
sp15.Update(series[i]);
}
double hotVal = sp15.Last.Value;
// Rewrite latest bar
sp15.Update(new TValue(DateTime.UtcNow.Ticks, 999.0), isNew: false);
double rewriteVal = sp15.Last.Value;
// Undo rewrite by sending original again
sp15.Update(series[19], isNew: false);
double restoredVal = sp15.Last.Value;
Assert.Equal(hotVal, restoredVal, 1e-10);
Assert.NotEqual(hotVal, rewriteVal);
}
[Fact]
public void IterativeCorrections_Restore()
{
var sp15 = new Sp15();
var series = MakeSeries(20);
for (int i = 0; i < 20; i++)
{
sp15.Update(series[i]);
}
double original = sp15.Last.Value;
// Multiple corrections
for (int c = 0; c < 5; c++)
{
sp15.Update(new TValue(DateTime.UtcNow.Ticks, 500.0 + c * 10), isNew: false);
}
// Restore
sp15.Update(series[19], isNew: false);
Assert.Equal(original, sp15.Last.Value, 1e-10);
}
[Fact]
public void Reset_ClearsState()
{
var sp15 = new Sp15();
var series = MakeSeries(20);
for (int i = 0; i < 20; i++)
{
sp15.Update(series[i]);
}
Assert.True(sp15.IsHot);
sp15.Reset();
Assert.False(sp15.IsHot);
Assert.Equal(default, sp15.Last);
}
// ── D) Warmup / convergence ────────────────────────────────
[Fact]
public void IsHot_BecomesTrue_After15Bars()
{
var sp15 = new Sp15();
for (int i = 0; i < 14; i++)
{
sp15.Update(new TValue(DateTime.UtcNow.AddMinutes(i).Ticks, 100.0 + i));
Assert.False(sp15.IsHot);
}
sp15.Update(new TValue(DateTime.UtcNow.AddMinutes(14).Ticks, 114.0));
Assert.True(sp15.IsHot);
}
[Fact]
public void DuringWarmup_ReturnsRawValue()
{
var sp15 = new Sp15();
for (int i = 0; i < 14; i++)
{
double val = 100.0 + i;
var result = sp15.Update(new TValue(DateTime.UtcNow.AddMinutes(i).Ticks, val));
Assert.Equal(val, result.Value, 1e-10);
}
}
// ── E) Robustness ──────────────────────────────────────────
[Fact]
public void NaN_SubstitutesLastValid()
{
var sp15 = new Sp15();
for (int i = 0; i < 20; i++)
{
sp15.Update(new TValue(DateTime.UtcNow.AddMinutes(i).Ticks, 100.0));
}
double beforeNaN = sp15.Last.Value;
sp15.Update(new TValue(DateTime.UtcNow.AddMinutes(20).Ticks, double.NaN));
Assert.Equal(beforeNaN, sp15.Last.Value, 1e-10);
}
[Fact]
public void Infinity_SubstitutesLastValid()
{
var sp15 = new Sp15();
for (int i = 0; i < 20; i++)
{
sp15.Update(new TValue(DateTime.UtcNow.AddMinutes(i).Ticks, 100.0));
}
double beforeInf = sp15.Last.Value;
sp15.Update(new TValue(DateTime.UtcNow.AddMinutes(20).Ticks, double.PositiveInfinity));
Assert.Equal(beforeInf, sp15.Last.Value, 1e-10);
}
[Fact]
public void NaN_BeforeAnyValid_ReturnsNaN()
{
var sp15 = new Sp15();
var result = sp15.Update(new TValue(DateTime.UtcNow.Ticks, double.NaN));
Assert.True(double.IsNaN(result.Value));
}
[Fact]
public void BatchNaN_Safe()
{
double[] src = new double[30];
for (int i = 0; i < 30; i++)
{
src[i] = i < 5 ? double.NaN : 100.0;
}
double[] output = new double[30];
Sp15.Batch(src, output);
for (int i = 20; i < 30; i++)
{
Assert.True(double.IsFinite(output[i]));
}
}
// ── F) Consistency (4 modes match) ─────────────────────────
[Fact]
public void AllModes_ProduceSameResults()
{
var series = MakeSeries(100);
// Mode 1: Streaming
var sp15Stream = new Sp15();
var streaming = new double[100];
for (int i = 0; i < 100; i++)
{
streaming[i] = sp15Stream.Update(series[i]).Value;
}
// Mode 2: Batch TSeries
var batchResult = Sp15.Batch(series);
// Mode 3: Span
double[] spanOutput = new double[100];
Sp15.Batch(series.Values, spanOutput);
// Mode 4: Event
var sp15Event = new Sp15();
var eventResults = new double[100];
int eventIdx = 0;
sp15Event.Pub += (object? sender, in TValueEventArgs e) => { eventResults[eventIdx++] = e.Value.Value; };
for (int i = 0; i < 100; i++)
{
sp15Event.Update(series[i]);
}
for (int i = 0; i < 100; i++)
{
Assert.Equal(streaming[i], batchResult[i].Value, 1e-10);
Assert.Equal(streaming[i], spanOutput[i], 1e-10);
Assert.Equal(streaming[i], eventResults[i], 1e-10);
}
}
// ── G) Span API tests ──────────────────────────────────────
[Fact]
public void Batch_Span_MismatchLength_Throws()
{
double[] src = [1, 2, 3];
double[] output = [0, 0];
var ex = Assert.Throws<ArgumentException>(() => Sp15.Batch(src, output));
Assert.Equal("output", ex.ParamName);
}
[Fact]
public void Batch_Span_EmptyInput_NoOutput()
{
Sp15.Batch(ReadOnlySpan<double>.Empty, Span<double>.Empty);
Assert.True(true); // no-throw is the assertion
}
[Fact]
public void Batch_Span_MatchesTSeries()
{
var series = MakeSeries(50);
var batchTSeries = Sp15.Batch(series);
double[] spanOutput = new double[50];
Sp15.Batch(series.Values, spanOutput);
for (int i = 0; i < 50; i++)
{
Assert.Equal(batchTSeries[i].Value, spanOutput[i], 1e-10);
}
}
[Fact]
public void Batch_Span_HandlesNaN()
{
double[] src = new double[30];
for (int i = 0; i < 30; i++)
{
src[i] = i == 10 ? double.NaN : 50.0;
}
double[] output = new double[30];
Sp15.Batch(src, output);
for (int i = 15; i < 30; i++)
{
Assert.True(double.IsFinite(output[i]));
}
}
[Fact]
public void Batch_Span_LargeData_NoStackOverflow()
{
int size = 10_000;
double[] src = new double[size];
double[] output = new double[size];
for (int i = 0; i < size; i++)
{
src[i] = 100.0 + Math.Sin(i * 0.1);
}
Sp15.Batch(src, output);
Assert.True(double.IsFinite(output[size - 1]));
}
// ── H) Chainability ────────────────────────────────────────
[Fact]
public void Pub_Fires_OnUpdate()
{
var sp15 = new Sp15();
int pubCount = 0;
sp15.Pub += (object? sender, in TValueEventArgs e) => pubCount++;
for (int i = 0; i < 5; i++)
{
sp15.Update(new TValue(DateTime.UtcNow.AddMinutes(i).Ticks, 100.0 + i));
}
Assert.Equal(5, pubCount);
}
[Fact]
public void EventChaining_Works()
{
var series = new TSeries();
var sp15 = new Sp15(series);
double lastValue = double.NaN;
sp15.Pub += (object? sender, in TValueEventArgs e) => { lastValue = e.Value.Value; };
for (int i = 0; i < 20; i++)
{
series.Add(new TValue(DateTime.UtcNow.AddMinutes(i).Ticks, 100.0 + i));
}
Assert.True(double.IsFinite(lastValue));
}
// ── SP15-specific tests ────────────────────────────────────
[Fact]
public void WeightSum_Is320()
{
double[] rawWeights = [-3, -6, -5, 3, 21, 46, 67, 74, 67, 46, 21, 3, -5, -6, -3];
double sum = 0;
for (int i = 0; i < rawWeights.Length; i++)
{
sum += rawWeights[i];
}
Assert.Equal(320.0, sum, 1e-10);
}
[Fact]
public void Weights_AreSymmetric()
{
double[] rawWeights = [-3, -6, -5, 3, 21, 46, 67, 74, 67, 46, 21, 3, -5, -6, -3];
for (int i = 0; i < 7; i++)
{
Assert.Equal(rawWeights[i], rawWeights[14 - i], 1e-12);
}
}
[Fact]
public void NegativeEdgeWeights_CanExceedInputRange()
{
// Extreme values at boundaries with negative weights push output outside input range
var sp15 = new Sp15();
for (int i = 0; i < 15; i++)
{
double val = i == 0 || i == 14 ? 1000.0 : 0.0;
sp15.Update(new TValue(DateTime.UtcNow.AddMinutes(i).Ticks, val));
}
// w[0]*1000 + w[14]*1000 = 2*(-3/320)*1000 = -18.75
Assert.True(sp15.Last.Value < 0);
}
[Fact]
public void Calculate_ReturnsTupleWithIndicator()
{
var series = MakeSeries(30);
var (results, indicator) = Sp15.Calculate(series);
Assert.Equal(30, results.Count);
Assert.NotNull(indicator);
Assert.True(indicator.IsHot);
}
[Fact]
public void Update_TSeries_ReturnsCorrectLength()
{
var series = MakeSeries(50);
var sp15 = new Sp15();
var result = sp15.Update(series);
Assert.Equal(50, result.Count);
}
[Fact]
public void Update_TSeries_RestoresState()
{
var series = MakeSeries(30);
var sp15 = new Sp15();
_ = sp15.Update(series);
var nextBar = new TValue(DateTime.UtcNow.AddMinutes(100).Ticks, 100.0);
var result = sp15.Update(nextBar);
Assert.True(double.IsFinite(result.Value));
Assert.True(sp15.IsHot);
}
[Fact]
public void Prime_FillsState()
{
var sp15 = new Sp15();
double[] data = new double[20];
for (int i = 0; i < 20; i++)
{
data[i] = 100.0 + i;
}
sp15.Prime(data);
Assert.True(sp15.IsHot);
}
[Fact]
public void Update_EmptyTSeries_ReturnsEmpty()
{
var sp15 = new Sp15();
var result = sp15.Update(new TSeries([], []));
Assert.Empty(result);
}
[Fact]
public void Dispose_UnsubscribesFromSource()
{
var series = new TSeries();
var sp15 = new Sp15(series);
sp15.Dispose();
// After dispose, adding to series should not affect sp15
series.Add(new TValue(DateTime.UtcNow.Ticks, 100.0));
Assert.True(true); // no-throw proves unsubscription
}
[Fact]
public void KnownValue_HandComputed()
{
// Hand-computed SP15 with known inputs
// Input: 15 bars all = 100 except bar[7] (center) = 200
var sp15 = new Sp15();
for (int i = 0; i < 15; i++)
{
double val = i == 7 ? 200.0 : 100.0;
sp15.Update(new TValue(DateTime.UtcNow.AddMinutes(i).Ticks, val));
}
// All 100 contributes: 100 * sum(weights) = 100
// Extra 100 at center contributes: 100 * (74/320) = 23.125
// Total = 100 + 23.125 = 123.125
double expected = 100.0 + 100.0 * 74.0 / 320.0;
Assert.Equal(expected, sp15.Last.Value, 1e-10);
}
}
@@ -0,0 +1,275 @@
namespace QuanTAlib.Tests;
public class Sp15ValidationTests
{
private static TSeries MakeSeries(int count = 500)
{
var source = new TSeries();
var gbm = new GBM(startPrice: 100, seed: 42);
for (int i = 0; i < count; i++)
{
var bar = gbm.Next();
source.Add(bar.C);
}
return source;
}
[Fact]
public void BatchVsStreaming_Match()
{
var source = MakeSeries(100);
// Streaming
var sp15 = new Sp15();
var streaming = new double[100];
for (int i = 0; i < 100; i++)
{
streaming[i] = sp15.Update(source[i]).Value;
}
// Batch
var batchResult = Sp15.Batch(source);
for (int i = 0; i < 100; i++)
{
Assert.Equal(streaming[i], batchResult[i].Value, 1e-10);
}
}
[Fact]
public void SpanVsStreaming_Match()
{
var source = MakeSeries(100);
// Streaming
var sp15 = new Sp15();
var streaming = new double[100];
for (int i = 0; i < 100; i++)
{
streaming[i] = sp15.Update(source[i]).Value;
}
// Span
double[] spanOutput = new double[100];
Sp15.Batch(source.Values, spanOutput);
for (int i = 0; i < 100; i++)
{
Assert.Equal(streaming[i], spanOutput[i], 1e-10);
}
}
[Fact]
public void LinearPolynomial_ExactFit()
{
var sp15 = new Sp15();
const int total = 50;
const double a = 5.0, b = 3.0;
for (int i = 0; i < total; i++)
{
double val = a + b * i;
sp15.Update(new TValue(DateTime.UtcNow.AddSeconds(i), val));
}
int centerIdx = total - 1 - 7;
double expected = a + b * centerIdx;
Assert.Equal(expected, sp15.Last.Value, 1e-6);
}
[Fact]
public void QuadraticPolynomial_ExactFit()
{
var sp15 = new Sp15();
const int total = 50;
const double a = 2.0, b = 1.5, c = 0.3;
for (int i = 0; i < total; i++)
{
double val = a + b * i + c * i * i;
sp15.Update(new TValue(DateTime.UtcNow.AddSeconds(i), val));
}
int centerIdx = total - 1 - 7;
double expected = a + b * centerIdx + c * centerIdx * centerIdx;
Assert.Equal(expected, sp15.Last.Value, 1e-4);
}
[Fact]
public void CubicPolynomial_ExactFit()
{
var sp15 = new Sp15();
const int total = 50;
const double a = 1.0, b = 0.5, c = 0.1, d = 0.005;
for (int i = 0; i < total; i++)
{
double val = a + b * i + c * i * i + d * i * i * i;
sp15.Update(new TValue(DateTime.UtcNow.AddSeconds(i), val));
}
int centerIdx = total - 1 - 7;
double expected = a + b * centerIdx + c * centerIdx * centerIdx + d * centerIdx * centerIdx * centerIdx;
Assert.Equal(expected, sp15.Last.Value, 1.0);
}
[Fact]
public void Calculate_ReturnsHotIndicator()
{
var source = MakeSeries(50);
var (results, indicator) = Sp15.Calculate(source);
Assert.True(indicator.IsHot);
Assert.Equal(50, results.Count);
}
[Fact]
public void ConstantPropagation_AllModes()
{
const double c = 77.0;
const int len = 30;
// Build constant series
var source = new TSeries();
for (int i = 0; i < len; i++)
{
source.Add(new TValue(DateTime.UtcNow.AddSeconds(i), c));
}
// Streaming
var sp15 = new Sp15();
for (int i = 0; i < len; i++)
{
sp15.Update(source[i]);
}
Assert.Equal(c, sp15.Last.Value, 1e-10);
// Batch
var batch = Sp15.Batch(source);
for (int i = 15; i < len; i++)
{
Assert.Equal(c, batch[i].Value, 1e-10);
}
// Span
double[] spanOut = new double[len];
Sp15.Batch(source.Values, spanOut);
for (int i = 15; i < len; i++)
{
Assert.Equal(c, spanOut[i], 1e-10);
}
}
[Fact]
public void WeightSymmetry_ForwardReverse()
{
// Symmetric weights: reversing input gives same center value for linear input
var sp15Fwd = new Sp15();
var sp15Rev = new Sp15();
double[] forward = new double[15];
double[] reverse = new double[15];
for (int i = 0; i < 15; i++)
{
forward[i] = 10.0 + 2.0 * i;
reverse[i] = 10.0 + 2.0 * (14 - i);
}
TValue fwdResult = default;
TValue revResult = default;
for (int i = 0; i < 15; i++)
{
fwdResult = sp15Fwd.Update(new TValue(DateTime.UtcNow.AddSeconds(i), forward[i]));
revResult = sp15Rev.Update(new TValue(DateTime.UtcNow.AddSeconds(i), reverse[i]));
}
// For linear input centered at i=7: forward center = 10+14=24, reverse center = 10+14=24
// Both should give the same result for symmetric weights applied to symmetric-about-center linear data
double expected = 2.0 * (10.0 + 2.0 * 7.0);
Assert.Equal(expected, fwdResult.Value + revResult.Value, 1e-6);
}
[Fact]
public void Period4_Sinusoid_Suppressed()
{
// Spencer filter zeros out period-4 signals
var sp15 = new Sp15();
const int n = 60;
for (int i = 0; i < n; i++)
{
// Pure period-4 sinusoid centered at 100
double val = 100.0 + 10.0 * Math.Sin(2.0 * Math.PI * i / 4.0);
sp15.Update(new TValue(DateTime.UtcNow.AddSeconds(i), val));
}
// After warmup, the output should be ~100 (sinusoid suppressed)
Assert.Equal(100.0, sp15.Last.Value, 0.5);
}
[Fact]
public void Period5_Sinusoid_Suppressed()
{
// Spencer filter zeros out period-5 signals
var sp15 = new Sp15();
const int n = 60;
for (int i = 0; i < n; i++)
{
double val = 100.0 + 10.0 * Math.Sin(2.0 * Math.PI * i / 5.0);
sp15.Update(new TValue(DateTime.UtcNow.AddSeconds(i), val));
}
Assert.Equal(100.0, sp15.Last.Value, 0.5);
}
[Fact]
public void DifferentSeeds_ProduceDifferentResults()
{
var source1 = new TSeries();
var gbm1 = new GBM(startPrice: 100, seed: 42);
for (int i = 0; i < 30; i++)
{
source1.Add(gbm1.Next().C);
}
var source2 = new TSeries();
var gbm2 = new GBM(startPrice: 100, seed: 99);
for (int i = 0; i < 30; i++)
{
source2.Add(gbm2.Next().C);
}
var batch1 = Sp15.Batch(source1);
var batch2 = Sp15.Batch(source2);
// At least one value should differ
bool anyDifferent = false;
for (int i = 15; i < 30; i++)
{
if (Math.Abs(batch1[i].Value - batch2[i].Value) > 1e-6)
{
anyDifferent = true;
break;
}
}
Assert.True(anyDifferent);
}
[Fact]
public void LargeDataset_Consistency()
{
var source = MakeSeries(1000);
var sp15 = new Sp15();
var streaming = new double[1000];
for (int i = 0; i < 1000; i++)
{
streaming[i] = sp15.Update(source[i]).Value;
}
double[] spanOut = new double[1000];
Sp15.Batch(source.Values, spanOut);
for (int i = 0; i < 1000; i++)
{
Assert.Equal(streaming[i], spanOut[i], 1e-10);
}
}
}
+367
View File
@@ -0,0 +1,367 @@
using System.Buffers;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
namespace QuanTAlib;
/// <summary>
/// SP15: Spencer 15-Point Moving Average
/// </summary>
/// <remarks>
/// Fixed-coefficient symmetric FIR filter designed by John Spencer (1904) for
/// seasonal adjustment. The 15 weights [-3,-6,-5,3,21,46,67,74,67,46,21,3,-5,-6,-3]/320
/// zero out periodicities at 4 and 5 bars, preserving polynomial trends up to degree 3.
/// Negative edge weights give bandpass-like characteristics.
///
/// Calculation: Compile-time constant weights applied as FIR convolution over
/// a 15-bar sliding window. No configurable parameters.
/// </remarks>
/// <seealso href="Sp15.md">Detailed documentation</seealso>
[SkipLocalsInit]
public sealed class Sp15 : AbstractBase
{
private const int Period = 15;
private const double Divisor = 320.0;
// Normalized weights: w[i] / 320.0, oldest to newest
private static readonly double[] Weights =
[
-3.0 / Divisor, -6.0 / Divisor, -5.0 / Divisor, 3.0 / Divisor,
21.0 / Divisor, 46.0 / Divisor, 67.0 / Divisor, 74.0 / Divisor,
67.0 / Divisor, 46.0 / Divisor, 21.0 / Divisor, 3.0 / Divisor,
-5.0 / Divisor, -6.0 / Divisor, -3.0 / Divisor
];
private readonly RingBuffer _buffer;
private readonly ITValuePublisher? _source;
private readonly TValuePublishedHandler? _pubHandler;
private bool _isNew = true;
private bool _disposed;
private double _lastValidValue = double.NaN;
private double _p_lastValidValue = double.NaN;
public bool IsNew => _isNew;
public override bool IsHot => _buffer.IsFull;
/// <summary>
/// Creates SP15 (Spencer 15-Point Moving Average). No parameters required.
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public Sp15()
{
Name = "Sp15";
WarmupPeriod = Period;
_buffer = new RingBuffer(Period);
}
/// <summary>
/// Creates SP15 connected to a data source for event-based updates.
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public Sp15(ITValuePublisher source) : this()
{
_source = source;
_pubHandler = Handle;
_source.Pub += _pubHandler;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public override TValue Update(TValue input, bool isNew = true)
{
_isNew = isNew;
return Update(input, isNew, publish: true);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private TValue Update(TValue input, bool isNew, bool publish)
{
if (isNew)
{
_p_lastValidValue = _lastValidValue;
}
else
{
_lastValidValue = _p_lastValidValue;
}
double val = GetValidValue(input.Value);
if (!double.IsFinite(val))
{
Last = new TValue(input.Time, double.NaN);
if (publish) { PubEvent(Last, isNew); }
return Last;
}
if (isNew)
{
_lastValidValue = val;
_buffer.Add(val);
int count = _buffer.Count;
double result;
if (count < Period)
{
result = val;
}
else
{
result = ConvolveFull(_buffer);
}
Last = new TValue(input.Time, result);
if (publish) { PubEvent(Last, isNew); }
return Last;
}
else
{
// Bar correction: snapshot, compute, restore
_buffer.Snapshot();
double prevLast = _lastValidValue;
double prevPLast = _p_lastValidValue;
_lastValidValue = val;
_buffer.UpdateNewest(val);
int count = _buffer.Count;
double result;
if (count < Period)
{
result = val;
}
else
{
result = ConvolveFull(_buffer);
}
Last = new TValue(input.Time, result);
// Restore buffer and state
_buffer.Restore();
_lastValidValue = prevLast;
_p_lastValidValue = prevPLast;
if (publish) { PubEvent(Last, isNew); }
return Last;
}
}
public override TSeries Update(TSeries source)
{
if (source.Count == 0)
{
return new TSeries([], []);
}
int len = source.Count;
var t = new List<long>(len);
var v = new List<double>(len);
CollectionsMarshal.SetCount(t, len);
CollectionsMarshal.SetCount(v, len);
var tSpan = CollectionsMarshal.AsSpan(t);
var vSpan = CollectionsMarshal.AsSpan(v);
Batch(source.Values, vSpan);
source.Times.CopyTo(tSpan);
// Restore state by replaying last Period bars
Reset();
int startIndex = Math.Max(0, len - Period);
for (int i = startIndex; i < len; i++)
{
Update(source[i], isNew: true, publish: false);
}
return new TSeries(t, v);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private void Handle(object? sender, in TValueEventArgs e) => Update(e.Value, e.IsNew);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private double GetValidValue(double input)
{
if (double.IsFinite(input))
{
return input;
}
return double.IsFinite(_lastValidValue) ? _lastValidValue : double.NaN;
}
/// <summary>
/// FIR convolution using SIMD DotProduct over circular buffer.
/// Weight[0] corresponds to oldest bar, Weight[Period-1] to newest.
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static double ConvolveFull(RingBuffer buffer)
{
ReadOnlySpan<double> internalBuf = buffer.InternalBuffer;
int head = buffer.StartIndex;
int capacity = buffer.Capacity;
int part1Len = capacity - head;
double sum1 = internalBuf.Slice(head, part1Len).DotProduct(Weights.AsSpan(0, part1Len));
double sum2 = internalBuf[..head].DotProduct(Weights.AsSpan(part1Len));
return sum1 + sum2;
}
public override void Prime(ReadOnlySpan<double> source, TimeSpan? step = null)
{
foreach (var value in source)
{
Update(new TValue(DateTime.MinValue, value));
}
}
/// <summary>
/// Calculates SP15 from a TSeries using streaming updates.
/// </summary>
public static TSeries Batch(TSeries source)
{
var sp15 = new Sp15();
return sp15.Update(source);
}
/// <summary>
/// Calculates Spencer 15-Point Moving Average over a span of values.
/// </summary>
/// <param name="source">Input values</param>
/// <param name="output">Output buffer (must be same length as source)</param>
/// <param name="nanValue">Value to use for NaN substitution (default: NaN)</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void Batch(ReadOnlySpan<double> source, Span<double> output, double nanValue = double.NaN)
{
if (source.Length != output.Length)
{
throw new ArgumentException("Source and output must have the same length", nameof(output));
}
if (source.Length == 0)
{
return;
}
int len = source.Length;
const int StackallocThreshold = 256;
// Allocate ring buffer
double[]? ringRented = Period > StackallocThreshold ? ArrayPool<double>.Shared.Rent(Period) : null;
Span<double> ring = Period <= StackallocThreshold
? stackalloc double[Period]
: ringRented!.AsSpan(0, Period);
// Allocate NaN-corrected values array
double[]? cleanRented = len > StackallocThreshold ? ArrayPool<double>.Shared.Rent(len) : null;
Span<double> clean = len <= StackallocThreshold
? stackalloc double[len]
: cleanRented!.AsSpan(0, len);
try
{
// Build NaN-corrected values array
double lastValid = nanValue;
for (int i = 0; i < len; i++)
{
double val = source[i];
if (double.IsFinite(val))
{
lastValid = val;
clean[i] = val;
}
else if (double.IsFinite(lastValid))
{
clean[i] = lastValid;
}
else
{
clean[i] = double.NaN;
}
}
// Apply Spencer FIR convolution
int ringIdx = 0;
int count = 0;
for (int i = 0; i < len; i++)
{
double val = clean[i];
ring[ringIdx] = val;
ringIdx++;
if (ringIdx >= Period)
{
ringIdx = 0;
}
if (count < Period)
{
count++;
}
if (count < Period)
{
// Warmup: return raw value
output[i] = val;
continue;
}
// Full window: DotProduct convolution over circular buffer
// ringIdx points to next-write = oldest entry
int part1Len = Period - ringIdx;
ReadOnlySpan<double> ringRo = ring;
double sum = ringRo.Slice(ringIdx, part1Len).DotProduct(Weights.AsSpan(0, part1Len))
+ ringRo[..ringIdx].DotProduct(Weights.AsSpan(part1Len));
output[i] = sum;
}
}
finally
{
if (ringRented != null)
{
ArrayPool<double>.Shared.Return(ringRented);
}
if (cleanRented != null)
{
ArrayPool<double>.Shared.Return(cleanRented);
}
}
}
/// <summary>
/// Creates an SP15 indicator and calculates results from source.
/// </summary>
public static (TSeries Results, Sp15 Indicator) Calculate(TSeries source)
{
var indicator = new Sp15();
TSeries results = indicator.Update(source);
return (results, indicator);
}
public override void Reset()
{
_buffer.Clear();
_lastValidValue = double.NaN;
_p_lastValidValue = double.NaN;
Last = default;
}
protected override void Dispose(bool disposing)
{
if (!_disposed)
{
if (disposing && _source != null && _pubHandler != null)
{
_source.Pub -= _pubHandler;
}
_disposed = true;
}
base.Dispose(disposing);
}
}