mirror of
https://github.com/mihakralj/QuanTAlib.git
synced 2026-08-24 05: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,88 @@
|
||||
using TradingPlatform.BusinessLayer;
|
||||
|
||||
namespace QuanTAlib.Tests;
|
||||
|
||||
public class WadIndicatorTests
|
||||
{
|
||||
[Fact]
|
||||
public void WadIndicator_Constructor_SetsDefaults()
|
||||
{
|
||||
var indicator = new WadIndicator();
|
||||
|
||||
Assert.Equal("WAD - Williams Accumulation/Distribution", indicator.Name);
|
||||
Assert.True(indicator.SeparateWindow);
|
||||
Assert.True(indicator.OnBackGround);
|
||||
Assert.Equal(1, WadIndicator.MinHistoryDepths);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void WadIndicator_ShortName_IsCorrect()
|
||||
{
|
||||
var indicator = new WadIndicator();
|
||||
Assert.Equal("WAD", indicator.ShortName);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void WadIndicator_MinHistoryDepths_EqualsOne()
|
||||
{
|
||||
var indicator = new WadIndicator();
|
||||
|
||||
Assert.Equal(1, WadIndicator.MinHistoryDepths);
|
||||
Assert.Equal(1, ((IWatchlistIndicator)indicator).MinHistoryDepths);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void WadIndicator_Initialize_CreatesInternalWad()
|
||||
{
|
||||
var indicator = new WadIndicator();
|
||||
|
||||
// Initialize should not throw
|
||||
indicator.Initialize();
|
||||
|
||||
// After init, line series should exist
|
||||
Assert.Single(indicator.LinesSeries);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void WadIndicator_ProcessUpdate_HistoricalBar_ComputesValue()
|
||||
{
|
||||
var indicator = new WadIndicator();
|
||||
indicator.Initialize();
|
||||
|
||||
// Add historical data
|
||||
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, 1000);
|
||||
|
||||
// Process update for each bar to simulate history loading
|
||||
var args = new UpdateArgs(UpdateReason.HistoricalBar);
|
||||
indicator.ProcessUpdate(args);
|
||||
}
|
||||
|
||||
// Line series should have a value
|
||||
double val = indicator.LinesSeries[0].GetValue(0);
|
||||
Assert.True(double.IsFinite(val));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void WadIndicator_ProcessUpdate_NewBar_ComputesValue()
|
||||
{
|
||||
var indicator = new WadIndicator();
|
||||
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, 1000);
|
||||
}
|
||||
|
||||
indicator.ProcessUpdate(new UpdateArgs(UpdateReason.HistoricalBar));
|
||||
|
||||
// Add new bar
|
||||
indicator.HistoricalData.AddBar(now.AddMinutes(20), 120, 130, 110, 125, 1500);
|
||||
indicator.ProcessUpdate(new UpdateArgs(UpdateReason.NewBar));
|
||||
|
||||
Assert.Equal(2, indicator.LinesSeries[0].Count);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,260 @@
|
||||
namespace QuanTAlib.Tests;
|
||||
|
||||
public class WadTests
|
||||
{
|
||||
[Fact]
|
||||
public void Wad_BasicCalculation_ReturnsExpectedValues()
|
||||
{
|
||||
// Arrange
|
||||
var wad = new Wad();
|
||||
var time = DateTime.UtcNow;
|
||||
|
||||
// Bar 1: First bar, WAD = 0 (no previous close)
|
||||
var bar1 = new TBar(time, 100, 105, 95, 100, 1000);
|
||||
var val1 = wad.Update(bar1);
|
||||
Assert.Equal(0, val1.Value);
|
||||
|
||||
// Bar 2: Close=110 > PrevClose=100, TrueLow = min(92, 100) = 92
|
||||
// PM = 110 - 92 = 18, Vol = 2000
|
||||
// AD = 18 * 2000 = 36000, WAD = 0 + 36000 = 36000
|
||||
var bar2 = new TBar(time.AddMinutes(1), 100, 115, 92, 110, 2000);
|
||||
var val2 = wad.Update(bar2);
|
||||
Assert.Equal(36000, val2.Value);
|
||||
|
||||
// Bar 3: Close=105 < PrevClose=110, TrueHigh = max(108, 110) = 110
|
||||
// PM = 105 - 110 = -5, Vol = 1500
|
||||
// AD = -5 * 1500 = -7500, WAD = 36000 - 7500 = 28500
|
||||
var bar3 = new TBar(time.AddMinutes(2), 110, 108, 102, 105, 1500);
|
||||
var val3 = wad.Update(bar3);
|
||||
Assert.Equal(28500, val3.Value);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Wad_CloseUnchanged_ZeroPriceMovement()
|
||||
{
|
||||
var wad = new Wad();
|
||||
var time = DateTime.UtcNow;
|
||||
|
||||
// Bar 1
|
||||
var bar1 = new TBar(time, 100, 105, 95, 100, 1000);
|
||||
wad.Update(bar1);
|
||||
|
||||
// Bar 2: Close=100 == PrevClose=100 -> PM = 0
|
||||
var bar2 = new TBar(time.AddMinutes(1), 100, 110, 90, 100, 2000);
|
||||
var val2 = wad.Update(bar2);
|
||||
Assert.Equal(0, val2.Value);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Wad_IsNew_False_UpdatesSameBar()
|
||||
{
|
||||
var wad = new Wad();
|
||||
var time = DateTime.UtcNow;
|
||||
|
||||
// Initial bar
|
||||
var bar1 = new TBar(time, 100, 105, 95, 100, 1000);
|
||||
wad.Update(bar1, isNew: true);
|
||||
Assert.Equal(0, wad.Last.Value);
|
||||
|
||||
// Bar 2: Close=110 > PrevClose=100
|
||||
var bar2 = new TBar(time.AddMinutes(1), 100, 115, 92, 110, 2000);
|
||||
wad.Update(bar2, isNew: true);
|
||||
Assert.Equal(36000, wad.Last.Value);
|
||||
|
||||
// Update same bar with different data (isNew=false)
|
||||
// Close=108 > PrevClose=100, TrueLow = min(92, 100) = 92
|
||||
// PM = 108 - 92 = 16, Vol = 1000
|
||||
// AD = 16 * 1000 = 16000, WAD = 0 + 16000 = 16000
|
||||
var bar2Update = new TBar(time.AddMinutes(1), 100, 115, 92, 108, 1000);
|
||||
wad.Update(bar2Update, isNew: false);
|
||||
Assert.Equal(16000, wad.Last.Value);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Wad_Reset_ClearsState()
|
||||
{
|
||||
var wad = new Wad();
|
||||
var time = DateTime.UtcNow;
|
||||
|
||||
var bar1 = new TBar(time, 100, 105, 95, 100, 1000);
|
||||
wad.Update(bar1);
|
||||
var bar2 = new TBar(time.AddMinutes(1), 100, 115, 92, 110, 2000);
|
||||
wad.Update(bar2);
|
||||
|
||||
Assert.True(wad.IsHot);
|
||||
Assert.NotEqual(0, wad.Last.Value);
|
||||
|
||||
wad.Reset();
|
||||
Assert.False(wad.IsHot);
|
||||
Assert.Equal(0, wad.Last.Value);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Wad_TValueUpdate_ThrowsNotSupportedException()
|
||||
{
|
||||
var wad = new Wad();
|
||||
var bar = new TBar(DateTime.UtcNow, 100, 105, 95, 100, 1000);
|
||||
wad.Update(bar);
|
||||
|
||||
Assert.Throws<NotSupportedException>(() => wad.Update(new TValue(DateTime.UtcNow, 15)));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Wad_Name_IsCorrect()
|
||||
{
|
||||
Assert.Equal("WAD", Wad.Name);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Wad_PubEvent_FiresOnUpdate()
|
||||
{
|
||||
var wad = new Wad();
|
||||
bool eventFired = false;
|
||||
wad.Pub += (object? sender, in TValueEventArgs args) => eventFired = true;
|
||||
|
||||
wad.Update(new TBar(DateTime.UtcNow, 100, 105, 95, 100, 1000));
|
||||
Assert.True(eventFired);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Wad_UpdateTBarSeries_ReturnsCorrectSeries()
|
||||
{
|
||||
var wad = new Wad();
|
||||
var bars = new TBarSeries();
|
||||
var time = DateTime.UtcNow;
|
||||
|
||||
bars.Add(new TBar(time, 100, 105, 95, 100, 1000));
|
||||
bars.Add(new TBar(time.AddMinutes(1), 100, 115, 92, 110, 2000));
|
||||
bars.Add(new TBar(time.AddMinutes(2), 110, 108, 102, 105, 1500));
|
||||
|
||||
var result = wad.Update(bars);
|
||||
|
||||
Assert.Equal(3, result.Count);
|
||||
Assert.Equal(0, result[0].Value);
|
||||
Assert.Equal(36000, result[1].Value);
|
||||
Assert.Equal(28500, result[2].Value);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Wad_CalculateTBarSeries_ReturnsCorrectSeries()
|
||||
{
|
||||
var bars = new TBarSeries();
|
||||
var time = DateTime.UtcNow;
|
||||
|
||||
bars.Add(new TBar(time, 100, 105, 95, 100, 1000));
|
||||
bars.Add(new TBar(time.AddMinutes(1), 100, 115, 92, 110, 2000));
|
||||
bars.Add(new TBar(time.AddMinutes(2), 110, 108, 102, 105, 1500));
|
||||
|
||||
var result = Wad.Batch(bars);
|
||||
|
||||
Assert.Equal(3, result.Count);
|
||||
Assert.Equal(0, result[0].Value);
|
||||
Assert.Equal(36000, result[1].Value);
|
||||
Assert.Equal(28500, result[2].Value);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Wad_CalculateSpan_ReturnsCorrectValues()
|
||||
{
|
||||
double[] high = { 105, 115, 108 };
|
||||
double[] low = { 95, 92, 102 };
|
||||
double[] close = { 100, 110, 105 };
|
||||
double[] volume = { 1000, 2000, 1500 };
|
||||
double[] output = new double[3];
|
||||
|
||||
Wad.Batch(high, low, close, volume, output);
|
||||
|
||||
Assert.Equal(0, output[0]);
|
||||
Assert.Equal(36000, output[1]);
|
||||
Assert.Equal(28500, output[2]);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Wad_CalculateSpan_ThrowsOnMismatchedLengths()
|
||||
{
|
||||
double[] high = { 105, 115 };
|
||||
double[] low = { 95, 92 };
|
||||
double[] close = { 100, 110 };
|
||||
double[] volume = { 1000 }; // Short
|
||||
double[] output = new double[2];
|
||||
|
||||
Assert.Throws<ArgumentException>(() =>
|
||||
Wad.Batch(high, low, close, volume, output));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Wad_Calculate_EmptySeries_ReturnsEmpty()
|
||||
{
|
||||
var bars = new TBarSeries();
|
||||
var result = Wad.Batch(bars);
|
||||
Assert.Empty(result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Wad_CalculateSpan_LargeDataset()
|
||||
{
|
||||
const int count = 1000;
|
||||
double[] high = new double[count];
|
||||
double[] low = new double[count];
|
||||
double[] close = new double[count];
|
||||
double[] volume = new double[count];
|
||||
double[] output = new double[count];
|
||||
|
||||
// Setup: Ascending close pattern
|
||||
for (int i = 0; i < count; i++)
|
||||
{
|
||||
close[i] = 100 + i;
|
||||
high[i] = close[i] + 5;
|
||||
low[i] = close[i] - 5;
|
||||
volume[i] = 100;
|
||||
}
|
||||
|
||||
Wad.Batch(high, low, close, volume, output);
|
||||
|
||||
// First bar should be 0
|
||||
Assert.Equal(0, output[0]);
|
||||
|
||||
// All subsequent bars should have positive accumulation since close is always rising
|
||||
for (int i = 1; i < count; i++)
|
||||
{
|
||||
Assert.True(output[i] > output[i - 1], $"WAD should increase at index {i}");
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Wad_StreamingMatchesBatch()
|
||||
{
|
||||
var bars = new TBarSeries();
|
||||
var gbm = new GBM();
|
||||
|
||||
// Generate bars using GBM
|
||||
for (int i = 0; i < 100; i++)
|
||||
{
|
||||
bars.Add(gbm.Next());
|
||||
}
|
||||
|
||||
// Batch calculation
|
||||
var batchResult = Wad.Batch(bars);
|
||||
|
||||
// Streaming calculation
|
||||
var wad = new Wad();
|
||||
var streamingResult = wad.Update(bars);
|
||||
|
||||
// Compare results
|
||||
Assert.Equal(batchResult.Count, streamingResult.Count);
|
||||
for (int i = 0; i < batchResult.Count; i++)
|
||||
{
|
||||
Assert.Equal(batchResult[i].Value, streamingResult[i].Value, precision: 10);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Wad_IsHot_BecomesTrue_AfterFirstBar()
|
||||
{
|
||||
var wad = new Wad();
|
||||
Assert.False(wad.IsHot);
|
||||
|
||||
wad.Update(new TBar(DateTime.UtcNow, 100, 105, 95, 100, 1000));
|
||||
Assert.True(wad.IsHot);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,154 @@
|
||||
using Xunit.Abstractions;
|
||||
|
||||
namespace QuanTAlib.Tests;
|
||||
|
||||
/// <summary>
|
||||
/// Williams Accumulation/Distribution validation tests.
|
||||
/// Cross-validated against: Tulip (wad).
|
||||
/// Skender, TA-Lib, and Ooples do not have WAD implementations.
|
||||
///
|
||||
/// NOTE: QuanTAlib WAD = cumulative sum(PM × Volume) — volume-weighted.
|
||||
/// Tulip WAD = cumulative sum(PM) — NOT volume-weighted.
|
||||
/// Direct value comparison is not possible due to this formula difference.
|
||||
/// Instead, we verify bar-over-bar directional agreement (both should trend
|
||||
/// in the same direction when only price movement drives the delta).
|
||||
/// </summary>
|
||||
public sealed class WadValidationTests : IDisposable
|
||||
{
|
||||
private readonly ValidationTestData _data;
|
||||
private readonly ITestOutputHelper _output;
|
||||
|
||||
public WadValidationTests(ITestOutputHelper output)
|
||||
{
|
||||
_data = new ValidationTestData();
|
||||
_output = output;
|
||||
}
|
||||
|
||||
public void Dispose() { /* nothing to dispose */ }
|
||||
|
||||
#region Tulip Cross Validation Tests
|
||||
|
||||
[Fact]
|
||||
public void Validate_Tulip_WAD()
|
||||
{
|
||||
// Tulip wad: inputs={high, low, close}, options={}, outputs={wad}
|
||||
// Tulip WAD computes WAD = cumulative(PM) without volume weighting
|
||||
// QuanTAlib WAD computes WAD = cumulative(PM × Volume)
|
||||
// Since volume is always positive, PM sign is identical so
|
||||
// bar-over-bar changes should have the same SIGN.
|
||||
var high = _data.Bars.High.Values.ToArray();
|
||||
var low = _data.Bars.Low.Values.ToArray();
|
||||
var close = _data.Bars.Close.Values.ToArray();
|
||||
|
||||
var tulipIndicator = Tulip.Indicators.wad;
|
||||
double[][] inputs = { high, low, close };
|
||||
double[] options = Array.Empty<double>();
|
||||
double[][] outputs = { new double[high.Length] };
|
||||
|
||||
tulipIndicator.Run(inputs, options, outputs);
|
||||
double[] tResult = outputs[0];
|
||||
int lookback = tulipIndicator.Start(options);
|
||||
|
||||
// QuanTAlib WAD
|
||||
var wad = new Wad();
|
||||
var qValues = new double[_data.Bars.Count];
|
||||
int idx = 0;
|
||||
foreach (var bar in _data.Bars)
|
||||
{
|
||||
qValues[idx++] = wad.Update(bar).Value;
|
||||
}
|
||||
|
||||
_output.WriteLine($"Tulip WAD lookback: {lookback}, output length: {tResult.Length}");
|
||||
_output.WriteLine($"Tulip first 5: {string.Join(", ", tResult.Take(5).Select(v => v.ToString("F4", System.Globalization.CultureInfo.InvariantCulture)))}");
|
||||
_output.WriteLine($"QuanTAlib first 5: {string.Join(", ", qValues.Skip(lookback + 1).Take(5).Select(v => v.ToString("F4", System.Globalization.CultureInfo.InvariantCulture)))}");
|
||||
|
||||
// Compare bar-over-bar sign agreement
|
||||
// When Tulip WAD delta > 0 (accumulation), QuanTAlib WAD delta should also be > 0
|
||||
int compared = 0;
|
||||
int agreed = 0;
|
||||
int startIdx = lookback + 3; // skip initial convergence
|
||||
|
||||
for (int i = startIdx; i < qValues.Length && (i - lookback) < tResult.Length; i++)
|
||||
{
|
||||
int tIdx = i - lookback;
|
||||
if (tIdx < 1)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
double qDelta = qValues[i] - qValues[i - 1];
|
||||
double tDelta = tResult[tIdx] - tResult[tIdx - 1];
|
||||
|
||||
// Skip near-zero deltas (ambiguous direction)
|
||||
if (Math.Abs(tDelta) < 1e-10 || Math.Abs(qDelta) < 1e-10)
|
||||
{
|
||||
compared++;
|
||||
agreed++;
|
||||
continue;
|
||||
}
|
||||
|
||||
compared++;
|
||||
if (Math.Sign(qDelta) == Math.Sign(tDelta))
|
||||
{
|
||||
agreed++;
|
||||
}
|
||||
}
|
||||
|
||||
double agreementRate = compared > 0 ? (double)agreed / compared : 0;
|
||||
_output.WriteLine($"Tulip WAD directional agreement: {agreed}/{compared} = {agreementRate:P1}");
|
||||
|
||||
// Both formulas use the same PM (price movement) sign, so direction should match strongly
|
||||
// Volume only scales the magnitude, not the direction
|
||||
Assert.True(agreementRate > 0.95,
|
||||
$"WAD directional agreement should exceed 95%, got {agreementRate:P1} ({agreed}/{compared})");
|
||||
Assert.True(compared > 100, $"Should compare at least 100 values, got {compared}");
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
[Fact]
|
||||
public void Wad_BatchMatchesStreaming()
|
||||
{
|
||||
// Batch calculation
|
||||
var batchResult = Wad.Batch(_data.Bars);
|
||||
|
||||
// Streaming calculation
|
||||
var wad = new Wad();
|
||||
var streamingResult = wad.Update(_data.Bars);
|
||||
|
||||
// Compare all values
|
||||
Assert.Equal(batchResult.Count, streamingResult.Count);
|
||||
for (int i = 0; i < batchResult.Count; i++)
|
||||
{
|
||||
Assert.Equal(batchResult[i].Value, streamingResult[i].Value, precision: 10);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Wad_SpanMatchesStreaming()
|
||||
{
|
||||
var high = _data.Bars.High.Values.ToArray();
|
||||
var low = _data.Bars.Low.Values.ToArray();
|
||||
var close = _data.Bars.Close.Values.ToArray();
|
||||
var volume = _data.Bars.Volume.Values.ToArray();
|
||||
var spanOutput = new double[high.Length];
|
||||
|
||||
// Span calculation
|
||||
Wad.Batch(high, low, close, volume, spanOutput);
|
||||
|
||||
// Streaming calculation
|
||||
var wad = new Wad();
|
||||
var streamingValues = new List<double>();
|
||||
foreach (var bar in _data.Bars)
|
||||
{
|
||||
streamingValues.Add(wad.Update(bar).Value);
|
||||
}
|
||||
|
||||
// Compare all values
|
||||
Assert.Equal(spanOutput.Length, streamingValues.Count);
|
||||
for (int i = 0; i < spanOutput.Length; i++)
|
||||
{
|
||||
Assert.Equal(spanOutput[i], streamingValues[i], precision: 10);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user