mirror of
https://github.com/mihakralj/QuanTAlib.git
synced 2026-08-23 21:18:04 +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,192 @@
|
||||
using TradingPlatform.BusinessLayer;
|
||||
|
||||
namespace QuanTAlib.Tests;
|
||||
|
||||
public class VwapIndicatorTests
|
||||
{
|
||||
[Fact]
|
||||
public void VwapIndicator_Constructor_SetsDefaults()
|
||||
{
|
||||
var indicator = new VwapIndicator();
|
||||
|
||||
Assert.Equal("VWAP - Volume Weighted Average Price", indicator.Name);
|
||||
Assert.Equal(0, indicator.Period);
|
||||
Assert.False(indicator.SeparateWindow);
|
||||
Assert.True(indicator.OnBackGround);
|
||||
Assert.Equal(1, indicator.MinHistoryDepths);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void VwapIndicator_ShortName_ReflectsPeriod()
|
||||
{
|
||||
var indicator = new VwapIndicator { Period = 14 };
|
||||
Assert.Equal("VWAP(14)", indicator.ShortName);
|
||||
|
||||
var indicatorNoPeriod = new VwapIndicator { Period = 0 };
|
||||
Assert.Equal("VWAP", indicatorNoPeriod.ShortName);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void VwapIndicator_MinHistoryDepths_EqualsDefault()
|
||||
{
|
||||
var indicator = new VwapIndicator();
|
||||
|
||||
Assert.Equal(1, indicator.MinHistoryDepths);
|
||||
Assert.Equal(1, ((IWatchlistIndicator)indicator).MinHistoryDepths);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void VwapIndicator_Initialize_CreatesInternalVwap()
|
||||
{
|
||||
var indicator = new VwapIndicator();
|
||||
|
||||
// Initialize should not throw
|
||||
indicator.Initialize();
|
||||
|
||||
// After init, line series should exist
|
||||
Assert.Single(indicator.LinesSeries);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void VwapIndicator_ProcessUpdate_HistoricalBar_ComputesValue()
|
||||
{
|
||||
var indicator = new VwapIndicator();
|
||||
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 VwapIndicator_ProcessUpdate_NewBar_ComputesValue()
|
||||
{
|
||||
var indicator = new VwapIndicator();
|
||||
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 VwapIndicator_Value_TracksVolumeWeightedPrice()
|
||||
{
|
||||
var indicator = new VwapIndicator();
|
||||
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;
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
// VWAP should produce finite values
|
||||
Assert.True(values.Count > 0, "Should have recorded values");
|
||||
Assert.All(values, v => Assert.True(double.IsFinite(v)));
|
||||
|
||||
// VWAP values should be within price range (approximately)
|
||||
double avgValue = values.Average();
|
||||
Assert.True(avgValue > 90 && avgValue < 200, $"VWAP {avgValue} should be within reasonable price range");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void VwapIndicator_DifferentPeriods_ProduceDifferentResults()
|
||||
{
|
||||
var indicator0 = new VwapIndicator { Period = 0 }; // No reset
|
||||
var indicator10 = new VwapIndicator { Period = 10 }; // Reset every 10 bars
|
||||
|
||||
indicator0.Initialize();
|
||||
indicator10.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);
|
||||
|
||||
indicator0.HistoricalData.AddBar(now.AddMinutes(i), open, high, low, close, volume);
|
||||
indicator10.HistoricalData.AddBar(now.AddMinutes(i), open, high, low, close, volume);
|
||||
|
||||
indicator0.ProcessUpdate(new UpdateArgs(UpdateReason.HistoricalBar));
|
||||
indicator10.ProcessUpdate(new UpdateArgs(UpdateReason.HistoricalBar));
|
||||
}
|
||||
|
||||
double val0 = indicator0.LinesSeries[0].GetValue(0);
|
||||
double val10 = indicator10.LinesSeries[0].GetValue(0);
|
||||
|
||||
// Different periods should produce different results
|
||||
// Period 0 accumulates all history, Period 10 resets every 10 bars
|
||||
Assert.NotEqual(val0, val10, 6);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void VwapIndicator_PeriodReset_ResetsAccumulation()
|
||||
{
|
||||
var indicator = new VwapIndicator { Period = 5 }; // Reset every 5 bars
|
||||
indicator.Initialize();
|
||||
|
||||
var now = DateTime.UtcNow;
|
||||
var valuesAtReset = new List<double>();
|
||||
|
||||
for (int i = 0; i < 20; i++)
|
||||
{
|
||||
double price = 100.0; // Constant price
|
||||
double volume = 1000.0; // Constant volume
|
||||
|
||||
indicator.HistoricalData.AddBar(now.AddMinutes(i), price, price + 1, price - 1, price, volume);
|
||||
indicator.ProcessUpdate(new UpdateArgs(UpdateReason.HistoricalBar));
|
||||
|
||||
// Record value right after reset (at bars 5, 10, 15)
|
||||
if (i > 0 && (i + 1) % 5 == 1)
|
||||
{
|
||||
double val = indicator.LinesSeries[0].GetValue(0);
|
||||
valuesAtReset.Add(val);
|
||||
}
|
||||
}
|
||||
|
||||
// After reset, VWAP should be close to typical price for constant price input
|
||||
// All values after reset should be similar (since price is constant)
|
||||
Assert.True(valuesAtReset.Count >= 2, "Should have multiple reset points");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,429 @@
|
||||
namespace QuanTAlib.Tests;
|
||||
|
||||
public class VwapTests
|
||||
{
|
||||
private readonly GBM _feed;
|
||||
private readonly TBarSeries _bars;
|
||||
|
||||
public VwapTests()
|
||||
{
|
||||
_feed = new GBM();
|
||||
_bars = new TBarSeries();
|
||||
for (int i = 0; i < 1000; i++)
|
||||
{
|
||||
_bars.Add(_feed.Next());
|
||||
}
|
||||
}
|
||||
|
||||
// ============ Constructor Tests ============
|
||||
|
||||
[Fact]
|
||||
public void Constructor_DefaultPeriod_ShouldBeZero()
|
||||
{
|
||||
var vwap = new Vwap();
|
||||
Assert.Equal("VWAP", vwap.Name);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Constructor_WithPeriod_ShouldSetName()
|
||||
{
|
||||
var vwap = new Vwap(390);
|
||||
Assert.Equal("VWAP(390)", vwap.Name);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Constructor_NegativePeriod_ShouldThrow()
|
||||
{
|
||||
var ex = Assert.Throws<ArgumentException>(() => new Vwap(-1));
|
||||
Assert.Equal("period", ex.ParamName);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Constructor_ZeroPeriod_ShouldNotThrow()
|
||||
{
|
||||
var vwap = new Vwap(0);
|
||||
Assert.Equal("VWAP", vwap.Name);
|
||||
}
|
||||
|
||||
// ============ Basic Calculation Tests ============
|
||||
|
||||
[Fact]
|
||||
public void Update_ReturnsValidTValue()
|
||||
{
|
||||
var vwap = new Vwap();
|
||||
var bar = _bars[0];
|
||||
var result = vwap.Update(bar);
|
||||
|
||||
Assert.NotEqual(default, result);
|
||||
Assert.True(double.IsFinite(result.Value));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Update_FirstBar_ShouldBeTypicalPrice()
|
||||
{
|
||||
var vwap = new Vwap();
|
||||
var bar = new TBar(DateTime.UtcNow, 10, 15, 8, 12, 1000);
|
||||
var result = vwap.Update(bar);
|
||||
|
||||
// VWAP of first bar = typical price = (H+L+C)/3 = (15+8+12)/3 = 11.666...
|
||||
double expectedTypicalPrice = (15.0 + 8.0 + 12.0) / 3.0;
|
||||
Assert.Equal(expectedTypicalPrice, result.Value, 10);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Update_MultipleBarsSamePrice_ShouldReturnSameVwap()
|
||||
{
|
||||
var vwap = new Vwap();
|
||||
// All bars have same typical price = 10
|
||||
var bar1 = new TBar(DateTime.UtcNow, 10, 10, 10, 10, 100);
|
||||
var bar2 = new TBar(DateTime.UtcNow.AddMinutes(1), 10, 10, 10, 10, 200);
|
||||
var bar3 = new TBar(DateTime.UtcNow.AddMinutes(2), 10, 10, 10, 10, 300);
|
||||
|
||||
vwap.Update(bar1);
|
||||
vwap.Update(bar2);
|
||||
var result = vwap.Update(bar3);
|
||||
|
||||
Assert.Equal(10.0, result.Value, 10);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Update_VolumeWeighting_Works()
|
||||
{
|
||||
var vwap = new Vwap();
|
||||
// Bar 1: price=10, volume=100
|
||||
// Bar 2: price=20, volume=300
|
||||
// VWAP = (10*100 + 20*300) / (100+300) = (1000 + 6000) / 400 = 17.5
|
||||
var bar1 = new TBar(DateTime.UtcNow, 10, 10, 10, 10, 100);
|
||||
var bar2 = new TBar(DateTime.UtcNow.AddMinutes(1), 20, 20, 20, 20, 300);
|
||||
|
||||
vwap.Update(bar1);
|
||||
var result = vwap.Update(bar2);
|
||||
|
||||
Assert.Equal(17.5, result.Value, 10);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void IsHot_AfterFirstBar_ShouldBeTrue()
|
||||
{
|
||||
var vwap = new Vwap();
|
||||
Assert.False(vwap.IsHot);
|
||||
|
||||
vwap.Update(_bars[0]);
|
||||
Assert.True(vwap.IsHot);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void WarmupPeriod_ShouldBeOne()
|
||||
{
|
||||
var vwap = new Vwap();
|
||||
Assert.Equal(1, vwap.WarmupPeriod);
|
||||
}
|
||||
|
||||
// ============ Bar Correction Tests (isNew) ============
|
||||
|
||||
[Fact]
|
||||
public void Update_IsNewTrue_ShouldAdvanceState()
|
||||
{
|
||||
var vwap = new Vwap();
|
||||
var bar1 = new TBar(DateTime.UtcNow, 10, 10, 10, 10, 100);
|
||||
var bar2 = new TBar(DateTime.UtcNow.AddMinutes(1), 20, 20, 20, 20, 100);
|
||||
|
||||
vwap.Update(bar1, isNew: true);
|
||||
var result1 = vwap.Last.Value;
|
||||
|
||||
vwap.Update(bar2, isNew: true);
|
||||
var result2 = vwap.Last.Value;
|
||||
|
||||
Assert.NotEqual(result1, result2);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Update_IsNewFalse_ShouldRollback()
|
||||
{
|
||||
var vwap = new Vwap();
|
||||
var bar1 = new TBar(DateTime.UtcNow, 10, 10, 10, 10, 100);
|
||||
var bar2 = new TBar(DateTime.UtcNow.AddMinutes(1), 20, 20, 20, 20, 100);
|
||||
var bar2Updated = new TBar(DateTime.UtcNow.AddMinutes(1), 15, 15, 15, 15, 100);
|
||||
|
||||
vwap.Update(bar1, isNew: true);
|
||||
vwap.Update(bar2, isNew: true);
|
||||
var afterBar2 = vwap.Last.Value;
|
||||
|
||||
// Correct bar2 with updated values
|
||||
vwap.Update(bar2Updated, isNew: false);
|
||||
var afterCorrection = vwap.Last.Value;
|
||||
|
||||
Assert.NotEqual(afterBar2, afterCorrection);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Update_IterativeCorrections_ShouldRestoreState()
|
||||
{
|
||||
var vwap = new Vwap();
|
||||
|
||||
// Process first 10 bars
|
||||
for (int i = 0; i < 10; i++)
|
||||
{
|
||||
vwap.Update(_bars[i], isNew: true);
|
||||
}
|
||||
_ = vwap.Last.Value; // capture state before bar 11
|
||||
|
||||
// Process bar 11
|
||||
vwap.Update(_bars[10], isNew: true);
|
||||
var valueAfter11 = vwap.Last.Value;
|
||||
|
||||
// Correct bar 11 multiple times with same data
|
||||
for (int i = 0; i < 5; i++)
|
||||
{
|
||||
vwap.Update(_bars[10], isNew: false);
|
||||
}
|
||||
var valueAfterCorrections = vwap.Last.Value;
|
||||
|
||||
// Should get same result as after first processing of bar 11
|
||||
Assert.Equal(valueAfter11, valueAfterCorrections, 10);
|
||||
}
|
||||
|
||||
// ============ Reset Tests ============
|
||||
|
||||
[Fact]
|
||||
public void Reset_ShouldClearState()
|
||||
{
|
||||
var vwap = new Vwap();
|
||||
|
||||
for (int i = 0; i < 100; i++)
|
||||
{
|
||||
vwap.Update(_bars[i]);
|
||||
}
|
||||
Assert.True(vwap.IsHot);
|
||||
|
||||
vwap.Reset();
|
||||
|
||||
Assert.False(vwap.IsHot);
|
||||
Assert.Equal(default, vwap.Last);
|
||||
}
|
||||
|
||||
// ============ Period Reset Tests ============
|
||||
|
||||
[Fact]
|
||||
public void Update_WithPeriod_ShouldResetAtPeriodBoundary()
|
||||
{
|
||||
var vwap = new Vwap(5);
|
||||
var results = new List<double>();
|
||||
|
||||
// Create bars with consistent price/volume
|
||||
for (int i = 0; i < 10; i++)
|
||||
{
|
||||
var bar = new TBar(DateTime.UtcNow.AddMinutes(i), 100, 100, 100, 100, 1000);
|
||||
results.Add(vwap.Update(bar).Value);
|
||||
}
|
||||
|
||||
// All values should be 100 since price is constant
|
||||
foreach (var value in results)
|
||||
{
|
||||
Assert.Equal(100.0, value, 10);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Update_PeriodReset_ShouldClearCumulativeSums()
|
||||
{
|
||||
var vwap = new Vwap(3);
|
||||
|
||||
// Bars 0-2: price=10, VWAP=10
|
||||
for (int i = 0; i < 3; i++)
|
||||
{
|
||||
vwap.Update(new TBar(DateTime.UtcNow.AddMinutes(i), 10, 10, 10, 10, 100));
|
||||
}
|
||||
var beforeReset = vwap.Last.Value;
|
||||
Assert.Equal(10.0, beforeReset, 10);
|
||||
|
||||
// Bar 3: Reset happens, price=20, VWAP should be 20
|
||||
var result = vwap.Update(new TBar(DateTime.UtcNow.AddMinutes(3), 20, 20, 20, 20, 100));
|
||||
Assert.Equal(20.0, result.Value, 10);
|
||||
}
|
||||
|
||||
// ============ NaN/Infinity Handling ============
|
||||
|
||||
[Fact]
|
||||
public void Update_NaN_ShouldUseLastValidValue()
|
||||
{
|
||||
var vwap = new Vwap();
|
||||
|
||||
// First bar establishes valid values
|
||||
var bar1 = new TBar(DateTime.UtcNow, 10, 15, 8, 12, 1000);
|
||||
vwap.Update(bar1);
|
||||
_ = vwap.Last.Value; // establish first valid value
|
||||
|
||||
// Second bar with NaN should use last valid
|
||||
var bar2 = new TBar(DateTime.UtcNow.AddMinutes(1), double.NaN, double.NaN, double.NaN, double.NaN, double.NaN);
|
||||
var result = vwap.Update(bar2);
|
||||
|
||||
Assert.True(double.IsFinite(result.Value));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Update_Infinity_ShouldUseLastValidValue()
|
||||
{
|
||||
var vwap = new Vwap();
|
||||
|
||||
var bar1 = new TBar(DateTime.UtcNow, 10, 15, 8, 12, 1000);
|
||||
vwap.Update(bar1);
|
||||
|
||||
var bar2 = new TBar(DateTime.UtcNow.AddMinutes(1), double.PositiveInfinity, double.PositiveInfinity, double.PositiveInfinity, double.PositiveInfinity, double.PositiveInfinity);
|
||||
var result = vwap.Update(bar2);
|
||||
|
||||
Assert.True(double.IsFinite(result.Value));
|
||||
}
|
||||
|
||||
// ============ TValue Input Tests ============
|
||||
|
||||
[Fact]
|
||||
public void Update_TValue_ShouldWork()
|
||||
{
|
||||
var vwap = new Vwap();
|
||||
var input = new TValue(DateTime.UtcNow, 100.0);
|
||||
var result = vwap.Update(input);
|
||||
|
||||
// With TValue, it creates synthetic bar with price as OHLC and volume=1
|
||||
Assert.Equal(100.0, result.Value, 10);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Update_TValue_MultipleInputs()
|
||||
{
|
||||
var vwap = new Vwap();
|
||||
|
||||
// TValue input assumes volume=1 for all
|
||||
// VWAP = (100*1 + 200*1) / 2 = 150
|
||||
vwap.Update(new TValue(DateTime.UtcNow, 100.0));
|
||||
var result = vwap.Update(new TValue(DateTime.UtcNow.AddMinutes(1), 200.0));
|
||||
|
||||
Assert.Equal(150.0, result.Value, 10);
|
||||
}
|
||||
|
||||
// ============ Batch/Series Tests ============
|
||||
|
||||
[Fact]
|
||||
public void Update_TBarSeries_ShouldReturnTSeries()
|
||||
{
|
||||
var vwap = new Vwap();
|
||||
var result = vwap.Update(_bars);
|
||||
|
||||
Assert.NotNull(result);
|
||||
Assert.Equal(_bars.Count, result.Count);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Calculate_Static_ShouldReturnTSeries()
|
||||
{
|
||||
var result = Vwap.Batch(_bars);
|
||||
|
||||
Assert.NotNull(result);
|
||||
Assert.Equal(_bars.Count, result.Count);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Calculate_Static_WithPeriod_ShouldWork()
|
||||
{
|
||||
var result = Vwap.Batch(_bars, 100);
|
||||
|
||||
Assert.NotNull(result);
|
||||
Assert.Equal(_bars.Count, result.Count);
|
||||
}
|
||||
|
||||
// ============ Span API Tests ============
|
||||
|
||||
[Fact]
|
||||
public void Calculate_Span_ShouldMatchBatch()
|
||||
{
|
||||
var batchResult = Vwap.Batch(_bars);
|
||||
|
||||
var high = _bars.High.Values.ToArray();
|
||||
var low = _bars.Low.Values.ToArray();
|
||||
var close = _bars.Close.Values.ToArray();
|
||||
var volume = _bars.Volume.Values.ToArray();
|
||||
var spanOutput = new double[_bars.Count];
|
||||
|
||||
Vwap.Batch(high, low, close, volume, spanOutput);
|
||||
|
||||
for (int i = 0; i < _bars.Count; i++)
|
||||
{
|
||||
Assert.Equal(batchResult.Values[i], spanOutput[i], 12);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Calculate_Span_MismatchedLengths_ShouldThrow()
|
||||
{
|
||||
var high = new double[100];
|
||||
var low = new double[99]; // Mismatched
|
||||
var close = new double[100];
|
||||
var volume = new double[100];
|
||||
var output = new double[100];
|
||||
|
||||
Assert.Throws<ArgumentException>(() => Vwap.Batch(high, low, close, volume, output));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Calculate_Span_OutputLengthMismatch_ShouldThrow()
|
||||
{
|
||||
var high = new double[100];
|
||||
var low = new double[100];
|
||||
var close = new double[100];
|
||||
var volume = new double[100];
|
||||
var output = new double[50]; // Mismatched
|
||||
|
||||
Assert.Throws<ArgumentException>(() => Vwap.Batch(high, low, close, volume, output));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Calculate_Span_NegativePeriod_ShouldThrow()
|
||||
{
|
||||
var high = new double[100];
|
||||
var low = new double[100];
|
||||
var close = new double[100];
|
||||
var volume = new double[100];
|
||||
var output = new double[100];
|
||||
|
||||
Assert.Throws<ArgumentException>(() => Vwap.Batch(high, low, close, volume, output, -1));
|
||||
}
|
||||
|
||||
// ============ Event Tests ============
|
||||
|
||||
[Fact]
|
||||
public void Pub_ShouldFireOnUpdate()
|
||||
{
|
||||
var vwap = new Vwap();
|
||||
int eventCount = 0;
|
||||
|
||||
vwap.Pub += (object? sender, in TValueEventArgs args) => eventCount++;
|
||||
|
||||
vwap.Update(_bars[0]);
|
||||
vwap.Update(_bars[1]);
|
||||
|
||||
Assert.Equal(2, eventCount);
|
||||
}
|
||||
|
||||
// ============ Streaming/Batch Consistency ============
|
||||
|
||||
[Fact]
|
||||
public void Streaming_ShouldMatchBatch()
|
||||
{
|
||||
// Streaming
|
||||
var vwap = new Vwap();
|
||||
var streamingResults = new List<double>();
|
||||
foreach (var bar in _bars)
|
||||
{
|
||||
streamingResults.Add(vwap.Update(bar).Value);
|
||||
}
|
||||
|
||||
// Batch
|
||||
var batchResult = Vwap.Batch(_bars);
|
||||
|
||||
// Compare last 100 values
|
||||
for (int i = _bars.Count - 100; i < _bars.Count; i++)
|
||||
{
|
||||
Assert.Equal(batchResult.Values[i], streamingResults[i], 10);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,261 @@
|
||||
namespace QuanTAlib.Tests;
|
||||
|
||||
public class VwapValidationTests
|
||||
{
|
||||
private readonly ValidationTestData _data;
|
||||
|
||||
public VwapValidationTests()
|
||||
{
|
||||
_data = new ValidationTestData();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Vwap_NotAvailable_Skender()
|
||||
{
|
||||
// Skender has VWAP but it uses anchor-based sessions, not period-based
|
||||
// Our implementation uses period-based reset for flexibility
|
||||
Assert.True(true, "VWAP implementations differ in session handling");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Vwap_NotAvailable_Talib()
|
||||
{
|
||||
// TA-Lib does not have VWAP
|
||||
Assert.True(true, "VWAP is not available in TA-Lib");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Vwap_NotAvailable_Tulip()
|
||||
{
|
||||
// Tulip does not have VWAP
|
||||
Assert.True(true, "VWAP is not available in Tulip");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Vwap_NotAvailable_Ooples()
|
||||
{
|
||||
// Ooples has VWAP but implementation details may differ
|
||||
Assert.True(true, "VWAP implementations may differ in session handling");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Vwap_Streaming_Matches_Batch()
|
||||
{
|
||||
// Streaming
|
||||
var vwap = new Vwap();
|
||||
var streamingValues = new List<double>();
|
||||
foreach (var bar in _data.Bars)
|
||||
{
|
||||
streamingValues.Add(vwap.Update(bar).Value);
|
||||
}
|
||||
|
||||
// Batch
|
||||
var batchResult = Vwap.Batch(_data.Bars);
|
||||
var batchValues = batchResult.Values.ToArray();
|
||||
|
||||
// Cumulative indicators accumulate floating-point errors over many bars
|
||||
ValidationHelper.VerifyData(streamingValues.ToArray(), batchValues, 0, 100, 1e-10);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Vwap_Span_Matches_Streaming()
|
||||
{
|
||||
// Streaming
|
||||
var vwap = new Vwap();
|
||||
var streamingValues = new List<double>();
|
||||
foreach (var bar in _data.Bars)
|
||||
{
|
||||
streamingValues.Add(vwap.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];
|
||||
|
||||
Vwap.Batch(high, low, close, volume, spanValues);
|
||||
|
||||
// Cumulative indicators accumulate floating-point errors over many bars
|
||||
ValidationHelper.VerifyData(streamingValues.ToArray(), spanValues, 0, 100, 1e-10);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Vwap_Batch_Matches_Span()
|
||||
{
|
||||
// Batch
|
||||
var batchResult = Vwap.Batch(_data.Bars);
|
||||
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];
|
||||
|
||||
Vwap.Batch(high, low, close, volume, spanValues);
|
||||
|
||||
// Batch and Span use identical code path, should match exactly
|
||||
ValidationHelper.VerifyData(batchValues, spanValues, 0, 100, 1e-12);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Vwap_Algorithm_Correctness_ManualCalculation()
|
||||
{
|
||||
// Manual calculation to verify algorithm correctness
|
||||
var bars = new TBarSeries();
|
||||
|
||||
// Create test bars with known OHLCV values
|
||||
// Bar 0: H=12, L=10, C=11, V=100 -> TP = (12+10+11)/3 = 11
|
||||
// Bar 1: H=15, L=12, C=14, V=200 -> TP = (15+12+14)/3 = 13.667
|
||||
// Bar 2: H=14, L=11, C=12, V=150 -> TP = (14+11+12)/3 = 12.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 vwap = new Vwap();
|
||||
var results = new List<double>();
|
||||
foreach (var bar in bars)
|
||||
{
|
||||
results.Add(vwap.Update(bar).Value);
|
||||
}
|
||||
|
||||
// Bar 0: VWAP = 11*100 / 100 = 11
|
||||
double tp0 = (12.0 + 10.0 + 11.0) / 3.0;
|
||||
Assert.Equal(tp0, results[0], 6);
|
||||
|
||||
// Bar 1: VWAP = (11*100 + 13.667*200) / 300 = (1100 + 2733.33) / 300 = 12.778
|
||||
double tp1 = (15.0 + 12.0 + 14.0) / 3.0;
|
||||
double expectedBar1 = (tp0 * 100 + tp1 * 200) / 300.0;
|
||||
Assert.Equal(expectedBar1, results[1], 6);
|
||||
|
||||
// Bar 2: VWAP = (11*100 + 13.667*200 + 12.333*150) / 450
|
||||
double tp2 = (14.0 + 11.0 + 12.0) / 3.0;
|
||||
double expectedBar2 = (tp0 * 100 + tp1 * 200 + tp2 * 150) / 450.0;
|
||||
Assert.Equal(expectedBar2, results[2], 6);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Vwap_Algorithm_Correctness_VolumeWeighting()
|
||||
{
|
||||
// Verify volume weighting: high-volume bars have more influence
|
||||
var bars = new TBarSeries();
|
||||
|
||||
// Two bars: one with high volume at low price, one with low volume at high price
|
||||
// Bar 0: price=10, volume=1000
|
||||
// Bar 1: price=20, volume=100
|
||||
// VWAP should be closer to 10 due to higher volume
|
||||
bars.Add(new TBar(DateTime.UtcNow, 10, 10, 10, 10, 1000));
|
||||
bars.Add(new TBar(DateTime.UtcNow.AddMinutes(1), 20, 20, 20, 20, 100));
|
||||
|
||||
var vwap = new Vwap();
|
||||
vwap.Update(bars[0]);
|
||||
var result = vwap.Update(bars[1]);
|
||||
|
||||
// VWAP = (10*1000 + 20*100) / 1100 = 12000/1100 = 10.909
|
||||
double expected = (10.0 * 1000.0 + 20.0 * 100.0) / 1100.0;
|
||||
Assert.Equal(expected, result.Value, 6);
|
||||
|
||||
// VWAP should be much closer to 10 than to 20
|
||||
Assert.True(result.Value < 15, "VWAP should be weighted toward high-volume price");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Vwap_DifferentPeriods_ProduceDifferentResults()
|
||||
{
|
||||
// VWAP with different periods should produce different results after reset
|
||||
var vwap0 = new Vwap(0); // No reset
|
||||
var vwap10 = new Vwap(10); // Reset every 10 bars
|
||||
var vwap50 = new Vwap(50); // Reset every 50 bars
|
||||
|
||||
var results0 = new List<double>();
|
||||
var results10 = new List<double>();
|
||||
var results50 = new List<double>();
|
||||
|
||||
foreach (var bar in _data.Bars)
|
||||
{
|
||||
results0.Add(vwap0.Update(bar).Value);
|
||||
results10.Add(vwap10.Update(bar).Value);
|
||||
results50.Add(vwap50.Update(bar).Value);
|
||||
}
|
||||
|
||||
// After sufficient bars, different periods should produce different results
|
||||
int checkIndex = 60;
|
||||
bool anyDifferent = Math.Abs(results0[checkIndex] - results10[checkIndex]) > 1e-6 ||
|
||||
Math.Abs(results10[checkIndex] - results50[checkIndex]) > 1e-6;
|
||||
|
||||
Assert.True(anyDifferent, "Different periods should produce different VWAP values after resets");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Vwap_WithPeriod_ResetsBehavior()
|
||||
{
|
||||
// Verify that period-based reset works correctly
|
||||
var vwap = new Vwap(5);
|
||||
|
||||
// First 5 bars at price=100
|
||||
for (int i = 0; i < 5; i++)
|
||||
{
|
||||
vwap.Update(new TBar(DateTime.UtcNow.AddMinutes(i), 100, 100, 100, 100, 1000));
|
||||
}
|
||||
var afterFirst5 = vwap.Last.Value;
|
||||
Assert.Equal(100.0, afterFirst5, 6);
|
||||
|
||||
// Bar 5 triggers reset, price=200
|
||||
var afterReset = vwap.Update(new TBar(DateTime.UtcNow.AddMinutes(5), 200, 200, 200, 200, 1000));
|
||||
Assert.Equal(200.0, afterReset.Value, 6);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Vwap_StableWithConstantPrice()
|
||||
{
|
||||
// VWAP should remain stable when price is constant
|
||||
var vwap = new Vwap();
|
||||
var results = new List<double>();
|
||||
|
||||
for (int i = 0; i < 100; i++)
|
||||
{
|
||||
var bar = new TBar(DateTime.UtcNow.AddMinutes(i), 50, 50, 50, 50, 1000 + i * 10);
|
||||
results.Add(vwap.Update(bar).Value);
|
||||
}
|
||||
|
||||
// All VWAP values should be 50
|
||||
foreach (var value in results)
|
||||
{
|
||||
Assert.Equal(50.0, value, 10);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Vwap_ZeroVolume_HandledCorrectly()
|
||||
{
|
||||
// VWAP should handle zero volume gracefully
|
||||
var vwap = new Vwap();
|
||||
|
||||
// First bar with volume
|
||||
vwap.Update(new TBar(DateTime.UtcNow, 10, 10, 10, 10, 1000));
|
||||
|
||||
// Second bar with zero volume
|
||||
var result = vwap.Update(new TBar(DateTime.UtcNow.AddMinutes(1), 20, 20, 20, 20, 0));
|
||||
|
||||
// VWAP should remain at 10 (zero volume doesn't contribute)
|
||||
Assert.Equal(10.0, result.Value, 10);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Vwap_TypicalPriceCalculation()
|
||||
{
|
||||
// Verify typical price is (H+L+C)/3
|
||||
var vwap = new Vwap();
|
||||
|
||||
var bar = new TBar(DateTime.UtcNow, 10, 30, 10, 20, 1000); // O=10, H=30, L=10, C=20
|
||||
var result = vwap.Update(bar);
|
||||
|
||||
// Typical price = (30+10+20)/3 = 20
|
||||
double expectedTypicalPrice = (30.0 + 10.0 + 20.0) / 3.0;
|
||||
Assert.Equal(expectedTypicalPrice, result.Value, 10);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user