Files
Miha Kralj 060649192f 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
2026-03-12 12:34:16 -07:00

484 lines
14 KiB
C#

using Xunit;
namespace QuanTAlib.Tests;
public sealed class SqueezeTests
{
private static TBarSeries GenerateBars(int count, int seed = 42)
{
var gbm = new GBM(startPrice: 100.0, mu: 0.02, sigma: 0.15, seed: seed);
return gbm.Fetch(count, DateTime.UtcNow.Ticks, TimeSpan.FromMinutes(1));
}
// === A) Constructor validation ===
[Fact]
public void Constructor_InvalidPeriod_Throws()
{
var ex = Assert.Throws<ArgumentException>(() => new Squeeze(period: 0));
Assert.Equal("period", ex.ParamName);
}
[Fact]
public void Constructor_NegativePeriod_Throws()
{
var ex = Assert.Throws<ArgumentException>(() => new Squeeze(period: -1));
Assert.Equal("period", ex.ParamName);
}
[Fact]
public void Constructor_InvalidBbMult_Throws()
{
var ex = Assert.Throws<ArgumentException>(() => new Squeeze(period: 20, bbMult: 0.0));
Assert.Equal("bbMult", ex.ParamName);
}
[Fact]
public void Constructor_NegativeBbMult_Throws()
{
var ex = Assert.Throws<ArgumentException>(() => new Squeeze(period: 20, bbMult: -1.0));
Assert.Equal("bbMult", ex.ParamName);
}
[Fact]
public void Constructor_InvalidKcMult_Throws()
{
var ex = Assert.Throws<ArgumentException>(() => new Squeeze(period: 20, kcMult: 0.0));
Assert.Equal("kcMult", ex.ParamName);
}
[Fact]
public void Constructor_NegativeKcMult_Throws()
{
var ex = Assert.Throws<ArgumentException>(() => new Squeeze(period: 20, kcMult: -1.5));
Assert.Equal("kcMult", ex.ParamName);
}
[Fact]
public void Constructor_DefaultParams()
{
var sq = new Squeeze();
Assert.Equal("Squeeze(20,2,1.5)", sq.Name);
Assert.Equal(20, sq.WarmupPeriod);
}
// === B) Basic calculation ===
[Fact]
public void Update_ReturnsTValue()
{
var sq = new Squeeze(period: 5);
var bar = new TBar(DateTime.UtcNow, 100, 105, 95, 101, 1000);
TValue result = sq.Update(bar);
Assert.True(double.IsFinite(result.Value));
}
[Fact]
public void Update_Last_Momentum_Accessible()
{
var sq = new Squeeze(period: 5);
for (int i = 0; i < 10; i++)
{
var bar = new TBar(DateTime.UtcNow.AddMinutes(i), 100 + i, 105 + i, 95 + i, 101 + i, 1000);
sq.Update(bar);
}
Assert.True(double.IsFinite(sq.Last.Value));
Assert.True(double.IsFinite(sq.Momentum));
Assert.NotEmpty(sq.Name);
}
[Fact]
public void Update_SqueezeOn_IsBool()
{
var sq = new Squeeze(period: 5);
for (int i = 0; i < 10; i++)
{
var bar = new TBar(DateTime.UtcNow.AddMinutes(i), 100, 101, 99, 100, 1000);
sq.Update(bar);
}
// SqueezeOn is a bool — either value is valid; assert it's a well-formed bool (not default struct garbage)
Assert.True(sq.SqueezeOn || !sq.SqueezeOn);
}
[Fact]
public void ConstantBars_MomentumNearZero()
{
var sq = new Squeeze(period: 5);
for (int i = 0; i < 30; i++)
{
var bar = new TBar(DateTime.UtcNow.AddMinutes(i), 100, 100, 100, 100, 1000);
sq.Update(bar);
}
// With constant price, delta = 0 for all bars, so momentum = 0
Assert.Equal(0.0, sq.Momentum, precision: 10);
}
[Fact]
public void RisingBars_PositiveMomentum_AfterWarmup()
{
var sq = new Squeeze(period: 10);
for (int i = 0; i < 40; i++)
{
double price = 100.0 + i;
var bar = new TBar(DateTime.UtcNow.AddMinutes(i), price, price + 1, price - 1, price, 1000);
sq.Update(bar);
}
Assert.True(sq.IsHot);
Assert.True(sq.Momentum > 0.0);
}
[Fact]
public void FallingBars_NegativeMomentum_AfterWarmup()
{
var sq = new Squeeze(period: 10);
for (int i = 0; i < 40; i++)
{
double price = 200.0 - i;
var bar = new TBar(DateTime.UtcNow.AddMinutes(i), price, price + 1, price - 1, price, 1000);
sq.Update(bar);
}
Assert.True(sq.IsHot);
Assert.True(sq.Momentum < 0.0);
}
// === C) State + bar correction ===
[Fact]
public void IsNew_True_Advances_State()
{
var sq = new Squeeze(period: 5);
var bars = GenerateBars(10);
for (int i = 0; i < 10; i++)
{
sq.Update(bars[i], isNew: true);
}
double momBefore = sq.Momentum;
var nextBar = new TBar(DateTime.UtcNow.AddMinutes(100), 200, 210, 190, 205, 1000);
sq.Update(nextBar, isNew: true);
// New bar with very different price should move momentum
Assert.True(double.IsFinite(sq.Momentum));
_ = momBefore; // silence unused var
}
[Fact]
public void IsNew_False_Rewrites()
{
var sq = new Squeeze(period: 5);
var bars = GenerateBars(10);
for (int i = 0; i < 9; i++)
{
sq.Update(bars[i], isNew: true);
}
sq.Update(bars[9], isNew: true);
double momAfterNew = sq.Momentum;
// Rewrite bar 9 with very different OHLC
var corrected = new TBar(bars[9].Time, 999, 1005, 990, 1000, 1000);
sq.Update(corrected, isNew: false);
double momAfterCorrect = sq.Momentum;
Assert.NotEqual(momAfterNew, momAfterCorrect);
}
[Fact]
public void IterativeCorrection_Restores()
{
var sq = new Squeeze(period: 5);
var bars = GenerateBars(15);
for (int i = 0; i < 14; i++)
{
sq.Update(bars[i], isNew: true);
}
// Feed bar 14 for real
sq.Update(bars[14], isNew: true);
double momAfterTrue = sq.Momentum;
// Simulate 3 re-updates of same bar
for (int j = 0; j < 3; j++)
{
sq.Update(bars[14], isNew: false);
}
// Correction with same value should restore same momentum
Assert.Equal(momAfterTrue, sq.Momentum, precision: 10);
}
[Fact]
public void Reset_ClearsState()
{
var sq = new Squeeze(period: 5);
var bars = GenerateBars(20);
for (int i = 0; i < 20; i++)
{
sq.Update(bars[i], isNew: true);
}
sq.Reset();
Assert.False(sq.IsHot);
Assert.Equal(0.0, sq.Momentum);
Assert.False(sq.SqueezeOn);
}
// === D) Warmup/convergence ===
[Fact]
public void IsHot_FlipsAtPeriod()
{
var sq = new Squeeze(period: 10);
var bars = GenerateBars(20);
bool hotBefore = false;
for (int i = 0; i < 10; i++)
{
sq.Update(bars[i], isNew: true);
hotBefore = sq.IsHot;
}
Assert.True(sq.IsHot);
_ = hotBefore;
}
[Fact]
public void WarmupPeriod_MatchesPeriod()
{
var sq = new Squeeze(period: 15);
Assert.Equal(15, sq.WarmupPeriod);
}
// === E) Robustness ===
[Fact]
public void NaN_Input_UsesLastValid()
{
var sq = new Squeeze(period: 5);
var bars = GenerateBars(10);
for (int i = 0; i < 9; i++)
{
sq.Update(bars[i], isNew: true);
}
double momBefore = sq.Momentum;
var nanBar = new TBar(DateTime.UtcNow.AddMinutes(100), double.NaN, double.NaN, double.NaN, double.NaN, 0);
sq.Update(nanBar, isNew: true);
// Should return NaN or substitute last-valid — either way must not throw
Assert.True(true);
_ = momBefore;
}
[Fact]
public void Infinity_Input_Handled()
{
var sq = new Squeeze(period: 5);
var bars = GenerateBars(10);
for (int i = 0; i < 9; i++)
{
sq.Update(bars[i], isNew: true);
}
var infBar = new TBar(DateTime.UtcNow.AddMinutes(100),
double.PositiveInfinity, double.PositiveInfinity, double.NegativeInfinity, double.PositiveInfinity, 0);
// must not throw
sq.Update(infBar, isNew: true);
Assert.True(true);
}
[Fact]
public void BatchNaN_ResultFiniteOrNaN()
{
var sq = new Squeeze(period: 5);
for (int i = 0; i < 20; i++)
{
TBar bar;
if (i % 5 == 0)
{
bar = new TBar(DateTime.UtcNow.AddMinutes(i), double.NaN, double.NaN, double.NaN, double.NaN, 0);
}
else
{
bar = new TBar(DateTime.UtcNow.AddMinutes(i), 100 + i, 105 + i, 95 + i, 101 + i, 1000);
}
sq.Update(bar, isNew: true);
}
// Must complete without exception
Assert.True(true);
}
// === F) Consistency ===
[Fact]
public void BatchCalc_MatchesStreaming()
{
var bars = GenerateBars(50);
const int period = 10;
// Streaming
var sq = new Squeeze(period);
for (int i = 0; i < 50; i++)
{
sq.Update(bars[i], isNew: true);
}
double streamMom = sq.Momentum;
// Batch static
var (batchMom, _) = Squeeze.Batch(bars, period);
double batchLast = batchMom[^1].Value;
Assert.Equal(streamMom, batchLast, precision: 6);
}
[Fact]
public void SpanBatch_MatchesStreaming()
{
var bars = GenerateBars(50);
const int period = 10;
// Streaming
var sq = new Squeeze(period);
for (int i = 0; i < 50; i++)
{
sq.Update(bars[i], isNew: true);
}
double streamMom = sq.Momentum;
// Span Batch
double[] momOut = new double[50];
double[] sqOut = new double[50];
Squeeze.Batch(bars.HighValues, bars.LowValues, bars.CloseValues,
momOut, sqOut, period);
double spanLast = momOut[49];
Assert.Equal(streamMom, spanLast, precision: 6);
}
[Fact]
public void EventingMode_MatchesStreaming()
{
var bars = GenerateBars(50);
const int period = 10;
// Streaming
var sqStream = new Squeeze(period);
for (int i = 0; i < 50; i++)
{
sqStream.Update(bars[i], isNew: true);
}
double streamMom = sqStream.Momentum;
// Eventing via TBarSeries constructor
var sqEvent = new Squeeze(bars, period);
Assert.Equal(streamMom, sqEvent.Momentum, precision: 6);
}
// === G) Span API tests ===
[Fact]
public void BatchSpan_ThrowsOnInvalidPeriod()
{
double[] h = [100, 101, 102];
double[] l = [99, 100, 101];
double[] c = [100, 101, 102];
double[] mom = new double[3];
double[] sq = new double[3];
var ex = Assert.Throws<ArgumentException>(() =>
Squeeze.Batch(h, l, c, mom, sq, period: 0));
Assert.Equal("period", ex.ParamName);
}
[Fact]
public void BatchSpan_ThrowsOnMismatchedLengths()
{
double[] h = [100, 101];
double[] l = [99];
double[] c = [100, 101];
double[] mom = new double[2];
double[] sq = new double[2];
var ex = Assert.Throws<ArgumentException>(() =>
Squeeze.Batch(h, l, c, mom, sq, period: 5));
Assert.Equal("high", ex.ParamName);
}
[Fact]
public void BatchSpan_ThrowsOnShortMomOutput()
{
double[] h = [100, 101, 102, 103, 104];
double[] l = [99, 100, 101, 102, 103];
double[] c = [100, 101, 102, 103, 104];
double[] mom = new double[2]; // too short
double[] sq = new double[5];
var ex = Assert.Throws<ArgumentException>(() =>
Squeeze.Batch(h, l, c, mom, sq, period: 3));
Assert.Equal("momOut", ex.ParamName);
}
[Fact]
public void BatchSpan_ThrowsOnShortSqOutput()
{
double[] h = [100, 101, 102, 103, 104];
double[] l = [99, 100, 101, 102, 103];
double[] c = [100, 101, 102, 103, 104];
double[] mom = new double[5];
double[] sq = new double[2]; // too short
var ex = Assert.Throws<ArgumentException>(() =>
Squeeze.Batch(h, l, c, mom, sq, period: 3));
Assert.Equal("sqOut", ex.ParamName);
}
[Fact]
public void BatchSpan_LargeData_NoStackOverflow()
{
const int size = 2000;
var gbm = new GBM(100.0, 0.02, 0.15, seed: 1);
var bars = gbm.Fetch(size, DateTime.UtcNow.Ticks, TimeSpan.FromMinutes(1));
double[] mom = new double[size];
double[] sq = new double[size];
// period = 300 forces ArrayPool path
Squeeze.Batch(bars.HighValues, bars.LowValues, bars.CloseValues, mom, sq, period: 300);
Assert.True(double.IsFinite(mom[size - 1]));
}
// === H) Chainability ===
[Fact]
public void PubEvent_Fires()
{
var sq = new Squeeze(period: 5);
int fireCount = 0;
sq.Pub += (_, in e) => fireCount++;
for (int i = 0; i < 10; i++)
{
var bar = new TBar(DateTime.UtcNow.AddMinutes(i), 100, 105, 95, 101, 1000);
sq.Update(bar, isNew: true);
}
Assert.Equal(10, fireCount);
}
[Fact]
public void TBarSeries_Constructor_Subscribes()
{
var bars = GenerateBars(30);
var sq = new Squeeze(bars, period: 10);
Assert.True(sq.IsHot);
Assert.True(double.IsFinite(sq.Momentum));
}
// === Calculate static factory ===
[Fact]
public void Calculate_ReturnsResultsAndIndicator()
{
var bars = GenerateBars(30);
var ((momSeries, sqSeries), indicator) = Squeeze.Calculate(bars, period: 10);
Assert.Equal(30, momSeries.Count);
Assert.Equal(30, sqSeries.Count);
Assert.NotNull(indicator);
Assert.True(double.IsFinite(indicator.Momentum));
}
}