mirror of
https://github.com/mihakralj/QuanTAlib.git
synced 2026-08-23 21:18: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,166 @@
|
||||
using TradingPlatform.BusinessLayer;
|
||||
using Xunit;
|
||||
|
||||
namespace QuanTAlib.Tests;
|
||||
|
||||
public class FcbIndicatorTests
|
||||
{
|
||||
[Fact]
|
||||
public void Constructor_SetsDefaults()
|
||||
{
|
||||
var ind = new FcbIndicator();
|
||||
|
||||
Assert.Equal(20, ind.Period);
|
||||
Assert.True(ind.ShowColdValues);
|
||||
Assert.Equal("Fcb - Fractal Chaos Bands", ind.Name);
|
||||
Assert.False(ind.SeparateWindow);
|
||||
Assert.True(ind.OnBackGround);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void MinHistoryDepths_EqualsPeriodPlusTwo()
|
||||
{
|
||||
var ind = new FcbIndicator { Period = 15 };
|
||||
Assert.Equal(17, ind.MinHistoryDepths); // Period + 2 for fractal detection
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ShortName_ReflectsParameters()
|
||||
{
|
||||
var ind = new FcbIndicator { Period = 12 };
|
||||
Assert.Contains("12", ind.ShortName, StringComparison.Ordinal);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Initialize_AddsThreeLineSeries()
|
||||
{
|
||||
var ind = new FcbIndicator { Period = 14 };
|
||||
ind.Initialize();
|
||||
|
||||
Assert.Equal(3, ind.LinesSeries.Count);
|
||||
Assert.Equal("Middle", ind.LinesSeries[0].Name);
|
||||
Assert.Equal("Upper", ind.LinesSeries[1].Name);
|
||||
Assert.Equal("Lower", ind.LinesSeries[2].Name);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ProcessUpdate_Historical_ComputesValues()
|
||||
{
|
||||
var ind = new FcbIndicator { Period = 3 };
|
||||
ind.Initialize();
|
||||
|
||||
var now = DateTime.UtcNow;
|
||||
ind.HistoricalData.AddBar(now, 100, 110, 90, 102);
|
||||
|
||||
ind.ProcessUpdate(new UpdateArgs(UpdateReason.HistoricalBar));
|
||||
|
||||
Assert.Equal(1, ind.LinesSeries[0].Count);
|
||||
Assert.True(double.IsFinite(ind.LinesSeries[0].GetValue(0)));
|
||||
Assert.True(double.IsFinite(ind.LinesSeries[1].GetValue(0)));
|
||||
Assert.True(double.IsFinite(ind.LinesSeries[2].GetValue(0)));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ProcessUpdate_NewBar_Appends()
|
||||
{
|
||||
var ind = new FcbIndicator { Period = 3 };
|
||||
ind.Initialize();
|
||||
|
||||
var now = DateTime.UtcNow;
|
||||
ind.HistoricalData.AddBar(now, 100, 110, 90, 102);
|
||||
ind.HistoricalData.AddBar(now.AddMinutes(1), 102, 112, 92, 104);
|
||||
|
||||
ind.ProcessUpdate(new UpdateArgs(UpdateReason.HistoricalBar));
|
||||
ind.ProcessUpdate(new UpdateArgs(UpdateReason.NewBar));
|
||||
|
||||
Assert.Equal(2, ind.LinesSeries[0].Count);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ProcessUpdate_NewTick_DoesNotThrow()
|
||||
{
|
||||
var ind = new FcbIndicator { Period = 5 };
|
||||
ind.Initialize();
|
||||
|
||||
var now = DateTime.UtcNow;
|
||||
ind.HistoricalData.AddBar(now, 100, 105, 95, 102);
|
||||
|
||||
ind.ProcessUpdate(new UpdateArgs(UpdateReason.HistoricalBar));
|
||||
ind.ProcessUpdate(new UpdateArgs(UpdateReason.NewTick));
|
||||
|
||||
Assert.Equal(2, ind.LinesSeries[0].Count);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void MultipleUpdates_ProducesFiniteSeries()
|
||||
{
|
||||
var ind = new FcbIndicator { Period = 5 };
|
||||
ind.Initialize();
|
||||
|
||||
var now = DateTime.UtcNow;
|
||||
for (int i = 0; i < 10; i++)
|
||||
{
|
||||
ind.HistoricalData.AddBar(now.AddMinutes(i), 100 + i, 105 + i, 95 + i, 102 + i);
|
||||
ind.ProcessUpdate(new UpdateArgs(i == 0 ? UpdateReason.HistoricalBar : UpdateReason.NewBar));
|
||||
}
|
||||
|
||||
Assert.Equal(10, ind.LinesSeries[0].Count);
|
||||
Assert.Equal(10, ind.LinesSeries[1].Count);
|
||||
Assert.Equal(10, ind.LinesSeries[2].Count);
|
||||
|
||||
for (int i = 0; i < 10; i++)
|
||||
{
|
||||
Assert.True(double.IsFinite(ind.LinesSeries[0].GetValue(i)));
|
||||
Assert.True(double.IsFinite(ind.LinesSeries[1].GetValue(i)));
|
||||
Assert.True(double.IsFinite(ind.LinesSeries[2].GetValue(i)));
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Bands_Order_Correct()
|
||||
{
|
||||
var ind = new FcbIndicator { Period = 3 };
|
||||
ind.Initialize();
|
||||
|
||||
var now = DateTime.UtcNow;
|
||||
// Create fractal patterns
|
||||
for (int i = 0; i < 10; i++)
|
||||
{
|
||||
ind.HistoricalData.AddBar(now.AddMinutes(i), 100, 110 + i, 90 - i, 100, 1000);
|
||||
ind.ProcessUpdate(new UpdateArgs(i == 0 ? UpdateReason.HistoricalBar : UpdateReason.NewBar));
|
||||
}
|
||||
|
||||
double middle = ind.LinesSeries[0].GetValue(0);
|
||||
double upper = ind.LinesSeries[1].GetValue(0);
|
||||
double lower = ind.LinesSeries[2].GetValue(0);
|
||||
|
||||
Assert.True(upper >= middle, $"Upper ({upper}) should be >= Middle ({middle})");
|
||||
Assert.True(lower <= middle, $"Lower ({lower}) should be <= Middle ({middle})");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void FractalDetection_WorksCorrectly()
|
||||
{
|
||||
var ind = new FcbIndicator { Period = 5 };
|
||||
ind.Initialize();
|
||||
|
||||
var now = DateTime.UtcNow;
|
||||
|
||||
// Create clear fractal high pattern: 100, 120, 110 (index 1 is fractal high)
|
||||
// Create clear fractal low pattern: 90, 70, 80 (index 1 is fractal low)
|
||||
ind.HistoricalData.AddBar(now.AddMinutes(0), 95, 100, 90, 95);
|
||||
ind.ProcessUpdate(new UpdateArgs(UpdateReason.HistoricalBar));
|
||||
|
||||
ind.HistoricalData.AddBar(now.AddMinutes(1), 95, 120, 70, 95); // Potential fractal
|
||||
ind.ProcessUpdate(new UpdateArgs(UpdateReason.NewBar));
|
||||
|
||||
ind.HistoricalData.AddBar(now.AddMinutes(2), 95, 110, 80, 95); // Confirms fractals
|
||||
ind.ProcessUpdate(new UpdateArgs(UpdateReason.NewBar));
|
||||
|
||||
// After 3 bars, we should have detected fractals
|
||||
Assert.Equal(3, ind.LinesSeries[0].Count);
|
||||
Assert.True(double.IsFinite(ind.LinesSeries[0].GetValue(0)));
|
||||
Assert.True(double.IsFinite(ind.LinesSeries[1].GetValue(0)));
|
||||
Assert.True(double.IsFinite(ind.LinesSeries[2].GetValue(0)));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,322 @@
|
||||
using System;
|
||||
using QuanTAlib;
|
||||
using Xunit;
|
||||
|
||||
namespace QuanTAlib.Tests;
|
||||
|
||||
public class FcbTests
|
||||
{
|
||||
[Fact]
|
||||
public void Fcb_Constructor_ValidatesInput()
|
||||
{
|
||||
Assert.Throws<ArgumentOutOfRangeException>(() => new Fcb(0));
|
||||
Assert.Throws<ArgumentOutOfRangeException>(() => new Fcb(-5));
|
||||
|
||||
var f = new Fcb(10);
|
||||
Assert.Equal(12, f.WarmupPeriod); // period + 2 for fractal detection
|
||||
Assert.Contains("Fcb", f.Name, StringComparison.OrdinalIgnoreCase);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Fcb_InitialState_Defaults()
|
||||
{
|
||||
var f = new Fcb(5);
|
||||
|
||||
Assert.Equal(0, f.Last.Value);
|
||||
Assert.Equal(0, f.Upper.Value);
|
||||
Assert.Equal(0, f.Lower.Value);
|
||||
Assert.False(f.IsHot);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Fcb_DetectsFractalHigh()
|
||||
{
|
||||
// Fractal high: high[1] > high[2] AND high[1] > high[0]
|
||||
// Create pattern: 100, 120, 110 (index 1 is fractal high = 120)
|
||||
var f = new Fcb(5);
|
||||
|
||||
f.Update(new TBar(DateTime.UtcNow, 95, 100, 90, 95, 1000)); // High = 100
|
||||
f.Update(new TBar(DateTime.UtcNow, 115, 120, 110, 115, 1000)); // High = 120 (will be fractal)
|
||||
f.Update(new TBar(DateTime.UtcNow, 105, 110, 100, 105, 1000)); // High = 110 < 120
|
||||
|
||||
// After 3 bars, fractal high at index 1 detected when we get index 2
|
||||
// Upper should track the highest fractal high = 120
|
||||
Assert.Equal(120.0, f.Upper.Value, 1e-10);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Fcb_DetectsFractalLow()
|
||||
{
|
||||
// Fractal low: low[1] < low[2] AND low[1] < low[0]
|
||||
// Create pattern: 100, 80, 90 (index 1 is fractal low = 80)
|
||||
var f = new Fcb(5);
|
||||
|
||||
f.Update(new TBar(DateTime.UtcNow, 105, 110, 100, 105, 1000)); // Low = 100
|
||||
f.Update(new TBar(DateTime.UtcNow, 85, 90, 80, 85, 1000)); // Low = 80 (will be fractal)
|
||||
f.Update(new TBar(DateTime.UtcNow, 95, 100, 90, 95, 1000)); // Low = 90 > 80
|
||||
|
||||
// After 3 bars, fractal low at index 1 detected when we get index 2
|
||||
// Lower should track the lowest fractal low = 80
|
||||
Assert.Equal(80.0, f.Lower.Value, 1e-10);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Fcb_NoFractalUsesPreviousValue()
|
||||
{
|
||||
// If no fractal is detected, bands should use previous fractal values
|
||||
var f = new Fcb(5);
|
||||
|
||||
// Create fractal high then no new fractals
|
||||
f.Update(new TBar(DateTime.UtcNow, 100, 105, 95, 100, 1000));
|
||||
f.Update(new TBar(DateTime.UtcNow, 110, 115, 105, 110, 1000)); // Will be fractal high
|
||||
f.Update(new TBar(DateTime.UtcNow, 105, 110, 100, 105, 1000)); // Confirms fractal at 115
|
||||
|
||||
_ = f.Upper.Value; // Store for comparison
|
||||
|
||||
// Add more bars that don't create new fractals (monotonic up)
|
||||
f.Update(new TBar(DateTime.UtcNow, 112, 117, 107, 112, 1000));
|
||||
f.Update(new TBar(DateTime.UtcNow, 120, 125, 115, 120, 1000));
|
||||
|
||||
// Upper should still be finite as previous fractal value is tracked via deque
|
||||
Assert.True(double.IsFinite(f.Upper.Value));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Fcb_SlidingWindow_Updates()
|
||||
{
|
||||
var f = new Fcb(3);
|
||||
|
||||
// Create initial fractals
|
||||
f.Update(new TBar(DateTime.UtcNow, 100, 110, 90, 100, 1000));
|
||||
f.Update(new TBar(DateTime.UtcNow, 110, 120, 100, 110, 1000)); // Fractal high at 120
|
||||
f.Update(new TBar(DateTime.UtcNow, 105, 115, 95, 105, 1000));
|
||||
|
||||
// Add more bars - window should slide
|
||||
for (int i = 0; i < 5; i++)
|
||||
{
|
||||
f.Update(new TBar(DateTime.UtcNow, 90 - i, 95 - i, 85 - i, 90 - i, 1000));
|
||||
}
|
||||
|
||||
// Upper may have changed as old fractals slide out
|
||||
Assert.NotEqual(0, f.Upper.Value);
|
||||
Assert.NotEqual(0, f.Lower.Value);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Fcb_IsHot_TurnsTrueAfterWarmup()
|
||||
{
|
||||
var f = new Fcb(4);
|
||||
// WarmupPeriod = 4 + 2 = 6
|
||||
|
||||
for (int i = 0; i < 5; i++)
|
||||
{
|
||||
f.Update(new TBar(DateTime.UtcNow, 100 + i, 101 + i, 99 + i, 100 + i, 1000));
|
||||
Assert.False(f.IsHot);
|
||||
}
|
||||
|
||||
f.Update(new TBar(DateTime.UtcNow, 200, 201, 199, 200, 1000));
|
||||
Assert.True(f.IsHot);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Fcb_IsNewFalse_RebuildsState()
|
||||
{
|
||||
var f = new Fcb(3);
|
||||
var gbm = new GBM(startPrice: 100, mu: 0.01, sigma: 0.1, seed: 7);
|
||||
|
||||
TBar remembered = default;
|
||||
for (int i = 0; i < 10; i++)
|
||||
{
|
||||
remembered = gbm.Next(isNew: true);
|
||||
f.Update(remembered, isNew: true);
|
||||
}
|
||||
|
||||
double mid = f.Last.Value;
|
||||
double up = f.Upper.Value;
|
||||
double lo = f.Lower.Value;
|
||||
|
||||
// Apply corrections
|
||||
for (int i = 0; i < 3; i++)
|
||||
{
|
||||
var corrected = gbm.Next(isNew: false);
|
||||
f.Update(corrected, isNew: false);
|
||||
}
|
||||
|
||||
// Restore with remembered bar
|
||||
f.Update(remembered, isNew: false);
|
||||
|
||||
Assert.Equal(mid, f.Last.Value, 1e-10);
|
||||
Assert.Equal(up, f.Upper.Value, 1e-10);
|
||||
Assert.Equal(lo, f.Lower.Value, 1e-10);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Fcb_NaN_UsesLastValid()
|
||||
{
|
||||
var f = new Fcb(3);
|
||||
|
||||
f.Update(new TBar(DateTime.UtcNow, 100, 110, 90, 105, 1000));
|
||||
f.Update(new TBar(DateTime.UtcNow, 101, 111, 91, 106, 1000));
|
||||
|
||||
var result = f.Update(new TBar(DateTime.UtcNow, 102, double.NaN, 92, 107, 1000));
|
||||
Assert.True(double.IsFinite(result.Value));
|
||||
Assert.True(double.IsFinite(f.Upper.Value));
|
||||
Assert.True(double.IsFinite(f.Lower.Value));
|
||||
|
||||
var result2 = f.Update(new TBar(DateTime.UtcNow, 103, 113, double.PositiveInfinity, 108, 1000));
|
||||
Assert.True(double.IsFinite(result2.Value));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Fcb_Reset_Clears()
|
||||
{
|
||||
var f = new Fcb(3);
|
||||
f.Update(new TBar(DateTime.UtcNow, 100, 110, 90, 100, 1000));
|
||||
f.Update(new TBar(DateTime.UtcNow, 101, 111, 91, 101, 1000));
|
||||
f.Update(new TBar(DateTime.UtcNow, 102, 112, 92, 102, 1000));
|
||||
|
||||
f.Reset();
|
||||
|
||||
Assert.Equal(0, f.Last.Value);
|
||||
Assert.Equal(0, f.Upper.Value);
|
||||
Assert.Equal(0, f.Lower.Value);
|
||||
Assert.False(f.IsHot);
|
||||
|
||||
f.Update(new TBar(DateTime.UtcNow, 50, 60, 40, 55, 1000));
|
||||
Assert.NotEqual(0, f.Last.Value);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Fcb_BatchVsStreaming_Match()
|
||||
{
|
||||
var fStream = new Fcb(10);
|
||||
var gbm = new GBM(startPrice: 100, mu: 0.02, sigma: 0.15, seed: 42);
|
||||
var series = new TBarSeries();
|
||||
|
||||
for (int i = 0; i < 200; i++)
|
||||
{
|
||||
var bar = gbm.Next(isNew: true);
|
||||
series.Add(bar);
|
||||
fStream.Update(bar, isNew: true);
|
||||
}
|
||||
|
||||
double expectedMid = fStream.Last.Value;
|
||||
double expectedUp = fStream.Upper.Value;
|
||||
double expectedLo = fStream.Lower.Value;
|
||||
|
||||
var (midBatch, upBatch, loBatch) = Fcb.Batch(series, 10);
|
||||
|
||||
Assert.Equal(expectedMid, midBatch.Last.Value, 1e-10);
|
||||
Assert.Equal(expectedUp, upBatch.Last.Value, 1e-10);
|
||||
Assert.Equal(expectedLo, loBatch.Last.Value, 1e-10);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Fcb_SpanBatch_Validates()
|
||||
{
|
||||
double[] high = [110, 115, 120];
|
||||
double[] low = [90, 95, 100];
|
||||
double[] middle = new double[3];
|
||||
double[] upper = new double[3];
|
||||
double[] lower = new double[3];
|
||||
|
||||
double[] highShort = [110, 115];
|
||||
double[] smallOut = new double[1];
|
||||
|
||||
Assert.Throws<ArgumentOutOfRangeException>(() => Fcb.Batch(high.AsSpan(), low.AsSpan(), middle.AsSpan(), upper.AsSpan(), lower.AsSpan(), 0));
|
||||
Assert.Throws<ArgumentOutOfRangeException>(() => Fcb.Batch(high.AsSpan(), low.AsSpan(), middle.AsSpan(), upper.AsSpan(), lower.AsSpan(), -1));
|
||||
Assert.Throws<ArgumentException>(() => Fcb.Batch(highShort.AsSpan(), low.AsSpan(), middle.AsSpan(), upper.AsSpan(), lower.AsSpan(), 2));
|
||||
Assert.Throws<ArgumentException>(() => Fcb.Batch(high.AsSpan(), low.AsSpan(), smallOut.AsSpan(), upper.AsSpan(), lower.AsSpan(), 2));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Fcb_SpanBatch_ComputesCorrectly()
|
||||
{
|
||||
// Create data with clear fractal patterns
|
||||
// Fractal high at index 1: 100, 120, 110
|
||||
// Fractal low at index 1: 90, 70, 80
|
||||
double[] high = [100, 120, 110, 115, 105];
|
||||
double[] low = [90, 70, 80, 75, 85];
|
||||
double[] middle = new double[5];
|
||||
double[] upper = new double[5];
|
||||
double[] lower = new double[5];
|
||||
|
||||
Fcb.Batch(high.AsSpan(), low.AsSpan(), middle.AsSpan(), upper.AsSpan(), lower.AsSpan(), 3);
|
||||
|
||||
// At index 2, we detect fractal high=120, fractal low=70
|
||||
// Upper=120, Lower=70, Middle=95
|
||||
Assert.Equal(120.0, upper[2], 1e-10);
|
||||
Assert.Equal(70.0, lower[2], 1e-10);
|
||||
Assert.Equal(95.0, middle[2], 1e-10);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Fcb_Calculate_ReturnsIndicatorAndResults()
|
||||
{
|
||||
var series = new TBarSeries();
|
||||
series.Add(DateTime.UtcNow, 100, 110, 90, 100, 1000);
|
||||
series.Add(DateTime.UtcNow, 105, 120, 80, 105, 1000); // Potential fractal high at 120, low at 80
|
||||
series.Add(DateTime.UtcNow, 102, 115, 85, 102, 1000); // Confirms fractals
|
||||
|
||||
var ((mid, up, lo), ind) = Fcb.Calculate(series, 2);
|
||||
|
||||
// After 3 bars: fractal high = 120, fractal low = 80
|
||||
Assert.Equal(120.0, up.Last.Value, 1e-10);
|
||||
Assert.Equal(80.0, lo.Last.Value, 1e-10);
|
||||
Assert.Equal(100.0, mid.Last.Value, 1e-10);
|
||||
|
||||
// Continue streaming
|
||||
ind.Update(new TBar(DateTime.UtcNow, 108, 130, 75, 108, 1000)); // Potential new fractals
|
||||
ind.Update(new TBar(DateTime.UtcNow, 106, 125, 78, 106, 1000)); // Confirms fractal high=130, low=75
|
||||
|
||||
Assert.Equal(130.0, ind.Upper.Value, 1e-10);
|
||||
Assert.Equal(75.0, ind.Lower.Value, 1e-10);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Fcb_Event_Publishes()
|
||||
{
|
||||
var src = new TBarSeries();
|
||||
var f = new Fcb(src, 2);
|
||||
bool fired = false;
|
||||
f.Pub += (object? sender, in TValueEventArgs args) => fired = true;
|
||||
|
||||
src.Add(new TBar(DateTime.UtcNow, 100, 110, 90, 100, 1000));
|
||||
Assert.True(fired);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Fcb_MiddleValue_IsAverage()
|
||||
{
|
||||
var f = new Fcb(3);
|
||||
|
||||
// Create clear fractals
|
||||
f.Update(new TBar(DateTime.UtcNow, 100, 110, 90, 100, 1000));
|
||||
f.Update(new TBar(DateTime.UtcNow, 110, 130, 70, 110, 1000)); // Fractal high=130, low=70
|
||||
f.Update(new TBar(DateTime.UtcNow, 105, 120, 80, 105, 1000)); // Confirms fractals
|
||||
|
||||
double expectedMiddle = (f.Upper.Value + f.Lower.Value) * 0.5;
|
||||
Assert.Equal(expectedMiddle, f.Last.Value, 1e-10);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Fcb_MultipleFractals_TracksHighestLowest()
|
||||
{
|
||||
var f = new Fcb(10);
|
||||
|
||||
// Create multiple fractals over time
|
||||
double[] highs = [100, 120, 110, 115, 140, 130, 125, 150, 145, 142, 148, 155, 150];
|
||||
double[] lows = [90, 70, 80, 75, 60, 65, 68, 50, 55, 58, 52, 45, 48];
|
||||
|
||||
for (int i = 0; i < highs.Length; i++)
|
||||
{
|
||||
f.Update(new TBar(DateTime.UtcNow, (highs[i] + lows[i]) / 2, highs[i], lows[i], (highs[i] + lows[i]) / 2, 1000));
|
||||
}
|
||||
|
||||
// Upper should be highest fractal high in window
|
||||
// Lower should be lowest fractal low in window
|
||||
Assert.True(f.Upper.Value > 0);
|
||||
Assert.True(f.Lower.Value > 0);
|
||||
Assert.True(f.Upper.Value > f.Lower.Value);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,395 @@
|
||||
using Skender.Stock.Indicators;
|
||||
using Xunit.Abstractions;
|
||||
|
||||
using OoplesFinance.StockIndicators;
|
||||
using OoplesFinance.StockIndicators.Models;
|
||||
|
||||
namespace QuanTAlib.Tests;
|
||||
|
||||
public sealed class FcbValidationTests : IDisposable
|
||||
{
|
||||
private readonly ValidationTestData _testData;
|
||||
private readonly ITestOutputHelper _output;
|
||||
private bool _disposed;
|
||||
|
||||
public FcbValidationTests(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]
|
||||
public void Validate_ManualCalculation_FractalDetection()
|
||||
{
|
||||
var series = new TBarSeries();
|
||||
var t0 = DateTime.UtcNow;
|
||||
|
||||
// Create clear fractal patterns:
|
||||
// Fractal high at bar 1: highs = 100, 120, 110
|
||||
// Fractal low at bar 1: lows = 90, 70, 80
|
||||
series.Add(new TBar(t0, 0, 100, 90, 95, 100));
|
||||
series.Add(new TBar(t0.AddMinutes(1), 0, 120, 70, 95, 100)); // Fractal high=120, low=70
|
||||
series.Add(new TBar(t0.AddMinutes(2), 0, 110, 80, 95, 100)); // Confirms fractals
|
||||
|
||||
var ind = new Fcb(3);
|
||||
var (mid, up, lo) = ind.Update(series);
|
||||
|
||||
// Upper should be highest fractal high = 120
|
||||
// Lower should be lowest fractal low = 70
|
||||
// Middle = (120 + 70) / 2 = 95
|
||||
Assert.Equal(120.0, up.Last.Value, 1e-10);
|
||||
Assert.Equal(70.0, lo.Last.Value, 1e-10);
|
||||
Assert.Equal(95.0, mid.Last.Value, 1e-10);
|
||||
|
||||
_output.WriteLine("FCB manual fractal detection validated");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Validate_AllModes_Consistency()
|
||||
{
|
||||
int[] periods = { 5, 10, 20, 50 };
|
||||
|
||||
foreach (int period in periods)
|
||||
{
|
||||
// Batch (instance)
|
||||
var inst = new Fcb(period);
|
||||
var (bMid, bUp, bLo) = inst.Update(_testData.Bars);
|
||||
|
||||
// Static batch
|
||||
var (sMid, sUp, sLo) = Fcb.Batch(_testData.Bars, period);
|
||||
|
||||
ValidationHelper.VerifySeriesEqual(bMid, sMid);
|
||||
ValidationHelper.VerifySeriesEqual(bUp, sUp);
|
||||
ValidationHelper.VerifySeriesEqual(bLo, sLo);
|
||||
|
||||
// Streaming
|
||||
var streaming = new Fcb(period);
|
||||
var sMidStream = new TSeries();
|
||||
var sUpStream = new TSeries();
|
||||
var sLoStream = new TSeries();
|
||||
foreach (var bar in _testData.Bars)
|
||||
{
|
||||
streaming.Update(bar);
|
||||
sMidStream.Add(streaming.Last);
|
||||
sUpStream.Add(streaming.Upper);
|
||||
sLoStream.Add(streaming.Lower);
|
||||
}
|
||||
|
||||
ValidationHelper.VerifySeriesEqual(sMid, sMidStream);
|
||||
ValidationHelper.VerifySeriesEqual(sUp, sUpStream);
|
||||
ValidationHelper.VerifySeriesEqual(sLo, sLoStream);
|
||||
|
||||
// Span
|
||||
double[] high = _testData.HighPrices.ToArray();
|
||||
double[] low = _testData.LowPrices.ToArray();
|
||||
double[] spanMid = new double[high.Length];
|
||||
double[] spanUp = new double[high.Length];
|
||||
double[] spanLo = new double[high.Length];
|
||||
Fcb.Batch(high.AsSpan(), low.AsSpan(),
|
||||
spanMid.AsSpan(), spanUp.AsSpan(), spanLo.AsSpan(), period);
|
||||
|
||||
for (int i = 0; i < high.Length; i++)
|
||||
{
|
||||
Assert.Equal(sMid[i].Value, spanMid[i], 9);
|
||||
Assert.Equal(sUp[i].Value, spanUp[i], 9);
|
||||
Assert.Equal(sLo[i].Value, spanLo[i], 9);
|
||||
}
|
||||
}
|
||||
|
||||
_output.WriteLine("FCB mode consistency validated (batch/stream/span)");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Validate_EventingMode_MatchesBatch()
|
||||
{
|
||||
const int period = 20;
|
||||
var pub = new TBarSeries();
|
||||
var evtInd = new Fcb(pub, period);
|
||||
var evtMid = new TSeries();
|
||||
var evtUp = new TSeries();
|
||||
var evtLo = new TSeries();
|
||||
|
||||
foreach (var bar in _testData.Bars)
|
||||
{
|
||||
pub.Add(bar);
|
||||
evtMid.Add(evtInd.Last);
|
||||
evtUp.Add(evtInd.Upper);
|
||||
evtLo.Add(evtInd.Lower);
|
||||
}
|
||||
|
||||
var (bMid, bUp, bLo) = Fcb.Batch(_testData.Bars, period);
|
||||
|
||||
ValidationHelper.VerifySeriesEqual(bMid, evtMid);
|
||||
ValidationHelper.VerifySeriesEqual(bUp, evtUp);
|
||||
ValidationHelper.VerifySeriesEqual(bLo, evtLo);
|
||||
|
||||
_output.WriteLine("FCB eventing mode validated");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Validate_Calculate_ReturnsHotIndicator()
|
||||
{
|
||||
const int period = 15;
|
||||
var ((mid, up, lo), ind) = Fcb.Calculate(_testData.Bars, period);
|
||||
|
||||
Assert.True(ind.IsHot);
|
||||
Assert.Equal(period + 2, ind.WarmupPeriod); // period + 2 for fractal detection
|
||||
Assert.Equal(mid.Last.Value, ind.Last.Value, 1e-10);
|
||||
Assert.Equal(up.Last.Value, ind.Upper.Value, 1e-10);
|
||||
Assert.Equal(lo.Last.Value, ind.Lower.Value, 1e-10);
|
||||
|
||||
// Continue streaming
|
||||
var next = new TBar(DateTime.UtcNow, 0, 150, 50, 100, 1000);
|
||||
ind.Update(next);
|
||||
Assert.True(ind.IsHot);
|
||||
|
||||
_output.WriteLine("FCB Calculate validated");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Validate_Prime_MatchesBatch()
|
||||
{
|
||||
const int period = 25;
|
||||
|
||||
var (bMid, bUp, bLo) = Fcb.Batch(_testData.Bars, period);
|
||||
|
||||
var primed = new Fcb(period);
|
||||
var subset = new TBarSeries();
|
||||
for (int i = 0; i < 200; i++)
|
||||
{
|
||||
subset.Add(_testData.Bars[i]);
|
||||
}
|
||||
|
||||
primed.Prime(subset);
|
||||
|
||||
for (int i = 200; i < _testData.Bars.Count; i++)
|
||||
{
|
||||
primed.Update(_testData.Bars[i]);
|
||||
}
|
||||
|
||||
Assert.Equal(bMid.Last.Value, primed.Last.Value, 1e-9);
|
||||
Assert.Equal(bUp.Last.Value, primed.Upper.Value, 1e-9);
|
||||
Assert.Equal(bLo.Last.Value, primed.Lower.Value, 1e-9);
|
||||
|
||||
_output.WriteLine("FCB Prime validated against batch");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Validate_LargeDataset_FiniteOutputs()
|
||||
{
|
||||
var (mid, up, lo) = Fcb.Batch(_testData.Bars, 50);
|
||||
|
||||
ValidationHelper.VerifyAllFinite(mid, startIndex: 0);
|
||||
ValidationHelper.VerifyAllFinite(up, startIndex: 0);
|
||||
ValidationHelper.VerifyAllFinite(lo, startIndex: 0);
|
||||
|
||||
// After warmup, upper should always be >= lower
|
||||
int warmup = 50 + 2; // period + 2 for fractal detection
|
||||
for (int i = warmup; i < mid.Count; i++)
|
||||
{
|
||||
Assert.True(up[i].Value >= lo[i].Value, $"Upper >= Lower at {i}");
|
||||
}
|
||||
|
||||
_output.WriteLine("FCB large dataset validated");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Validate_FractalPattern_CorrectDetection()
|
||||
{
|
||||
// Create a series with known fractal patterns
|
||||
var series = new TBarSeries();
|
||||
var t0 = DateTime.UtcNow;
|
||||
|
||||
// Pattern designed to have clear fractals:
|
||||
// highs: 100, 110, 105 -> fractal high at index 1 = 110
|
||||
// lows: 50, 45, 48 -> fractal low at index 1 = 45
|
||||
double[] highs = { 100, 110, 105, 108, 115, 112, 118, 125, 120 };
|
||||
double[] lows = { 50, 45, 48, 42, 47, 40, 44, 38, 42 };
|
||||
|
||||
for (int i = 0; i < highs.Length; i++)
|
||||
{
|
||||
series.Add(new TBar(t0.AddMinutes(i), 0, highs[i], lows[i], (highs[i] + lows[i]) / 2, 100));
|
||||
}
|
||||
|
||||
var ind = new Fcb(5);
|
||||
var (mid, up, lo) = ind.Update(series);
|
||||
|
||||
// All outputs should be finite
|
||||
for (int i = 0; i < series.Count; i++)
|
||||
{
|
||||
Assert.True(double.IsFinite(mid[i].Value), $"Mid finite at {i}");
|
||||
Assert.True(double.IsFinite(up[i].Value), $"Upper finite at {i}");
|
||||
Assert.True(double.IsFinite(lo[i].Value), $"Lower finite at {i}");
|
||||
}
|
||||
|
||||
// After enough bars, upper should be >= lower
|
||||
for (int i = 3; i < series.Count; i++)
|
||||
{
|
||||
Assert.True(up[i].Value >= lo[i].Value, $"Upper >= Lower at {i}");
|
||||
}
|
||||
|
||||
_output.WriteLine("FCB fractal pattern validation passed");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Validate_MiddleIsAverage_AllBars()
|
||||
{
|
||||
var ind = new Fcb(20);
|
||||
var (mid, up, lo) = ind.Update(_testData.Bars);
|
||||
|
||||
for (int i = 0; i < mid.Count; i++)
|
||||
{
|
||||
double expected = (up[i].Value + lo[i].Value) * 0.5;
|
||||
Assert.Equal(expected, mid[i].Value, 1e-10);
|
||||
}
|
||||
|
||||
_output.WriteLine("FCB middle = average validated for all bars");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Validate_BandsAreMonotonic_WithinWindow()
|
||||
{
|
||||
// The upper band should be the highest fractal high in the window
|
||||
// The lower band should be the lowest fractal low in the window
|
||||
// These values shouldn't jump erratically unless new fractals are detected
|
||||
|
||||
var ind = new Fcb(10);
|
||||
foreach (var bar in _testData.Bars)
|
||||
{
|
||||
ind.Update(bar);
|
||||
|
||||
// Upper should always be >= Lower
|
||||
if (ind.IsHot)
|
||||
{
|
||||
Assert.True(ind.Upper.Value >= ind.Lower.Value,
|
||||
$"Upper ({ind.Upper.Value}) should be >= Lower ({ind.Lower.Value})");
|
||||
}
|
||||
}
|
||||
|
||||
_output.WriteLine("FCB band monotonicity validated");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Validate_Skender_BandStructure()
|
||||
{
|
||||
// Skender GetFcb(windowSpan) uses Williams fractal carry-forward:
|
||||
// - windowSpan is half-width for 3-bar fractal detection (min=2)
|
||||
// - UpperBand = last confirmed FractalBear (highest high carry-forward)
|
||||
// - LowerBand = last confirmed FractalBull (lowest low carry-forward)
|
||||
// - Results are decimal? (need cast to double)
|
||||
//
|
||||
// NOTE: Skender's UpperBand can be LOWER than LowerBand when the last
|
||||
// bear fractal occurred at a lower price than the last bull fractal.
|
||||
// This is a known property of fractal carry-forward algorithms.
|
||||
//
|
||||
// QuanTAlib Fcb(period) uses monotonic deques over a lookback window
|
||||
// and always maintains Upper >= Lower ordering.
|
||||
|
||||
int windowSpan = 2;
|
||||
var sResult = _testData.SkenderQuotes
|
||||
.GetFcb(windowSpan)
|
||||
.ToList();
|
||||
|
||||
// Verify Skender produces finite values
|
||||
int validCount = 0;
|
||||
for (int i = 0; i < sResult.Count; i++)
|
||||
{
|
||||
if (sResult[i].UpperBand.HasValue && sResult[i].LowerBand.HasValue)
|
||||
{
|
||||
double upper = (double)sResult[i].UpperBand!.Value;
|
||||
double lower = (double)sResult[i].LowerBand!.Value;
|
||||
Assert.True(double.IsFinite(upper), $"Skender Upper finite at bar {i}");
|
||||
Assert.True(double.IsFinite(lower), $"Skender Lower finite at bar {i}");
|
||||
Assert.True(upper > 0, $"Skender Upper positive at bar {i}");
|
||||
Assert.True(lower > 0, $"Skender Lower positive at bar {i}");
|
||||
validCount++;
|
||||
}
|
||||
}
|
||||
|
||||
Assert.True(validCount > 0, "Skender should produce some valid FCB values");
|
||||
_output.WriteLine($"Skender FCB band structure validated ({validCount} valid bars with finite values)");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Validate_Skender_BothProduceChannels()
|
||||
{
|
||||
// Both QuanTAlib and Skender FCB should produce meaningful channels
|
||||
// that track price structure. Verify both produce valid finite values.
|
||||
//
|
||||
// NOTE: Skender bands can cross (Upper < Lower) due to fractal
|
||||
// carry-forward semantics, so we only validate finite positive values.
|
||||
|
||||
int windowSpan = 2;
|
||||
int period = 20;
|
||||
|
||||
var sResult = _testData.SkenderQuotes
|
||||
.GetFcb(windowSpan)
|
||||
.ToList();
|
||||
|
||||
var (qMiddle, qUpper, qLower) = Fcb.Batch(_testData.Bars, period);
|
||||
|
||||
// After warmup, both should have valid bands
|
||||
int qValidCount = 0;
|
||||
int sValidCount = 0;
|
||||
|
||||
for (int i = period + 2; i < qMiddle.Count && i < sResult.Count; i++)
|
||||
{
|
||||
if (qUpper[i].Value > 0 && qLower[i].Value > 0)
|
||||
{
|
||||
Assert.True(qUpper[i].Value >= qLower[i].Value,
|
||||
$"QuanTAlib Upper >= Lower at bar {i}");
|
||||
qValidCount++;
|
||||
}
|
||||
|
||||
if (sResult[i].UpperBand.HasValue && sResult[i].LowerBand.HasValue)
|
||||
{
|
||||
double sUpper = (double)sResult[i].UpperBand!.Value;
|
||||
double sLower = (double)sResult[i].LowerBand!.Value;
|
||||
Assert.True(double.IsFinite(sUpper) && sUpper > 0,
|
||||
$"Skender Upper finite and positive at bar {i}");
|
||||
Assert.True(double.IsFinite(sLower) && sLower > 0,
|
||||
$"Skender Lower finite and positive at bar {i}");
|
||||
sValidCount++;
|
||||
}
|
||||
}
|
||||
|
||||
Assert.True(qValidCount > 100, $"QuanTAlib produced {qValidCount} valid bars");
|
||||
Assert.True(sValidCount > 100, $"Skender produced {sValidCount} valid bars");
|
||||
|
||||
_output.WriteLine($"FCB channel comparison: QuanTAlib={qValidCount}, Skender={sValidCount} valid bars");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Fcb_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).CalculateFractalChaosBands();
|
||||
var values = result.OutputValues.Values.First();
|
||||
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