mirror of
https://github.com/mihakralj/QuanTAlib.git
synced 2026-08-25 22:08: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,159 @@
|
||||
using TradingPlatform.BusinessLayer;
|
||||
|
||||
namespace QuanTAlib.Tests;
|
||||
|
||||
public class HendIndicatorTests
|
||||
{
|
||||
[Fact]
|
||||
public void HendIndicator_Constructor_SetsDefaults()
|
||||
{
|
||||
var indicator = new HendIndicator();
|
||||
|
||||
Assert.Equal(7, indicator.Period);
|
||||
Assert.Equal(SourceType.Close, indicator.Source);
|
||||
Assert.True(indicator.ShowColdValues);
|
||||
Assert.Equal("HEND - Henderson Moving Average", indicator.Name);
|
||||
Assert.False(indicator.SeparateWindow);
|
||||
Assert.True(indicator.OnBackGround);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void HendIndicator_MinHistoryDepths_IsZero()
|
||||
{
|
||||
var indicator = new HendIndicator { Period = 13 };
|
||||
|
||||
Assert.Equal(0, HendIndicator.MinHistoryDepths);
|
||||
Assert.Equal(0, ((IWatchlistIndicator)indicator).MinHistoryDepths);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void HendIndicator_ShortName_IncludesPeriodAndSource()
|
||||
{
|
||||
var indicator = new HendIndicator { Period = 9 };
|
||||
|
||||
Assert.Contains("HEND", indicator.ShortName, StringComparison.Ordinal);
|
||||
Assert.Contains("9", indicator.ShortName, StringComparison.Ordinal);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void HendIndicator_SourceCodeLink_IsValid()
|
||||
{
|
||||
var indicator = new HendIndicator();
|
||||
|
||||
Assert.Contains("github.com", indicator.SourceCodeLink, StringComparison.Ordinal);
|
||||
Assert.Contains("Hend.Quantower.cs", indicator.SourceCodeLink, StringComparison.Ordinal);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void HendIndicator_Initialize_CreatesInternalHend()
|
||||
{
|
||||
var indicator = new HendIndicator { Period = 7 };
|
||||
|
||||
indicator.Initialize();
|
||||
|
||||
Assert.Single(indicator.LinesSeries);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void HendIndicator_ProcessUpdate_HistoricalBar_ComputesValue()
|
||||
{
|
||||
var indicator = new HendIndicator { Period = 5 };
|
||||
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 HendIndicator_ProcessUpdate_NewBar_ComputesValue()
|
||||
{
|
||||
var indicator = new HendIndicator { Period = 5 };
|
||||
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 HendIndicator_ProcessUpdate_NewTick_ProcessesWithoutError()
|
||||
{
|
||||
var indicator = new HendIndicator { Period = 5 };
|
||||
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 HendIndicator_MultipleUpdates_ProducesCorrectSequence()
|
||||
{
|
||||
var indicator = new HendIndicator { Period = 5 };
|
||||
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 HendIndicator_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 HendIndicator { Period = 5, 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");
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void HendIndicator_Period_CanBeChanged()
|
||||
{
|
||||
var indicator = new HendIndicator { Period = 7 };
|
||||
Assert.Equal(7, indicator.Period);
|
||||
|
||||
indicator.Period = 13;
|
||||
Assert.Equal(13, indicator.Period);
|
||||
Assert.Equal(0, HendIndicator.MinHistoryDepths);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,461 @@
|
||||
using Xunit;
|
||||
|
||||
namespace QuanTAlib.Tests;
|
||||
|
||||
public class HendTests
|
||||
{
|
||||
private const int DefaultPeriod = 7;
|
||||
private const double Epsilon = 1e-10;
|
||||
|
||||
// ── A) Constructor validation ──────────────────────────────────────
|
||||
|
||||
[Fact]
|
||||
public void Constructor_PeriodTooSmall_Throws()
|
||||
{
|
||||
var ex = Assert.Throws<ArgumentException>(() => new Hend(period: 3));
|
||||
Assert.Equal("period", ex.ParamName);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Constructor_ValidPeriod_SetsName()
|
||||
{
|
||||
var hend = new Hend(period: 7);
|
||||
Assert.Equal("Hend(7)", hend.Name);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Constructor_EvenPeriod_AdjustedToOdd()
|
||||
{
|
||||
var hend = new Hend(period: 8);
|
||||
Assert.Equal("Hend(9)", hend.Name);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Constructor_MinPeriod5_Works()
|
||||
{
|
||||
var hend = new Hend(period: 5);
|
||||
Assert.Equal("Hend(5)", hend.Name);
|
||||
}
|
||||
|
||||
// ── B) Basic calculation ───────────────────────────────────────────
|
||||
|
||||
[Fact]
|
||||
public void Update_ReturnsTValue()
|
||||
{
|
||||
var hend = new Hend(DefaultPeriod);
|
||||
var result = hend.Update(new TValue(DateTime.UtcNow, 100.0));
|
||||
Assert.IsType<TValue>(result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Last_IsAccessible()
|
||||
{
|
||||
var hend = new Hend(DefaultPeriod);
|
||||
hend.Update(new TValue(DateTime.UtcNow, 50.0));
|
||||
Assert.Equal(50.0, hend.Last.Value, Epsilon);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ConstantInput_ReturnsConstant()
|
||||
{
|
||||
var hend = new Hend(5);
|
||||
const double c = 42.0;
|
||||
for (int i = 0; i < 20; i++)
|
||||
{
|
||||
hend.Update(new TValue(DateTime.UtcNow.AddSeconds(i), c));
|
||||
}
|
||||
Assert.Equal(c, hend.Last.Value, 1e-9);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void LinearTrend_PreservedExactly()
|
||||
{
|
||||
// Henderson preserves up to cubic polynomials at the CENTER of the window.
|
||||
// For period=5, half=2, the output at bar N represents polynomial at index N-2.
|
||||
const int period = 5;
|
||||
int half = (period - 1) / 2;
|
||||
var hend = new Hend(period);
|
||||
int total = 20;
|
||||
double lastResult = double.NaN;
|
||||
for (int i = 0; i < total; i++)
|
||||
{
|
||||
double val = 10.0 + 3.0 * i;
|
||||
var result = hend.Update(new TValue(DateTime.UtcNow.AddSeconds(i), val));
|
||||
lastResult = result.Value;
|
||||
}
|
||||
// Centered filter: output at bar N = polynomial value at bar N - half
|
||||
int centerIdx = total - 1 - half;
|
||||
double expected = 10.0 + 3.0 * centerIdx;
|
||||
Assert.Equal(expected, lastResult, 1e-6);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void QuadraticTrend_PreservedExactly()
|
||||
{
|
||||
const int period = 5;
|
||||
int half = (period - 1) / 2;
|
||||
var hend = new Hend(period);
|
||||
int total = 20;
|
||||
double lastResult = double.NaN;
|
||||
for (int i = 0; i < total; i++)
|
||||
{
|
||||
double val = 5.0 + 2.0 * i + 0.5 * i * i;
|
||||
var result = hend.Update(new TValue(DateTime.UtcNow.AddSeconds(i), val));
|
||||
lastResult = result.Value;
|
||||
}
|
||||
int centerIdx = total - 1 - half;
|
||||
double expected = 5.0 + 2.0 * centerIdx + 0.5 * centerIdx * centerIdx;
|
||||
Assert.Equal(expected, lastResult, 1e-4);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void CubicTrend_PreservedExactly()
|
||||
{
|
||||
const int period = 5;
|
||||
int half = (period - 1) / 2;
|
||||
var hend = new Hend(period);
|
||||
int total = 20;
|
||||
double lastResult = double.NaN;
|
||||
for (int i = 0; i < total; i++)
|
||||
{
|
||||
double val = 1.0 + 0.5 * i + 0.1 * i * i + 0.01 * i * i * i;
|
||||
var result = hend.Update(new TValue(DateTime.UtcNow.AddSeconds(i), val));
|
||||
lastResult = result.Value;
|
||||
}
|
||||
int centerIdx = total - 1 - half;
|
||||
double expected = 1.0 + 0.5 * centerIdx + 0.1 * centerIdx * centerIdx + 0.01 * centerIdx * centerIdx * centerIdx;
|
||||
Assert.Equal(expected, lastResult, 1e-2);
|
||||
}
|
||||
|
||||
// ── C) State + bar correction ──────────────────────────────────────
|
||||
|
||||
[Fact]
|
||||
public void IsNew_True_AdvancesState()
|
||||
{
|
||||
var hend = new Hend(5);
|
||||
for (int i = 0; i < 10; i++)
|
||||
{
|
||||
hend.Update(new TValue(DateTime.UtcNow.AddSeconds(i), 100 + i), isNew: true);
|
||||
}
|
||||
Assert.True(hend.IsHot);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void IsNew_False_Rewrites()
|
||||
{
|
||||
var hend = new Hend(5);
|
||||
for (int i = 0; i < 6; i++)
|
||||
{
|
||||
hend.Update(new TValue(DateTime.UtcNow.AddSeconds(i), 100.0), isNew: true);
|
||||
}
|
||||
var before = hend.Last.Value;
|
||||
|
||||
// Bar correction with different value
|
||||
hend.Update(new TValue(DateTime.UtcNow.AddSeconds(5), 200.0), isNew: false);
|
||||
var corrected = hend.Last.Value;
|
||||
|
||||
// Should be different since one value changed
|
||||
Assert.NotEqual(before, corrected);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void IterativeCorrections_Restore()
|
||||
{
|
||||
var hend = new Hend(5);
|
||||
for (int i = 0; i < 10; i++)
|
||||
{
|
||||
hend.Update(new TValue(DateTime.UtcNow.AddSeconds(i), 50.0 + i), isNew: true);
|
||||
}
|
||||
var snapshot = hend.Last.Value;
|
||||
|
||||
// Multiple corrections, then re-send same value
|
||||
hend.Update(new TValue(DateTime.UtcNow.AddSeconds(10), 999.0), isNew: false);
|
||||
hend.Update(new TValue(DateTime.UtcNow.AddSeconds(10), 888.0), isNew: false);
|
||||
hend.Update(new TValue(DateTime.UtcNow.AddSeconds(10), 50.0 + 9), isNew: false);
|
||||
|
||||
// Last correction with original value should restore
|
||||
Assert.Equal(snapshot, hend.Last.Value, 1e-10);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Reset_ClearsState()
|
||||
{
|
||||
var hend = new Hend(5);
|
||||
for (int i = 0; i < 10; i++)
|
||||
{
|
||||
hend.Update(new TValue(DateTime.UtcNow.AddSeconds(i), 100.0));
|
||||
}
|
||||
Assert.True(hend.IsHot);
|
||||
|
||||
hend.Reset();
|
||||
Assert.False(hend.IsHot);
|
||||
Assert.Equal(default, hend.Last);
|
||||
}
|
||||
|
||||
// ── D) Warmup / convergence ────────────────────────────────────────
|
||||
|
||||
[Fact]
|
||||
public void IsHot_FlipsWhenBufferFull()
|
||||
{
|
||||
var hend = new Hend(5);
|
||||
for (int i = 0; i < 4; i++)
|
||||
{
|
||||
hend.Update(new TValue(DateTime.UtcNow.AddSeconds(i), 100.0));
|
||||
Assert.False(hend.IsHot);
|
||||
}
|
||||
hend.Update(new TValue(DateTime.UtcNow.AddSeconds(4), 100.0));
|
||||
Assert.True(hend.IsHot);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void WarmupPeriod_EqualsUserPeriod()
|
||||
{
|
||||
var hend = new Hend(7);
|
||||
Assert.Equal(7, hend.WarmupPeriod);
|
||||
}
|
||||
|
||||
// ── E) Robustness ──────────────────────────────────────────────────
|
||||
|
||||
[Fact]
|
||||
public void NaN_SubstitutesLastValid()
|
||||
{
|
||||
var hend = new Hend(5);
|
||||
for (int i = 0; i < 6; i++)
|
||||
{
|
||||
hend.Update(new TValue(DateTime.UtcNow.AddSeconds(i), 100.0));
|
||||
}
|
||||
|
||||
// Send NaN - should substitute last valid
|
||||
hend.Update(new TValue(DateTime.UtcNow.AddSeconds(6), double.NaN));
|
||||
|
||||
Assert.True(double.IsFinite(hend.Last.Value));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Infinity_SubstitutesLastValid()
|
||||
{
|
||||
var hend = new Hend(5);
|
||||
for (int i = 0; i < 6; i++)
|
||||
{
|
||||
hend.Update(new TValue(DateTime.UtcNow.AddSeconds(i), 100.0));
|
||||
}
|
||||
|
||||
hend.Update(new TValue(DateTime.UtcNow.AddSeconds(6), double.PositiveInfinity));
|
||||
Assert.True(double.IsFinite(hend.Last.Value));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void BatchNaN_Safe()
|
||||
{
|
||||
double[] src = [1, 2, double.NaN, 4, 5, 6, 7, 8, 9, 10];
|
||||
double[] output = new double[src.Length];
|
||||
Hend.Batch(src, output, period: 5);
|
||||
|
||||
for (int i = 0; i < output.Length; i++)
|
||||
{
|
||||
Assert.True(double.IsFinite(output[i]), $"output[{i}] is not finite");
|
||||
}
|
||||
}
|
||||
|
||||
// ── F) Consistency ─────────────────────────────────────────────────
|
||||
|
||||
[Fact]
|
||||
public void Batch_MatchesStreaming()
|
||||
{
|
||||
const int len = 50;
|
||||
var source = new TSeries();
|
||||
var gbm = new GBM(startPrice: 100, seed: 42);
|
||||
|
||||
for (int i = 0; i < len; i++)
|
||||
{
|
||||
var bar = gbm.Next();
|
||||
source.Add(bar.C);
|
||||
}
|
||||
|
||||
// Streaming
|
||||
var hend = new Hend(DefaultPeriod);
|
||||
var streaming = new double[len];
|
||||
for (int i = 0; i < len; i++)
|
||||
{
|
||||
var result = hend.Update(source[i]);
|
||||
streaming[i] = result.Value;
|
||||
}
|
||||
|
||||
// Batch TSeries
|
||||
var batchResult = Hend.Batch(source, DefaultPeriod);
|
||||
|
||||
for (int i = 0; i < len; i++)
|
||||
{
|
||||
Assert.Equal(streaming[i], batchResult[i].Value, 1e-10);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Span_MatchesStreaming()
|
||||
{
|
||||
const int len = 50;
|
||||
var source = new TSeries();
|
||||
var gbm = new GBM(startPrice: 100, seed: 42);
|
||||
|
||||
for (int i = 0; i < len; i++)
|
||||
{
|
||||
var bar = gbm.Next();
|
||||
source.Add(bar.C);
|
||||
}
|
||||
|
||||
// Streaming
|
||||
var hend = new Hend(DefaultPeriod);
|
||||
var streaming = new double[len];
|
||||
for (int i = 0; i < len; i++)
|
||||
{
|
||||
var result = hend.Update(source[i]);
|
||||
streaming[i] = result.Value;
|
||||
}
|
||||
|
||||
// Span
|
||||
double[] spanOutput = new double[len];
|
||||
Hend.Batch(source.Values, spanOutput, DefaultPeriod);
|
||||
|
||||
for (int i = 0; i < len; i++)
|
||||
{
|
||||
Assert.Equal(streaming[i], spanOutput[i], 1e-10);
|
||||
}
|
||||
}
|
||||
|
||||
// ── G) Span API tests ──────────────────────────────────────────────
|
||||
|
||||
[Fact]
|
||||
public void Batch_Span_MismatchedLengths_Throws()
|
||||
{
|
||||
double[] src = [1, 2, 3, 4, 5];
|
||||
double[] output = new double[3];
|
||||
var ex = Assert.Throws<ArgumentException>(() => Hend.Batch(src, output, period: 5));
|
||||
Assert.Equal("output", ex.ParamName);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Batch_Span_PeriodTooSmall_Throws()
|
||||
{
|
||||
double[] src = [1, 2, 3];
|
||||
double[] output = new double[3];
|
||||
var ex = Assert.Throws<ArgumentException>(() => Hend.Batch(src, output, period: 3));
|
||||
Assert.Equal("period", ex.ParamName);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Batch_Span_EmptyInput_NoOp()
|
||||
{
|
||||
Hend.Batch(ReadOnlySpan<double>.Empty, Span<double>.Empty, period: 5);
|
||||
Assert.True(true); // no-throw is the assertion
|
||||
}
|
||||
|
||||
// ── H) Chainability ────────────────────────────────────────────────
|
||||
|
||||
[Fact]
|
||||
public void Pub_Fires()
|
||||
{
|
||||
var hend = new Hend(5);
|
||||
bool fired = false;
|
||||
hend.Pub += (object? sender, in TValueEventArgs e) => fired = true;
|
||||
hend.Update(new TValue(DateTime.UtcNow, 100.0));
|
||||
Assert.True(fired);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void EventBased_Chaining()
|
||||
{
|
||||
var source = new TSeries();
|
||||
var hend = new Hend(source, period: 5);
|
||||
|
||||
for (int i = 0; i < 10; i++)
|
||||
{
|
||||
source.Add(new TValue(DateTime.UtcNow.AddSeconds(i), 100.0 + i));
|
||||
}
|
||||
Assert.True(hend.IsHot);
|
||||
Assert.True(double.IsFinite(hend.Last.Value));
|
||||
}
|
||||
|
||||
// ── I) Dispose ─────────────────────────────────────────────────────
|
||||
|
||||
[Fact]
|
||||
public void Dispose_Idempotent()
|
||||
{
|
||||
var hend = new Hend(5);
|
||||
hend.Dispose();
|
||||
hend.Dispose(); // Should not throw
|
||||
Assert.True(true); // no-throw is the assertion
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Dispose_UnsubscribesFromSource()
|
||||
{
|
||||
var source = new TSeries();
|
||||
var hend = new Hend(source, period: 5);
|
||||
hend.Dispose();
|
||||
|
||||
// Adding to source after dispose should not affect hend
|
||||
source.Add(new TValue(DateTime.UtcNow, 999.0));
|
||||
Assert.False(hend.IsHot);
|
||||
}
|
||||
|
||||
// ── J) Henderson-specific: Wolfram-verified H5 weights ─────────────
|
||||
|
||||
[Fact]
|
||||
public void H5_ConstInput_ReturnsConstant()
|
||||
{
|
||||
// Wolfram-verified: H5 weights = {-21/286, 42/143, 80/143, 42/143, -21/286}
|
||||
// For constant input, sum of weights * constant = constant (weights sum to 1)
|
||||
var hend = new Hend(5);
|
||||
const double c = 100.0;
|
||||
|
||||
for (int i = 0; i < 5; i++)
|
||||
{
|
||||
hend.Update(new TValue(DateTime.UtcNow.AddSeconds(i), c));
|
||||
}
|
||||
Assert.Equal(c, hend.Last.Value, 1e-10);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void H5_NegativeEdgeWeights_BandpassProperty()
|
||||
{
|
||||
// Henderson has negative weights at edges — verify filter can output
|
||||
// values outside the min-max range of inputs (bandpass property)
|
||||
var hend = new Hend(5);
|
||||
// Step function: 0,0,100,0,0 — negative edge weights will push result outside [0,100]
|
||||
double[] vals = [0, 0, 100, 0, 0];
|
||||
TValue result = default;
|
||||
for (int i = 0; i < 5; i++)
|
||||
{
|
||||
result = hend.Update(new TValue(DateTime.UtcNow.AddSeconds(i), vals[i]));
|
||||
}
|
||||
// Henderson H5 center weight = 80/143 ≈ 0.5594
|
||||
// Expected: 0*w0 + 0*w1 + 100*w2 + 0*w3 + 0*w4 = 100 * 80/143 ≈ 55.944
|
||||
double expected = 100.0 * 80.0 / 143.0;
|
||||
Assert.Equal(expected, result.Value, 1e-6);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void H5_Symmetric_Weights()
|
||||
{
|
||||
// Henderson weights are symmetric: w(k) = w(-k)
|
||||
// Reversing the input order of a symmetric window should give same center value
|
||||
var hend1 = new Hend(5);
|
||||
var hend2 = new Hend(5);
|
||||
|
||||
double[] forward = [10, 20, 30, 40, 50];
|
||||
double[] reverse = [50, 40, 30, 20, 10];
|
||||
|
||||
TValue r1 = default, r2 = default;
|
||||
for (int i = 0; i < 5; i++)
|
||||
{
|
||||
r1 = hend1.Update(new TValue(DateTime.UtcNow.AddSeconds(i), forward[i]));
|
||||
r2 = hend2.Update(new TValue(DateTime.UtcNow.AddSeconds(i), reverse[i]));
|
||||
}
|
||||
|
||||
// For linear input, Henderson preserves the polynomial, so both
|
||||
// should give 30 (the center value of the linear trend)
|
||||
// forward: 10+20+30+40+50, reverse: 50+40+30+20+10
|
||||
// With symmetric weights applied, sum(w*forward) + sum(w*reverse) = 2*30*sum(w) = 60
|
||||
Assert.Equal(60.0, r1.Value + r2.Value, 1e-6);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,160 @@
|
||||
using Xunit;
|
||||
using Xunit.Abstractions;
|
||||
|
||||
namespace QuanTAlib.Tests;
|
||||
|
||||
public class HendValidationTests(ITestOutputHelper output)
|
||||
{
|
||||
private readonly ValidationTestData _testData = new();
|
||||
private readonly ITestOutputHelper _output = output;
|
||||
private const int DefaultPeriod = 7;
|
||||
|
||||
// ── Batch vs Streaming consistency ──────────────────────────────────
|
||||
|
||||
[Fact]
|
||||
public void BatchVsStreaming_Match()
|
||||
{
|
||||
var source = new TSeries();
|
||||
var gbm = new GBM(startPrice: 100, seed: 42);
|
||||
const int count = 100;
|
||||
|
||||
for (int i = 0; i < count; i++)
|
||||
{
|
||||
var bar = gbm.Next();
|
||||
source.Add(bar.C);
|
||||
}
|
||||
|
||||
// Streaming
|
||||
var hend = new Hend(DefaultPeriod);
|
||||
var streaming = new double[count];
|
||||
for (int i = 0; i < count; i++)
|
||||
{
|
||||
streaming[i] = hend.Update(source[i]).Value;
|
||||
}
|
||||
|
||||
// Batch
|
||||
var batchResult = Hend.Batch(source, DefaultPeriod);
|
||||
|
||||
for (int i = 0; i < count; i++)
|
||||
{
|
||||
Assert.Equal(streaming[i], batchResult[i].Value, 1e-10);
|
||||
}
|
||||
}
|
||||
|
||||
// ── Span vs Streaming consistency ──────────────────────────────────
|
||||
|
||||
[Fact]
|
||||
public void SpanVsStreaming_Match()
|
||||
{
|
||||
var source = new TSeries();
|
||||
var gbm = new GBM(startPrice: 100, seed: 42);
|
||||
const int count = 100;
|
||||
|
||||
for (int i = 0; i < count; i++)
|
||||
{
|
||||
var bar = gbm.Next();
|
||||
source.Add(bar.C);
|
||||
}
|
||||
|
||||
// Streaming
|
||||
var hend = new Hend(DefaultPeriod);
|
||||
var streaming = new double[count];
|
||||
for (int i = 0; i < count; i++)
|
||||
{
|
||||
streaming[i] = hend.Update(source[i]).Value;
|
||||
}
|
||||
|
||||
// Span
|
||||
double[] spanOutput = new double[count];
|
||||
Hend.Batch(source.Values, spanOutput, DefaultPeriod);
|
||||
|
||||
for (int i = 0; i < count; i++)
|
||||
{
|
||||
Assert.Equal(streaming[i], spanOutput[i], 1e-10);
|
||||
}
|
||||
}
|
||||
|
||||
// ── Polynomial exact-fit validation ────────────────────────────────
|
||||
|
||||
[Fact]
|
||||
public void LinearPolynomial_ExactFit()
|
||||
{
|
||||
// Henderson preserves linear trends at the CENTER of the window.
|
||||
// For period=7, half=3, output at bar N = polynomial at bar N-3.
|
||||
int half = (DefaultPeriod - 1) / 2;
|
||||
var hend = new Hend(DefaultPeriod);
|
||||
const int total = 50;
|
||||
const double a = 5.0, b = 3.0;
|
||||
|
||||
for (int i = 0; i < total; i++)
|
||||
{
|
||||
double val = a + b * i;
|
||||
hend.Update(new TValue(DateTime.UtcNow.AddSeconds(i), val));
|
||||
}
|
||||
|
||||
int centerIdx = total - 1 - half;
|
||||
double expected = a + b * centerIdx;
|
||||
_output.WriteLine($"Linear: expected={expected}, actual={hend.Last.Value}");
|
||||
Assert.Equal(expected, hend.Last.Value, 1e-6);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void QuadraticPolynomial_ExactFit()
|
||||
{
|
||||
int half = (DefaultPeriod - 1) / 2;
|
||||
var hend = new Hend(DefaultPeriod);
|
||||
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;
|
||||
hend.Update(new TValue(DateTime.UtcNow.AddSeconds(i), val));
|
||||
}
|
||||
|
||||
int centerIdx = total - 1 - half;
|
||||
double expected = a + b * centerIdx + c * centerIdx * centerIdx;
|
||||
_output.WriteLine($"Quadratic: expected={expected}, actual={hend.Last.Value}");
|
||||
Assert.Equal(expected, hend.Last.Value, 0.1);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void CubicPolynomial_ExactFit()
|
||||
{
|
||||
int half = (DefaultPeriod - 1) / 2;
|
||||
var hend = new Hend(DefaultPeriod);
|
||||
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;
|
||||
hend.Update(new TValue(DateTime.UtcNow.AddSeconds(i), val));
|
||||
}
|
||||
|
||||
int centerIdx = total - 1 - half;
|
||||
double expected = a + b * centerIdx + c * centerIdx * centerIdx + d * centerIdx * centerIdx * centerIdx;
|
||||
_output.WriteLine($"Cubic: expected={expected}, actual={hend.Last.Value}");
|
||||
Assert.Equal(expected, hend.Last.Value, 1.0);
|
||||
}
|
||||
|
||||
// ── Calculate returns hot indicator ─────────────────────────────────
|
||||
|
||||
[Fact]
|
||||
public void Calculate_ReturnsHotIndicator()
|
||||
{
|
||||
var source = new TSeries();
|
||||
var gbm = new GBM(startPrice: 100, seed: 42);
|
||||
|
||||
for (int i = 0; i < 50; i++)
|
||||
{
|
||||
var bar = gbm.Next();
|
||||
source.Add(bar.C);
|
||||
}
|
||||
|
||||
var (results, indicator) = Hend.Calculate(source, DefaultPeriod);
|
||||
|
||||
Assert.True(indicator.IsHot);
|
||||
Assert.Equal(50, results.Count);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user