mirror of
https://github.com/mihakralj/QuanTAlib.git
synced 2026-08-25 13:58:04 +00:00
docs: remove C# Implementation Considerations sections, clean up temp scripts, reorganize test files
- Remove 'C# Implementation Considerations' sections from 34 indicator .md files - Delete 29 temp PowerShell scripts (_fix_mojibake.ps1, _hex_scan.ps1, etc.) - Move test files into tests/ subdirectories for consistent project structure - Add trader-focused bullet points to indicator documentation
This commit is contained in:
@@ -0,0 +1,111 @@
|
||||
using TradingPlatform.BusinessLayer;
|
||||
using QuanTAlib;
|
||||
|
||||
namespace QuanTAlib.Tests;
|
||||
|
||||
public sealed class DpoIndicatorTests
|
||||
{
|
||||
[Fact]
|
||||
public void DpoIndicator_Constructor_SetsDefaults()
|
||||
{
|
||||
var indicator = new DpoIndicator();
|
||||
|
||||
Assert.Equal(20, indicator.Period);
|
||||
Assert.Equal(SourceType.Close, indicator.Source);
|
||||
Assert.True(indicator.ShowColdValues);
|
||||
Assert.Equal("DPO - Detrended Price Oscillator", indicator.Name);
|
||||
Assert.True(indicator.SeparateWindow);
|
||||
Assert.True(indicator.OnBackGround);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void DpoIndicator_MinHistoryDepths_EqualsZero()
|
||||
{
|
||||
var indicator = new DpoIndicator { Period = 20 };
|
||||
|
||||
Assert.Equal(0, DpoIndicator.MinHistoryDepths);
|
||||
IWatchlistIndicator watchlistIndicator = indicator;
|
||||
Assert.Equal(0, watchlistIndicator.MinHistoryDepths);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void DpoIndicator_ShortName_IncludesParameters()
|
||||
{
|
||||
var indicator = new DpoIndicator { Period = 10 };
|
||||
indicator.Initialize();
|
||||
|
||||
Assert.Contains("DPO", indicator.ShortName, StringComparison.Ordinal);
|
||||
Assert.Contains("10", indicator.ShortName, StringComparison.Ordinal);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void DpoIndicator_SourceCodeLink_IsValid()
|
||||
{
|
||||
var indicator = new DpoIndicator();
|
||||
|
||||
Assert.Contains("github.com", indicator.SourceCodeLink, StringComparison.Ordinal);
|
||||
Assert.Contains("Dpo.Quantower.cs", indicator.SourceCodeLink, StringComparison.Ordinal);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void DpoIndicator_Initialize_CreatesInternalDpo()
|
||||
{
|
||||
var indicator = new DpoIndicator { Period = 10 };
|
||||
|
||||
indicator.Initialize();
|
||||
|
||||
Assert.Single(indicator.LinesSeries);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void DpoIndicator_ProcessUpdate_HistoricalBar_ComputesValue()
|
||||
{
|
||||
var indicator = new DpoIndicator { Period = 5 };
|
||||
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 value = indicator.LinesSeries[0].GetValue(0);
|
||||
Assert.True(double.IsFinite(value));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void DpoIndicator_ProcessUpdate_NewBar_ComputesValue()
|
||||
{
|
||||
var indicator = new DpoIndicator { Period = 5 };
|
||||
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);
|
||||
}
|
||||
|
||||
indicator.ProcessUpdate(new UpdateArgs(UpdateReason.HistoricalBar));
|
||||
|
||||
indicator.HistoricalData.AddBar(now.AddMinutes(20), 120, 130, 110, 125);
|
||||
indicator.ProcessUpdate(new UpdateArgs(UpdateReason.NewBar));
|
||||
|
||||
Assert.Equal(2, indicator.LinesSeries[0].Count);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void DpoIndicator_Parameters_CanBeChanged()
|
||||
{
|
||||
var indicator = new DpoIndicator { Period = 20 };
|
||||
|
||||
indicator.Period = 10;
|
||||
indicator.Source = SourceType.Open;
|
||||
|
||||
Assert.Equal(10, indicator.Period);
|
||||
Assert.Equal(SourceType.Open, indicator.Source);
|
||||
Assert.Equal(0, DpoIndicator.MinHistoryDepths);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,396 @@
|
||||
using Xunit;
|
||||
|
||||
namespace QuanTAlib.Tests;
|
||||
|
||||
public sealed class DpoTests
|
||||
{
|
||||
private const int DefaultPeriod = 20;
|
||||
private const double Tolerance = 1e-10;
|
||||
|
||||
// ───── A) Constructor validation ─────
|
||||
|
||||
[Fact]
|
||||
public void Constructor_PeriodZero_ThrowsArgumentException()
|
||||
{
|
||||
var ex = Assert.Throws<ArgumentException>(() => new Dpo(period: 0));
|
||||
Assert.Equal("period", ex.ParamName);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Constructor_NegativePeriod_ThrowsArgumentException()
|
||||
{
|
||||
var ex = Assert.Throws<ArgumentException>(() => new Dpo(period: -1));
|
||||
Assert.Equal("period", ex.ParamName);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Constructor_ValidPeriod_SetsProperties()
|
||||
{
|
||||
var dpo = new Dpo(period: 10);
|
||||
Assert.Equal(10, dpo.Period);
|
||||
Assert.Equal("Dpo(10)", dpo.Name);
|
||||
int expectedDisplacement = (10 / 2) + 1;
|
||||
Assert.Equal(expectedDisplacement, dpo.Displacement);
|
||||
Assert.Equal(10 + expectedDisplacement, dpo.WarmupPeriod);
|
||||
}
|
||||
|
||||
// ───── B) Basic calculation ─────
|
||||
|
||||
[Fact]
|
||||
public void Update_ReturnsTValue()
|
||||
{
|
||||
var dpo = new Dpo(DefaultPeriod);
|
||||
var result = dpo.Update(new TValue(DateTime.UtcNow, 100.0));
|
||||
Assert.IsType<TValue>(result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Update_Last_IsAccessible()
|
||||
{
|
||||
var dpo = new Dpo(DefaultPeriod);
|
||||
dpo.Update(new TValue(DateTime.UtcNow, 100.0));
|
||||
Assert.NotEqual(default, dpo.Last);
|
||||
Assert.False(dpo.IsHot);
|
||||
Assert.Equal($"Dpo({DefaultPeriod})", dpo.Name);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Update_ConstantInput_ZeroDpo()
|
||||
{
|
||||
var dpo = new Dpo(period: 5);
|
||||
int warmup = 5 + (5 / 2) + 1; // period + displacement
|
||||
for (int i = 0; i < warmup + 5; i++)
|
||||
{
|
||||
dpo.Update(new TValue(DateTime.UtcNow, 50.0));
|
||||
}
|
||||
// Constant input => SMA == source => DPO == 0
|
||||
Assert.Equal(0.0, dpo.Last.Value, Tolerance);
|
||||
}
|
||||
|
||||
// ───── C) State + bar correction ─────
|
||||
|
||||
[Fact]
|
||||
public void Update_IsNew_True_AdvancesState()
|
||||
{
|
||||
var dpo = new Dpo(DefaultPeriod);
|
||||
dpo.Update(new TValue(DateTime.UtcNow, 100.0), isNew: true);
|
||||
dpo.Update(new TValue(DateTime.UtcNow, 110.0), isNew: true);
|
||||
|
||||
var last = dpo.Last;
|
||||
Assert.NotEqual(default, last);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Update_IsNew_False_RollsBack()
|
||||
{
|
||||
var dpo = new Dpo(period: 5);
|
||||
int warmup = 5 + (5 / 2) + 1;
|
||||
for (int i = 0; i < warmup + 2; i++)
|
||||
{
|
||||
dpo.Update(new TValue(DateTime.UtcNow, 100.0 + i), isNew: true);
|
||||
}
|
||||
|
||||
dpo.Update(new TValue(DateTime.UtcNow, 105.0), isNew: false);
|
||||
var corrected = dpo.Last;
|
||||
|
||||
dpo.Update(new TValue(DateTime.UtcNow, 105.0), isNew: false);
|
||||
var corrected2 = dpo.Last;
|
||||
|
||||
Assert.Equal(corrected.Value, corrected2.Value, Tolerance);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Update_IterativeCorrections_Restore()
|
||||
{
|
||||
var dpo = new Dpo(period: 5);
|
||||
int warmup = 5 + (5 / 2) + 1;
|
||||
double[] data = new double[warmup + 3];
|
||||
for (int i = 0; i < data.Length; i++)
|
||||
{
|
||||
data[i] = 100 + i * 2;
|
||||
}
|
||||
|
||||
for (int i = 0; i < data.Length; i++)
|
||||
{
|
||||
dpo.Update(new TValue(DateTime.UtcNow, data[i]), isNew: true);
|
||||
}
|
||||
|
||||
var baseline = dpo.Last.Value;
|
||||
|
||||
dpo.Update(new TValue(DateTime.UtcNow, 999.0), isNew: false);
|
||||
dpo.Update(new TValue(DateTime.UtcNow, 888.0), isNew: false);
|
||||
dpo.Update(new TValue(DateTime.UtcNow, data[^1]), isNew: false);
|
||||
|
||||
Assert.Equal(baseline, dpo.Last.Value, Tolerance);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Reset_ClearsState()
|
||||
{
|
||||
var dpo = new Dpo(DefaultPeriod);
|
||||
for (int i = 0; i < 40; i++)
|
||||
{
|
||||
dpo.Update(new TValue(DateTime.UtcNow, 100.0 + i));
|
||||
}
|
||||
Assert.True(dpo.IsHot);
|
||||
|
||||
dpo.Reset();
|
||||
Assert.False(dpo.IsHot);
|
||||
Assert.Equal(default, dpo.Last);
|
||||
}
|
||||
|
||||
// ───── D) Warmup / convergence ─────
|
||||
|
||||
[Fact]
|
||||
public void IsHot_FlipsAtWarmupPeriod()
|
||||
{
|
||||
int period = 5;
|
||||
int displacement = (period / 2) + 1; // 3
|
||||
int warmup = period + displacement; // 8
|
||||
var dpo = new Dpo(period);
|
||||
|
||||
for (int i = 0; i < warmup - 1; i++)
|
||||
{
|
||||
dpo.Update(new TValue(DateTime.UtcNow, 100.0 + i));
|
||||
Assert.False(dpo.IsHot, $"Should not be hot at bar {i + 1}");
|
||||
}
|
||||
dpo.Update(new TValue(DateTime.UtcNow, 108.0));
|
||||
Assert.True(dpo.IsHot);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void WarmupPeriod_MatchesPeriodPlusDisplacement()
|
||||
{
|
||||
var dpo = new Dpo(period: 20);
|
||||
Assert.Equal(20 + (20 / 2) + 1, dpo.WarmupPeriod);
|
||||
}
|
||||
|
||||
// ───── E) Robustness ─────
|
||||
|
||||
[Fact]
|
||||
public void Update_NaN_UsesLastValid()
|
||||
{
|
||||
var dpo = new Dpo(period: 5);
|
||||
int warmup = 5 + (5 / 2) + 1;
|
||||
for (int i = 0; i < warmup + 2; i++)
|
||||
{
|
||||
dpo.Update(new TValue(DateTime.UtcNow, 100.0 + i));
|
||||
}
|
||||
|
||||
dpo.Update(new TValue(DateTime.UtcNow, double.NaN));
|
||||
Assert.True(double.IsFinite(dpo.Last.Value));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Update_Infinity_UsesLastValid()
|
||||
{
|
||||
var dpo = new Dpo(period: 5);
|
||||
int warmup = 5 + (5 / 2) + 1;
|
||||
for (int i = 0; i < warmup + 2; i++)
|
||||
{
|
||||
dpo.Update(new TValue(DateTime.UtcNow, 100.0 + i));
|
||||
}
|
||||
|
||||
dpo.Update(new TValue(DateTime.UtcNow, double.PositiveInfinity));
|
||||
Assert.True(double.IsFinite(dpo.Last.Value));
|
||||
|
||||
dpo.Update(new TValue(DateTime.UtcNow, double.NegativeInfinity));
|
||||
Assert.True(double.IsFinite(dpo.Last.Value));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Update_BatchNaN_Safe()
|
||||
{
|
||||
var dpo = new Dpo(period: 5);
|
||||
for (int i = 0; i < 3; i++)
|
||||
{
|
||||
dpo.Update(new TValue(DateTime.UtcNow, double.NaN));
|
||||
}
|
||||
Assert.True(double.IsFinite(dpo.Last.Value));
|
||||
}
|
||||
|
||||
// ───── F) Consistency (4 modes match) ─────
|
||||
|
||||
[Fact]
|
||||
public void AllModes_ProduceSameResults()
|
||||
{
|
||||
int period = 10;
|
||||
var gbm = new GBM(startPrice: 100.0, mu: 0.02, sigma: 0.1, seed: 42);
|
||||
var bars = gbm.Fetch(500, DateTime.UtcNow.Ticks, TimeSpan.FromMinutes(1));
|
||||
TSeries source = bars.Close;
|
||||
|
||||
// 1. Streaming
|
||||
var streaming = new Dpo(period);
|
||||
var streamResults = new double[source.Count];
|
||||
for (int i = 0; i < source.Count; i++)
|
||||
{
|
||||
streamResults[i] = streaming.Update(source[i]).Value;
|
||||
}
|
||||
|
||||
// 2. Batch TSeries
|
||||
TSeries batchSeries = Dpo.Batch(source, period);
|
||||
|
||||
// 3. Batch Span
|
||||
var spanOutput = new double[source.Count];
|
||||
Dpo.Batch(source.Values, spanOutput, period);
|
||||
|
||||
// 4. Event-based
|
||||
var eventSource = new TSeries();
|
||||
var eventIndicator = new Dpo(eventSource, period);
|
||||
var eventResults = new double[source.Count];
|
||||
for (int i = 0; i < source.Count; i++)
|
||||
{
|
||||
eventSource.Add(source[i]);
|
||||
eventResults[i] = eventIndicator.Last.Value;
|
||||
}
|
||||
|
||||
for (int i = 0; i < source.Count; i++)
|
||||
{
|
||||
Assert.Equal(streamResults[i], batchSeries.Values[i], Tolerance);
|
||||
Assert.Equal(streamResults[i], spanOutput[i], Tolerance);
|
||||
Assert.Equal(streamResults[i], eventResults[i], Tolerance);
|
||||
}
|
||||
}
|
||||
|
||||
// ───── G) Span API tests ─────
|
||||
|
||||
[Fact]
|
||||
public void Batch_Span_MismatchedLength_ThrowsArgumentException()
|
||||
{
|
||||
var source = new double[10];
|
||||
var output = new double[5];
|
||||
var ex = Assert.Throws<ArgumentException>(() => Dpo.Batch(source.AsSpan(), output.AsSpan(), DefaultPeriod));
|
||||
Assert.Equal("output", ex.ParamName);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Batch_Span_ZeroPeriod_ThrowsArgumentException()
|
||||
{
|
||||
var source = new double[10];
|
||||
var output = new double[10];
|
||||
var ex = Assert.Throws<ArgumentException>(() => Dpo.Batch(source.AsSpan(), output.AsSpan(), 0));
|
||||
Assert.Equal("period", ex.ParamName);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Batch_Span_Empty_NoException()
|
||||
{
|
||||
double[] source = [];
|
||||
double[] output = [];
|
||||
var ex = Record.Exception(() => Dpo.Batch(source.AsSpan(), output.AsSpan(), DefaultPeriod));
|
||||
Assert.Null(ex);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Batch_Span_MatchesTSeries()
|
||||
{
|
||||
var gbm = new GBM(startPrice: 100.0, mu: 0.02, sigma: 0.1, seed: 7);
|
||||
var bars = gbm.Fetch(200, DateTime.UtcNow.Ticks, TimeSpan.FromMinutes(1));
|
||||
TSeries source = bars.Close;
|
||||
int period = 10;
|
||||
|
||||
TSeries batchTs = Dpo.Batch(source, period);
|
||||
var spanOutput = new double[source.Count];
|
||||
Dpo.Batch(source.Values, spanOutput, period);
|
||||
|
||||
for (int i = 0; i < source.Count; i++)
|
||||
{
|
||||
Assert.Equal(batchTs.Values[i], spanOutput[i], Tolerance);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Batch_Span_NaN_Handled()
|
||||
{
|
||||
double[] src = [1, 2, double.NaN, 4, 5, 6, 7, 8, 9, 10];
|
||||
var output = new double[src.Length];
|
||||
var ex = Record.Exception(() => Dpo.Batch(src.AsSpan(), output.AsSpan(), 5));
|
||||
Assert.Null(ex);
|
||||
}
|
||||
|
||||
// ───── H) Chainability ─────
|
||||
|
||||
[Fact]
|
||||
public void PubEvent_FiresOnUpdate()
|
||||
{
|
||||
var dpo = new Dpo(DefaultPeriod);
|
||||
int firedCount = 0;
|
||||
dpo.Pub += (object? _, in TValueEventArgs _) => firedCount++;
|
||||
|
||||
dpo.Update(new TValue(DateTime.UtcNow, 100.0));
|
||||
Assert.Equal(1, firedCount);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void EventChaining_Works()
|
||||
{
|
||||
var source = new TSeries();
|
||||
var dpo = new Dpo(source, period: 5);
|
||||
var downstream = new TSeries();
|
||||
dpo.Pub += (object? _, in TValueEventArgs e) => downstream.Add(e.Value);
|
||||
|
||||
for (int i = 0; i < 15; i++)
|
||||
{
|
||||
source.Add(new TValue(DateTime.UtcNow, 100.0 + i));
|
||||
}
|
||||
|
||||
Assert.Equal(15, downstream.Count);
|
||||
}
|
||||
|
||||
// ───── Calculate ─────
|
||||
|
||||
[Fact]
|
||||
public void Calculate_ReturnsResultsAndHotIndicator()
|
||||
{
|
||||
var gbm = new GBM(startPrice: 100.0, mu: 0.02, sigma: 0.1, seed: 42);
|
||||
var bars = gbm.Fetch(50, DateTime.UtcNow.Ticks, TimeSpan.FromMinutes(1));
|
||||
TSeries source = bars.Close;
|
||||
|
||||
var (results, indicator) = Dpo.Calculate(source, period: 5);
|
||||
|
||||
Assert.Equal(source.Count, results.Count);
|
||||
Assert.True(indicator.IsHot);
|
||||
}
|
||||
|
||||
// ───── Update(TSeries) ─────
|
||||
|
||||
[Fact]
|
||||
public void UpdateTSeries_MatchesStreaming()
|
||||
{
|
||||
var gbm = new GBM(startPrice: 100.0, mu: 0.02, sigma: 0.1, seed: 42);
|
||||
var bars = gbm.Fetch(200, DateTime.UtcNow.Ticks, TimeSpan.FromMinutes(1));
|
||||
TSeries source = bars.Close;
|
||||
int period = 10;
|
||||
|
||||
var streaming = new Dpo(period);
|
||||
var streamResults = new double[source.Count];
|
||||
for (int i = 0; i < source.Count; i++)
|
||||
{
|
||||
streamResults[i] = streaming.Update(source[i]).Value;
|
||||
}
|
||||
|
||||
var batch = new Dpo(period);
|
||||
TSeries batchResults = batch.Update(source);
|
||||
|
||||
for (int i = 0; i < source.Count; i++)
|
||||
{
|
||||
Assert.Equal(streamResults[i], batchResults.Values[i], Tolerance);
|
||||
}
|
||||
}
|
||||
|
||||
// ───── Displacement property ─────
|
||||
|
||||
[Fact]
|
||||
public void Displacement_Correct_EvenPeriod()
|
||||
{
|
||||
var dpo = new Dpo(period: 20);
|
||||
Assert.Equal(11, dpo.Displacement); // 20/2 + 1
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Displacement_Correct_OddPeriod()
|
||||
{
|
||||
var dpo = new Dpo(period: 21);
|
||||
Assert.Equal(11, dpo.Displacement); // 21/2 + 1 = 10 + 1 (integer division)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,283 @@
|
||||
using System.Runtime.CompilerServices;
|
||||
using OoplesFinance.StockIndicators;
|
||||
using OoplesFinance.StockIndicators.Models;
|
||||
using Skender.Stock.Indicators;
|
||||
using Xunit;
|
||||
using Xunit.Abstractions;
|
||||
|
||||
namespace QuanTAlib.Tests;
|
||||
|
||||
/// <summary>
|
||||
/// Tulip NETCore uses a centered DPO formula: close[back] - SMA (backward-looking).
|
||||
/// QuanTAlib uses the PineScript non-centered formula: close - SMA[back] (forward-looking).
|
||||
/// These are fundamentally different algorithms producing different results,
|
||||
/// so cross-library validation against Tulip is not applicable.
|
||||
/// Instead, we validate against manual SMA computation and internal consistency.
|
||||
/// </summary>
|
||||
public sealed class DpoValidationTests(ITestOutputHelper output) : IDisposable
|
||||
{
|
||||
private readonly ValidationTestData _testData = new();
|
||||
private readonly ITestOutputHelper _output = output;
|
||||
private bool _disposed;
|
||||
|
||||
private const int TestPeriod = 20;
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
Dispose(disposing: true);
|
||||
}
|
||||
|
||||
private void Dispose(bool disposing)
|
||||
{
|
||||
if (_disposed) { return; }
|
||||
_disposed = true;
|
||||
if (disposing) { _testData?.Dispose(); }
|
||||
}
|
||||
|
||||
#region Manual SMA Cross-Validation
|
||||
|
||||
[Fact]
|
||||
[SkipLocalsInit]
|
||||
public void Validate_Against_Manual_SMA()
|
||||
{
|
||||
double[] values = _testData.RawData.ToArray();
|
||||
int[] periods = [5, 10, 14, 20];
|
||||
|
||||
foreach (int period in periods)
|
||||
{
|
||||
int displacement = (period / 2) + 1;
|
||||
int warmup = period + displacement;
|
||||
|
||||
double[] batchOutput = new double[values.Length];
|
||||
Dpo.Batch(values.AsSpan(), batchOutput.AsSpan(), period);
|
||||
|
||||
int validCount = 0;
|
||||
|
||||
for (int i = warmup - 1; i < values.Length; i++)
|
||||
{
|
||||
// Compute displaced SMA: SMA from `displacement` bars ago
|
||||
int anchor = i - displacement;
|
||||
if (anchor < period - 1)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
double dsum = 0.0;
|
||||
for (int j = anchor - period + 1; j <= anchor; j++)
|
||||
{
|
||||
dsum += values[j];
|
||||
}
|
||||
double displacedSma = dsum / period;
|
||||
|
||||
double expectedDpo = values[i] - displacedSma;
|
||||
double actualDpo = batchOutput[i];
|
||||
|
||||
Assert.True(Math.Abs(expectedDpo - actualDpo) < 1e-9,
|
||||
$"DPO mismatch at i={i}, period={period}: expected={expectedDpo}, actual={actualDpo}, diff={Math.Abs(expectedDpo - actualDpo)}");
|
||||
validCount++;
|
||||
}
|
||||
|
||||
Assert.True(validCount > 0, $"No valid comparison points for period {period}");
|
||||
_output.WriteLine($"DPO period={period}: validated {validCount} points against manual SMA.");
|
||||
}
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(5)]
|
||||
[InlineData(10)]
|
||||
[InlineData(20)]
|
||||
[InlineData(50)]
|
||||
public void Validate_Manual_SMA_DifferentPeriods(int period)
|
||||
{
|
||||
double[] values = _testData.RawData.ToArray();
|
||||
int displacement = (period / 2) + 1;
|
||||
int warmup = period + displacement;
|
||||
|
||||
double[] batchOutput = new double[values.Length];
|
||||
Dpo.Batch(values.AsSpan(), batchOutput.AsSpan(), period);
|
||||
|
||||
int validCount = 0;
|
||||
|
||||
for (int i = warmup - 1; i < values.Length; i++)
|
||||
{
|
||||
int anchor = i - displacement;
|
||||
if (anchor < period - 1) { continue; }
|
||||
|
||||
double dsum = 0.0;
|
||||
for (int j = anchor - period + 1; j <= anchor; j++)
|
||||
{
|
||||
dsum += values[j];
|
||||
}
|
||||
double displacedSma = dsum / period;
|
||||
double expectedDpo = values[i] - displacedSma;
|
||||
|
||||
Assert.True(Math.Abs(expectedDpo - batchOutput[i]) < 1e-9,
|
||||
$"DPO mismatch at i={i}, period={period}: expected={expectedDpo}, actual={batchOutput[i]}");
|
||||
validCount++;
|
||||
}
|
||||
|
||||
Assert.True(validCount > 0, $"No valid comparison points for period {period}");
|
||||
_output.WriteLine($"DPO period={period}: validated {validCount} points.");
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Consistency Validation
|
||||
|
||||
[Fact]
|
||||
[SkipLocalsInit]
|
||||
public void Validate_Streaming_Batch_Span_Agree()
|
||||
{
|
||||
double[] tData = _testData.RawData.ToArray();
|
||||
|
||||
// Batch TSeries
|
||||
TSeries batchSeries = Dpo.Batch(_testData.Data, TestPeriod);
|
||||
|
||||
// Batch Span
|
||||
var spanOutput = new double[tData.Length];
|
||||
Dpo.Batch(tData.AsSpan(), spanOutput.AsSpan(), TestPeriod);
|
||||
|
||||
// Batch and Span should be identical (same code path)
|
||||
for (int i = 0; i < tData.Length; i++)
|
||||
{
|
||||
Assert.Equal(batchSeries.Values[i], spanOutput[i], 12);
|
||||
}
|
||||
|
||||
// Streaming
|
||||
var dpo = new Dpo(TestPeriod);
|
||||
var streamResults = new double[tData.Length];
|
||||
for (int i = 0; i < tData.Length; i++)
|
||||
{
|
||||
streamResults[i] = dpo.Update(_testData.Data[i]).Value;
|
||||
}
|
||||
|
||||
// Streaming vs Batch: may have minor drift from RingBuffer.Sum maintenance
|
||||
int warmup = TestPeriod + (TestPeriod / 2) + 1;
|
||||
int count = tData.Length;
|
||||
int start = Math.Max(warmup, count - ValidationHelper.DefaultVerificationCount);
|
||||
|
||||
for (int i = start; i < count; i++)
|
||||
{
|
||||
Assert.Equal(streamResults[i], batchSeries.Values[i], 4);
|
||||
}
|
||||
|
||||
_output.WriteLine("DPO streaming/batch/span agreement verified.");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
[SkipLocalsInit]
|
||||
public void Validate_Event_Matches_Streaming()
|
||||
{
|
||||
// Streaming
|
||||
var streamDpo = new Dpo(TestPeriod);
|
||||
var streamResults = new double[_testData.Data.Count];
|
||||
for (int i = 0; i < _testData.Data.Count; i++)
|
||||
{
|
||||
streamResults[i] = streamDpo.Update(_testData.Data[i]).Value;
|
||||
}
|
||||
|
||||
// Event-based
|
||||
var eventSource = new TSeries();
|
||||
var eventDpo = new Dpo(eventSource, TestPeriod);
|
||||
var eventResults = new double[_testData.Data.Count];
|
||||
for (int i = 0; i < _testData.Data.Count; i++)
|
||||
{
|
||||
eventSource.Add(_testData.Data[i]);
|
||||
eventResults[i] = eventDpo.Last.Value;
|
||||
}
|
||||
|
||||
for (int i = 0; i < _testData.Data.Count; i++)
|
||||
{
|
||||
Assert.Equal(streamResults[i], eventResults[i], 12);
|
||||
}
|
||||
|
||||
_output.WriteLine("DPO event-based matches streaming.");
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Ooples Cross-Validation
|
||||
|
||||
[Fact]
|
||||
public void Dpo_MatchesOoples_Structural()
|
||||
{
|
||||
// CalculateDetrendedPriceOscillator — structural test (different centering convention)
|
||||
var ooplesData = _testData.SkenderQuotes
|
||||
.Select(q => new TickerData { Date = q.Date, Open = (double)q.Open, High = (double)q.High, Low = (double)q.Low, Close = (double)q.Close, Volume = (double)q.Volume })
|
||||
.ToList();
|
||||
|
||||
var result = new StockData(ooplesData).CalculateDetrendedPriceOscillator();
|
||||
var values = result.CustomValuesList;
|
||||
|
||||
int finiteCount = values.Count(v => double.IsFinite(v));
|
||||
Assert.True(finiteCount > 100, $"Expected >100 finite Ooples DPO values, got {finiteCount}");
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Skender Cross-Validation
|
||||
|
||||
[Fact]
|
||||
public void Validate_Skender_Batch()
|
||||
{
|
||||
var dpo = new Dpo(TestPeriod);
|
||||
var qResult = dpo.Update(_testData.Data);
|
||||
|
||||
var sResult = _testData.SkenderQuotes.GetDpo(TestPeriod).ToList();
|
||||
|
||||
int qFinite = qResult.Count(x => double.IsFinite(x.Value));
|
||||
int sFinite = sResult.Count(x => x.Dpo.HasValue && double.IsFinite(x.Dpo.Value));
|
||||
|
||||
Assert.Equal(_testData.Data.Count, qResult.Count);
|
||||
Assert.Equal(_testData.Data.Count, sResult.Count);
|
||||
Assert.True(qFinite > 100, $"Expected >100 finite QuanTAlib DPO values, got {qFinite}");
|
||||
Assert.True(sFinite > 100, $"Expected >100 finite Skender DPO values, got {sFinite}");
|
||||
|
||||
_output.WriteLine("DPO Batch structural parity verified against Skender GetDpo (non-centered vs centered formula).");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Validate_Skender_Streaming()
|
||||
{
|
||||
var dpo = new Dpo(TestPeriod);
|
||||
var qResults = new List<double>();
|
||||
foreach (var item in _testData.Data)
|
||||
{
|
||||
qResults.Add(dpo.Update(item).Value);
|
||||
}
|
||||
|
||||
var sResult = _testData.SkenderQuotes.GetDpo(TestPeriod).ToList();
|
||||
|
||||
int qFinite = qResults.Count(double.IsFinite);
|
||||
int sFinite = sResult.Count(x => x.Dpo.HasValue && double.IsFinite(x.Dpo.Value));
|
||||
|
||||
Assert.Equal(_testData.Data.Count, qResults.Count);
|
||||
Assert.Equal(_testData.Data.Count, sResult.Count);
|
||||
Assert.True(qFinite > 100, $"Expected >100 finite QuanTAlib DPO values, got {qFinite}");
|
||||
Assert.True(sFinite > 100, $"Expected >100 finite Skender DPO values, got {sFinite}");
|
||||
|
||||
_output.WriteLine("DPO Streaming structural parity verified against Skender GetDpo (non-centered vs centered formula).");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Validate_Skender_Span()
|
||||
{
|
||||
double[] close = _testData.ClosePrices.ToArray();
|
||||
var spanOutput = new double[close.Length];
|
||||
Dpo.Batch(close, spanOutput, TestPeriod);
|
||||
|
||||
var sResult = _testData.SkenderQuotes.GetDpo(TestPeriod).ToList();
|
||||
|
||||
int qFinite = spanOutput.Count(double.IsFinite);
|
||||
int sFinite = sResult.Count(x => x.Dpo.HasValue && double.IsFinite(x.Dpo.Value));
|
||||
|
||||
Assert.Equal(close.Length, spanOutput.Length);
|
||||
Assert.Equal(close.Length, sResult.Count);
|
||||
Assert.True(qFinite > 100, $"Expected >100 finite QuanTAlib DPO values, got {qFinite}");
|
||||
Assert.True(sFinite > 100, $"Expected >100 finite Skender DPO values, got {sFinite}");
|
||||
|
||||
_output.WriteLine("DPO Span structural parity verified against Skender GetDpo (non-centered vs centered formula).");
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
Reference in New Issue
Block a user