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,175 @@
using TradingPlatform.BusinessLayer;
namespace QuanTAlib.Tests;
public class ConvIndicatorTests
{
[Fact]
public void ConvIndicator_Constructor_SetsDefaults()
{
var indicator = new ConvIndicator();
Assert.Equal("0.1, 0.2, 0.3, 0.4", indicator.WeightsInput);
Assert.Equal(SourceType.Close, indicator.Source);
Assert.True(indicator.ShowColdValues);
Assert.Equal("CONV - Convolution", indicator.Name);
Assert.False(indicator.SeparateWindow);
Assert.True(indicator.OnBackGround);
}
[Fact]
public void ConvIndicator_MinHistoryDepths_EqualsWeightsLength()
{
var indicator = new ConvIndicator { WeightsInput = "1, 2, 3, 4, 5" };
indicator.Initialize(); // Initialize to parse weights
Assert.Equal(0, ConvIndicator.MinHistoryDepths);
Assert.Equal(0, ((IWatchlistIndicator)indicator).MinHistoryDepths);
}
[Fact]
public void ConvIndicator_ShortName_IncludesSource()
{
var indicator = new ConvIndicator();
Assert.Contains("CONV", indicator.ShortName, StringComparison.Ordinal);
Assert.Contains("Close", indicator.ShortName, StringComparison.Ordinal);
}
[Fact]
public void ConvIndicator_SourceCodeLink_IsValid()
{
var indicator = new ConvIndicator();
Assert.Contains("github.com", indicator.SourceCodeLink, StringComparison.Ordinal);
Assert.Contains("Conv.Quantower.cs", indicator.SourceCodeLink, StringComparison.Ordinal);
}
[Fact]
public void ConvIndicator_Initialize_CreatesInternalConv()
{
var indicator = new ConvIndicator();
// Initialize should not throw
indicator.Initialize();
// After init, line series should exist
Assert.Single(indicator.LinesSeries);
}
[Fact]
public void ConvIndicator_ProcessUpdate_HistoricalBar_ComputesValue()
{
var indicator = new ConvIndicator { WeightsInput = "0.5, 0.5" };
indicator.Initialize();
// Add historical data
var now = DateTime.UtcNow;
indicator.HistoricalData.AddBar(now, 100, 105, 95, 102);
// Process update
var args = new UpdateArgs(UpdateReason.HistoricalBar);
indicator.ProcessUpdate(args);
// Line series should have a value
Assert.Equal(1, indicator.LinesSeries[0].Count);
Assert.True(double.IsFinite(indicator.LinesSeries[0].GetValue(0)));
}
[Fact]
public void ConvIndicator_ProcessUpdate_NewBar_ComputesValue()
{
var indicator = new ConvIndicator { WeightsInput = "0.5, 0.5" };
indicator.Initialize();
var now = DateTime.UtcNow;
indicator.HistoricalData.AddBar(now, 100, 105, 95, 102);
indicator.HistoricalData.AddBar(now.AddMinutes(1), 102, 108, 100, 106);
indicator.ProcessUpdate(new UpdateArgs(UpdateReason.HistoricalBar));
indicator.ProcessUpdate(new UpdateArgs(UpdateReason.NewBar));
Assert.Equal(2, indicator.LinesSeries[0].Count);
}
[Fact]
public void ConvIndicator_ProcessUpdate_NewTick_ProcessesWithoutError()
{
var indicator = new ConvIndicator { WeightsInput = "0.5, 0.5" };
indicator.Initialize();
var now = DateTime.UtcNow;
indicator.HistoricalData.AddBar(now, 100, 105, 95, 102);
indicator.ProcessUpdate(new UpdateArgs(UpdateReason.HistoricalBar));
double firstValue = indicator.LinesSeries[0].GetValue(0);
indicator.ProcessUpdate(new UpdateArgs(UpdateReason.NewTick));
double secondValue = indicator.LinesSeries[0].GetValue(0);
Assert.True(double.IsFinite(firstValue));
Assert.True(double.IsFinite(secondValue));
}
[Fact]
public void ConvIndicator_MultipleUpdates_ProducesCorrectSequence()
{
// Weights [0.5, 1.0]
var indicator = new ConvIndicator { WeightsInput = "0.5, 1.0" };
indicator.Initialize();
var now = DateTime.UtcNow;
double[] closes = { 100, 102, 104, 103, 105 };
foreach (var close in closes)
{
indicator.HistoricalData.AddBar(now, close, close + 2, close - 2, close);
indicator.ProcessUpdate(new UpdateArgs(UpdateReason.HistoricalBar));
now = now.AddMinutes(1);
}
// All values should be finite
for (int i = 0; i < closes.Length; i++)
{
Assert.True(double.IsFinite(indicator.LinesSeries[0].GetValue(closes.Length - 1 - i)));
}
}
[Fact]
public void ConvIndicator_DifferentSourceTypes_Work()
{
var sources = new[] { SourceType.Open, SourceType.High, SourceType.Low, SourceType.Close, SourceType.HL2, SourceType.HLC3 };
foreach (var source in sources)
{
var indicator = new ConvIndicator { WeightsInput = "0.5, 0.5", Source = source };
indicator.Initialize();
var now = DateTime.UtcNow;
indicator.HistoricalData.AddBar(now, 100, 110, 90, 105);
indicator.ProcessUpdate(new UpdateArgs(UpdateReason.HistoricalBar));
Assert.True(double.IsFinite(indicator.LinesSeries[0].GetValue(0)),
$"Source {source} should produce finite value");
}
}
[Fact]
public void ConvIndicator_InvalidWeights_FallsBackToDefault()
{
var indicator = new ConvIndicator { WeightsInput = "invalid" };
// Should not throw, but fallback
indicator.Initialize();
Assert.Single(indicator.LinesSeries);
}
[Fact]
public void ConvIndicator_DescriptionIsSet()
{
var indicator = new ConvIndicator();
Assert.Contains("Convolution", indicator.Description, StringComparison.Ordinal);
}
}
+231
View File
@@ -0,0 +1,231 @@
namespace QuanTAlib;
public class ConvTests
{
[Fact]
public void Constructor_EmptyKernel_ThrowsArgumentException()
{
Assert.Throws<ArgumentException>(() => new Conv(Array.Empty<double>()));
Assert.Throws<ArgumentException>(() => new Conv(null!));
}
[Fact]
public void BasicCalculation_MatchesExpected()
{
// Kernel: [0.5, 1.0]
// Data: [1, 2, 3, 4]
// 1: 1*1.0 = 1.0 (partial)
// 2: 1*0.5 + 2*1.0 = 2.5
// 3: 2*0.5 + 3*1.0 = 4.0
// 4: 3*0.5 + 4*1.0 = 5.5
double[] kernel = [0.5, 1.0];
var conv = new Conv(kernel);
var result1 = conv.Update(new TValue(DateTime.UtcNow, 1));
Assert.Equal(1.0, result1.Value);
var result2 = conv.Update(new TValue(DateTime.UtcNow, 2));
Assert.Equal(2.5, result2.Value);
var result3 = conv.Update(new TValue(DateTime.UtcNow, 3));
Assert.Equal(4.0, result3.Value);
var result4 = conv.Update(new TValue(DateTime.UtcNow, 4));
Assert.Equal(5.5, result4.Value);
}
[Fact]
public void BarCorrection_UpdatesCorrectly()
{
double[] kernel = [0.5, 1.0];
var conv = new Conv(kernel);
// 1
conv.Update(new TValue(DateTime.UtcNow, 1));
// 2 (isNew=true) -> 2.5
var res1 = conv.Update(new TValue(DateTime.UtcNow, 2), isNew: true);
Assert.Equal(2.5, res1.Value);
// Update 2 to 3 (isNew=false)
// Buffer was [1, 2]. Now [1, 3].
// 1*0.5 + 3*1.0 = 3.5
var res2 = conv.Update(new TValue(DateTime.UtcNow, 3), isNew: false);
Assert.Equal(3.5, res2.Value);
// New bar 4 (isNew=true)
// Buffer was [1, 3]. New bar 4. Buffer becomes [3, 4].
// 3*0.5 + 4*1.0 = 1.5 + 4 = 5.5
var res3 = conv.Update(new TValue(DateTime.UtcNow, 4), isNew: true);
Assert.Equal(5.5, res3.Value);
}
[Fact]
public void NanHandling_UsesLastValid()
{
double[] kernel = [1.0, 1.0]; // Sum of last 2
var conv = new Conv(kernel);
// 1 -> 1
conv.Update(new TValue(DateTime.UtcNow, 1));
// NaN -> treated as 1. Buffer: [1, 1]. Result: 2.
var res = conv.Update(new TValue(DateTime.UtcNow, double.NaN));
Assert.Equal(2.0, res.Value);
// 2 -> Buffer: [1, 2]. Result: 3.
res = conv.Update(new TValue(DateTime.UtcNow, 2));
Assert.Equal(3.0, res.Value);
}
[Fact]
public void StaticCalculate_MatchesObjectApi()
{
double[] kernel = [0.5, 1.0];
var source = new TSeries();
source.Add(new TValue(DateTime.UtcNow, 1));
source.Add(new TValue(DateTime.UtcNow, 2));
source.Add(new TValue(DateTime.UtcNow, 3));
source.Add(new TValue(DateTime.UtcNow, 4));
var result = Conv.Batch(source, kernel);
Assert.Equal(1.0, result.Values[0]);
Assert.Equal(2.5, result.Values[1]);
Assert.Equal(4.0, result.Values[2]);
Assert.Equal(5.5, result.Values[3]);
}
[Fact]
public void Reset_ClearsState()
{
double[] kernel = [1.0, 1.0];
var conv = new Conv(kernel);
conv.Update(new TValue(DateTime.UtcNow, 1));
conv.Update(new TValue(DateTime.UtcNow, 2));
Assert.True(conv.IsHot);
conv.Reset();
Assert.False(conv.IsHot);
Assert.Equal(0, conv.Last.Value);
// Should behave as new
var res = conv.Update(new TValue(DateTime.UtcNow, 1));
Assert.Equal(1.0, res.Value);
}
[Fact]
public void LeadingNaN_RemainsNaN()
{
double[] kernel = [1.0];
var conv = new Conv(kernel);
var res = conv.Update(new TValue(DateTime.UtcNow, double.NaN));
Assert.True(double.IsNaN(res.Value));
}
[Fact]
public void IterativeCorrections_RestoreToOriginalState()
{
double[] kernel = [0.5, 1.0];
var conv = new Conv(kernel);
var gbm = new GBM(startPrice: 100.0, mu: 0.02, sigma: 0.1);
// 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);
conv.Update(tenthInput, isNew: true);
}
// Remember state after 10 values
double valueAfterTen = conv.Last.Value;
// Generate 9 corrections with isNew=false (different values)
for (int i = 0; i < 9; i++)
{
var bar = gbm.Next(isNew: false);
conv.Update(new TValue(bar.Time, bar.Close), isNew: false);
}
// Feed the remembered 10th input again with isNew=false
TValue finalValue = conv.Update(tenthInput, isNew: false);
// Should match the original state after 10 values
Assert.Equal(valueAfterTen, finalValue.Value, 1e-9);
}
[Fact]
public void AllModes_ProduceSameResult()
{
// Arrange
double[] kernel = [0.1, 0.2, 0.3, 0.4];
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
var batchSeries = Conv.Batch(series, kernel);
double expected = batchSeries.Last.Value;
// 2. Span Mode
var tValues = series.Values.ToArray();
var spanInput = new ReadOnlySpan<double>(tValues);
var spanOutput = new double[tValues.Length];
Conv.Batch(spanInput, spanOutput, kernel);
double spanResult = spanOutput[^1];
// 3. Streaming Mode
var streamingInd = new Conv(kernel);
for (int i = 0; i < series.Count; i++)
{
streamingInd.Update(series[i]);
}
double streamingResult = streamingInd.Last.Value;
// 4. Eventing Mode
var pubSource = new TSeries();
var eventingInd = new Conv(pubSource, kernel);
for (int i = 0; i < series.Count; i++)
{
pubSource.Add(series[i]);
}
double eventingResult = eventingInd.Last.Value;
// Assert
Assert.Equal(expected, spanResult, 1e-9);
Assert.Equal(expected, streamingResult, 1e-9);
Assert.Equal(expected, eventingResult, 1e-9);
}
[Fact]
public void SpanCalc_ValidatesInput()
{
double[] source = [1, 2, 3, 4, 5];
double[] output = new double[5];
double[] wrongSizeOutput = new double[3];
double[] kernel = [0.5, 0.5];
Assert.Throws<ArgumentException>(() => Conv.Batch(source.AsSpan(), output.AsSpan(), Array.Empty<double>()));
Assert.Throws<ArgumentException>(() => Conv.Batch(source.AsSpan(), wrongSizeOutput.AsSpan(), kernel));
}
[Fact]
public void SpanCalc_HandlesNaN()
{
double[] source = [100, 110, double.NaN, 120, 130];
double[] output = new double[5];
double[] kernel = [0.5, 0.5];
Conv.Batch(source.AsSpan(), output.AsSpan(), kernel);
foreach (var val in output)
{
Assert.True(double.IsFinite(val));
}
}
}
@@ -0,0 +1,210 @@
using QuanTAlib.Tests;
using Skender.Stock.Indicators;
using OoplesFinance.StockIndicators;
using OoplesFinance.StockIndicators.Models;
namespace QuanTAlib;
public sealed class ConvValidationTests : IDisposable
{
private readonly ValidationTestData _testData;
private bool _disposed;
public ConvValidationTests()
{
_testData = new ValidationTestData(count: 10000, seed: 123);
}
public void Dispose()
{
Dispose(true);
}
private void Dispose(bool disposing)
{
if (_disposed)
{
return;
}
_disposed = true;
if (disposing)
{
_testData?.Dispose();
}
}
private static double[] GenerateWmaKernel(int period)
{
double divisor = period * (period + 1) / 2.0;
double[] kernel = new double[period];
for (int i = 0; i < period; i++)
{
kernel[i] = (i + 1) / divisor;
}
return kernel;
}
[Fact]
public void Validate_Against_Sma()
{
// SMA(10) is equivalent to Conv with 10 weights of 1/10
const int period = 10;
double weight = 1.0 / period;
double[] kernel = new double[period];
Array.Fill(kernel, weight);
var sma = new Sma(period);
var conv = new Conv(kernel);
for (int i = 0; i < _testData.Data.Count; i++)
{
var item = _testData.Data[i];
var smaVal = sma.Update(item);
var convVal = conv.Update(item);
if (i >= period) // Skip warmup
{
Assert.Equal(smaVal.Value, convVal.Value, ValidationHelper.DefaultTolerance);
}
}
}
[Fact]
public void Validate_Against_Wma()
{
int period = 10;
double[] kernel = GenerateWmaKernel(period);
var wma = new Wma(period);
var conv = new Conv(kernel);
for (int i = 0; i < _testData.Data.Count; i++)
{
var item = _testData.Data[i];
var wmaVal = wma.Update(item);
var convVal = conv.Update(item);
if (i >= period) // Skip warmup
{
Assert.Equal(wmaVal.Value, convVal.Value, ValidationHelper.DefaultTolerance);
}
}
}
[Fact]
public void Validate_Against_Trima()
{
// TRIMA(10) - Even period
// Weights: 1, 2, 3, 4, 5, 5, 4, 3, 2, 1
// Sum: 30
int period = 10;
double[] kernel = new double[period];
double sum = 0;
// Generate triangular weights
int mid = period / 2;
for (int i = 0; i < period; i++)
{
double val = (i < mid) ? (i + 1) : (period - i);
kernel[i] = val;
sum += val;
}
// Normalize
for (int i = 0; i < period; i++)
{
kernel[i] /= sum;
}
var trima = new Trima(period);
var conv = new Conv(kernel);
for (int i = 0; i < _testData.Data.Count; i++)
{
var item = _testData.Data[i];
var trimaVal = trima.Update(item);
var convVal = conv.Update(item);
if (i >= period) // Skip warmup
{
Assert.Equal(trimaVal.Value, convVal.Value, ValidationHelper.DefaultTolerance);
}
}
}
[Fact]
public void Validate_Against_Skender_Wma()
{
int period = 14;
var skenderWma = _testData.SkenderQuotes.GetWma(period).ToList();
double[] kernel = GenerateWmaKernel(period);
var conv = new Conv(kernel);
var result = conv.Update(_testData.Data);
ValidationHelper.VerifyData(result, skenderWma, (s) => s.Wma, skip: period);
}
[Fact]
public void Validate_Against_TALib_Wma()
{
int period = 14;
double[] input = _testData.Data.Values.ToArray();
double[] output = new double[input.Length];
var retCode = TALib.Functions.Wma<double>(input, 0..^0, output, out var outRange, period);
Assert.Equal(TALib.Core.RetCode.Success, retCode);
double[] kernel = GenerateWmaKernel(period);
var conv = new Conv(kernel);
var result = conv.Update(_testData.Data);
ValidationHelper.VerifyData(result, output, outRange, lookback: period - 1);
}
[Fact]
public void Validate_Against_Tulip_Wma()
{
int period = 14;
double[] input = _testData.Data.Values.ToArray();
var wmaIndicator = Tulip.Indicators.wma;
double[][] inputs = { input };
double[] options = { period };
double[][] outputs = { new double[input.Length - period + 1] };
wmaIndicator.Run(inputs, options, outputs);
double[] output = outputs[0];
double[] kernel = GenerateWmaKernel(period);
var conv = new Conv(kernel);
var result = conv.Update(_testData.Data);
ValidationHelper.VerifyData(result, output, lookback: period - 1);
}
[Fact]
public void Validate_Against_Ooples_Wma()
{
int period = 14;
var ooplesData = _testData.SkenderQuotes.Select(q => new TickerData
{
Date = q.Date,
Open = (double)q.Open,
High = (double)q.High,
Low = (double)q.Low,
Close = (double)q.Close,
Volume = (double)q.Volume
}).ToList();
var stockData = new StockData(ooplesData);
var ooplesWma = stockData.CalculateWeightedMovingAverage(length: period).OutputValues["Wma"];
double[] kernel = GenerateWmaKernel(period);
var conv = new Conv(kernel);
var result = conv.Update(_testData.Data);
ValidationHelper.VerifyData(result, ooplesWma, (s) => s, skip: period, tolerance: ValidationHelper.OoplesTolerance);
}
}