mirror of
https://github.com/mihakralj/QuanTAlib.git
synced 2026-08-20 03:28:05 +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,197 @@
|
||||
using TALib;
|
||||
using Xunit.Abstractions;
|
||||
|
||||
namespace QuanTAlib.Tests;
|
||||
|
||||
public sealed class MidpointValidationTests : IDisposable
|
||||
{
|
||||
private readonly ValidationTestData _testData;
|
||||
private readonly ITestOutputHelper _output;
|
||||
private bool _disposed;
|
||||
|
||||
public MidpointValidationTests(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_Talib_Batch()
|
||||
{
|
||||
int[] periods = { 5, 10, 14, 20, 50 };
|
||||
|
||||
double[] tData = _testData.RawData.ToArray();
|
||||
double[] output = new double[tData.Length];
|
||||
|
||||
foreach (var period in periods)
|
||||
{
|
||||
// Calculate QuanTAlib Midpoint (batch TSeries)
|
||||
var midpoint = new Midpoint(period);
|
||||
var qResult = midpoint.Update(_testData.Data);
|
||||
|
||||
// Calculate TA-Lib MIDPOINT
|
||||
var retCode = TALib.Functions.MidPoint<double>(tData, 0..^0, output, out var outRange, period);
|
||||
Assert.Equal(TALib.Core.RetCode.Success, retCode);
|
||||
|
||||
int lookback = TALib.Functions.MidPointLookback(period);
|
||||
|
||||
// Compare last 100 records
|
||||
ValidationHelper.VerifyData(qResult, output, outRange, lookback);
|
||||
}
|
||||
_output.WriteLine("Midpoint Batch(TSeries) validated successfully against TA-Lib MIDPOINT");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Validate_Talib_Streaming()
|
||||
{
|
||||
int[] periods = { 5, 10, 14, 20, 50 };
|
||||
|
||||
double[] tData = _testData.RawData.ToArray();
|
||||
double[] output = new double[tData.Length];
|
||||
|
||||
foreach (var period in periods)
|
||||
{
|
||||
// Calculate QuanTAlib Midpoint (streaming)
|
||||
var midpoint = new Midpoint(period);
|
||||
var qResults = new List<double>();
|
||||
foreach (var item in _testData.Data)
|
||||
{
|
||||
qResults.Add(midpoint.Update(item).Value);
|
||||
}
|
||||
|
||||
// Calculate TA-Lib MIDPOINT
|
||||
var retCode = TALib.Functions.MidPoint<double>(tData, 0..^0, output, out var outRange, period);
|
||||
Assert.Equal(TALib.Core.RetCode.Success, retCode);
|
||||
|
||||
int lookback = TALib.Functions.MidPointLookback(period);
|
||||
|
||||
// Compare last 100 records
|
||||
ValidationHelper.VerifyData(qResults, output, outRange, lookback);
|
||||
}
|
||||
_output.WriteLine("Midpoint Streaming validated successfully against TA-Lib MIDPOINT");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Validate_Talib_Span()
|
||||
{
|
||||
int[] periods = { 5, 10, 14, 20, 50 };
|
||||
|
||||
double[] sourceData = _testData.RawData.ToArray();
|
||||
double[] talibOutput = new double[sourceData.Length];
|
||||
|
||||
foreach (var period in periods)
|
||||
{
|
||||
// Calculate QuanTAlib Midpoint (Span API)
|
||||
double[] qOutput = new double[sourceData.Length];
|
||||
Midpoint.Batch(sourceData.AsSpan(), qOutput.AsSpan(), period);
|
||||
|
||||
// Calculate TA-Lib MIDPOINT
|
||||
var retCode = TALib.Functions.MidPoint<double>(sourceData, 0..^0, talibOutput, out var outRange, period);
|
||||
Assert.Equal(TALib.Core.RetCode.Success, retCode);
|
||||
|
||||
int lookback = TALib.Functions.MidPointLookback(period);
|
||||
|
||||
// Compare last 100 records
|
||||
ValidationHelper.VerifyData(qOutput, talibOutput, outRange, lookback);
|
||||
}
|
||||
_output.WriteLine("Midpoint Span validated successfully against TA-Lib MIDPOINT");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Validate_KnownValues()
|
||||
{
|
||||
// Test with simple known sequence
|
||||
double[] data = { 1, 5, 3, 8, 2, 9, 4, 7, 6, 10 };
|
||||
int period = 3;
|
||||
|
||||
// For each window:
|
||||
// [1] -> (1+1)/2 = 1
|
||||
// [1,5] -> (5+1)/2 = 3
|
||||
// [1,5,3] -> (5+1)/2 = 3
|
||||
// [5,3,8] -> (8+3)/2 = 5.5
|
||||
// [3,8,2] -> (8+2)/2 = 5
|
||||
// [8,2,9] -> (9+2)/2 = 5.5
|
||||
// [2,9,4] -> (9+2)/2 = 5.5
|
||||
// [9,4,7] -> (9+4)/2 = 6.5
|
||||
// [4,7,6] -> (7+4)/2 = 5.5
|
||||
// [7,6,10] -> (10+6)/2 = 8
|
||||
double[] expected = { 1, 3, 3, 5.5, 5, 5.5, 5.5, 6.5, 5.5, 8 };
|
||||
|
||||
var midpoint = new Midpoint(period);
|
||||
for (int i = 0; i < data.Length; i++)
|
||||
{
|
||||
var result = midpoint.Update(new TValue(DateTime.UtcNow, data[i]));
|
||||
Assert.Equal(expected[i], result.Value, precision: 10);
|
||||
}
|
||||
_output.WriteLine("Midpoint validated with known values");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Validate_ConsistencyBatchStreamingSpan()
|
||||
{
|
||||
// Verify that Batch, Streaming, and Span all produce the same results
|
||||
int period = 14;
|
||||
var gbm = new GBM(42);
|
||||
var bars = gbm.Fetch(100, DateTime.UtcNow.Ticks, TimeSpan.FromMinutes(1));
|
||||
var source = bars.Close;
|
||||
|
||||
// Streaming
|
||||
var streaming = new Midpoint(period);
|
||||
var streamResults = new List<double>();
|
||||
foreach (var item in source)
|
||||
{
|
||||
streamResults.Add(streaming.Update(item).Value);
|
||||
}
|
||||
|
||||
// Batch TSeries
|
||||
var batchResult = Midpoint.Batch(source, period);
|
||||
|
||||
// Span
|
||||
double[] sourceArray = source.Values.ToArray();
|
||||
double[] spanOutput = new double[sourceArray.Length];
|
||||
Midpoint.Batch(sourceArray.AsSpan(), spanOutput.AsSpan(), period);
|
||||
|
||||
for (int i = period; i < source.Count; i++)
|
||||
{
|
||||
Assert.Equal(streamResults[i], batchResult[i].Value, precision: 10);
|
||||
Assert.Equal(streamResults[i], spanOutput[i], precision: 10);
|
||||
}
|
||||
_output.WriteLine("Midpoint consistency validated: Batch == Streaming == Span");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Validate_ConstantInput()
|
||||
{
|
||||
// For constant input, midpoint should equal that constant
|
||||
double constant = 42.5;
|
||||
int period = 10;
|
||||
var midpoint = new Midpoint(period);
|
||||
|
||||
for (int i = 0; i < 50; i++)
|
||||
{
|
||||
var result = midpoint.Update(new TValue(DateTime.UtcNow, constant));
|
||||
Assert.Equal(constant, result.Value, precision: 10);
|
||||
}
|
||||
_output.WriteLine("Midpoint validated with constant input");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user