mirror of
https://github.com/mihakralj/QuanTAlib.git
synced 2026-08-24 05:28:05 +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 InertiaIndicatorTests
|
||||
{
|
||||
[Fact]
|
||||
public void InertiaIndicator_Constructor_SetsDefaults()
|
||||
{
|
||||
var indicator = new InertiaIndicator();
|
||||
|
||||
Assert.Equal(20, indicator.Period);
|
||||
Assert.Equal(SourceType.Close, indicator.Source);
|
||||
Assert.True(indicator.ShowColdValues);
|
||||
Assert.Equal("INERTIA - Inertia Oscillator", indicator.Name);
|
||||
Assert.True(indicator.SeparateWindow);
|
||||
Assert.True(indicator.OnBackGround);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void InertiaIndicator_MinHistoryDepths_EqualsZero()
|
||||
{
|
||||
var indicator = new InertiaIndicator { Period = 20 };
|
||||
|
||||
Assert.Equal(0, InertiaIndicator.MinHistoryDepths);
|
||||
IWatchlistIndicator watchlistIndicator = indicator;
|
||||
Assert.Equal(0, watchlistIndicator.MinHistoryDepths);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void InertiaIndicator_ShortName_IncludesParameters()
|
||||
{
|
||||
var indicator = new InertiaIndicator { Period = 14 };
|
||||
indicator.Initialize();
|
||||
|
||||
Assert.Contains("INERTIA", indicator.ShortName, StringComparison.Ordinal);
|
||||
Assert.Contains("14", indicator.ShortName, StringComparison.Ordinal);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void InertiaIndicator_SourceCodeLink_IsValid()
|
||||
{
|
||||
var indicator = new InertiaIndicator();
|
||||
|
||||
Assert.Contains("github.com", indicator.SourceCodeLink, StringComparison.Ordinal);
|
||||
Assert.Contains("Inertia.Quantower.cs", indicator.SourceCodeLink, StringComparison.Ordinal);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void InertiaIndicator_Initialize_CreatesInternalInertia()
|
||||
{
|
||||
var indicator = new InertiaIndicator { Period = 10 };
|
||||
|
||||
indicator.Initialize();
|
||||
|
||||
Assert.Single(indicator.LinesSeries);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void InertiaIndicator_ProcessUpdate_HistoricalBar_ComputesValue()
|
||||
{
|
||||
var indicator = new InertiaIndicator { 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 InertiaIndicator_ProcessUpdate_NewBar_ComputesValue()
|
||||
{
|
||||
var indicator = new InertiaIndicator { 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 InertiaIndicator_Parameters_CanBeChanged()
|
||||
{
|
||||
var indicator = new InertiaIndicator { Period = 20 };
|
||||
|
||||
indicator.Period = 14;
|
||||
indicator.Source = SourceType.Open;
|
||||
|
||||
Assert.Equal(14, indicator.Period);
|
||||
Assert.Equal(SourceType.Open, indicator.Source);
|
||||
Assert.Equal(0, InertiaIndicator.MinHistoryDepths);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,431 @@
|
||||
using Xunit;
|
||||
|
||||
namespace QuanTAlib.Tests;
|
||||
|
||||
public sealed class InertiaTests
|
||||
{
|
||||
private const int DefaultPeriod = 20;
|
||||
|
||||
// === A) Constructor validation ===
|
||||
|
||||
[Fact]
|
||||
public void Constructor_DefaultPeriod_Is20()
|
||||
{
|
||||
var inertia = new Inertia();
|
||||
Assert.Equal(DefaultPeriod, inertia.Period);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Constructor_CustomPeriod_IsStored()
|
||||
{
|
||||
var inertia = new Inertia(period: 10);
|
||||
Assert.Equal(10, inertia.Period);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Constructor_ZeroPeriod_Throws()
|
||||
{
|
||||
var ex = Assert.Throws<ArgumentException>(() => new Inertia(period: 0));
|
||||
Assert.Equal("period", ex.ParamName);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Constructor_NegativePeriod_Throws()
|
||||
{
|
||||
var ex = Assert.Throws<ArgumentException>(() => new Inertia(period: -1));
|
||||
Assert.Equal("period", ex.ParamName);
|
||||
}
|
||||
|
||||
// === B) Basic calculation ===
|
||||
|
||||
[Fact]
|
||||
public void Update_ReturnsFiniteValue()
|
||||
{
|
||||
var inertia = new Inertia(period: 5);
|
||||
for (int i = 0; i < 10; i++)
|
||||
{
|
||||
inertia.Update(new TValue(DateTime.UtcNow, 100.0 + i));
|
||||
}
|
||||
Assert.True(double.IsFinite(inertia.Last.Value));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Update_Last_IsAccessible()
|
||||
{
|
||||
var inertia = new Inertia(period: 5);
|
||||
inertia.Update(new TValue(DateTime.UtcNow, 100.0));
|
||||
Assert.True(double.IsFinite(inertia.Last.Value));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Name_ContainsPeriod()
|
||||
{
|
||||
var inertia = new Inertia(period: 14);
|
||||
Assert.Contains("14", inertia.Name, StringComparison.Ordinal);
|
||||
Assert.Contains("Inertia", inertia.Name, StringComparison.Ordinal);
|
||||
}
|
||||
|
||||
// === C) State + bar correction ===
|
||||
|
||||
[Fact]
|
||||
public void Update_IsNewTrue_AdvancesState()
|
||||
{
|
||||
var inertia = new Inertia(period: 5);
|
||||
for (int i = 0; i < 5; i++)
|
||||
{
|
||||
inertia.Update(new TValue(DateTime.UtcNow, 100.0 + i), isNew: true);
|
||||
}
|
||||
double first = inertia.Last.Value;
|
||||
inertia.Update(new TValue(DateTime.UtcNow, 110.0), isNew: true);
|
||||
Assert.NotEqual(first, inertia.Last.Value);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Update_IsNewFalse_CorrectsSameBar()
|
||||
{
|
||||
var inertia = new Inertia(period: 5);
|
||||
for (int i = 0; i < 6; i++)
|
||||
{
|
||||
inertia.Update(new TValue(DateTime.UtcNow, 100.0 + i), isNew: true);
|
||||
}
|
||||
|
||||
double before = inertia.Last.Value;
|
||||
inertia.Update(new TValue(DateTime.UtcNow, 200.0), isNew: false);
|
||||
double corrected = inertia.Last.Value;
|
||||
Assert.NotEqual(before, corrected);
|
||||
|
||||
// Restore original
|
||||
inertia.Update(new TValue(DateTime.UtcNow, 105.0), isNew: false);
|
||||
Assert.Equal(before, inertia.Last.Value, precision: 10);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Update_IterativeCorrections_RestoreState()
|
||||
{
|
||||
var inertia = new Inertia(period: 5);
|
||||
for (int i = 0; i < 10; i++)
|
||||
{
|
||||
inertia.Update(new TValue(DateTime.UtcNow, 100.0 + i), isNew: true);
|
||||
}
|
||||
|
||||
double baseline = inertia.Last.Value;
|
||||
|
||||
// Multiple corrections on same bar
|
||||
for (int c = 0; c < 5; c++)
|
||||
{
|
||||
inertia.Update(new TValue(DateTime.UtcNow, 150.0 + c), isNew: false);
|
||||
}
|
||||
|
||||
// Restore original value
|
||||
inertia.Update(new TValue(DateTime.UtcNow, 109.0), isNew: false);
|
||||
Assert.Equal(baseline, inertia.Last.Value, precision: 10);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Reset_ClearsState()
|
||||
{
|
||||
var inertia = new Inertia(period: 5);
|
||||
for (int i = 0; i < 10; i++)
|
||||
{
|
||||
inertia.Update(new TValue(DateTime.UtcNow, 100.0 + i));
|
||||
}
|
||||
Assert.True(inertia.IsHot);
|
||||
|
||||
inertia.Reset();
|
||||
Assert.False(inertia.IsHot);
|
||||
Assert.Equal(0.0, inertia.Last.Value);
|
||||
}
|
||||
|
||||
// === D) Warmup/convergence ===
|
||||
|
||||
[Fact]
|
||||
public void IsHot_FlipsAtPeriod()
|
||||
{
|
||||
var inertia = new Inertia(period: 5);
|
||||
for (int i = 0; i < 4; i++)
|
||||
{
|
||||
inertia.Update(new TValue(DateTime.UtcNow, 100.0 + i));
|
||||
Assert.False(inertia.IsHot);
|
||||
}
|
||||
inertia.Update(new TValue(DateTime.UtcNow, 104.0));
|
||||
Assert.True(inertia.IsHot);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void WarmupPeriod_MatchesPeriod()
|
||||
{
|
||||
var inertia = new Inertia(period: 10);
|
||||
Assert.Equal(10, inertia.WarmupPeriod);
|
||||
}
|
||||
|
||||
// === E) Robustness ===
|
||||
|
||||
[Fact]
|
||||
public void Update_NaN_UsesLastValid()
|
||||
{
|
||||
var inertia = new Inertia(period: 5);
|
||||
for (int i = 0; i < 10; i++)
|
||||
{
|
||||
inertia.Update(new TValue(DateTime.UtcNow, 100.0 + i));
|
||||
}
|
||||
|
||||
_ = inertia.Last.Value;
|
||||
inertia.Update(new TValue(DateTime.UtcNow, double.NaN));
|
||||
Assert.True(double.IsFinite(inertia.Last.Value));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Update_Infinity_UsesLastValid()
|
||||
{
|
||||
var inertia = new Inertia(period: 5);
|
||||
for (int i = 0; i < 10; i++)
|
||||
{
|
||||
inertia.Update(new TValue(DateTime.UtcNow, 100.0 + i));
|
||||
}
|
||||
|
||||
inertia.Update(new TValue(DateTime.UtcNow, double.PositiveInfinity));
|
||||
Assert.True(double.IsFinite(inertia.Last.Value));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Update_NegativeInfinity_UsesLastValid()
|
||||
{
|
||||
var inertia = new Inertia(period: 5);
|
||||
for (int i = 0; i < 10; i++)
|
||||
{
|
||||
inertia.Update(new TValue(DateTime.UtcNow, 100.0 + i));
|
||||
}
|
||||
|
||||
inertia.Update(new TValue(DateTime.UtcNow, double.NegativeInfinity));
|
||||
Assert.True(double.IsFinite(inertia.Last.Value));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Update_BatchNaN_StaysFinite()
|
||||
{
|
||||
var inertia = new Inertia(period: 5);
|
||||
for (int i = 0; i < 10; i++)
|
||||
{
|
||||
inertia.Update(new TValue(DateTime.UtcNow, 100.0 + i));
|
||||
}
|
||||
|
||||
for (int i = 0; i < 5; i++)
|
||||
{
|
||||
inertia.Update(new TValue(DateTime.UtcNow, double.NaN));
|
||||
Assert.True(double.IsFinite(inertia.Last.Value));
|
||||
}
|
||||
}
|
||||
|
||||
// === F) Consistency (4 modes) ===
|
||||
|
||||
[Fact]
|
||||
public void AllModes_ProduceSameResults()
|
||||
{
|
||||
int period = 10;
|
||||
int count = 50;
|
||||
var gbm = new GBM(startPrice: 100.0, seed: 42);
|
||||
var bars = gbm.Fetch(count, DateTime.UtcNow.Ticks, TimeSpan.FromMinutes(1));
|
||||
var source = new TSeries();
|
||||
for (int i = 0; i < bars.Count; i++)
|
||||
{
|
||||
source.Add(new TValue(bars[i].Time, bars[i].Close));
|
||||
}
|
||||
|
||||
// Mode 1: Streaming
|
||||
var streaming = new Inertia(period);
|
||||
var streamResults = new double[count];
|
||||
for (int i = 0; i < count; i++)
|
||||
{
|
||||
streaming.Update(new TValue(source.Times[i], source.Values[i]));
|
||||
streamResults[i] = streaming.Last.Value;
|
||||
}
|
||||
|
||||
// Mode 2: Batch (TSeries)
|
||||
var batchResults = Inertia.Batch(source, period);
|
||||
for (int i = 0; i < count; i++)
|
||||
{
|
||||
Assert.Equal(streamResults[i], batchResults.Values[i], precision: 4);
|
||||
}
|
||||
|
||||
// Mode 3: Span
|
||||
Span<double> spanOutput = new double[count];
|
||||
Inertia.Batch(source.Values, spanOutput, period);
|
||||
for (int i = 0; i < count; i++)
|
||||
{
|
||||
Assert.Equal(streamResults[i], spanOutput[i], precision: 4);
|
||||
}
|
||||
|
||||
// Mode 4: Event-based
|
||||
var eventInertia = new Inertia(period);
|
||||
var eventResults = new double[count];
|
||||
int idx = 0;
|
||||
eventInertia.Pub += (object? _, in TValueEventArgs e) =>
|
||||
{
|
||||
if (idx < count)
|
||||
{
|
||||
eventResults[idx++] = e.Value.Value;
|
||||
}
|
||||
};
|
||||
for (int i = 0; i < count; i++)
|
||||
{
|
||||
eventInertia.Update(new TValue(source.Times[i], source.Values[i]));
|
||||
}
|
||||
for (int i = 0; i < count; i++)
|
||||
{
|
||||
Assert.Equal(streamResults[i], eventResults[i], precision: 10);
|
||||
}
|
||||
}
|
||||
|
||||
// === G) Span API tests ===
|
||||
|
||||
[Fact]
|
||||
public void Batch_Span_MismatchedLength_Throws()
|
||||
{
|
||||
var src = new double[] { 1, 2, 3 };
|
||||
var output = new double[5];
|
||||
var ex = Assert.Throws<ArgumentException>(() =>
|
||||
Inertia.Batch(src.AsSpan(), output.AsSpan(), 3));
|
||||
Assert.Equal("output", ex.ParamName);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Batch_Span_ZeroPeriod_Throws()
|
||||
{
|
||||
var src = new double[] { 1, 2, 3 };
|
||||
var output = new double[3];
|
||||
var ex = Assert.Throws<ArgumentException>(() =>
|
||||
Inertia.Batch(src.AsSpan(), output.AsSpan(), 0));
|
||||
Assert.Equal("period", ex.ParamName);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Batch_Span_Empty_NoException()
|
||||
{
|
||||
var src = ReadOnlySpan<double>.Empty;
|
||||
var output = Span<double>.Empty;
|
||||
Inertia.Batch(src, output, 5);
|
||||
Assert.True(true);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Batch_Span_MatchesTSeries()
|
||||
{
|
||||
int period = 5;
|
||||
int count = 30;
|
||||
var gbm = new GBM(startPrice: 100.0, seed: 42);
|
||||
var bars = gbm.Fetch(count, DateTime.UtcNow.Ticks, TimeSpan.FromMinutes(1));
|
||||
var source = new TSeries();
|
||||
for (int i = 0; i < bars.Count; i++)
|
||||
{
|
||||
source.Add(new TValue(bars[i].Time, bars[i].Close));
|
||||
}
|
||||
|
||||
var batchTs = Inertia.Batch(source, period);
|
||||
|
||||
Span<double> spanOutput = new double[count];
|
||||
Inertia.Batch(source.Values, spanOutput, period);
|
||||
|
||||
for (int i = 0; i < count; i++)
|
||||
{
|
||||
Assert.Equal(batchTs.Values[i], spanOutput[i], precision: 12);
|
||||
}
|
||||
}
|
||||
|
||||
// === H) Chainability ===
|
||||
|
||||
[Fact]
|
||||
public void Pub_FiresOnUpdate()
|
||||
{
|
||||
var inertia = new Inertia(period: 5);
|
||||
int count = 0;
|
||||
inertia.Pub += (object? _, in TValueEventArgs _) => count++;
|
||||
for (int i = 0; i < 10; i++)
|
||||
{
|
||||
inertia.Update(new TValue(DateTime.UtcNow, 100.0 + i));
|
||||
}
|
||||
Assert.Equal(10, count);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Chaining_EventBased_Works()
|
||||
{
|
||||
var source = new TSeries();
|
||||
var inertia = new Inertia(source, period: 5);
|
||||
|
||||
for (int i = 0; i < 10; i++)
|
||||
{
|
||||
source.Add(new TValue(DateTime.UtcNow, 100.0 + i), isNew: true);
|
||||
}
|
||||
|
||||
Assert.True(inertia.IsHot);
|
||||
Assert.True(double.IsFinite(inertia.Last.Value));
|
||||
}
|
||||
|
||||
// === Mathematical behavior ===
|
||||
|
||||
[Fact]
|
||||
public void ConstantPrice_InertiaIsZero()
|
||||
{
|
||||
var inertia = new Inertia(period: 5);
|
||||
for (int i = 0; i < 10; i++)
|
||||
{
|
||||
inertia.Update(new TValue(DateTime.UtcNow, 100.0));
|
||||
}
|
||||
// Constant price → perfect regression → residual = 0
|
||||
Assert.Equal(0.0, inertia.Last.Value, precision: 10);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void LinearTrend_InertiaIsZero()
|
||||
{
|
||||
var inertia = new Inertia(period: 5);
|
||||
for (int i = 0; i < 10; i++)
|
||||
{
|
||||
inertia.Update(new TValue(DateTime.UtcNow, 100.0 + i * 2.0));
|
||||
}
|
||||
// Perfect linear trend → regression fits perfectly → residual = 0
|
||||
Assert.Equal(0.0, inertia.Last.Value, precision: 10);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void RisingAboveTrend_InertiaPositive()
|
||||
{
|
||||
var inertia = new Inertia(period: 5);
|
||||
// Feed a trend, then spike up
|
||||
for (int i = 0; i < 5; i++)
|
||||
{
|
||||
inertia.Update(new TValue(DateTime.UtcNow, 100.0 + i));
|
||||
}
|
||||
// Spike above the trend line
|
||||
inertia.Update(new TValue(DateTime.UtcNow, 200.0));
|
||||
Assert.True(inertia.Last.Value > 0);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void FallingBelowTrend_InertiaNegative()
|
||||
{
|
||||
var inertia = new Inertia(period: 5);
|
||||
// Feed a trend, then drop
|
||||
for (int i = 0; i < 5; i++)
|
||||
{
|
||||
inertia.Update(new TValue(DateTime.UtcNow, 100.0 + i));
|
||||
}
|
||||
// Drop below the trend line
|
||||
inertia.Update(new TValue(DateTime.UtcNow, 90.0));
|
||||
Assert.True(inertia.Last.Value < 0);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Calculate_ReturnsResultsAndIndicator()
|
||||
{
|
||||
var source = new TSeries();
|
||||
for (int i = 0; i < 30; i++)
|
||||
{
|
||||
source.Add(new TValue(DateTime.UtcNow, 100.0 + i));
|
||||
}
|
||||
|
||||
var (results, indicator) = Inertia.Calculate(source, period: 10);
|
||||
Assert.Equal(30, results.Count);
|
||||
Assert.True(indicator.IsHot);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,229 @@
|
||||
using System.Runtime.CompilerServices;
|
||||
using Xunit.Abstractions;
|
||||
|
||||
using OoplesFinance.StockIndicators;
|
||||
using OoplesFinance.StockIndicators.Models;
|
||||
|
||||
namespace QuanTAlib.Tests;
|
||||
|
||||
/// <summary>
|
||||
/// Validation tests for Inertia (linear regression residual).
|
||||
/// Cross-validates against manual OLS computation and our CFO/LinReg classes.
|
||||
/// No external library has an Inertia indicator — validated via math identity:
|
||||
/// Inertia = source - TSF, where TSF = slope*(period-1) + intercept.
|
||||
/// </summary>
|
||||
public sealed class InertiaValidationTests : IDisposable
|
||||
{
|
||||
private readonly ValidationTestData _testData;
|
||||
private readonly ITestOutputHelper _output;
|
||||
private bool _disposed;
|
||||
|
||||
public InertiaValidationTests(ITestOutputHelper output)
|
||||
{
|
||||
_output = output;
|
||||
_testData = new ValidationTestData();
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
Dispose(true);
|
||||
}
|
||||
|
||||
private void Dispose(bool disposing)
|
||||
{
|
||||
if (_disposed)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
_disposed = true;
|
||||
|
||||
if (disposing)
|
||||
{
|
||||
_testData?.Dispose();
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
[SkipLocalsInit]
|
||||
public void Validate_Streaming_Batch_Span_Agree()
|
||||
{
|
||||
int period = 14;
|
||||
|
||||
// Streaming
|
||||
var streaming = new Inertia(period);
|
||||
var streamValues = new List<double>(_testData.Data.Count);
|
||||
foreach (var item in _testData.Data)
|
||||
{
|
||||
streamValues.Add(streaming.Update(item).Value);
|
||||
}
|
||||
|
||||
// Batch (TSeries)
|
||||
TSeries batchSeries = Inertia.Batch(_testData.Data, period);
|
||||
|
||||
// Span
|
||||
double[] src = _testData.RawData.ToArray();
|
||||
double[] spanOutput = new double[src.Length];
|
||||
Inertia.Batch(src.AsSpan(), spanOutput.AsSpan(), period);
|
||||
|
||||
// O(1) streaming sumXY maintenance accumulates cancellation drift vs full-recalc batch.
|
||||
// ResyncInterval=1000 bounds drift, but between resyncs tolerance must be relaxed.
|
||||
// Batch vs span should match exactly (same code path).
|
||||
int start = Math.Max(0, src.Length - 200);
|
||||
for (int i = start; i < src.Length; i++)
|
||||
{
|
||||
Assert.Equal(batchSeries[i].Value, spanOutput[i], 12); // batch≡span (same path)
|
||||
Assert.Equal(batchSeries[i].Value, streamValues[i], 4); // streaming drifts ~1e-5 between resyncs
|
||||
}
|
||||
|
||||
_output.WriteLine("Inertia validation: streaming, batch, and span outputs agree within tolerance.");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
[SkipLocalsInit]
|
||||
public void Validate_Against_CfoRelationship()
|
||||
{
|
||||
// Cross-validate Inertia against CFO.
|
||||
// Inertia = source - TSF
|
||||
// CFO = 100 * (source - TSF) / source
|
||||
// Therefore: Inertia = CFO * source / 100
|
||||
int[] periods = [5, 10, 14, 20, 50];
|
||||
|
||||
foreach (int period in periods)
|
||||
{
|
||||
var inertia = new Inertia(period);
|
||||
var cfo = new Cfo(period);
|
||||
|
||||
int validCount = 0;
|
||||
|
||||
foreach (var item in _testData.Data)
|
||||
{
|
||||
inertia.Update(item);
|
||||
cfo.Update(item);
|
||||
|
||||
if (!inertia.IsHot || !cfo.IsHot)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
double src = item.Value;
|
||||
if (src == 0.0)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
double expectedInertia = cfo.Last.Value * src / 100.0;
|
||||
double actualInertia = inertia.Last.Value;
|
||||
|
||||
// skipcq: CS-R1140 - Two independent O(1) streaming implementations accumulate floating-point drift independently
|
||||
Assert.True(Math.Abs(expectedInertia - actualInertia) < 1e-6,
|
||||
$"Inertia mismatch at period={period}: expected={expectedInertia}, actual={actualInertia}, diff={Math.Abs(expectedInertia - actualInertia)}");
|
||||
validCount++;
|
||||
}
|
||||
|
||||
Assert.True(validCount > 0, $"No valid comparison points for period {period}");
|
||||
_output.WriteLine($"Inertia period={period}: validated {validCount} points against CFO relationship.");
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
[SkipLocalsInit]
|
||||
public void Validate_KnownValues_LinearTrend()
|
||||
{
|
||||
// For a perfect linear trend y = a + b*x, the regression line exactly fits.
|
||||
// TSF should equal the source value, so Inertia should be 0.
|
||||
int period = 5;
|
||||
var inertia = new Inertia(period);
|
||||
|
||||
// Feed a perfect linear trend: 10, 11, 12, 13, 14, 15, ...
|
||||
for (int i = 0; i < 20; i++)
|
||||
{
|
||||
inertia.Update(new TValue(DateTime.UtcNow, 10.0 + i));
|
||||
}
|
||||
|
||||
// After warmup, Inertia should be ~0 for a perfect linear trend
|
||||
Assert.Equal(0.0, inertia.Last.Value, 10);
|
||||
_output.WriteLine("Inertia known-values: perfect linear trend produces Inertia=0.");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
[SkipLocalsInit]
|
||||
public void Validate_ManualOls_LastWindow()
|
||||
{
|
||||
// Validate last Inertia value against manual OLS computation
|
||||
int period = 14;
|
||||
var inertia = new Inertia(period);
|
||||
|
||||
foreach (var item in _testData.Data)
|
||||
{
|
||||
inertia.Update(item);
|
||||
}
|
||||
|
||||
// Manual OLS for the last window
|
||||
double[] raw = _testData.RawData.ToArray();
|
||||
int n = period;
|
||||
double sumX = 0, sumY = 0, sumXY = 0, sumX2 = 0;
|
||||
int windowStart = raw.Length - period;
|
||||
for (int j = 0; j < n; j++)
|
||||
{
|
||||
double x = j;
|
||||
double y = raw[windowStart + j];
|
||||
sumX += x;
|
||||
sumY += y;
|
||||
sumXY += x * y;
|
||||
sumX2 += x * x;
|
||||
}
|
||||
|
||||
double denom = n * sumX2 - sumX * sumX;
|
||||
double slope = (n * sumXY - sumX * sumY) / denom;
|
||||
double intercept = (sumY - slope * sumX) / n;
|
||||
double tsf = slope * (n - 1) + intercept;
|
||||
double expected = raw[^1] - tsf;
|
||||
|
||||
_output.WriteLine($"Manual Inertia: {expected:F12}");
|
||||
_output.WriteLine($"Computed Inertia: {inertia.Last.Value:F12}");
|
||||
_output.WriteLine($"Delta: {Math.Abs(expected - inertia.Last.Value):E3}");
|
||||
|
||||
Assert.Equal(expected, inertia.Last.Value, 6);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
[SkipLocalsInit]
|
||||
public void Validate_MultiPeriod_Consistency()
|
||||
{
|
||||
// Different periods should produce different results
|
||||
int[] periods = [5, 14, 50];
|
||||
var results = new List<TSeries>();
|
||||
|
||||
foreach (int period in periods)
|
||||
{
|
||||
results.Add(Inertia.Batch(_testData.Data, period));
|
||||
}
|
||||
|
||||
// After all warmups, values should differ for different periods
|
||||
int checkIdx = 100;
|
||||
for (int i = 0; i < results.Count - 1; i++)
|
||||
{
|
||||
Assert.NotEqual(results[i][checkIdx].Value, results[i + 1][checkIdx].Value);
|
||||
}
|
||||
|
||||
_output.WriteLine("Inertia multi-period: different periods produce different results.");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Inertia_MatchesOoples_Structural()
|
||||
{
|
||||
var gbm = new GBM(startPrice: 100.0, mu: 0.02, sigma: 0.15, seed: 42);
|
||||
var bars = gbm.Fetch(500, DateTime.UtcNow.Ticks, TimeSpan.FromMinutes(1));
|
||||
var ooplesData = bars.Select(b => new TickerData
|
||||
{
|
||||
Date = new DateTime(b.Time, DateTimeKind.Utc),
|
||||
Open = b.Open, High = b.High, Low = b.Low,
|
||||
Close = b.Close, Volume = b.Volume
|
||||
}).ToList();
|
||||
var result = new StockData(ooplesData).CalculateInertiaIndicator();
|
||||
var values = result.CustomValuesList;
|
||||
int finiteCount = values.Count(v => double.IsFinite(v));
|
||||
Assert.True(finiteCount > 100, $"Expected >100 finite values, got {finiteCount}");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user