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
+272
View File
@@ -0,0 +1,272 @@
namespace QuanTAlib.Tests;
public class StdDevTests
{
[Fact]
public void Constructor_ValidatesPeriod()
{
Assert.Throws<ArgumentOutOfRangeException>(() => new StdDev(1));
Assert.Throws<ArgumentOutOfRangeException>(() => new StdDev(0));
Assert.Throws<ArgumentOutOfRangeException>(() => new StdDev(-1));
}
[Fact]
public void Properties_Accessible()
{
var stddev = new StdDev(5);
Assert.Equal(0, stddev.Last.Value);
Assert.False(stddev.IsHot);
Assert.Contains("StdDev", stddev.Name, StringComparison.Ordinal);
}
[Fact]
public void Calc_IsNew_False_UpdatesValue()
{
var stddev = new StdDev(3);
stddev.Update(new TValue(DateTime.UtcNow, 10));
stddev.Update(new TValue(DateTime.UtcNow, 20));
stddev.Update(new TValue(DateTime.UtcNow, 30));
double valueBefore = stddev.Last.Value;
// Update with isNew=false should change the result
stddev.Update(new TValue(DateTime.UtcNow, 100), isNew: false);
double valueAfter = stddev.Last.Value;
Assert.NotEqual(valueBefore, valueAfter);
}
[Fact]
public void NaN_Input_UsesLastValidValue()
{
var stddev = new StdDev(5);
stddev.Update(new TValue(DateTime.UtcNow, 10));
stddev.Update(new TValue(DateTime.UtcNow, 20));
var result = stddev.Update(new TValue(DateTime.UtcNow, double.NaN));
Assert.True(double.IsFinite(result.Value));
}
[Fact]
public void Infinity_Input_UsesLastValidValue()
{
var stddev = new StdDev(5);
stddev.Update(new TValue(DateTime.UtcNow, 10));
stddev.Update(new TValue(DateTime.UtcNow, 20));
var resultPosInf = stddev.Update(new TValue(DateTime.UtcNow, double.PositiveInfinity));
Assert.True(double.IsFinite(resultPosInf.Value));
var resultNegInf = stddev.Update(new TValue(DateTime.UtcNow, double.NegativeInfinity));
Assert.True(double.IsFinite(resultNegInf.Value));
}
[Fact]
public void IterativeCorrections_RestoreToOriginalState()
{
var stddev = new StdDev(5);
var gbm = new GBM(startPrice: 100.0, mu: 0.02, sigma: 0.1, seed: 42);
// Feed 10 new values
TValue tenthInput = default;
for (int i = 0; i < 10; i++)
{
var bar = gbm.Next(isNew: true);
tenthInput = new TValue(bar.Time, bar.Close);
stddev.Update(tenthInput, isNew: true);
}
// Remember state after 10 values
double stateAfterTen = stddev.Last.Value;
// Generate 9 corrections with isNew=false (different values)
for (int i = 0; i < 9; i++)
{
var bar = gbm.Next(isNew: false);
stddev.Update(new TValue(bar.Time, bar.Close), isNew: false);
}
// Feed the remembered 10th input again with isNew=false
TValue finalResult = stddev.Update(tenthInput, isNew: false);
// State should match the original state after 10 values
// Note: FMA optimization in RingBuffer provides better precision, so we use a slightly relaxed tolerance
Assert.Equal(stateAfterTen, finalResult.Value, 1e-9);
}
[Fact]
public void SpanBatch_ValidatesInput()
{
double[] source = [1, 2, 3, 4, 5];
double[] output = new double[5];
double[] wrongSizeOutput = new double[3];
// Period must be > 1
Assert.Throws<ArgumentException>(() =>
StdDev.Batch(source.AsSpan(), output.AsSpan(), 1));
// Output must be same length as source
Assert.Throws<ArgumentException>(() =>
StdDev.Batch(source.AsSpan(), wrongSizeOutput.AsSpan(), 3));
}
[Fact]
public void AllModes_ProduceSameResult()
{
const int period = 10;
var gbm = new GBM(startPrice: 100, mu: 0.05, sigma: 0.2, seed: 123);
var bars = gbm.Fetch(1000, DateTime.UtcNow.Ticks, TimeSpan.FromMinutes(1));
var series = bars.Close;
// 1. Batch Mode (static span)
var tValues = series.Values.ToArray();
var batchOutput = new double[tValues.Length];
StdDev.Batch(tValues, batchOutput, period);
double expected = batchOutput[^1];
// 2. Streaming Mode
var streamingInd = new StdDev(period);
for (int i = 0; i < series.Count; i++)
{
streamingInd.Update(series[i]);
}
double streamingResult = streamingInd.Last.Value;
// 3. TSeries Batch Mode
var batchSeriesResult = StdDev.Batch(series, period);
double tseriesResult = batchSeriesResult.Last.Value;
Assert.Equal(expected, streamingResult, precision: 6);
Assert.Equal(expected, tseriesResult, precision: 6);
}
[Fact]
public void Calculation_KnownValues()
{
// Data: 2, 4, 4, 4, 5, 5, 7, 9
// Mean: 5
// Deviations: -3, -1, -1, -1, 0, 0, 2, 4
// Sq Devs: 9, 1, 1, 1, 0, 0, 4, 16
// Sum Sq Devs: 32
// Population Variance (N=8): 32 / 8 = 4
// Population StdDev: Sqrt(4) = 2
// Sample Variance (N-1=7): 32 / 7 = 4.571428...
// Sample StdDev: Sqrt(4.571428...) = 2.1380899...
double[] data = [2, 4, 4, 4, 5, 5, 7, 9];
// Test Population StdDev
var popStd = new StdDev(8, isPopulation: true);
foreach (var val in data)
{
popStd.Update(new TValue(DateTime.UtcNow, val));
}
Assert.Equal(2.0, popStd.Last.Value, precision: 6);
// Test Sample StdDev
var sampStd = new StdDev(8, isPopulation: false);
foreach (var val in data)
{
sampStd.Update(new TValue(DateTime.UtcNow, val));
}
Assert.Equal(Math.Sqrt(32.0 / 7.0), sampStd.Last.Value, precision: 6);
}
[Fact]
public void IsHot_BecomesTrueAfterPeriod()
{
const int period = 5;
var stdDev = new StdDev(period);
for (int i = 0; i < period; i++)
{
Assert.False(stdDev.IsHot);
stdDev.Update(new TValue(DateTime.UtcNow, i));
}
Assert.True(stdDev.IsHot);
}
[Fact]
public void Reset_ClearsState()
{
var stdDev = new StdDev(5);
for (int i = 0; i < 10; i++)
{
stdDev.Update(new TValue(DateTime.UtcNow, i));
}
Assert.True(stdDev.IsHot);
stdDev.Reset();
Assert.False(stdDev.IsHot);
Assert.Equal(0, stdDev.Last.Value);
}
[Fact]
public void Batch_Matches_Iterative()
{
int period = 10;
int count = 1000;
var data = new double[count];
var gbm = new GBM(startPrice: 100, mu: 0.05, sigma: 0.2, seed: 123);
for (int i = 0; i < count; i++)
{
data[i] = gbm.Next().Close;
}
// Iterative
var stdDev = new StdDev(period);
var iterativeResults = new double[count];
for (int i = 0; i < count; i++)
{
stdDev.Update(new TValue(DateTime.UtcNow, data[i]));
iterativeResults[i] = stdDev.Last.Value;
}
// Batch
var batchResults = new double[count];
StdDev.Batch(data, batchResults, period);
// Compare
for (int i = 0; i < count; i++)
{
Assert.Equal(iterativeResults[i], batchResults[i], precision: 6);
}
}
[Fact]
public void Update_TSeries_Matches_Iterative()
{
int period = 10;
int count = 1000;
var data = new TSeries();
var gbm = new GBM(startPrice: 100, mu: 0.05, sigma: 0.2, seed: 123);
for (int i = 0; i < count; i++)
{
var bar = gbm.Next();
data.Add(new TValue(bar.Time, bar.Close));
}
// Iterative
var stdDev = new StdDev(period);
var iterativeResults = new double[count];
for (int i = 0; i < count; i++)
{
stdDev.Update(data[i]);
iterativeResults[i] = stdDev.Last.Value;
}
// TSeries Batch
var stdDevBatch = new StdDev(period);
var batchSeries = stdDevBatch.Update(data);
// Compare
for (int i = 0; i < count; i++)
{
Assert.Equal(iterativeResults[i], batchSeries[i].Value, precision: 6);
}
}
}
@@ -0,0 +1,525 @@
using Skender.Stock.Indicators;
namespace QuanTAlib.Tests;
public sealed class StdDevValidationTests : IDisposable
{
private readonly ValidationTestData _testData;
private bool _disposed;
public StdDevValidationTests()
{
_testData = new ValidationTestData();
}
public void Dispose()
{
Dispose(true);
}
private void Dispose(bool disposing)
{
if (_disposed)
{
return;
}
_disposed = true;
if (disposing)
{
_testData?.Dispose();
}
}
#region Skender Validation
[Fact]
public void StdDev_Matches_Skender_Batch()
{
// Skender StdDev uses Population Standard Deviation (N)
int[] periods = { 5, 10, 20, 50, 100 };
foreach (var period in periods)
{
var stdDev = new StdDev(period, isPopulation: true);
var qResult = stdDev.Update(_testData.Data);
var sResult = _testData.SkenderQuotes.GetStdDev(period).ToList();
ValidationHelper.VerifyData(qResult, sResult, (s) => s.StdDev);
}
}
[Fact]
public void StdDev_Matches_Skender_Streaming()
{
// Skender StdDev uses Population Standard Deviation (N)
int[] periods = { 5, 10, 20, 50, 100 };
foreach (var period in periods)
{
var stdDev = new StdDev(period, isPopulation: true);
var qResults = new List<double>();
foreach (var item in _testData.Data)
{
qResults.Add(stdDev.Update(item).Value);
}
var sResult = _testData.SkenderQuotes.GetStdDev(period).ToList();
ValidationHelper.VerifyData(qResults, sResult, (s) => s.StdDev);
}
}
[Fact]
public void StdDev_Matches_Skender_Span()
{
// Skender StdDev uses Population Standard Deviation (N)
int[] periods = { 5, 10, 20, 50, 100 };
double[] sourceData = _testData.RawData.ToArray();
foreach (var period in periods)
{
double[] qOutput = new double[sourceData.Length];
StdDev.Batch(sourceData.AsSpan(), qOutput.AsSpan(), period, isPopulation: true);
var sResult = _testData.SkenderQuotes.GetStdDev(period).ToList();
ValidationHelper.VerifyData(qOutput, sResult, (s) => s.StdDev);
}
}
#endregion
#region TA-Lib Validation
[Fact]
public void StdDev_Matches_Talib_Batch()
{
// TA-Lib STDDEV uses Population Standard Deviation (N)
int[] periods = { 5, 10, 20, 50, 100 };
double[] tData = _testData.RawData.ToArray();
double[] output = new double[tData.Length];
foreach (var period in periods)
{
var stdDev = new StdDev(period, isPopulation: true);
var qResult = stdDev.Update(_testData.Data);
var retCode = TALib.Functions.StdDev(tData, 0..^0, output, out var outRange, period, 1.0);
Assert.Equal(TALib.Core.RetCode.Success, retCode);
int lookback = TALib.Functions.StdDevLookback(period);
ValidationHelper.VerifyData(qResult, output, outRange, lookback);
}
}
[Fact]
public void StdDev_Matches_Talib_Streaming()
{
// TA-Lib STDDEV uses Population Standard Deviation (N)
int[] periods = { 5, 10, 20, 50, 100 };
double[] tData = _testData.RawData.ToArray();
double[] output = new double[tData.Length];
foreach (var period in periods)
{
var stdDev = new StdDev(period, isPopulation: true);
var qResults = new List<double>();
foreach (var item in _testData.Data)
{
qResults.Add(stdDev.Update(item).Value);
}
var retCode = TALib.Functions.StdDev(tData, 0..^0, output, out var outRange, period, 1.0);
Assert.Equal(TALib.Core.RetCode.Success, retCode);
int lookback = TALib.Functions.StdDevLookback(period);
ValidationHelper.VerifyData(qResults, output, outRange, lookback);
}
}
[Fact]
public void StdDev_Matches_Talib_Span()
{
// TA-Lib STDDEV uses Population Standard Deviation (N)
int[] periods = { 5, 10, 20, 50, 100 };
double[] sourceData = _testData.RawData.ToArray();
double[] output = new double[sourceData.Length];
foreach (var period in periods)
{
double[] qOutput = new double[sourceData.Length];
StdDev.Batch(sourceData.AsSpan(), qOutput.AsSpan(), period, isPopulation: true);
var retCode = TALib.Functions.StdDev(sourceData, 0..^0, output, out var outRange, period, 1.0);
Assert.Equal(TALib.Core.RetCode.Success, retCode);
int lookback = TALib.Functions.StdDevLookback(period);
ValidationHelper.VerifyData(qOutput, output, outRange, lookback);
}
}
#endregion
#region Tulip Validation
[Fact]
public void StdDev_Matches_Tulip_Batch()
{
// Tulip STDDEV uses Population Standard Deviation (N)
int[] periods = { 5, 10, 20, 50, 100 };
double[] tData = _testData.RawData.ToArray();
foreach (var period in periods)
{
var stdDev = new StdDev(period, isPopulation: true);
var qResult = stdDev.Update(_testData.Data);
var stdDevInd = Tulip.Indicators.stddev;
double[][] inputs = { tData };
double[] options = { period };
int lookback = stdDevInd.Start(options);
double[][] outputs = { new double[tData.Length - lookback] };
stdDevInd.Run(inputs, options, outputs);
var tResult = outputs[0];
ValidationHelper.VerifyData(qResult, tResult, lookback);
}
}
[Fact]
public void StdDev_Matches_Tulip_Streaming()
{
// Tulip STDDEV uses Population Standard Deviation (N)
int[] periods = { 5, 10, 20, 50, 100 };
double[] tData = _testData.RawData.ToArray();
foreach (var period in periods)
{
var stdDev = new StdDev(period, isPopulation: true);
var qResults = new List<double>();
foreach (var item in _testData.Data)
{
qResults.Add(stdDev.Update(item).Value);
}
var stdDevInd = Tulip.Indicators.stddev;
double[][] inputs = { tData };
double[] options = { period };
int lookback = stdDevInd.Start(options);
double[][] outputs = { new double[tData.Length - lookback] };
stdDevInd.Run(inputs, options, outputs);
var tResult = outputs[0];
ValidationHelper.VerifyData(qResults, tResult, lookback);
}
}
[Fact]
public void StdDev_Matches_Tulip_Span()
{
// Tulip STDDEV uses Population Standard Deviation (N)
int[] periods = { 5, 10, 20, 50, 100 };
double[] sourceData = _testData.RawData.ToArray();
foreach (var period in periods)
{
double[] qOutput = new double[sourceData.Length];
StdDev.Batch(sourceData.AsSpan(), qOutput.AsSpan(), period, isPopulation: true);
var stdDevInd = Tulip.Indicators.stddev;
double[][] inputs = { sourceData };
double[] options = { period };
int lookback = stdDevInd.Start(options);
double[][] outputs = { new double[sourceData.Length - lookback] };
stdDevInd.Run(inputs, options, outputs);
var tResult = outputs[0];
ValidationHelper.VerifyData(qOutput, tResult, lookback);
}
}
#endregion
#region MathNet Validation
[Fact]
public void StdDev_Matches_MathNet_Sample()
{
const int period = 20;
var stdDev = new StdDev(period, isPopulation: false);
double[] input = _testData.RawData.ToArray();
for (int i = 0; i < input.Length; i++)
{
var val = stdDev.Update(new TValue(DateTime.UtcNow, input[i]));
if (i >= period - 1)
{
var window = input[(i - period + 1)..(i + 1)];
double expected = MathNet.Numerics.Statistics.Statistics.StandardDeviation(window);
Assert.Equal(expected, val.Value, ValidationHelper.DefaultTolerance);
}
}
}
[Fact]
public void StdDev_Matches_MathNet_Population()
{
int period = 20;
var stdDev = new StdDev(period, isPopulation: true);
double[] input = _testData.RawData.ToArray();
for (int i = 0; i < input.Length; i++)
{
var val = stdDev.Update(new TValue(DateTime.UtcNow, input[i]));
if (i >= period - 1)
{
var window = input[(i - period + 1)..(i + 1)];
double expected = MathNet.Numerics.Statistics.Statistics.PopulationStandardDeviation(window);
Assert.Equal(expected, val.Value, ValidationHelper.DefaultTolerance);
}
}
}
#endregion
#region Comprehensive Tests
[Fact]
public void StdDev_AllModes_ProduceIdenticalResults()
{
// Critical validation: All 3 API modes must produce identical results
int[] periods = { 5, 10, 20, 50 };
foreach (var period in periods)
{
// Test both population and sample
foreach (bool isPopulation in new[] { true, false })
{
// 1. Batch Mode (TSeries)
var batchStdDev = new StdDev(period, isPopulation);
var batchResult = batchStdDev.Update(_testData.Data);
// 2. Span Mode
double[] sourceData = _testData.RawData.ToArray();
double[] spanOutput = new double[sourceData.Length];
StdDev.Batch(sourceData.AsSpan(), spanOutput.AsSpan(), period, isPopulation);
// 3. Streaming Mode
var streamingStdDev = new StdDev(period, isPopulation);
var streamingResults = new List<double>();
foreach (var item in _testData.Data)
{
streamingResults.Add(streamingStdDev.Update(item).Value);
}
// Compare all modes (allow 1e-7 tolerance for accumulated floating-point errors
// between SIMD batch paths and scalar streaming paths with different FMA/sum ordering)
for (int i = 0; i < _testData.Data.Count; i++)
{
Assert.Equal(batchResult[i].Value, spanOutput[i], 1e-7);
Assert.Equal(batchResult[i].Value, streamingResults[i], 1e-7);
}
}
}
}
[Fact]
public void StdDev_Matches_SqrtVariance()
{
// StdDev = Sqrt(Variance)
// Validate this relationship holds
int[] periods = { 5, 10, 20, 50, 100 };
foreach (var period in periods)
{
foreach (bool isPopulation in new[] { true, false })
{
var stdDev = new StdDev(period, isPopulation);
var variance = new Variance(period, isPopulation);
for (int i = 0; i < _testData.Data.Count; i++)
{
var input = _testData.Data[i];
var s = stdDev.Update(input);
var v = variance.Update(input);
double expected = Math.Sqrt(Math.Max(0, v.Value));
Assert.Equal(expected, s.Value, 1e-10);
}
}
}
}
[Fact]
public void StdDev_FlatLine_ProducesZero()
{
// Flat price should produce zero standard deviation
var stdDev = new StdDev(10);
for (int i = 0; i < 50; i++)
{
stdDev.Update(new TValue(DateTime.UtcNow, 100));
}
// After sufficient warmup, flat line should produce StdDev ≈ 0
Assert.True(Math.Abs(stdDev.Last.Value) < 1e-10,
$"Expected StdDev ≈ 0 for flat line, got {stdDev.Last.Value}");
}
[Fact]
public void StdDev_LargeDataset_MaintainsPrecision()
{
// Test with large dataset to ensure no drift
int period = 20;
var stdDev = new StdDev(period, isPopulation: true);
var variance = new Variance(period, isPopulation: true);
var gbm = new GBM(startPrice: 100, mu: 0.05, sigma: 0.2, seed: 42);
var bars = gbm.Fetch(10000, DateTime.UtcNow.Ticks, TimeSpan.FromMinutes(1));
for (int i = 0; i < bars.Close.Count; i++)
{
var input = bars.Close[i];
var s = stdDev.Update(input);
var v = variance.Update(input);
// Every 1000th point, verify precision
if (i % 1000 == 0 && i > period)
{
double expected = Math.Sqrt(Math.Max(0, v.Value));
Assert.Equal(expected, s.Value, 1e-9);
}
}
}
[Fact]
public void StdDev_PopulationVsSample_Difference()
{
// Population and Sample StdDev should differ
int period = 10;
var popStdDev = new StdDev(period, isPopulation: true);
var sampStdDev = new StdDev(period, isPopulation: false);
var gbm = new GBM(startPrice: 100, mu: 0.05, sigma: 0.3, seed: 123);
var bars = gbm.Fetch(200, DateTime.UtcNow.Ticks, TimeSpan.FromMinutes(1));
foreach (var bar in bars.Close)
{
popStdDev.Update(bar);
sampStdDev.Update(bar);
}
// Sample StdDev should be larger than Population StdDev (divides by N-1 instead of N)
Assert.True(sampStdDev.IsHot && popStdDev.IsHot);
Assert.True(sampStdDev.Last.Value > popStdDev.Last.Value,
$"Sample StdDev ({sampStdDev.Last.Value}) should be > Population StdDev ({popStdDev.Last.Value})");
}
[Fact]
public void StdDev_BatchSpan_HandlesNaN_InMiddle()
{
double[] data = new double[100];
var gbm = new GBM(startPrice: 100, seed: 42);
for (int i = 0; i < 100; i++)
{
data[i] = gbm.Next().Close;
}
// Insert NaN in the middle
data[50] = double.NaN;
double[] output = new double[100];
StdDev.Batch(data.AsSpan(), output.AsSpan(), 10);
// All outputs should be finite
foreach (var value in output)
{
Assert.True(double.IsFinite(value), $"Expected finite value, got {value}");
}
}
[Fact]
public void StdDev_Convergence_AfterWarmup()
{
// After warmup period, indicator should be "hot"
int[] periods = { 5, 10, 20, 50 };
foreach (var period in periods)
{
var stdDev = new StdDev(period);
Assert.False(stdDev.IsHot);
// Feed period number of bars
for (int i = 0; i < period - 1; i++)
{
stdDev.Update(_testData.Data[i]);
Assert.False(stdDev.IsHot);
}
stdDev.Update(_testData.Data[period - 1]);
Assert.True(stdDev.IsHot);
}
}
[Fact]
public void StdDev_DifferentPeriods_ProduceDifferentSensitivity()
{
// Shorter periods should be more sensitive to price changes
var stdDev5 = new StdDev(5);
var stdDev20 = new StdDev(20);
var stdDev50 = new StdDev(50);
var gbm = new GBM(startPrice: 100, mu: 0.05, sigma: 0.3, seed: 123);
var bars = gbm.Fetch(200, DateTime.UtcNow.Ticks, TimeSpan.FromMinutes(1));
foreach (var bar in bars.Close)
{
stdDev5.Update(bar);
stdDev20.Update(bar);
stdDev50.Update(bar);
}
// All periods should produce finite numeric results
Assert.True(double.IsFinite(stdDev5.Last.Value));
Assert.True(double.IsFinite(stdDev20.Last.Value));
Assert.True(double.IsFinite(stdDev50.Last.Value));
// All should be hot
Assert.True(stdDev5.IsHot && stdDev20.IsHot && stdDev50.IsHot);
}
[Fact]
public void StdDev_EdgeCase_Period2()
{
// Period=2 is minimum (constructor throws on period=1)
var stdDev = new StdDev(2);
stdDev.Update(new TValue(DateTime.UtcNow, 100));
stdDev.Update(new TValue(DateTime.UtcNow, 100));
// Two identical values should produce StdDev = 0
Assert.Equal(0, stdDev.Last.Value, 1e-10);
stdDev.Update(new TValue(DateTime.UtcNow, 110));
// 100, 110: mean = 105, deviations = -5, 5, squared = 25, 25, sum = 50
// Population variance = 50/2 = 25, StdDev = 5
// Sample variance = 50/1 = 50, StdDev = 7.071...
// Default is sample (isPopulation=false)
Assert.Equal(Math.Sqrt(50), stdDev.Last.Value, 1e-10);
}
#endregion
}