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,107 @@
using TradingPlatform.BusinessLayer;
using QuanTAlib;
namespace QuanTAlib.Tests;
public sealed class ErIndicatorTests
{
[Fact]
public void ErIndicator_Constructor_SetsDefaults()
{
var indicator = new ErIndicator();
Assert.Equal(10, indicator.Period);
Assert.Equal(SourceType.Close, indicator.Source);
Assert.True(indicator.ShowColdValues);
Assert.Equal("ER - Kaufman Efficiency Ratio", indicator.Name);
Assert.True(indicator.SeparateWindow);
Assert.True(indicator.OnBackGround);
}
[Fact]
public void ErIndicator_MinHistoryDepths_EqualsZero()
{
var indicator = new ErIndicator { Period = 10 };
Assert.Equal(0, ErIndicator.MinHistoryDepths);
IWatchlistIndicator watchlistIndicator = indicator;
Assert.Equal(0, watchlistIndicator.MinHistoryDepths);
}
[Fact]
public void ErIndicator_ShortName_IncludesParameters()
{
var indicator = new ErIndicator { Period = 20 };
indicator.Initialize();
Assert.Contains("ER", indicator.ShortName, StringComparison.Ordinal);
Assert.Contains("20", indicator.ShortName, StringComparison.Ordinal);
}
[Fact]
public void ErIndicator_SourceCodeLink_IsValid()
{
var indicator = new ErIndicator();
Assert.Contains("github.com", indicator.SourceCodeLink, StringComparison.Ordinal);
Assert.Contains("Er.Quantower.cs", indicator.SourceCodeLink, StringComparison.Ordinal);
}
[Fact]
public void ErIndicator_Initialize_CreatesInternalEr()
{
var indicator = new ErIndicator { Period = 10 };
indicator.Initialize();
Assert.Single(indicator.LinesSeries);
}
[Fact]
public void ErIndicator_ProcessUpdate_HistoricalBar_ComputesValue()
{
var indicator = new ErIndicator { Period = 5 };
indicator.Initialize();
var now = DateTime.UtcNow;
for (int i = 0; i < 20; i++)
{
indicator.HistoricalData.AddBar(now.AddMinutes(i), 100 + i, 110 + i, 90 + i, 105 + i);
var args = new UpdateArgs(UpdateReason.HistoricalBar);
indicator.ProcessUpdate(args);
}
double value = indicator.LinesSeries[0].GetValue(0);
Assert.True(double.IsFinite(value));
}
[Fact]
public void ErIndicator_ProcessUpdate_NewBar_ComputesValue()
{
var indicator = new ErIndicator { Period = 5 };
indicator.Initialize();
var now = DateTime.UtcNow;
for (int i = 0; i < 20; i++)
{
indicator.HistoricalData.AddBar(now.AddMinutes(i), 100 + i, 110 + i, 90 + i, 105 + i);
}
indicator.ProcessUpdate(new UpdateArgs(UpdateReason.HistoricalBar));
indicator.HistoricalData.AddBar(now.AddMinutes(20), 120, 130, 110, 125);
indicator.ProcessUpdate(new UpdateArgs(UpdateReason.NewBar));
Assert.Single(indicator.LinesSeries);
}
[Fact]
public void ErIndicator_Parameters_CanBeChanged()
{
var indicator = new ErIndicator { Period = 20 };
indicator.Initialize();
Assert.Equal(20, indicator.Period);
}
}
+342
View File
@@ -0,0 +1,342 @@
using Xunit;
namespace QuanTAlib.Tests;
public sealed class ErTests
{
private const double Tolerance = 1e-9;
// ───── A) Constructor validation ─────
[Fact]
public void Constructor_DefaultPeriod_IsValid()
{
var er = new Er();
Assert.Equal(10, er.Period);
Assert.Equal("Er(10)", er.Name);
}
[Fact]
public void Constructor_InvalidPeriod_Throws()
{
var ex = Assert.Throws<ArgumentException>(() => new Er(period: 0));
Assert.Equal("period", ex.ParamName);
}
[Fact]
public void Constructor_NegativePeriod_Throws()
{
var ex = Assert.Throws<ArgumentException>(() => new Er(period: -5));
Assert.Equal("period", ex.ParamName);
}
[Fact]
public void Constructor_CustomPeriod_SetsCorrectly()
{
var er = new Er(period: 20);
Assert.Equal(20, er.Period);
Assert.Equal("Er(20)", er.Name);
}
// ───── B) Basic calculation ─────
[Fact]
public void Update_ReturnsTValue()
{
var er = new Er(period: 5);
var result = er.Update(new TValue(DateTime.UtcNow, 100.0));
Assert.IsType<TValue>(result);
}
[Fact]
public void Update_Last_IsAccessible()
{
var er = new Er(period: 5);
er.Update(new TValue(DateTime.UtcNow, 100.0));
Assert.True(double.IsFinite(er.Last.Value));
}
[Fact]
public void Update_TrendingPrices_HighER()
{
var er = new Er(period: 10);
for (int i = 0; i < 20; i++)
{
er.Update(new TValue(DateTime.UtcNow, 100.0 + i * 2));
}
Assert.True(er.Last.Value > 0.8, "Strongly trending prices should produce high ER");
}
[Fact]
public void Update_ChoppyPrices_LowER()
{
var er = new Er(period: 10);
for (int i = 0; i < 30; i++)
{
double price = 100.0 + (i % 2 == 0 ? 5.0 : -5.0);
er.Update(new TValue(DateTime.UtcNow, price));
}
Assert.True(er.Last.Value < 0.3, "Choppy prices should produce low ER");
}
[Fact]
public void Update_Output_ClampedTo01()
{
var er = new Er(period: 5);
for (int i = 0; i < 20; i++)
{
var result = er.Update(new TValue(DateTime.UtcNow, 100.0 + i));
Assert.InRange(result.Value, 0.0, 1.0);
}
}
// ───── C) State + bar correction ─────
[Fact]
public void Update_IsNew_False_RollsBack()
{
var er = new Er(period: 5);
for (int i = 0; i < 12; i++)
{
er.Update(new TValue(DateTime.UtcNow, 100.0 + i), isNew: true);
}
er.Update(new TValue(DateTime.UtcNow, 105.0), isNew: false);
var corrected = er.Last;
er.Update(new TValue(DateTime.UtcNow, 105.0), isNew: false);
var corrected2 = er.Last;
Assert.Equal(corrected.Value, corrected2.Value, Tolerance);
}
[Fact]
public void Update_IterativeCorrections_Restore()
{
var er = new Er(period: 5);
double[] data = new double[15];
for (int i = 0; i < data.Length; i++)
{
data[i] = 100 + i * 2;
}
for (int i = 0; i < data.Length; i++)
{
er.Update(new TValue(DateTime.UtcNow, data[i]), isNew: true);
}
var baseline = er.Last.Value;
er.Update(new TValue(DateTime.UtcNow, 999.0), isNew: false);
er.Update(new TValue(DateTime.UtcNow, 888.0), isNew: false);
er.Update(new TValue(DateTime.UtcNow, data[^1]), isNew: false);
Assert.Equal(baseline, er.Last.Value, Tolerance);
}
[Fact]
public void Reset_ClearsState()
{
var er = new Er(period: 5);
for (int i = 0; i < 10; i++)
{
er.Update(new TValue(DateTime.UtcNow, 100.0 + i));
}
er.Reset();
Assert.False(er.IsHot);
Assert.Equal(0.0, er.Last.Value);
}
// ───── D) Warmup/convergence ─────
[Fact]
public void IsHot_FlipsWhenBufferFull()
{
int period = 10;
var er = new Er(period);
for (int i = 0; i < period; i++)
{
er.Update(new TValue(DateTime.UtcNow, 100.0 + i));
Assert.False(er.IsHot);
}
er.Update(new TValue(DateTime.UtcNow, 120.0));
Assert.True(er.IsHot);
}
[Fact]
public void WarmupPeriod_MatchesPeriodPlusOne()
{
var er = new Er(period: 14);
Assert.Equal(15, er.WarmupPeriod);
}
// ───── E) Robustness ─────
[Fact]
public void Update_NaN_UsesLastValid()
{
var er = new Er(period: 5);
for (int i = 0; i < 10; i++)
{
er.Update(new TValue(DateTime.UtcNow, 100.0 + i));
}
er.Update(new TValue(DateTime.UtcNow, double.NaN));
Assert.True(double.IsFinite(er.Last.Value));
}
[Fact]
public void Update_Infinity_UsesLastValid()
{
var er = new Er(period: 5);
for (int i = 0; i < 10; i++)
{
er.Update(new TValue(DateTime.UtcNow, 100.0 + i));
}
er.Update(new TValue(DateTime.UtcNow, double.PositiveInfinity));
Assert.True(double.IsFinite(er.Last.Value));
}
[Fact]
public void Update_BatchNaN_RemainsFinite()
{
var er = new Er(period: 5);
for (int i = 0; i < 3; i++)
{
er.Update(new TValue(DateTime.UtcNow, double.NaN));
}
Assert.True(double.IsFinite(er.Last.Value));
}
// ───── F) Consistency (4 modes match) ─────
[Fact]
public void AllModes_ProduceSameResults()
{
int period = 10;
var gbm = new GBM(startPrice: 100.0, mu: 0.02, sigma: 0.1, seed: 42);
var bars = gbm.Fetch(500, DateTime.UtcNow.Ticks, TimeSpan.FromMinutes(1));
TSeries source = bars.Close;
// 1. Streaming
var streaming = new Er(period);
var streamResults = new double[source.Count];
for (int i = 0; i < source.Count; i++)
{
streamResults[i] = streaming.Update(source[i]).Value;
}
// 2. Batch TSeries
TSeries batchSeries = Er.Batch(source, period);
// 3. Batch Span
var spanOutput = new double[source.Count];
Er.Batch(source.Values, spanOutput, period);
// 4. Event-driven
var eventSource = new TSeries();
var eventIndicator = new Er(eventSource, period);
var eventResults = new double[source.Count];
for (int i = 0; i < source.Count; i++)
{
eventSource.Add(source[i]);
eventResults[i] = eventIndicator.Last.Value;
}
// Compare all modes
for (int i = 0; i < source.Count; i++)
{
Assert.Equal(streamResults[i], batchSeries.Values[i], Tolerance);
Assert.Equal(streamResults[i], spanOutput[i], Tolerance);
Assert.Equal(streamResults[i], eventResults[i], Tolerance);
}
}
// ───── G) Span API tests ─────
[Fact]
public void Batch_Span_MismatchedLength_Throws()
{
var src = new double[10];
var output = new double[5];
var ex = Assert.Throws<ArgumentException>(() => Er.Batch(src, output, 5));
Assert.Equal("output", ex.ParamName);
}
[Fact]
public void Batch_Span_InvalidPeriod_Throws()
{
var src = new double[10];
var output = new double[10];
var ex = Assert.Throws<ArgumentException>(() => Er.Batch(src, output, 0));
Assert.Equal("period", ex.ParamName);
}
[Fact]
public void Batch_Span_Empty_NoException()
{
var src = ReadOnlySpan<double>.Empty;
var output = Span<double>.Empty;
Er.Batch(src, output, 5);
Assert.True(true); // S2699: assertion confirms no-exception completion
}
[Fact]
public void Batch_Span_MatchesTSeries()
{
var gbm = new GBM(startPrice: 100.0, mu: 0.02, sigma: 0.1, seed: 42);
var bars = gbm.Fetch(200, DateTime.UtcNow.Ticks, TimeSpan.FromMinutes(1));
TSeries source = bars.Close;
TSeries batchSeries = Er.Batch(source, 10);
var spanOutput = new double[source.Count];
Er.Batch(source.Values, spanOutput, 10);
for (int i = 0; i < source.Count; i++)
{
Assert.Equal(batchSeries.Values[i], spanOutput[i], Tolerance);
}
}
[Fact]
public void Batch_Span_NaN_Handled()
{
double[] src = [100, double.NaN, 102, 103, 104, 105, 106, 107, 108, 109];
var output = new double[src.Length];
Er.Batch(src, output, 5);
for (int i = 0; i < output.Length; i++)
{
Assert.True(double.IsFinite(output[i]));
}
}
// ───── H) Chainability ─────
[Fact]
public void Pub_Fires_OnUpdate()
{
var er = new Er(period: 5);
int fireCount = 0;
er.Pub += (object? _, in TValueEventArgs _) => fireCount++;
er.Update(new TValue(DateTime.UtcNow, 100.0));
Assert.Equal(1, fireCount);
}
[Fact]
public void EventBased_Chaining_Works()
{
var source = new TSeries();
var er = new Er(source, period: 5);
source.Add(new TValue(DateTime.UtcNow, 100.0));
Assert.True(double.IsFinite(er.Last.Value));
}
}
@@ -0,0 +1,226 @@
using System.Runtime.CompilerServices;
using Xunit;
using Xunit.Abstractions;
namespace QuanTAlib.Tests;
/// <summary>
/// Self-consistency validation for ER (Efficiency Ratio).
/// ER is not implemented by TA-Lib, Skender, Tulip, or Ooples as a standalone
/// indicator, so validation uses streaming == batch == span mode consistency
/// plus mathematical identity checks against the signal/noise definition.
/// </summary>
public sealed class ErValidationTests(ITestOutputHelper output)
{
private readonly ITestOutputHelper _output = output;
private const double Tolerance = 1e-12;
// ── A) Streaming == Batch(TSeries) ────────────────────────────────────────
[Fact]
[SkipLocalsInit]
public void Validate_Streaming_Equals_Batch_Period10()
{
const int N = 200;
const int period = 10;
var gbm = new GBM(100.0, 0.05, 0.2, seed: 1001);
var prices = new double[N];
for (int i = 0; i < N; i++) { prices[i] = gbm.Next(isNew: true).Close; }
// Streaming
var er = new Er(period);
for (int i = 0; i < N; i++)
{
er.Update(new TValue(DateTime.UtcNow.AddSeconds(i), prices[i]), isNew: true);
}
double streamVal = er.Last.Value;
// Batch span
var output2 = new double[N];
Er.Batch(prices.AsSpan(), output2.AsSpan(), period);
_output.WriteLine($"Streaming ER={streamVal:F10}, Batch ER={output2[N - 1]:F10}");
Assert.Equal(streamVal, output2[N - 1], Tolerance);
}
[Fact]
[SkipLocalsInit]
public void Validate_Streaming_Equals_Batch_Period20()
{
const int N = 300;
const int period = 20;
var gbm = new GBM(100.0, 0.05, 0.3, seed: 2002);
var prices = new double[N];
for (int i = 0; i < N; i++) { prices[i] = gbm.Next(isNew: true).Close; }
var er = new Er(period);
for (int i = 0; i < N; i++)
{
er.Update(new TValue(DateTime.UtcNow.AddSeconds(i), prices[i]), isNew: true);
}
var output2 = new double[N];
Er.Batch(prices.AsSpan(), output2.AsSpan(), period);
Assert.Equal(er.Last.Value, output2[N - 1], Tolerance);
}
// ── B) Batch(TSeries) == Calculate(TSeries) ───────────────────────────────
[Fact]
public void Validate_Batch_Equals_Calculate()
{
const int period = 14;
var gbm = new GBM(100.0, 0.05, 0.2, seed: 77);
var t0 = DateTime.UtcNow;
var times = new System.Collections.Generic.List<long>(200);
var vals = new System.Collections.Generic.List<double>(200);
for (int i = 0; i < 200; i++)
{
times.Add(t0.AddSeconds(i).Ticks);
vals.Add(gbm.Next(isNew: true).Close);
}
var series = new TSeries(times, vals);
var batchResult = Er.Batch(series, period);
var (calcResult, _) = Er.Calculate(series, period);
for (int i = 0; i < series.Count; i++)
{
Assert.Equal(batchResult.Values[i], calcResult.Values[i], 1e-9);
}
_output.WriteLine("ER Batch == Calculate: PASSED");
}
// ── C) Trending price → ER approaches 1 ─────────────────────────────────
[Fact]
public void Validate_StrictlyRising_ErApproachesOne()
{
const int N = 100;
const int period = 10;
double[] prices = new double[N];
for (int i = 0; i < N; i++) { prices[i] = 100.0 + i * 1.0; }
var output2 = new double[N];
Er.Batch(prices.AsSpan(), output2.AsSpan(), period);
int warmup = period + 1;
for (int i = warmup; i < N; i++)
{
Assert.True(output2[i] > 0.99,
$"ER should be near 1.0 for perfectly trending data at index {i}, got {output2[i]}");
}
_output.WriteLine("ER strictly rising → ER ≈ 1.0: PASSED");
}
// ── D) Choppy price → ER approaches 0 ────────────────────────────────────
[Fact]
public void Validate_ChoppyPrice_ErApproachesZero()
{
const int N = 100;
const int period = 10;
double[] prices = new double[N];
for (int i = 0; i < N; i++) { prices[i] = 100.0 + (i % 2 == 0 ? 1.0 : -1.0); }
var output2 = new double[N];
Er.Batch(prices.AsSpan(), output2.AsSpan(), period);
int warmup = period + 1;
for (int i = warmup; i < N; i++)
{
Assert.True(output2[i] < 0.1,
$"ER should be near 0 for choppy data at index {i}, got {output2[i]}");
}
_output.WriteLine("ER choppy price → ER ≈ 0: PASSED");
}
// ── E) Output clamped [0, 1] ──────────────────────────────────────────────
[Fact]
public void Validate_OutputClamped_ZeroToOne()
{
const int N = 300;
const int period = 10;
var gbm = new GBM(100.0, 0.5, 2.0, seed: 42);
double[] prices = new double[N];
for (int i = 0; i < N; i++) { prices[i] = gbm.Next(isNew: true).Close; }
var output2 = new double[N];
Er.Batch(prices.AsSpan(), output2.AsSpan(), period);
for (int i = 0; i < N; i++)
{
Assert.True(output2[i] >= 0.0 && output2[i] <= 1.0,
$"ER out of [0,1] range at index {i}: {output2[i]}");
}
_output.WriteLine("ER output clamped [0, 1]: PASSED");
}
// ── F) Determinism across runs ────────────────────────────────────────────
[Fact]
public void Validate_Deterministic()
{
const int N = 200;
const int period = 14;
var gbm = new GBM(100.0, 0.05, 0.2, seed: 99);
double[] prices = new double[N];
for (int i = 0; i < N; i++) { prices[i] = gbm.Next(isNew: true).Close; }
var out1 = new double[N];
var out2 = new double[N];
Er.Batch(prices.AsSpan(), out1.AsSpan(), period);
Er.Batch(prices.AsSpan(), out2.AsSpan(), period);
for (int i = 0; i < N; i++)
{
Assert.Equal(out1[i], out2[i], 15);
}
_output.WriteLine("ER determinism: PASSED");
}
// ── G) Different periods produce different results ────────────────────────
[Fact]
public void Validate_DifferentPeriods_DifferentResults()
{
const int N = 200;
var gbm = new GBM(100.0, 0.05, 0.2, seed: 55);
double[] prices = new double[N];
for (int i = 0; i < N; i++) { prices[i] = gbm.Next(isNew: true).Close; }
var out5 = new double[N];
var out20 = new double[N];
Er.Batch(prices.AsSpan(), out5.AsSpan(), 5);
Er.Batch(prices.AsSpan(), out20.AsSpan(), 20);
bool anyDiff = false;
for (int i = 25; i < N; i++)
{
if (Math.Abs(out5[i] - out20[i]) > 0.001)
{
anyDiff = true;
break;
}
}
Assert.True(anyDiff, "Different periods should produce different ER values");
_output.WriteLine("ER different periods produce different results: PASSED");
}
// ── H) Constant price → ER = 0 ───────────────────────────────────────────
[Fact]
public void Validate_ConstantPrice_ErIsZero()
{
const int N = 50;
const int period = 10;
double[] prices = new double[N];
Array.Fill(prices, 100.0);
var output2 = new double[N];
Er.Batch(prices.AsSpan(), output2.AsSpan(), period);
int warmup = period + 1;
for (int i = warmup; i < N; i++)
{
Assert.Equal(0.0, output2[i], 1e-10);
}
_output.WriteLine("ER constant price → ER = 0: PASSED");
}
}