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:
Miha Kralj
2026-03-12 12:34:16 -07:00
parent 8937b0c0fa
commit 060649192f
1149 changed files with 1780 additions and 3316 deletions
@@ -0,0 +1,160 @@
using TradingPlatform.BusinessLayer;
namespace QuanTAlib.Tests;
public class PfeIndicatorTests
{
[Fact]
public void PfeIndicator_Constructor_SetsDefaults()
{
var indicator = new PfeIndicator();
Assert.Equal(10, indicator.Period);
Assert.Equal(5, indicator.SmoothPeriod);
Assert.True(indicator.ShowColdValues);
Assert.Equal("PFE - Polarized Fractal Efficiency", indicator.Name);
Assert.True(indicator.SeparateWindow);
Assert.True(indicator.OnBackGround);
}
[Fact]
public void PfeIndicator_ShortName_IncludesParameters()
{
var indicator = new PfeIndicator { Period = 20, SmoothPeriod = 8 };
indicator.Initialize();
Assert.Contains("PFE", indicator.ShortName, StringComparison.Ordinal);
Assert.Contains("20", indicator.ShortName, StringComparison.Ordinal);
Assert.Contains("8", indicator.ShortName, StringComparison.Ordinal);
}
[Fact]
public void PfeIndicator_MinHistoryDepths_EqualsZero()
{
var indicator = new PfeIndicator();
Assert.Equal(0, PfeIndicator.MinHistoryDepths);
Assert.Equal(0, ((IWatchlistIndicator)indicator).MinHistoryDepths);
}
[Fact]
public void PfeIndicator_Initialize_CreatesInternalPfe()
{
var indicator = new PfeIndicator();
// Initialize should not throw
indicator.Initialize();
// After init, line series should exist (single PFE line)
Assert.Single(indicator.LinesSeries);
}
[Fact]
public void PfeIndicator_ProcessUpdate_HistoricalBar_ComputesValue()
{
var indicator = new PfeIndicator { Period = 5, SmoothPeriod = 3 };
indicator.Initialize();
var now = DateTime.UtcNow;
for (int i = 0; i < 20; i++)
{
double basePrice = 100 + i;
indicator.HistoricalData.AddBar(now.AddMinutes(i), basePrice, basePrice + 5, basePrice - 5, basePrice + 2, 1000);
var args = new UpdateArgs(UpdateReason.HistoricalBar);
indicator.ProcessUpdate(args);
}
double pfeVal = indicator.LinesSeries[0].GetValue(0);
Assert.True(double.IsFinite(pfeVal));
}
[Fact]
public void PfeIndicator_ProcessUpdate_NewBar_ComputesValue()
{
var indicator = new PfeIndicator { Period = 5, SmoothPeriod = 3 };
indicator.Initialize();
var now = DateTime.UtcNow;
for (int i = 0; i < 20; i++)
{
double basePrice = 100 + i;
indicator.HistoricalData.AddBar(now.AddMinutes(i), basePrice, basePrice + 5, basePrice - 5, basePrice + 2, 1000);
}
indicator.ProcessUpdate(new UpdateArgs(UpdateReason.HistoricalBar));
// Add new bar
indicator.HistoricalData.AddBar(now.AddMinutes(20), 120, 128, 115, 125, 1500);
indicator.ProcessUpdate(new UpdateArgs(UpdateReason.NewBar));
Assert.Equal(2, indicator.LinesSeries[0].Count);
}
[Fact]
public void PfeIndicator_DifferentPeriods_Work()
{
int[][] paramSets = { new[] { 3, 2 }, new[] { 10, 5 }, new[] { 20, 8 } };
foreach (var ps in paramSets)
{
var indicator = new PfeIndicator { Period = ps[0], SmoothPeriod = ps[1] };
indicator.Initialize();
var now = DateTime.UtcNow;
for (int i = 0; i < 100; i++)
{
double basePrice = 100 + i;
indicator.HistoricalData.AddBar(now.AddMinutes(i), basePrice, basePrice + 5, basePrice - 5, basePrice + 2, 1000);
indicator.ProcessUpdate(new UpdateArgs(UpdateReason.HistoricalBar));
}
double pfeVal = indicator.LinesSeries[0].GetValue(0);
Assert.True(double.IsFinite(pfeVal), $"Periods ({ps[0]},{ps[1]}) should produce finite PFE");
}
}
[Fact]
public void PfeIndicator_Period_CanBeChanged()
{
var indicator = new PfeIndicator();
Assert.Equal(10, indicator.Period);
Assert.Equal(5, indicator.SmoothPeriod);
indicator.Period = 20;
indicator.SmoothPeriod = 8;
Assert.Equal(20, indicator.Period);
Assert.Equal(8, indicator.SmoothPeriod);
}
[Fact]
public void PfeIndicator_ShowColdValues_CanBeToggled()
{
var indicator = new PfeIndicator();
Assert.True(indicator.ShowColdValues);
indicator.ShowColdValues = false;
Assert.False(indicator.ShowColdValues);
indicator.ShowColdValues = true;
Assert.True(indicator.ShowColdValues);
}
[Fact]
public void PfeIndicator_SourceCodeLink_IsValid()
{
var indicator = new PfeIndicator();
Assert.Contains("github.com", indicator.SourceCodeLink, StringComparison.Ordinal);
Assert.Contains("Pfe.Quantower.cs", indicator.SourceCodeLink, StringComparison.Ordinal);
}
[Fact]
public void PfeIndicator_HasOneLineSeries_WithCorrectName()
{
var indicator = new PfeIndicator();
indicator.Initialize();
Assert.Single(indicator.LinesSeries);
Assert.Equal("PFE", indicator.LinesSeries[0].Name);
}
}
+704
View File
@@ -0,0 +1,704 @@
namespace QuanTAlib.Tests;
public class PfeTests
{
// ============== A) Constructor & Parameter Validation ==============
[Fact]
public void Constructor_ValidatesPeriodTooSmall()
{
var ex = Assert.Throws<ArgumentException>(() => new Pfe(1, 5));
Assert.Equal("period", ex.ParamName);
}
[Fact]
public void Constructor_ValidatesPeriodZero()
{
var ex = Assert.Throws<ArgumentException>(() => new Pfe(0, 5));
Assert.Equal("period", ex.ParamName);
}
[Fact]
public void Constructor_ValidatesPeriodNegative()
{
var ex = Assert.Throws<ArgumentException>(() => new Pfe(-5, 5));
Assert.Equal("period", ex.ParamName);
}
[Fact]
public void Constructor_ValidatesSmoothPeriodZero()
{
var ex = Assert.Throws<ArgumentException>(() => new Pfe(10, 0));
Assert.Equal("smoothPeriod", ex.ParamName);
}
[Fact]
public void Constructor_ValidatesSmoothPeriodNegative()
{
var ex = Assert.Throws<ArgumentException>(() => new Pfe(10, -1));
Assert.Equal("smoothPeriod", ex.ParamName);
}
[Fact]
public void Constructor_DefaultParameters_Work()
{
var pfe = new Pfe();
Assert.Contains("10", pfe.Name, StringComparison.Ordinal);
Assert.Contains("5", pfe.Name, StringComparison.Ordinal);
}
[Fact]
public void Constructor_CustomParameters_Work()
{
var pfe = new Pfe(20, 8);
Assert.Contains("20", pfe.Name, StringComparison.Ordinal);
Assert.Contains("8", pfe.Name, StringComparison.Ordinal);
}
[Fact]
public void Constructor_MinimumPeriods_Work()
{
var pfe = new Pfe(2, 1);
Assert.NotNull(pfe);
}
// ============== B) Basic Calculation ==============
[Fact]
public void BasicCalculation_DoesNotCrash()
{
var pfe = new Pfe(10, 5);
var gbm = new GBM();
var bars = gbm.Fetch(200, DateTime.UtcNow.Ticks, TimeSpan.FromMinutes(1));
foreach (var bar in bars)
{
pfe.Update(new TValue(bar.Time, bar.Close));
}
Assert.True(double.IsFinite(pfe.Last.Value));
}
[Fact]
public void Calc_ReturnsValue()
{
var pfe = new Pfe(5, 3);
Assert.Equal(0, pfe.Last.Value);
var result = pfe.Update(new TValue(DateTime.UtcNow, 100));
Assert.True(double.IsFinite(result.Value));
Assert.Equal(result.Value, pfe.Last.Value);
}
[Fact]
public void Properties_Accessible()
{
var pfe = new Pfe(10, 5);
Assert.Equal(0, pfe.Last.Value);
Assert.False(pfe.IsHot);
Assert.Contains("Pfe", pfe.Name, StringComparison.Ordinal);
Assert.True(pfe.WarmupPeriod > 0);
Assert.Equal(11, pfe.WarmupPeriod);
}
[Fact]
public void ConstantPrice_ReturnsHundredAfterWarmup()
{
// Constant price: priceDiff=0, straightLine=sqrt(0+period^2)=period
// fractalPath = period*sqrt(1) = period, efficiency = 100%
// Sign convention: priceDiff >= 0 → positive, so PFE = +100
var pfe = new Pfe(5, 3);
for (int i = 0; i < 30; i++)
{
pfe.Update(new TValue(DateTime.UtcNow.AddMinutes(i), 100));
}
Assert.Equal(100.0, pfe.Last.Value, 1e-4);
}
[Fact]
public void OutputBounded_WhenHot()
{
// Raw PFE is always in [-100, +100]. EMA warmup bias compensation
// (c = 1/(1-e)) can overshoot up to ~5% when IsHot first fires
// (E <= 0.05 → c ≈ 1.053). Values converge to [-100, +100] as e→0.
var pfe = new Pfe(10, 5);
var gbm = new GBM(startPrice: 100.0, mu: 0.5, sigma: 1.0);
var bars = gbm.Fetch(500, DateTime.UtcNow.Ticks, TimeSpan.FromMinutes(1));
foreach (var bar in bars)
{
var result = pfe.Update(new TValue(bar.Time, bar.Close));
if (pfe.IsHot)
{
Assert.True(result.Value >= -106 && result.Value <= 106,
$"PFE must be approximately in [-100, +100] when hot, got {result.Value}");
}
}
}
// ============== C) State Management & Bar Correction ==============
[Fact]
public void Calc_IsNew_AcceptsParameter()
{
var pfe = new Pfe(5, 3);
pfe.Update(new TValue(DateTime.UtcNow, 100), isNew: true);
pfe.Update(new TValue(DateTime.UtcNow.AddMinutes(1), 105), isNew: true);
Assert.True(double.IsFinite(pfe.Last.Value));
}
[Fact]
public void Calc_IsNew_False_UpdatesValue()
{
var pfe = new Pfe(5, 3);
var gbm = new GBM(startPrice: 100.0);
var bars = gbm.Fetch(20, DateTime.UtcNow.Ticks, TimeSpan.FromMinutes(1));
// Feed past warmup
for (int i = 0; i < 15; i++)
{
pfe.Update(new TValue(bars[i].Time, bars[i].Close), isNew: true);
}
double beforeUpdate = pfe.Last.Value;
// Correct with a very different value
pfe.Update(new TValue(bars[14].Time, bars[14].Close * 2), isNew: false);
double afterUpdate = pfe.Last.Value;
Assert.NotEqual(beforeUpdate, afterUpdate);
}
[Fact]
public void IsNew_Consistency()
{
var pfe = new Pfe(5, 3);
var gbm = new GBM();
var bars = gbm.Fetch(30, DateTime.UtcNow.Ticks, TimeSpan.FromMinutes(1));
// Feed first 14
for (int i = 0; i < 14; i++)
{
pfe.Update(new TValue(bars[i].Time, bars[i].Close));
}
// Feed 15th bar (isNew=true)
pfe.Update(new TValue(bars[14].Time, bars[14].Close), true);
// Correct with modified value (isNew=false)
double modifiedClose = bars[14].Close + 50.0;
double val2 = pfe.Update(new TValue(bars[14].Time, modifiedClose), false).Value;
// Create new instance and feed up to modified
var pfe2 = new Pfe(5, 3);
for (int i = 0; i < 14; i++)
{
pfe2.Update(new TValue(bars[i].Time, bars[i].Close));
}
double val3 = pfe2.Update(new TValue(bars[14].Time, modifiedClose), true).Value;
Assert.Equal(val3, val2, 1e-9);
}
[Fact]
public void IterativeCorrections_RestoreToOriginalState()
{
var pfe = new Pfe(5, 3);
var gbm = new GBM(startPrice: 100.0, mu: 0.02, sigma: 0.1);
var bars = gbm.Fetch(30, DateTime.UtcNow.Ticks, TimeSpan.FromMinutes(1));
// Feed 15 new values
TValue fifteenthValue = default;
for (int i = 0; i < 15; i++)
{
fifteenthValue = new TValue(bars[i].Time, bars[i].Close);
pfe.Update(fifteenthValue, isNew: true);
}
// Remember state after 15 values
double stateAfter15 = pfe.Last.Value;
// Generate corrections with isNew=false (different values)
for (int i = 15; i < 25; i++)
{
pfe.Update(new TValue(bars[i].Time, bars[i].Close), isNew: false);
}
// Feed the remembered 15th value again with isNew=false
TValue finalResult = pfe.Update(fifteenthValue, isNew: false);
// State should match the original state after 15 values
Assert.Equal(stateAfter15, finalResult.Value, 1e-10);
}
[Fact]
public void Reset_Works()
{
var pfe = new Pfe(5, 3);
var gbm = new GBM();
var bars = gbm.Fetch(50, DateTime.UtcNow.Ticks, TimeSpan.FromMinutes(1));
foreach (var bar in bars)
{
pfe.Update(new TValue(bar.Time, bar.Close));
}
pfe.Reset();
Assert.Equal(0, pfe.Last.Value);
Assert.False(pfe.IsHot);
// After reset, should accept new values
pfe.Update(new TValue(bars[0].Time, bars[0].Close));
Assert.True(double.IsFinite(pfe.Last.Value));
}
// ============== D) Warmup & Convergence ==============
[Fact]
public void IsHot_BecomesTrueAfterEnoughData()
{
var pfe = new Pfe(5, 3);
Assert.False(pfe.IsHot);
var baseTime = DateTime.UtcNow;
// Feed period+1 = 6 bars to get first raw PFE, then EMA needs more for IsHot
for (int i = 0; i < 50; i++)
{
pfe.Update(new TValue(baseTime.AddMinutes(i), 100 + i));
}
Assert.True(pfe.IsHot);
}
[Fact]
public void IsHot_IsPeriodDependent()
{
var pfe10_5 = new Pfe(10, 5);
var pfe5_3 = new Pfe(5, 3);
Assert.Equal(11, pfe10_5.WarmupPeriod);
Assert.Equal(6, pfe5_3.WarmupPeriod);
}
// ============== E) NaN/Infinity Handling ==============
[Fact]
public void NaN_Input_UsesLastValidValue()
{
var pfe = new Pfe(5, 3);
for (int i = 0; i < 15; i++)
{
pfe.Update(new TValue(DateTime.UtcNow.AddMinutes(i), 100 + i));
}
// Feed NaN
var resultAfterNaN = pfe.Update(new TValue(DateTime.UtcNow.AddMinutes(15), double.NaN));
Assert.True(double.IsFinite(resultAfterNaN.Value));
}
[Fact]
public void Infinity_Input_UsesLastValidValue()
{
var pfe = new Pfe(5, 3);
for (int i = 0; i < 15; i++)
{
pfe.Update(new TValue(DateTime.UtcNow.AddMinutes(i), 100 + i));
}
var resultAfterInf = pfe.Update(new TValue(DateTime.UtcNow.AddMinutes(15), double.PositiveInfinity));
Assert.True(double.IsFinite(resultAfterInf.Value));
var resultAfterNegInf = pfe.Update(new TValue(DateTime.UtcNow.AddMinutes(16), double.NegativeInfinity));
Assert.True(double.IsFinite(resultAfterNegInf.Value));
}
[Fact]
public void MultipleNaN_ContinuesWithLastValid()
{
var pfe = new Pfe(5, 3);
for (int i = 0; i < 15; i++)
{
pfe.Update(new TValue(DateTime.UtcNow.AddMinutes(i), 100 + i));
}
// Feed several NaN values
for (int i = 0; i < 5; i++)
{
var result = pfe.Update(new TValue(DateTime.UtcNow.AddMinutes(15 + i), double.NaN));
Assert.True(double.IsFinite(result.Value));
}
}
[Fact]
public void BatchNaN_Safe()
{
var pfe = new Pfe(5, 3);
var gbm = new GBM();
var bars = gbm.Fetch(30, DateTime.UtcNow.Ticks, TimeSpan.FromMinutes(1));
// Feed normal values
for (int i = 0; i < 15; i++)
{
pfe.Update(new TValue(bars[i].Time, bars[i].Close));
}
// Feed NaN values
for (int i = 0; i < 5; i++)
{
var result = pfe.Update(new TValue(DateTime.UtcNow.AddHours(i + 1), double.NaN));
Assert.True(double.IsFinite(result.Value));
}
// Resume normal
for (int i = 15; i < 25; i++)
{
var result = pfe.Update(new TValue(bars[i].Time, bars[i].Close));
Assert.True(double.IsFinite(result.Value));
}
}
// ============== F) Consistency Tests ==============
[Fact]
public void BatchCalc_MatchesIterativeCalc()
{
var pfeIterative = new Pfe(5, 3);
var gbm = new GBM(startPrice: 100.0, mu: 0.02, sigma: 0.1);
var bars = gbm.Fetch(100, DateTime.UtcNow.Ticks, TimeSpan.FromMinutes(1));
var series = bars.Close;
// Iterative
var iterativeResults = new TSeries();
foreach (var tv in series)
{
iterativeResults.Add(pfeIterative.Update(tv));
}
// Batch
var batchResults = Pfe.Batch(series, 5, 3);
Assert.Equal(iterativeResults.Count, batchResults.Count);
for (int i = 0; i < iterativeResults.Count; i++)
{
Assert.Equal(iterativeResults[i].Value, batchResults[i].Value, 1e-10);
}
}
[Fact]
public void TSeries_Update_MatchesStreaming()
{
var pfe1 = new Pfe(5, 3);
var pfe2 = new Pfe(5, 3);
var gbm = new GBM();
var bars = gbm.Fetch(100, DateTime.UtcNow.Ticks, TimeSpan.FromMinutes(1));
var series = bars.Close;
// Streaming
foreach (var tv in series)
{
pfe1.Update(tv);
}
// Batch via Update(TSeries)
pfe2.Update(series);
Assert.Equal(pfe1.Last.Value, pfe2.Last.Value, 1e-10);
}
[Fact]
public void SpanBatch_MatchesStreaming()
{
var pfe = new Pfe(5, 3);
var gbm = new GBM(startPrice: 100.0, mu: 0.02, sigma: 0.1);
var bars = gbm.Fetch(100, DateTime.UtcNow.Ticks, TimeSpan.FromMinutes(1));
var series = bars.Close;
// Streaming
var streamResults = new double[100];
for (int i = 0; i < 100; i++)
{
streamResults[i] = pfe.Update(series[i]).Value;
}
// Span batch
var values = series.Values.ToArray();
var spanResults = new double[100];
Pfe.Batch(values, spanResults, 5, 3);
for (int i = 0; i < 100; i++)
{
Assert.Equal(streamResults[i], spanResults[i], 1e-10);
}
}
[Fact]
public void EventBased_MatchesStreaming()
{
var pfe1 = new Pfe(5, 3);
var gbm = new GBM();
var bars = gbm.Fetch(50, DateTime.UtcNow.Ticks, TimeSpan.FromMinutes(1));
var series = bars.Close;
// Collect event-based results
var eventResults = new List<double>();
pfe1.Pub += (object? _, in TValueEventArgs e) => eventResults.Add(e.Value.Value);
foreach (var tv in series)
{
pfe1.Update(tv);
}
// Collect streaming results
var pfe2 = new Pfe(5, 3);
var streamResults = new List<double>();
foreach (var tv in series)
{
streamResults.Add(pfe2.Update(tv).Value);
}
Assert.Equal(streamResults.Count, eventResults.Count);
for (int i = 0; i < streamResults.Count; i++)
{
Assert.Equal(streamResults[i], eventResults[i], 1e-10);
}
}
[Fact]
public void AllModes_ProduceSameResult()
{
int period = 5;
int smooth = 3;
var gbm = new GBM(startPrice: 100.0, mu: 0.05, sigma: 0.2);
var bars = gbm.Fetch(200, DateTime.UtcNow.Ticks, TimeSpan.FromMinutes(1));
var series = bars.Close;
// 1. Batch
var batchSeries = Pfe.Batch(series, period, smooth);
double expected = batchSeries.Last.Value;
// 2. Span
var values = series.Values.ToArray();
var spanOutput = new double[values.Length];
Pfe.Batch(values, spanOutput, period, smooth);
double spanResult = spanOutput[^1];
// 3. Streaming
var streamingInd = new Pfe(period, smooth);
for (int i = 0; i < series.Count; i++)
{
streamingInd.Update(series[i]);
}
double streamingResult = streamingInd.Last.Value;
// 4. Eventing
var pubSource = new TSeries();
var eventingInd = new Pfe(pubSource, period, smooth);
for (int i = 0; i < series.Count; i++)
{
pubSource.Add(series[i]);
}
double eventingResult = eventingInd.Last.Value;
Assert.Equal(expected, spanResult, 1e-9);
Assert.Equal(expected, streamingResult, 1e-9);
Assert.Equal(expected, eventingResult, 1e-9);
}
// ============== G) Span API Tests ==============
[Fact]
public void SpanBatch_ValidatesLengths()
{
double[] source = new double[10];
double[] output = new double[5]; // too small
Assert.Throws<ArgumentException>(() => Pfe.Batch(source, output, 5, 3));
}
[Fact]
public void SpanBatch_ValidatesPeriod()
{
double[] source = new double[10];
double[] output = new double[10];
var ex = Assert.Throws<ArgumentException>(() => Pfe.Batch(source, output, 1, 5));
Assert.Equal("period", ex.ParamName);
}
[Fact]
public void SpanBatch_ValidatesSmoothPeriod()
{
double[] source = new double[10];
double[] output = new double[10];
var ex = Assert.Throws<ArgumentException>(() => Pfe.Batch(source, output, 10, 0));
Assert.Equal("smoothPeriod", ex.ParamName);
}
[Fact]
public void SpanBatch_EmptyInput_NoOp()
{
double[] source = Array.Empty<double>();
double[] output = Array.Empty<double>();
var ex = Record.Exception(() => Pfe.Batch(source, output, 5, 3));
Assert.Null(ex);
}
[Fact]
public void SpanBatch_NaN_HandledGracefully()
{
double[] source = { 100, 101, double.NaN, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112 };
double[] output = new double[source.Length];
Pfe.Batch(source, output, 5, 3);
for (int i = 0; i < output.Length; i++)
{
Assert.True(double.IsFinite(output[i]), $"Output[{i}] should be finite but was {output[i]}");
}
}
[Fact]
public void SpanBatch_MatchesTSeriesCalc()
{
var gbm = new GBM(startPrice: 100.0, mu: 0.02, sigma: 0.1);
var bars = gbm.Fetch(100, DateTime.UtcNow.Ticks, TimeSpan.FromMinutes(1));
var series = bars.Close;
// TSeries path
var tsResults = Pfe.Batch(series, 5, 3);
// Span path
var values = series.Values.ToArray();
var spanOutput = new double[values.Length];
Pfe.Batch(values, spanOutput, 5, 3);
for (int i = 0; i < values.Length; i++)
{
Assert.Equal(tsResults[i].Value, spanOutput[i], 1e-10);
}
}
// ============== H) Chainability ==============
[Fact]
public void Chainability_Works()
{
var pfe = new Pfe(5, 3);
var gbm = new GBM();
var bars = gbm.Fetch(50, DateTime.UtcNow.Ticks, TimeSpan.FromMinutes(1));
var series = bars.Close;
var result = pfe.Update(series);
Assert.Equal(50, result.Count);
Assert.Equal(pfe.Last.Value, result.Last.Value);
}
[Fact]
public void PubEvent_Fires()
{
var pfe = new Pfe(5, 3);
int eventCount = 0;
pfe.Pub += (object? _, in TValueEventArgs _) => eventCount++;
for (int i = 0; i < 15; i++)
{
pfe.Update(new TValue(DateTime.UtcNow.AddMinutes(i), 100 + i));
}
Assert.Equal(15, eventCount);
}
[Fact]
public void Chaining_ViaConstructor_Works()
{
// Create a source SMA
var sma = new Sma(5);
var pfe = new Pfe(sma, 5, 3);
var gbm = new GBM();
var bars = gbm.Fetch(30, DateTime.UtcNow.Ticks, TimeSpan.FromMinutes(1));
var series = bars.Close;
// When SMA updates, chained PFE should also update
foreach (var tv in series)
{
sma.Update(tv);
}
Assert.True(double.IsFinite(pfe.Last.Value));
}
// ============== PFE-Specific Tests ==============
[Fact]
public void MonotonicIncrease_ProducesPositivePfe()
{
var pfe = new Pfe(5, 3);
var baseTime = DateTime.UtcNow;
// Feed strictly increasing prices (equal steps)
for (int i = 0; i < 30; i++)
{
pfe.Update(new TValue(baseTime.AddMinutes(i), 100 + i));
}
Assert.True(pfe.Last.Value > 0, $"PFE should be positive for uptrend, got {pfe.Last.Value}");
}
[Fact]
public void MonotonicDecrease_ProducesNegativePfe()
{
var pfe = new Pfe(5, 3);
var baseTime = DateTime.UtcNow;
// Feed strictly decreasing prices
for (int i = 0; i < 30; i++)
{
pfe.Update(new TValue(baseTime.AddMinutes(i), 200 - i));
}
Assert.True(pfe.Last.Value < 0, $"PFE should be negative for downtrend, got {pfe.Last.Value}");
}
[Fact]
public void StaticBatch_Works()
{
var gbm = new GBM();
var bars = gbm.Fetch(100, DateTime.UtcNow.Ticks, TimeSpan.FromMinutes(1));
var series = bars.Close;
var results = Pfe.Batch(series, 10, 5);
Assert.Equal(100, results.Count);
Assert.True(double.IsFinite(results.Last.Value));
}
[Fact]
public void Calculate_ReturnsResultsAndIndicator()
{
var gbm = new GBM();
var bars = gbm.Fetch(100, DateTime.UtcNow.Ticks, TimeSpan.FromMinutes(1));
var series = bars.Close;
var (results, indicator) = Pfe.Calculate(series, 5, 3);
Assert.Equal(100, results.Count);
Assert.NotNull(indicator);
Assert.True(double.IsFinite(indicator.Last.Value));
}
}
@@ -0,0 +1,330 @@
namespace QuanTAlib.Tests;
/// <summary>
/// PFE Validation Tests — Self-consistency validation.
/// No external library (TA-Lib, Skender, Tulip, Ooples) implements PFE.
/// Validation focuses on internal consistency and mathematical correctness.
/// </summary>
public sealed class PfeValidationTests : IDisposable
{
private readonly ValidationTestData _testData;
private bool _disposed;
public PfeValidationTests()
{
_testData = new ValidationTestData();
}
public void Dispose()
{
Dispose(true);
}
private void Dispose(bool disposing)
{
if (_disposed)
{
return;
}
_disposed = true;
if (disposing)
{
_testData?.Dispose();
}
}
// ============== Self-Consistency ==============
[Fact]
public void Validation_BatchMatchesStreaming()
{
int[][] paramSets = { new[] { 5, 3 }, new[] { 10, 5 }, new[] { 20, 8 } };
var series = _testData.Data;
foreach (int[] ps in paramSets)
{
int period = ps[0];
int smooth = ps[1];
// Streaming
var pfeStream = new Pfe(period, smooth);
var streamResults = new List<double>();
foreach (var tv in series)
{
streamResults.Add(pfeStream.Update(tv).Value);
}
// Batch
var batchResults = Pfe.Batch(series, period, smooth);
Assert.Equal(streamResults.Count, batchResults.Count);
for (int i = 0; i < streamResults.Count; i++)
{
Assert.Equal(streamResults[i], batchResults[i].Value, 1e-10);
}
}
}
[Fact]
public void Validation_SpanMatchesStreaming()
{
int[][] paramSets = { new[] { 5, 3 }, new[] { 10, 5 }, new[] { 20, 8 } };
var series = _testData.Data;
int len = series.Count;
double[] values = series.Values.ToArray();
foreach (int[] ps in paramSets)
{
int period = ps[0];
int smooth = ps[1];
// Streaming
var pfeStream = new Pfe(period, smooth);
var streamResults = new double[len];
for (int i = 0; i < len; i++)
{
streamResults[i] = pfeStream.Update(series[i]).Value;
}
// Span batch
double[] spanResults = new double[len];
Pfe.Batch(values, spanResults, period, smooth);
for (int i = 0; i < len; i++)
{
Assert.Equal(streamResults[i], spanResults[i], 1e-10);
}
}
}
// ============== Known-Value Tests ==============
[Fact]
public void Validation_ConstantPrice_HundredPfe()
{
// Constant price: priceDiff=0, straightLine=sqrt(0+period^2)=period
// fractalPath = period*sqrt(1) = period. Efficiency = 100%.
// Sign: priceDiff=0 >= 0 → positive. So PFE = +100.
var pfe = new Pfe(5, 3);
var baseTime = DateTime.UtcNow;
for (int i = 0; i < 30; i++)
{
pfe.Update(new TValue(baseTime.AddMinutes(i), 100));
}
Assert.Equal(100.0, pfe.Last.Value, 1e-4);
}
[Fact]
public void Validation_MonotonicIncrease_PositivePfe()
{
// For strictly increasing prices, PFE should be positive
var pfe = new Pfe(5, 3);
var baseTime = DateTime.UtcNow;
for (int i = 0; i < 30; i++)
{
pfe.Update(new TValue(baseTime.AddMinutes(i), 100 + i));
}
Assert.True(pfe.Last.Value > 0, $"PFE should be positive for uptrend, got {pfe.Last.Value}");
}
[Fact]
public void Validation_MonotonicDecrease_NegativePfe()
{
// For strictly decreasing prices, PFE should be negative
var pfe = new Pfe(5, 3);
var baseTime = DateTime.UtcNow;
for (int i = 0; i < 30; i++)
{
pfe.Update(new TValue(baseTime.AddMinutes(i), 200 - i));
}
Assert.True(pfe.Last.Value < 0, $"PFE should be negative for downtrend, got {pfe.Last.Value}");
}
[Fact]
public void Validation_WarmupBarsReturnZero()
{
var pfe = new Pfe(5, 3);
var baseTime = DateTime.UtcNow;
// First period bars (before close buffer is full) should return 0
for (int i = 0; i < 5; i++)
{
var result = pfe.Update(new TValue(baseTime.AddMinutes(i), 100 + i));
Assert.Equal(0.0, result.Value, 1e-10);
}
}
[Fact]
public void Validation_DivByZero_ReturnsZero()
{
// If all prices are identical, fractal path = period * sqrt(0 + 1) = period
// But straight line distance has priceDiff=0, so straightLine = sqrt(0 + period^2) = period
// rawPfe = 0 because priceDiff >= 0 ? efficiency : -efficiency maps to +efficiency when priceDiff=0
// But efficiency = period/period*100 = 100 when constant
// Actually for constant: numerator = 0, so rawPfe = sign(0) * 100 = +100 (per sign convention)
// Wait: straightLine = sqrt(0 + 25) = 5, fractalPath = 5*1 = 5, efficiency = 100
// priceDiff = 0 >= 0, so rawPfe = +100
// Actually priceDiff=0 means no change, but the formula gives 100% efficiency
// No, rechecking: priceDiff = close - close[period] = 0 for constant
// straightLine = sqrt(0 + period^2) = period
// fractalPath = sum of sqrt(0 + 1) = period
// so rawPfe = sign(0) * (period/period)*100 = +100 for constant
// This is mathematically correct: a flat line IS efficient in the Euclidean sense
// But the PineScript code uses the sign as: priceDiff >= 0 ? efficiency : -efficiency
// So a flat line gets +100.
// Instead test div-by-zero guard for fractalPath near 0 (can't happen naturally)
// Just verify constant produces a defined result
var pfe = new Pfe(5, 3);
var baseTime = DateTime.UtcNow;
for (int i = 0; i < 15; i++)
{
var result = pfe.Update(new TValue(baseTime.AddMinutes(i), 50));
Assert.True(double.IsFinite(result.Value));
}
}
// ============== Bounded Output ==============
[Fact]
public void Validation_OutputAlwaysBounded()
{
var pfe = new Pfe(10, 5);
var gbm = new GBM(startPrice: 100.0, mu: 0.5, sigma: 2.0);
var bars = gbm.Fetch(500, DateTime.UtcNow.Ticks, TimeSpan.FromMinutes(1));
var series = bars.Close;
foreach (var tv in series)
{
var result = pfe.Update(tv);
if (pfe.IsHot)
{
Assert.True(result.Value >= -100.1 && result.Value <= 100.1,
$"PFE must be in [-100, +100] when hot, got {result.Value}");
}
}
}
// ============== Different Periods ==============
[Fact]
public void Validation_DifferentPeriods_ProduceDifferentResults()
{
var pfe_5 = new Pfe(5, 3);
var pfe_10 = new Pfe(10, 5);
var pfe_20 = new Pfe(20, 8);
var gbm = new GBM(startPrice: 100.0, mu: 0.1, sigma: 0.3);
var bars = gbm.Fetch(200, DateTime.UtcNow.Ticks, TimeSpan.FromMinutes(1));
var series = bars.Close;
foreach (var tv in series)
{
pfe_5.Update(tv);
pfe_10.Update(tv);
pfe_20.Update(tv);
}
// All should be finite and bounded
Assert.True(double.IsFinite(pfe_5.Last.Value));
Assert.True(double.IsFinite(pfe_10.Last.Value));
Assert.True(double.IsFinite(pfe_20.Last.Value));
}
[Fact]
public void Validation_Calculate_ReturnsHotIndicator()
{
var gbm = new GBM(startPrice: 100.0, mu: 0.02, sigma: 0.3);
var bars = gbm.Fetch(200, DateTime.UtcNow.Ticks, TimeSpan.FromMinutes(1));
var series = bars.Close;
var (results, indicator) = Pfe.Calculate(series, 10, 5);
Assert.Equal(series.Count, results.Count);
Assert.True(indicator.IsHot);
Assert.True(double.IsFinite(indicator.Last.Value));
}
[Fact]
public void Validation_BarCorrection_Consistent()
{
var pfe1 = new Pfe(10, 5);
var pfe2 = new Pfe(10, 5);
var gbm = new GBM(startPrice: 100.0, mu: 0.02, sigma: 0.3);
var bars = gbm.Fetch(50, DateTime.UtcNow.Ticks, TimeSpan.FromMinutes(1));
var series = bars.Close;
// Pfe1: feed all values normally
foreach (var tv in series)
{
pfe1.Update(tv, isNew: true);
}
// Pfe2: feed values with correction on last bar
for (int i = 0; i < series.Count - 1; i++)
{
pfe2.Update(series[i], isNew: true);
}
// Feed wrong last value first
pfe2.Update(new TValue(series[^1].Time, 999999), isNew: true);
// Correct it
pfe2.Update(series[^1], isNew: false);
Assert.Equal(pfe1.Last.Value, pfe2.Last.Value, 1e-10);
}
[Fact]
public void Validation_Symmetry_UpAndDownTrends()
{
// A linear rise should produce +PFE, a linear fall should produce -PFE
// with equal magnitude (symmetric)
var pfeUp = new Pfe(5, 3);
var pfeDown = new Pfe(5, 3);
var baseTime = DateTime.UtcNow;
double basePrice = 1000;
for (int i = 0; i < 30; i++)
{
pfeUp.Update(new TValue(baseTime.AddMinutes(i), basePrice + i));
pfeDown.Update(new TValue(baseTime.AddMinutes(i), basePrice - i));
}
// Up should be positive, down should be negative
Assert.True(pfeUp.Last.Value > 0);
Assert.True(pfeDown.Last.Value < 0);
// Absolute values should be approximately equal (symmetric efficiency)
Assert.Equal(Math.Abs(pfeUp.Last.Value), Math.Abs(pfeDown.Last.Value), 1e-10);
}
[Fact]
public void Validation_ManualKnownValue_LinearTrend()
{
// For a perfectly linear trend with step=1:
// straightLine = sqrt((close-close[period])^2 + period^2) = sqrt(period^2 + period^2) = period*sqrt(2)
// fractalPath = period * sqrt(1^2 + 1) = period * sqrt(2)
// rawPfe = +1 * (period*sqrt(2)) / (period*sqrt(2)) * 100 = 100
// After EMA settles, PFE should approach 100
var pfe = new Pfe(5, 1); // smoothPeriod=1 means no smoothing (EMA with alpha=1)
var baseTime = DateTime.UtcNow;
for (int i = 0; i < 30; i++)
{
pfe.Update(new TValue(baseTime.AddMinutes(i), 100.0 + i));
}
// With smoothPeriod=1, alpha=2/(1+1)=1, so EMA=rawPfe exactly
// rawPfe for perfect linear trend = 100
Assert.Equal(100.0, pfe.Last.Value, 1e-6);
}
}