mirror of
https://github.com/mihakralj/QuanTAlib.git
synced 2026-08-25 05:48:06 +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,164 @@
|
||||
using TradingPlatform.BusinessLayer;
|
||||
|
||||
namespace QuanTAlib.Tests;
|
||||
|
||||
public class VwadIndicatorTests
|
||||
{
|
||||
[Fact]
|
||||
public void VwadIndicator_Constructor_SetsDefaults()
|
||||
{
|
||||
var indicator = new VwadIndicator();
|
||||
|
||||
Assert.Equal("VWAD - Volume Weighted Accumulation/Distribution", indicator.Name);
|
||||
Assert.Equal(20, indicator.Period);
|
||||
Assert.True(indicator.SeparateWindow);
|
||||
Assert.True(indicator.OnBackGround);
|
||||
Assert.Equal(20, indicator.MinHistoryDepths);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void VwadIndicator_ShortName_ReflectsPeriod()
|
||||
{
|
||||
var indicator = new VwadIndicator { Period = 14 };
|
||||
Assert.Equal("VWAD(14)", indicator.ShortName);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void VwadIndicator_MinHistoryDepths_EqualsDefault()
|
||||
{
|
||||
var indicator = new VwadIndicator();
|
||||
|
||||
Assert.Equal(20, indicator.MinHistoryDepths);
|
||||
Assert.Equal(20, ((IWatchlistIndicator)indicator).MinHistoryDepths);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void VwadIndicator_Initialize_CreatesInternalVwad()
|
||||
{
|
||||
var indicator = new VwadIndicator();
|
||||
|
||||
// Initialize should not throw
|
||||
indicator.Initialize();
|
||||
|
||||
// After init, line series should exist
|
||||
Assert.Single(indicator.LinesSeries);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void VwadIndicator_ProcessUpdate_HistoricalBar_ComputesValue()
|
||||
{
|
||||
var indicator = new VwadIndicator();
|
||||
indicator.Initialize();
|
||||
|
||||
// Add historical data
|
||||
var now = DateTime.UtcNow;
|
||||
for (int i = 0; i < 30; 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 VwadIndicator_ProcessUpdate_NewBar_ComputesValue()
|
||||
{
|
||||
var indicator = new VwadIndicator();
|
||||
indicator.Initialize();
|
||||
|
||||
var now = DateTime.UtcNow;
|
||||
for (int i = 0; i < 30; 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(30), 130, 140, 120, 135, 1500);
|
||||
indicator.ProcessUpdate(new UpdateArgs(UpdateReason.NewBar));
|
||||
|
||||
Assert.Equal(2, indicator.LinesSeries[0].Count);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void VwadIndicator_Value_IsCumulative()
|
||||
{
|
||||
var indicator = new VwadIndicator();
|
||||
indicator.Initialize();
|
||||
|
||||
var now = DateTime.UtcNow;
|
||||
var values = new List<double>();
|
||||
|
||||
for (int i = 0; i < 50; i++)
|
||||
{
|
||||
// Create varying price patterns
|
||||
double open = 100 + i;
|
||||
double high = open + 10 + (i % 5);
|
||||
double low = open - 5;
|
||||
double close = (i % 2 == 0) ? high - 1 : low + 1; // Alternate high/low closes
|
||||
double volume = 1000 + (i * 100);
|
||||
|
||||
indicator.HistoricalData.AddBar(now.AddMinutes(i), open, high, low, close, volume);
|
||||
indicator.ProcessUpdate(new UpdateArgs(UpdateReason.HistoricalBar));
|
||||
|
||||
if (i > 0)
|
||||
{
|
||||
double val = indicator.LinesSeries[0].GetValue(0);
|
||||
values.Add(val);
|
||||
}
|
||||
}
|
||||
|
||||
// VWAD is cumulative and unbounded - values should change over time
|
||||
Assert.True(values.Count > 0, "Should have recorded values");
|
||||
|
||||
// Check that values are changing (not all the same)
|
||||
int changeCount = 0;
|
||||
for (int i = 1; i < values.Count; i++)
|
||||
{
|
||||
if (Math.Abs(values[i] - values[i - 1]) > 1e-10)
|
||||
{
|
||||
changeCount++;
|
||||
}
|
||||
}
|
||||
Assert.True(changeCount > values.Count / 2, "VWAD values should change for most bars");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void VwadIndicator_DifferentPeriods_ProduceDifferentResults()
|
||||
{
|
||||
var indicator10 = new VwadIndicator { Period = 10 };
|
||||
var indicator20 = new VwadIndicator { Period = 20 };
|
||||
|
||||
indicator10.Initialize();
|
||||
indicator20.Initialize();
|
||||
|
||||
var now = DateTime.UtcNow;
|
||||
for (int i = 0; i < 50; i++)
|
||||
{
|
||||
double open = 100 + i;
|
||||
double high = open + 10;
|
||||
double low = open - 5;
|
||||
double close = open + 5;
|
||||
double volume = 1000 + (i * 50);
|
||||
|
||||
indicator10.HistoricalData.AddBar(now.AddMinutes(i), open, high, low, close, volume);
|
||||
indicator20.HistoricalData.AddBar(now.AddMinutes(i), open, high, low, close, volume);
|
||||
|
||||
indicator10.ProcessUpdate(new UpdateArgs(UpdateReason.HistoricalBar));
|
||||
indicator20.ProcessUpdate(new UpdateArgs(UpdateReason.HistoricalBar));
|
||||
}
|
||||
|
||||
double val10 = indicator10.LinesSeries[0].GetValue(0);
|
||||
double val20 = indicator20.LinesSeries[0].GetValue(0);
|
||||
|
||||
// Different periods should produce different results
|
||||
Assert.NotEqual(val10, val20, 6);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,429 @@
|
||||
namespace QuanTAlib.Tests;
|
||||
|
||||
public class VwadTests
|
||||
{
|
||||
[Fact]
|
||||
public void Vwad_Constructor_DefaultPeriod_Is20()
|
||||
{
|
||||
var vwad = new Vwad();
|
||||
Assert.Equal("VWAD(20)", vwad.Name);
|
||||
Assert.Equal(20, vwad.WarmupPeriod);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Vwad_Constructor_CustomPeriod_SetsCorrectly()
|
||||
{
|
||||
var vwad = new Vwad(10);
|
||||
Assert.Equal("VWAD(10)", vwad.Name);
|
||||
Assert.Equal(10, vwad.WarmupPeriod);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Vwad_Constructor_InvalidPeriod_ThrowsArgumentException()
|
||||
{
|
||||
var ex = Assert.Throws<ArgumentException>(() => new Vwad(0));
|
||||
Assert.Equal("period", ex.ParamName);
|
||||
|
||||
ex = Assert.Throws<ArgumentException>(() => new Vwad(-1));
|
||||
Assert.Equal("period", ex.ParamName);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Vwad_BasicCalculation_ReturnsExpectedValues()
|
||||
{
|
||||
// VWAD with period 3 for easy manual verification
|
||||
var vwad = new Vwad(3);
|
||||
var time = DateTime.UtcNow;
|
||||
|
||||
// Bar 1: Close=10, High=12, Low=8. Range=4.
|
||||
// MFM = ((10-8) - (12-10)) / 4 = (2 - 2) / 4 = 0
|
||||
// Vol = 100. SumVol = 100. VolWeight = 100/100 = 1
|
||||
// WeightedMFV = 100 * 0 * 1 = 0
|
||||
// VWAD = 0
|
||||
var bar1 = new TBar(time, 10, 12, 8, 10, 100);
|
||||
var val1 = vwad.Update(bar1);
|
||||
Assert.Equal(0, val1.Value);
|
||||
|
||||
// Bar 2: Close=12, High=12, Low=8. Range=4.
|
||||
// MFM = ((12-8) - (12-12)) / 4 = (4 - 0) / 4 = 1
|
||||
// Vol = 200. SumVol = 100 + 200 = 300. VolWeight = 200/300 = 0.6667
|
||||
// WeightedMFV = 200 * 1 * 0.6667 = 133.33
|
||||
// VWAD = 0 + 133.33 = 133.33
|
||||
var bar2 = new TBar(time.AddMinutes(1), 10, 12, 8, 12, 200);
|
||||
var val2 = vwad.Update(bar2);
|
||||
double expectedMfv2 = 200.0 * 1.0 * (200.0 / 300.0);
|
||||
Assert.Equal(expectedMfv2, val2.Value, 6);
|
||||
|
||||
// Bar 3: Close=8, High=12, Low=8. Range=4.
|
||||
// MFM = ((8-8) - (12-8)) / 4 = (0 - 4) / 4 = -1
|
||||
// Vol = 100. SumVol = 100 + 200 + 100 = 400. VolWeight = 100/400 = 0.25
|
||||
// WeightedMFV = 100 * (-1) * 0.25 = -25
|
||||
// VWAD = 133.33 + (-25) = 108.33
|
||||
var bar3 = new TBar(time.AddMinutes(2), 12, 12, 8, 8, 100);
|
||||
var val3 = vwad.Update(bar3);
|
||||
double expectedMfv3 = 100.0 * (-1.0) * (100.0 / 400.0);
|
||||
Assert.Equal(expectedMfv2 + expectedMfv3, val3.Value, 6);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Vwad_RollingSumDropsOldestValue()
|
||||
{
|
||||
var vwad = new Vwad(2);
|
||||
var time = DateTime.UtcNow;
|
||||
|
||||
// Bar 1: MFM=1, Vol=100
|
||||
var bar1 = new TBar(time, 10, 12, 8, 12, 100);
|
||||
vwad.Update(bar1);
|
||||
|
||||
// Bar 2: MFM=-1, Vol=100
|
||||
var bar2 = new TBar(time.AddMinutes(1), 12, 12, 8, 8, 100);
|
||||
vwad.Update(bar2);
|
||||
|
||||
// Bar 3: MFM=1, Vol=100
|
||||
// Period=2, so bar1 drops out of volume sum
|
||||
// SumVol = 100 + 100 = 200 (bar2 + bar3)
|
||||
var bar3 = new TBar(time.AddMinutes(2), 8, 12, 8, 12, 100);
|
||||
var val3 = vwad.Update(bar3);
|
||||
|
||||
// VWAD should continue accumulating
|
||||
Assert.True(double.IsFinite(val3.Value));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Vwad_IsNew_False_UpdatesSameBar()
|
||||
{
|
||||
var vwad = new Vwad(3);
|
||||
var time = DateTime.UtcNow;
|
||||
|
||||
// Initial update: MFM = 1, Vol = 100
|
||||
var bar1 = new TBar(time, 10, 12, 8, 12, 100);
|
||||
vwad.Update(bar1, isNew: true);
|
||||
double value1 = vwad.Last.Value;
|
||||
|
||||
// Update same bar with different volume
|
||||
var bar1Update = new TBar(time, 10, 12, 8, 12, 200);
|
||||
vwad.Update(bar1Update, isNew: false);
|
||||
double value2 = vwad.Last.Value;
|
||||
|
||||
// Values should differ because volume weight changed
|
||||
Assert.NotEqual(value1, value2);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Vwad_IterativeCorrections_RestoreState()
|
||||
{
|
||||
var vwad = new Vwad(3);
|
||||
var time = DateTime.UtcNow;
|
||||
|
||||
// Build up some state
|
||||
vwad.Update(new TBar(time, 10, 12, 8, 12, 100), isNew: true);
|
||||
vwad.Update(new TBar(time.AddMinutes(1), 10, 12, 8, 10, 100), isNew: true);
|
||||
|
||||
// Add bar 3 and record state
|
||||
var bar3 = new TBar(time.AddMinutes(2), 10, 12, 8, 11, 100);
|
||||
vwad.Update(bar3, isNew: true);
|
||||
double valueAfterBar3 = vwad.Last.Value;
|
||||
|
||||
// Multiple corrections to bar 3
|
||||
vwad.Update(new TBar(time.AddMinutes(2), 10, 12, 8, 8, 100), isNew: false);
|
||||
vwad.Update(new TBar(time.AddMinutes(2), 10, 12, 8, 9, 100), isNew: false);
|
||||
vwad.Update(new TBar(time.AddMinutes(2), 10, 12, 8, 12, 100), isNew: false);
|
||||
|
||||
// Restore original bar 3
|
||||
vwad.Update(bar3, isNew: false);
|
||||
|
||||
// Should match original state after bar 3
|
||||
Assert.Equal(valueAfterBar3, vwad.Last.Value, 10);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Vwad_Reset_ClearsState()
|
||||
{
|
||||
var vwad = new Vwad(3);
|
||||
var bar = new TBar(DateTime.UtcNow, 10, 12, 8, 12, 100);
|
||||
vwad.Update(bar);
|
||||
|
||||
Assert.NotEqual(0, vwad.Last.Value);
|
||||
|
||||
vwad.Reset();
|
||||
Assert.False(vwad.IsHot);
|
||||
Assert.Equal(0, vwad.Last.Value);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Vwad_IsHot_TrueAfterFirstBar()
|
||||
{
|
||||
var vwad = new Vwad(3);
|
||||
var time = DateTime.UtcNow;
|
||||
|
||||
Assert.False(vwad.IsHot);
|
||||
|
||||
vwad.Update(new TBar(time, 10, 12, 8, 10, 100));
|
||||
Assert.True(vwad.IsHot);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Vwad_HighEqualsLow_HandlesDivisionByZero()
|
||||
{
|
||||
var vwad = new Vwad(3);
|
||||
// High = Low = 10. Range = 0. MFM should be 0.
|
||||
var bar = new TBar(DateTime.UtcNow, 10, 10, 10, 10, 100);
|
||||
var val = vwad.Update(bar);
|
||||
Assert.Equal(0, val.Value);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Vwad_ZeroVolume_HandlesDivisionByZero()
|
||||
{
|
||||
var vwad = new Vwad(3);
|
||||
var bar = new TBar(DateTime.UtcNow, 10, 12, 8, 10, 0);
|
||||
var val = vwad.Update(bar);
|
||||
Assert.Equal(0, val.Value); // 0 volume weight = 0 contribution
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Vwad_TValueUpdate_ThrowsNotSupportedException()
|
||||
{
|
||||
var vwad = new Vwad();
|
||||
Assert.Throws<NotSupportedException>(() => vwad.Update(new TValue(DateTime.UtcNow, 15)));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Vwad_PubEvent_FiresOnUpdate()
|
||||
{
|
||||
var vwad = new Vwad();
|
||||
bool eventFired = false;
|
||||
vwad.Pub += (object? sender, in TValueEventArgs args) => eventFired = true;
|
||||
|
||||
vwad.Update(new TBar(DateTime.UtcNow, 10, 12, 8, 10, 100));
|
||||
Assert.True(eventFired);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Vwad_UpdateTBarSeries_ReturnsCorrectSeries()
|
||||
{
|
||||
var vwad = new Vwad(3);
|
||||
var bars = new TBarSeries();
|
||||
var time = DateTime.UtcNow;
|
||||
|
||||
bars.Add(new TBar(time, 10, 12, 8, 10, 100));
|
||||
bars.Add(new TBar(time.AddMinutes(1), 10, 12, 8, 12, 200));
|
||||
bars.Add(new TBar(time.AddMinutes(2), 12, 12, 8, 8, 100));
|
||||
|
||||
var result = vwad.Update(bars);
|
||||
|
||||
Assert.Equal(3, result.Count);
|
||||
Assert.True(double.IsFinite(result[0].Value));
|
||||
Assert.True(double.IsFinite(result[1].Value));
|
||||
Assert.True(double.IsFinite(result[2].Value));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Vwad_CalculateTBarSeries_ReturnsCorrectSeries()
|
||||
{
|
||||
var bars = new TBarSeries();
|
||||
var time = DateTime.UtcNow;
|
||||
|
||||
bars.Add(new TBar(time, 10, 12, 8, 10, 100));
|
||||
bars.Add(new TBar(time.AddMinutes(1), 10, 12, 8, 12, 200));
|
||||
bars.Add(new TBar(time.AddMinutes(2), 12, 12, 8, 8, 100));
|
||||
|
||||
var result = Vwad.Batch(bars, 3);
|
||||
|
||||
Assert.Equal(3, result.Count);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Vwad_CalculateSpan_ReturnsCorrectValues()
|
||||
{
|
||||
double[] high = [12, 12, 12];
|
||||
double[] low = [8, 8, 8];
|
||||
double[] close = [10, 12, 8]; // MFM: 0, 1, -1
|
||||
double[] volume = [100, 200, 100];
|
||||
double[] output = new double[3];
|
||||
|
||||
Vwad.Batch(high, low, close, volume, output, 3);
|
||||
|
||||
// Bar 0: MFM=0, Vol=100, SumVol=100, VolWeight=1, WeightedMFV=0
|
||||
Assert.Equal(0, output[0]);
|
||||
|
||||
// Bar 1: MFM=1, Vol=200, SumVol=300, VolWeight=200/300
|
||||
// WeightedMFV = 200 * 1 * (200/300) = 133.33
|
||||
double expectedBar1 = 200.0 * 1.0 * (200.0 / 300.0);
|
||||
Assert.Equal(expectedBar1, output[1], 6);
|
||||
|
||||
// Bar 2: MFM=-1, Vol=100, SumVol=400, VolWeight=100/400
|
||||
// WeightedMFV = 100 * (-1) * (100/400) = -25
|
||||
double expectedBar2 = expectedBar1 + (100.0 * (-1.0) * (100.0 / 400.0));
|
||||
Assert.Equal(expectedBar2, output[2], 6);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Vwad_CalculateSpan_ThrowsOnMismatchedLengths()
|
||||
{
|
||||
double[] high = [10, 11];
|
||||
double[] low = [9, 10];
|
||||
double[] close = [9.5, 10.5];
|
||||
double[] volume = [100]; // Short
|
||||
double[] output = new double[2];
|
||||
|
||||
Assert.Throws<ArgumentException>(() =>
|
||||
Vwad.Batch(high, low, close, volume, output, 3));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Vwad_CalculateSpan_ThrowsOnInvalidPeriod()
|
||||
{
|
||||
double[] high = [10];
|
||||
double[] low = [9];
|
||||
double[] close = [9.5];
|
||||
double[] volume = [100];
|
||||
double[] output = new double[1];
|
||||
|
||||
Assert.Throws<ArgumentException>(() =>
|
||||
Vwad.Batch(high, low, close, volume, output, 0));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Vwad_Calculate_EmptySeries_ReturnsEmpty()
|
||||
{
|
||||
var bars = new TBarSeries();
|
||||
var result = Vwad.Batch(bars);
|
||||
Assert.Empty(result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Vwad_StreamingMatchesBatch()
|
||||
{
|
||||
var bars = new TBarSeries();
|
||||
var gbm = new GBM(seed: 42);
|
||||
|
||||
for (int i = 0; i < 100; i++)
|
||||
{
|
||||
bars.Add(gbm.Next());
|
||||
}
|
||||
|
||||
// Streaming
|
||||
var vwadStreaming = new Vwad(20);
|
||||
var streamingValues = new List<double>();
|
||||
foreach (var bar in bars)
|
||||
{
|
||||
streamingValues.Add(vwadStreaming.Update(bar).Value);
|
||||
}
|
||||
|
||||
// Batch
|
||||
var batchResult = Vwad.Batch(bars, 20);
|
||||
|
||||
// Compare all values
|
||||
for (int i = 0; i < 100; i++)
|
||||
{
|
||||
Assert.Equal(batchResult[i].Value, streamingValues[i], 9);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Vwad_NaN_Input_UsesLastValidValue()
|
||||
{
|
||||
var vwad = new Vwad(5);
|
||||
var time = DateTime.UtcNow;
|
||||
|
||||
// Feed some valid values
|
||||
vwad.Update(new TBar(time, 10, 12, 8, 10, 100));
|
||||
vwad.Update(new TBar(time.AddMinutes(1), 10, 12, 8, 11, 100));
|
||||
|
||||
// Feed NaN close - should use last valid
|
||||
var resultAfterNaN = vwad.Update(new TBar(time.AddMinutes(2), 10, 12, 8, double.NaN, 100));
|
||||
|
||||
// Result should be finite
|
||||
Assert.True(double.IsFinite(resultAfterNaN.Value));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Vwad_Infinity_Input_UsesLastValidValue()
|
||||
{
|
||||
var vwad = new Vwad(5);
|
||||
var time = DateTime.UtcNow;
|
||||
|
||||
// Feed some valid values
|
||||
vwad.Update(new TBar(time, 10, 12, 8, 10, 100));
|
||||
vwad.Update(new TBar(time.AddMinutes(1), 10, 12, 8, 11, 100));
|
||||
|
||||
// Feed positive infinity volume - should use last valid
|
||||
var result = vwad.Update(new TBar(time.AddMinutes(2), 10, 12, 8, 10, double.PositiveInfinity));
|
||||
Assert.True(double.IsFinite(result.Value));
|
||||
|
||||
// Feed negative infinity close - should use last valid
|
||||
result = vwad.Update(new TBar(time.AddMinutes(3), 10, 12, 8, double.NegativeInfinity, 100));
|
||||
Assert.True(double.IsFinite(result.Value));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Vwad_BatchCalc_HandlesNaN()
|
||||
{
|
||||
double[] high = [12, 12, double.NaN, 12, 12];
|
||||
double[] low = [8, 8, 8, 8, 8];
|
||||
double[] close = [10, 12, 10, 8, 10];
|
||||
double[] volume = [100, 200, 100, double.PositiveInfinity, 100];
|
||||
double[] output = new double[5];
|
||||
|
||||
Vwad.Batch(high, low, close, volume, output, 3);
|
||||
|
||||
// All outputs should be finite
|
||||
foreach (var val in output)
|
||||
{
|
||||
Assert.True(double.IsFinite(val), $"Expected finite value but got {val}");
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Vwad_CumulativeNature_ValuesContinueGrowing()
|
||||
{
|
||||
var bars = new TBarSeries();
|
||||
var gbm = new GBM(seed: 123, mu: 0.05); // Bullish trend
|
||||
|
||||
for (int i = 0; i < 50; i++)
|
||||
{
|
||||
bars.Add(gbm.Next());
|
||||
}
|
||||
|
||||
var result = Vwad.Batch(bars, 10);
|
||||
|
||||
// In a bullish trend, VWAD should generally be positive and growing
|
||||
// (this is a statistical expectation, not a guarantee)
|
||||
double firstHalf = result[24].Value;
|
||||
double secondHalf = result[49].Value;
|
||||
|
||||
// VWAD is cumulative, values should continue evolving
|
||||
Assert.NotEqual(firstHalf, secondHalf);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Vwad_AllModes_ProduceSameResult()
|
||||
{
|
||||
// Arrange
|
||||
int period = 10;
|
||||
var gbm = new GBM(startPrice: 100, mu: 0.05, sigma: 0.2, seed: 123);
|
||||
var bars = gbm.Fetch(100, DateTime.UtcNow.Ticks, TimeSpan.FromMinutes(1));
|
||||
|
||||
// 1. Batch Mode
|
||||
var batchSeries = Vwad.Batch(bars, period);
|
||||
double expected = batchSeries.Last.Value;
|
||||
|
||||
// 2. Span Mode
|
||||
var spanOutput = new double[bars.Count];
|
||||
Vwad.Batch(bars.High.Values, bars.Low.Values, bars.Close.Values, bars.Volume.Values, spanOutput, period);
|
||||
double spanResult = spanOutput[^1];
|
||||
|
||||
// 3. Streaming Mode
|
||||
var streamingInd = new Vwad(period);
|
||||
for (int i = 0; i < bars.Count; i++)
|
||||
{
|
||||
streamingInd.Update(bars[i]);
|
||||
}
|
||||
double streamingResult = streamingInd.Last.Value;
|
||||
|
||||
// Assert - precision 9 due to potential accumulation differences
|
||||
Assert.Equal(expected, spanResult, precision: 9);
|
||||
Assert.Equal(expected, streamingResult, precision: 9);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,247 @@
|
||||
namespace QuanTAlib.Tests;
|
||||
|
||||
public class VwadValidationTests
|
||||
{
|
||||
private readonly ValidationTestData _data;
|
||||
private const int DefaultPeriod = 20;
|
||||
|
||||
public VwadValidationTests()
|
||||
{
|
||||
_data = new ValidationTestData();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Vwad_NotAvailable_Skender()
|
||||
{
|
||||
// VWAD is a proprietary indicator not available in Skender.Stock.Indicators
|
||||
Assert.True(true, "VWAD is a proprietary indicator not available in Skender");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Vwad_NotAvailable_Talib()
|
||||
{
|
||||
// VWAD is not available in TA-Lib
|
||||
Assert.True(true, "VWAD is a proprietary indicator not available in TA-Lib");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Vwad_NotAvailable_Tulip()
|
||||
{
|
||||
// VWAD is not available in Tulip
|
||||
Assert.True(true, "VWAD is a proprietary indicator not available in Tulip");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Vwad_NotAvailable_Ooples()
|
||||
{
|
||||
// VWAD is not available in Ooples
|
||||
Assert.True(true, "VWAD is a proprietary indicator not available in Ooples");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Vwad_Streaming_Matches_Batch()
|
||||
{
|
||||
// Streaming
|
||||
var vwad = new Vwad(DefaultPeriod);
|
||||
var streamingValues = new List<double>();
|
||||
foreach (var bar in _data.Bars)
|
||||
{
|
||||
streamingValues.Add(vwad.Update(bar).Value);
|
||||
}
|
||||
|
||||
// Batch
|
||||
var batchResult = Vwad.Batch(_data.Bars, DefaultPeriod);
|
||||
var batchValues = batchResult.Values.ToArray();
|
||||
|
||||
// Cumulative indicators accumulate floating-point errors over many bars
|
||||
// 1e-10 tolerance is appropriate for ~5000 bar cumulative calculations
|
||||
ValidationHelper.VerifyData(streamingValues.ToArray(), batchValues, 0, 100, 1e-10);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Vwad_Span_Matches_Streaming()
|
||||
{
|
||||
// Streaming
|
||||
var vwad = new Vwad(DefaultPeriod);
|
||||
var streamingValues = new List<double>();
|
||||
foreach (var bar in _data.Bars)
|
||||
{
|
||||
streamingValues.Add(vwad.Update(bar).Value);
|
||||
}
|
||||
|
||||
// Span
|
||||
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 spanValues = new double[high.Length];
|
||||
|
||||
Vwad.Batch(high, low, close, volume, spanValues, DefaultPeriod);
|
||||
|
||||
// Cumulative indicators accumulate floating-point errors over many bars
|
||||
// 1e-10 tolerance is appropriate for ~5000 bar cumulative calculations
|
||||
ValidationHelper.VerifyData(streamingValues.ToArray(), spanValues, 0, 100, 1e-10);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Vwad_Batch_Matches_Span()
|
||||
{
|
||||
// Batch
|
||||
var batchResult = Vwad.Batch(_data.Bars, DefaultPeriod);
|
||||
var batchValues = batchResult.Values.ToArray();
|
||||
|
||||
// Span
|
||||
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 spanValues = new double[high.Length];
|
||||
|
||||
Vwad.Batch(high, low, close, volume, spanValues, DefaultPeriod);
|
||||
|
||||
// Batch and Span use identical code path, should match exactly
|
||||
ValidationHelper.VerifyData(batchValues, spanValues, 0, 100, 1e-12);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Vwad_Algorithm_Correctness_ManualCalculation()
|
||||
{
|
||||
// Manual calculation to verify algorithm correctness
|
||||
// Use a small dataset with known values
|
||||
int period = 3;
|
||||
var bars = new TBarSeries();
|
||||
|
||||
// Create test bars with predictable OHLCV values
|
||||
// Bar 0: H=12, L=10, C=11, V=100 -> MFM = (11-10 - (12-11))/(12-10) = (1-1)/2 = 0
|
||||
// Bar 1: H=15, L=12, C=14, V=200 -> MFM = (14-12 - (15-14))/(15-12) = (2-1)/3 = 0.333
|
||||
// Bar 2: H=14, L=11, C=12, V=150 -> MFM = (12-11 - (14-12))/(14-11) = (1-2)/3 = -0.333
|
||||
|
||||
bars.Add(new TBar(DateTime.UtcNow, 10, 12, 10, 11, 100));
|
||||
bars.Add(new TBar(DateTime.UtcNow.AddMinutes(1), 12, 15, 12, 14, 200));
|
||||
bars.Add(new TBar(DateTime.UtcNow.AddMinutes(2), 11, 14, 11, 12, 150));
|
||||
|
||||
var vwad = new Vwad(period);
|
||||
var results = new List<double>();
|
||||
foreach (var bar in bars)
|
||||
{
|
||||
results.Add(vwad.Update(bar).Value);
|
||||
}
|
||||
|
||||
// Bar 0: sumVol=100, volWeight=1, weightedMfv=100*0*1=0, cumVwad=0
|
||||
Assert.Equal(0, results[0], 6);
|
||||
|
||||
// Bar 1: sumVol=300, volWeight=200/300=0.667, MFM=0.333, weightedMfv=200*0.333*0.667=44.4
|
||||
// cumVwad = 0 + 44.4 = 44.4
|
||||
double expectedBar1 = 200 * (1.0 / 3.0) * (200.0 / 300.0);
|
||||
Assert.Equal(expectedBar1, results[1], 6);
|
||||
|
||||
// Bar 2: sumVol=450, volWeight=150/450=0.333, MFM=-0.333, weightedMfv=150*(-0.333)*0.333=-16.67
|
||||
// cumVwad = 44.4 - 16.67 = 27.8
|
||||
double expectedBar2 = expectedBar1 + 150 * (-1.0 / 3.0) * (150.0 / 450.0);
|
||||
Assert.Equal(expectedBar2, results[2], 6);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Vwad_Algorithm_Correctness_RollingPeriod()
|
||||
{
|
||||
// Verify that volume sum rolls correctly after period is exceeded
|
||||
int period = 2;
|
||||
var bars = new TBarSeries();
|
||||
|
||||
// Create 4 bars to test rolling behavior
|
||||
bars.Add(new TBar(DateTime.UtcNow, 10, 10, 10, 10, 100)); // MFM=0 (H=L=C)
|
||||
bars.Add(new TBar(DateTime.UtcNow.AddMinutes(1), 10, 10, 10, 10, 200)); // MFM=0
|
||||
bars.Add(new TBar(DateTime.UtcNow.AddMinutes(2), 10, 10, 10, 10, 300)); // MFM=0, but volume rolls
|
||||
|
||||
var vwad = new Vwad(period);
|
||||
|
||||
// Bar 0: sumVol=100
|
||||
var r0 = vwad.Update(bars[0]);
|
||||
Assert.Equal(0, r0.Value, 10);
|
||||
|
||||
// Bar 1: sumVol=300
|
||||
var r1 = vwad.Update(bars[1]);
|
||||
Assert.Equal(0, r1.Value, 10);
|
||||
|
||||
// Bar 2: sumVol should be 200+300=500 (100 rolled out)
|
||||
// This tests that the rolling sum works correctly
|
||||
var r2 = vwad.Update(bars[2]);
|
||||
Assert.Equal(0, r2.Value, 10); // Still 0 because MFM=0 for all bars
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Vwad_Algorithm_Correctness_VolumeWeighting()
|
||||
{
|
||||
// Verify volume weighting amplifies high-volume bars
|
||||
int period = 10; // Large period so no rolling
|
||||
var bars = new TBarSeries();
|
||||
|
||||
// Two bars with same MFM but different volumes
|
||||
// High volume bar should contribute more to VWAD
|
||||
bars.Add(new TBar(DateTime.UtcNow, 10, 20, 10, 15, 1000)); // MFM = 0 (close at midpoint)
|
||||
bars.Add(new TBar(DateTime.UtcNow.AddMinutes(1), 10, 20, 10, 20, 100)); // MFM = 1 (close at high)
|
||||
|
||||
var vwad = new Vwad(period);
|
||||
|
||||
// Bar 0: MFM = (15-10 - (20-15))/(20-10) = (5-5)/10 = 0
|
||||
var r0 = vwad.Update(bars[0]);
|
||||
Assert.Equal(0, r0.Value, 10);
|
||||
|
||||
// Bar 1: MFM = (20-10 - (20-20))/(20-10) = 10/10 = 1
|
||||
// sumVol = 1100, volWeight = 100/1100 = 0.0909
|
||||
// weightedMfv = 100 * 1 * 0.0909 = 9.09
|
||||
var r1 = vwad.Update(bars[1]);
|
||||
double expectedVolWeight = 100.0 / 1100.0;
|
||||
double expectedWeightedMfv = 100.0 * 1.0 * expectedVolWeight;
|
||||
Assert.Equal(expectedWeightedMfv, r1.Value, 6);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Vwad_DifferentPeriods_ProduceDifferentResults()
|
||||
{
|
||||
// Different periods should produce different results
|
||||
var vwad10 = new Vwad(10);
|
||||
var vwad20 = new Vwad(20);
|
||||
var vwad50 = new Vwad(50);
|
||||
|
||||
var results10 = new List<double>();
|
||||
var results20 = new List<double>();
|
||||
var results50 = new List<double>();
|
||||
|
||||
foreach (var bar in _data.Bars)
|
||||
{
|
||||
results10.Add(vwad10.Update(bar).Value);
|
||||
results20.Add(vwad20.Update(bar).Value);
|
||||
results50.Add(vwad50.Update(bar).Value);
|
||||
}
|
||||
|
||||
// After warmup, results should differ
|
||||
int checkIndex = 60; // Well past all warmup periods
|
||||
bool allSame = Math.Abs(results10[checkIndex] - results20[checkIndex]) < 1e-10 &&
|
||||
Math.Abs(results20[checkIndex] - results50[checkIndex]) < 1e-10;
|
||||
|
||||
Assert.False(allSame, "Different periods should produce different VWAD values");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Vwad_Cumulative_AlwaysChanges_WithNonZeroMfm()
|
||||
{
|
||||
// VWAD is cumulative - it should change when MFM is non-zero
|
||||
var vwad = new Vwad(DefaultPeriod);
|
||||
double? previousValue = null;
|
||||
int changeCount = 0;
|
||||
|
||||
foreach (var bar in _data.Bars)
|
||||
{
|
||||
var result = vwad.Update(bar);
|
||||
if (previousValue.HasValue && Math.Abs(result.Value - previousValue.Value) > 1e-15)
|
||||
{
|
||||
changeCount++;
|
||||
}
|
||||
previousValue = result.Value;
|
||||
}
|
||||
|
||||
// Most bars should cause changes (unless MFM happens to be exactly 0)
|
||||
Assert.True(changeCount > _data.Bars.Count * 0.5, "VWAD should change for most bars with non-zero MFM");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user