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,193 @@
|
||||
using TradingPlatform.BusinessLayer;
|
||||
|
||||
namespace QuanTAlib.Tests;
|
||||
|
||||
public class VwmaIndicatorTests
|
||||
{
|
||||
[Fact]
|
||||
public void VwmaIndicator_Constructor_SetsDefaults()
|
||||
{
|
||||
var indicator = new VwmaIndicator();
|
||||
|
||||
Assert.Equal("VWMA - Volume Weighted Moving Average", indicator.Name);
|
||||
Assert.Equal(20, indicator.Period);
|
||||
Assert.False(indicator.SeparateWindow);
|
||||
Assert.True(indicator.OnBackGround);
|
||||
Assert.Equal(20, indicator.MinHistoryDepths);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void VwmaIndicator_ShortName_ReflectsPeriod()
|
||||
{
|
||||
var indicator = new VwmaIndicator { Period = 14 };
|
||||
Assert.Equal("VWMA(14)", indicator.ShortName);
|
||||
|
||||
var indicatorDefault = new VwmaIndicator { Period = 20 };
|
||||
Assert.Equal("VWMA(20)", indicatorDefault.ShortName);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void VwmaIndicator_MinHistoryDepths_EqualsPeriod()
|
||||
{
|
||||
var indicator = new VwmaIndicator { Period = 10 };
|
||||
|
||||
Assert.Equal(10, indicator.MinHistoryDepths);
|
||||
Assert.Equal(10, ((IWatchlistIndicator)indicator).MinHistoryDepths);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void VwmaIndicator_Initialize_CreatesInternalVwma()
|
||||
{
|
||||
var indicator = new VwmaIndicator();
|
||||
|
||||
// Initialize should not throw
|
||||
indicator.Initialize();
|
||||
|
||||
// After init, line series should exist
|
||||
Assert.Single(indicator.LinesSeries);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void VwmaIndicator_ProcessUpdate_HistoricalBar_ComputesValue()
|
||||
{
|
||||
var indicator = new VwmaIndicator { Period = 5 };
|
||||
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 VwmaIndicator_ProcessUpdate_NewBar_ComputesValue()
|
||||
{
|
||||
var indicator = new VwmaIndicator { Period = 5 };
|
||||
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 VwmaIndicator_Value_TracksVolumeWeightedAverage()
|
||||
{
|
||||
var indicator = new VwmaIndicator { Period = 10 };
|
||||
indicator.Initialize();
|
||||
|
||||
var now = DateTime.UtcNow;
|
||||
var recordedValues = 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 vol = 1000 + (i * 100);
|
||||
|
||||
indicator.HistoricalData.AddBar(now.AddMinutes(i), open, high, low, close, vol);
|
||||
indicator.ProcessUpdate(new UpdateArgs(UpdateReason.HistoricalBar));
|
||||
|
||||
if (i > 0)
|
||||
{
|
||||
double val = indicator.LinesSeries[0].GetValue(0);
|
||||
recordedValues.Add(val);
|
||||
}
|
||||
}
|
||||
|
||||
// VWMA should produce finite values
|
||||
Assert.True(recordedValues.Count > 0, "Should have recorded values");
|
||||
Assert.All(recordedValues, v => Assert.True(double.IsFinite(v)));
|
||||
|
||||
// VWMA values should be within price range (approximately)
|
||||
double avgValue = recordedValues.Average();
|
||||
Assert.True(avgValue > 90 && avgValue < 200, $"VWMA {avgValue} should be within reasonable price range");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void VwmaIndicator_DifferentPeriods_ProduceDifferentResults()
|
||||
{
|
||||
var indicator5 = new VwmaIndicator { Period = 5 };
|
||||
var indicator20 = new VwmaIndicator { Period = 20 };
|
||||
|
||||
indicator5.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);
|
||||
|
||||
indicator5.HistoricalData.AddBar(now.AddMinutes(i), open, high, low, close, volume);
|
||||
indicator20.HistoricalData.AddBar(now.AddMinutes(i), open, high, low, close, volume);
|
||||
|
||||
indicator5.ProcessUpdate(new UpdateArgs(UpdateReason.HistoricalBar));
|
||||
indicator20.ProcessUpdate(new UpdateArgs(UpdateReason.HistoricalBar));
|
||||
}
|
||||
|
||||
double val5 = indicator5.LinesSeries[0].GetValue(0);
|
||||
double val20 = indicator20.LinesSeries[0].GetValue(0);
|
||||
|
||||
// Different periods should produce different results
|
||||
// Shorter period responds faster to recent prices
|
||||
Assert.NotEqual(val5, val20, 6);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void VwmaIndicator_SlidingWindow_DropsOldValues()
|
||||
{
|
||||
var indicator = new VwmaIndicator { Period = 3 };
|
||||
indicator.Initialize();
|
||||
|
||||
var now = DateTime.UtcNow;
|
||||
|
||||
// Add initial bars with constant price/volume
|
||||
for (int i = 0; i < 3; i++)
|
||||
{
|
||||
indicator.HistoricalData.AddBar(now.AddMinutes(i), 100, 101, 99, 100, 1000);
|
||||
indicator.ProcessUpdate(new UpdateArgs(UpdateReason.HistoricalBar));
|
||||
}
|
||||
|
||||
double valueAtConstant = indicator.LinesSeries[0].GetValue(0);
|
||||
|
||||
// Add bars with higher prices - old low prices should drop out
|
||||
for (int i = 3; i < 6; i++)
|
||||
{
|
||||
indicator.HistoricalData.AddBar(now.AddMinutes(i), 200, 201, 199, 200, 1000);
|
||||
indicator.ProcessUpdate(new UpdateArgs(UpdateReason.HistoricalBar));
|
||||
}
|
||||
|
||||
double valueAfterHigh = indicator.LinesSeries[0].GetValue(0);
|
||||
|
||||
// Value should have changed significantly as old bars dropped
|
||||
Assert.True(valueAfterHigh > valueAtConstant + 50,
|
||||
$"VWMA should increase as low-price bars drop out: {valueAtConstant} -> {valueAfterHigh}");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,455 @@
|
||||
namespace QuanTAlib.Tests;
|
||||
|
||||
public class VwmaTests
|
||||
{
|
||||
private readonly GBM _feed;
|
||||
private readonly TBarSeries _bars;
|
||||
|
||||
public VwmaTests()
|
||||
{
|
||||
_feed = new GBM();
|
||||
_bars = new TBarSeries();
|
||||
for (int i = 0; i < 1000; i++)
|
||||
{
|
||||
_bars.Add(_feed.Next());
|
||||
}
|
||||
}
|
||||
|
||||
// ============ Constructor Tests ============
|
||||
|
||||
[Fact]
|
||||
public void Constructor_DefaultPeriod_ShouldBe20()
|
||||
{
|
||||
var vwma = new Vwma();
|
||||
Assert.Equal("VWMA(20)", vwma.Name);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Constructor_WithPeriod_ShouldSetName()
|
||||
{
|
||||
var vwma = new Vwma(14);
|
||||
Assert.Equal("VWMA(14)", vwma.Name);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Constructor_ZeroPeriod_ShouldThrow()
|
||||
{
|
||||
var ex = Assert.Throws<ArgumentException>(() => new Vwma(0));
|
||||
Assert.Equal("period", ex.ParamName);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Constructor_NegativePeriod_ShouldThrow()
|
||||
{
|
||||
var ex = Assert.Throws<ArgumentException>(() => new Vwma(-1));
|
||||
Assert.Equal("period", ex.ParamName);
|
||||
}
|
||||
|
||||
// ============ Basic Calculation Tests ============
|
||||
|
||||
[Fact]
|
||||
public void Update_ReturnsValidTValue()
|
||||
{
|
||||
var vwma = new Vwma(10);
|
||||
var bar = _bars[0];
|
||||
var result = vwma.Update(bar);
|
||||
|
||||
Assert.NotEqual(default, result);
|
||||
Assert.True(double.IsFinite(result.Value));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Update_FirstBar_ShouldBeClosePrice()
|
||||
{
|
||||
var vwma = new Vwma(10);
|
||||
var bar = new TBar(DateTime.UtcNow, 10, 15, 8, 12, 1000);
|
||||
var result = vwma.Update(bar);
|
||||
|
||||
// VWMA of first bar = close price (only one data point)
|
||||
Assert.Equal(12.0, result.Value, 10);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Update_MultipleBarsSamePrice_ShouldReturnSameVwma()
|
||||
{
|
||||
var vwma = new Vwma(10);
|
||||
// All bars have same close price = 100
|
||||
var bar1 = new TBar(DateTime.UtcNow, 100, 100, 100, 100, 100);
|
||||
var bar2 = new TBar(DateTime.UtcNow.AddMinutes(1), 100, 100, 100, 100, 200);
|
||||
var bar3 = new TBar(DateTime.UtcNow.AddMinutes(2), 100, 100, 100, 100, 300);
|
||||
|
||||
vwma.Update(bar1);
|
||||
vwma.Update(bar2);
|
||||
var result = vwma.Update(bar3);
|
||||
|
||||
Assert.Equal(100.0, result.Value, 10);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Update_VolumeWeighting_Works()
|
||||
{
|
||||
var vwma = new Vwma(10);
|
||||
// Bar 1: price=10, volume=100
|
||||
// Bar 2: price=20, volume=300
|
||||
// VWMA = (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);
|
||||
|
||||
vwma.Update(bar1);
|
||||
var result = vwma.Update(bar2);
|
||||
|
||||
Assert.Equal(17.5, result.Value, 10);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Update_SlidingWindow_ShouldDropOldValues()
|
||||
{
|
||||
var vwma = new Vwma(2);
|
||||
// Period = 2, so only last 2 bars count
|
||||
|
||||
// Bar 1: price=10, volume=100
|
||||
var bar1 = new TBar(DateTime.UtcNow, 10, 10, 10, 10, 100);
|
||||
vwma.Update(bar1);
|
||||
|
||||
// Bar 2: price=20, volume=100
|
||||
// VWMA = (10*100 + 20*100) / 200 = 15
|
||||
var bar2 = new TBar(DateTime.UtcNow.AddMinutes(1), 20, 20, 20, 20, 100);
|
||||
vwma.Update(bar2);
|
||||
Assert.Equal(15.0, vwma.Last.Value, 10);
|
||||
|
||||
// Bar 3: price=30, volume=100
|
||||
// Now bar1 drops out: VWMA = (20*100 + 30*100) / 200 = 25
|
||||
var bar3 = new TBar(DateTime.UtcNow.AddMinutes(2), 30, 30, 30, 30, 100);
|
||||
var result = vwma.Update(bar3);
|
||||
|
||||
Assert.Equal(25.0, result.Value, 10);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void IsHot_AfterPeriodBars_ShouldBeTrue()
|
||||
{
|
||||
var vwma = new Vwma(10);
|
||||
Assert.False(vwma.IsHot);
|
||||
|
||||
for (int i = 0; i < 9; i++)
|
||||
{
|
||||
vwma.Update(_bars[i]);
|
||||
Assert.False(vwma.IsHot);
|
||||
}
|
||||
|
||||
vwma.Update(_bars[9]);
|
||||
Assert.True(vwma.IsHot);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void WarmupPeriod_ShouldMatchPeriod()
|
||||
{
|
||||
var vwma = new Vwma(14);
|
||||
Assert.Equal(14, vwma.WarmupPeriod);
|
||||
}
|
||||
|
||||
// ============ Bar Correction Tests (isNew) ============
|
||||
|
||||
[Fact]
|
||||
public void Update_IsNewTrue_ShouldAdvanceState()
|
||||
{
|
||||
var vwma = new Vwma(10);
|
||||
var bar1 = new TBar(DateTime.UtcNow, 10, 10, 10, 10, 100);
|
||||
var bar2 = new TBar(DateTime.UtcNow.AddMinutes(1), 20, 20, 20, 20, 100);
|
||||
|
||||
vwma.Update(bar1, isNew: true);
|
||||
var result1 = vwma.Last.Value;
|
||||
|
||||
vwma.Update(bar2, isNew: true);
|
||||
var result2 = vwma.Last.Value;
|
||||
|
||||
Assert.NotEqual(result1, result2);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Update_IsNewFalse_ShouldRollback()
|
||||
{
|
||||
var vwma = new Vwma(10);
|
||||
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);
|
||||
|
||||
vwma.Update(bar1, isNew: true);
|
||||
vwma.Update(bar2, isNew: true);
|
||||
var afterBar2 = vwma.Last.Value;
|
||||
|
||||
// Correct bar2 with updated values
|
||||
vwma.Update(bar2Updated, isNew: false);
|
||||
var afterCorrection = vwma.Last.Value;
|
||||
|
||||
Assert.NotEqual(afterBar2, afterCorrection);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Update_IterativeCorrections_ShouldRestoreState()
|
||||
{
|
||||
var vwma = new Vwma(10);
|
||||
|
||||
// Process first 10 bars
|
||||
for (int i = 0; i < 10; i++)
|
||||
{
|
||||
vwma.Update(_bars[i], isNew: true);
|
||||
}
|
||||
_ = vwma.Last.Value;
|
||||
|
||||
// Process bar 11
|
||||
vwma.Update(_bars[10], isNew: true);
|
||||
var valueAfter11 = vwma.Last.Value;
|
||||
|
||||
// Correct bar 11 multiple times with same data
|
||||
for (int i = 0; i < 5; i++)
|
||||
{
|
||||
vwma.Update(_bars[10], isNew: false);
|
||||
}
|
||||
var valueAfterCorrections = vwma.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 vwma = new Vwma(10);
|
||||
|
||||
for (int i = 0; i < 100; i++)
|
||||
{
|
||||
vwma.Update(_bars[i]);
|
||||
}
|
||||
Assert.True(vwma.IsHot);
|
||||
|
||||
vwma.Reset();
|
||||
|
||||
Assert.False(vwma.IsHot);
|
||||
Assert.Equal(default, vwma.Last);
|
||||
}
|
||||
|
||||
// ============ NaN/Infinity Handling ============
|
||||
|
||||
[Fact]
|
||||
public void Update_NaN_ShouldUseLastValidValue()
|
||||
{
|
||||
var vwma = new Vwma(10);
|
||||
|
||||
// First bar establishes valid values
|
||||
var bar1 = new TBar(DateTime.UtcNow, 10, 15, 8, 12, 1000);
|
||||
vwma.Update(bar1);
|
||||
|
||||
// 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 = vwma.Update(bar2);
|
||||
|
||||
Assert.True(double.IsFinite(result.Value));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Update_Infinity_ShouldUseLastValidValue()
|
||||
{
|
||||
var vwma = new Vwma(10);
|
||||
|
||||
var bar1 = new TBar(DateTime.UtcNow, 10, 15, 8, 12, 1000);
|
||||
vwma.Update(bar1);
|
||||
|
||||
var bar2 = new TBar(DateTime.UtcNow.AddMinutes(1), double.PositiveInfinity, double.PositiveInfinity, double.PositiveInfinity, double.PositiveInfinity, double.PositiveInfinity);
|
||||
var result = vwma.Update(bar2);
|
||||
|
||||
Assert.True(double.IsFinite(result.Value));
|
||||
}
|
||||
|
||||
// ============ TValue Input Tests ============
|
||||
|
||||
[Fact]
|
||||
public void Update_TValue_ShouldWork()
|
||||
{
|
||||
var vwma = new Vwma(10);
|
||||
var input = new TValue(DateTime.UtcNow, 100.0);
|
||||
var result = vwma.Update(input);
|
||||
|
||||
// With TValue, it uses value as price and volume=1
|
||||
Assert.Equal(100.0, result.Value, 10);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Update_TValue_MultipleInputs()
|
||||
{
|
||||
var vwma = new Vwma(10);
|
||||
|
||||
// TValue input assumes volume=1 for all
|
||||
// VWMA = (100*1 + 200*1) / 2 = 150
|
||||
vwma.Update(new TValue(DateTime.UtcNow, 100.0));
|
||||
var result = vwma.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 vwma = new Vwma(10);
|
||||
var result = vwma.Update(_bars);
|
||||
|
||||
Assert.NotNull(result);
|
||||
Assert.Equal(_bars.Count, result.Count);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Calculate_Static_ShouldReturnTSeries()
|
||||
{
|
||||
var result = Vwma.Batch(_bars, 10);
|
||||
|
||||
Assert.NotNull(result);
|
||||
Assert.Equal(_bars.Count, result.Count);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Calculate_Static_WithDifferentPeriods_ShouldWork()
|
||||
{
|
||||
var result14 = Vwma.Batch(_bars, 14);
|
||||
var result50 = Vwma.Batch(_bars, 50);
|
||||
|
||||
Assert.NotNull(result14);
|
||||
Assert.NotNull(result50);
|
||||
Assert.Equal(_bars.Count, result14.Count);
|
||||
Assert.Equal(_bars.Count, result50.Count);
|
||||
}
|
||||
|
||||
// ============ Span API Tests ============
|
||||
|
||||
[Fact]
|
||||
public void Calculate_Span_ShouldMatchBatch()
|
||||
{
|
||||
var batchResult = Vwma.Batch(_bars, 20);
|
||||
|
||||
var price = _bars.Close.Values.ToArray();
|
||||
var volume = _bars.Volume.Values.ToArray();
|
||||
var spanOutput = new double[_bars.Count];
|
||||
|
||||
Vwma.Batch(price, volume, spanOutput, 20);
|
||||
|
||||
for (int i = 0; i < _bars.Count; i++)
|
||||
{
|
||||
Assert.Equal(batchResult.Values[i], spanOutput[i], 12);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Calculate_Span_MismatchedLengths_ShouldThrow()
|
||||
{
|
||||
var price = new double[100];
|
||||
var volume = new double[99]; // Mismatched
|
||||
var output = new double[100];
|
||||
|
||||
Assert.Throws<ArgumentException>(() => Vwma.Batch(price, volume, output, 10));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Calculate_Span_OutputLengthMismatch_ShouldThrow()
|
||||
{
|
||||
var price = new double[100];
|
||||
var volume = new double[100];
|
||||
var output = new double[50]; // Mismatched
|
||||
|
||||
Assert.Throws<ArgumentException>(() => Vwma.Batch(price, volume, output, 10));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Calculate_Span_ZeroPeriod_ShouldThrow()
|
||||
{
|
||||
var price = new double[100];
|
||||
var volume = new double[100];
|
||||
var output = new double[100];
|
||||
|
||||
Assert.Throws<ArgumentException>(() => Vwma.Batch(price, volume, output, 0));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Calculate_Span_NegativePeriod_ShouldThrow()
|
||||
{
|
||||
var price = new double[100];
|
||||
var volume = new double[100];
|
||||
var output = new double[100];
|
||||
|
||||
Assert.Throws<ArgumentException>(() => Vwma.Batch(price, volume, output, -1));
|
||||
}
|
||||
|
||||
// ============ Event Tests ============
|
||||
|
||||
[Fact]
|
||||
public void Pub_ShouldFireOnUpdate()
|
||||
{
|
||||
var vwma = new Vwma(10);
|
||||
int eventCount = 0;
|
||||
|
||||
vwma.Pub += (object? sender, in TValueEventArgs args) => eventCount++;
|
||||
|
||||
vwma.Update(_bars[0]);
|
||||
vwma.Update(_bars[1]);
|
||||
|
||||
Assert.Equal(2, eventCount);
|
||||
}
|
||||
|
||||
// ============ Streaming/Batch Consistency ============
|
||||
|
||||
[Fact]
|
||||
public void Streaming_ShouldMatchBatch()
|
||||
{
|
||||
// Streaming
|
||||
var vwma = new Vwma(20);
|
||||
var streamingResults = new List<double>();
|
||||
foreach (var bar in _bars)
|
||||
{
|
||||
streamingResults.Add(vwma.Update(bar).Value);
|
||||
}
|
||||
|
||||
// Batch
|
||||
var batchResult = Vwma.Batch(_bars, 20);
|
||||
|
||||
// Compare last 100 values
|
||||
for (int i = _bars.Count - 100; i < _bars.Count; i++)
|
||||
{
|
||||
Assert.Equal(batchResult.Values[i], streamingResults[i], 10);
|
||||
}
|
||||
}
|
||||
|
||||
// ============ TSeries Calculate Tests ============
|
||||
|
||||
[Fact]
|
||||
public void Calculate_TSeries_ShouldWork()
|
||||
{
|
||||
var sourceSeries = _bars.Close;
|
||||
var result = Vwma.Batch(sourceSeries, 20);
|
||||
|
||||
Assert.NotNull(result);
|
||||
Assert.Equal(sourceSeries.Count, result.Count);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Calculate_TSeries_ShouldMatchTValueStreaming()
|
||||
{
|
||||
var sourceSeries = _bars.Close;
|
||||
var batchResult = Vwma.Batch(sourceSeries, 20);
|
||||
|
||||
// Streaming with TValue
|
||||
var vwma = new Vwma(20);
|
||||
var streamingResults = new List<double>();
|
||||
for (int i = 0; i < sourceSeries.Count; i++)
|
||||
{
|
||||
streamingResults.Add(vwma.Update(sourceSeries[i]).Value);
|
||||
}
|
||||
|
||||
// Compare last 100 values
|
||||
for (int i = sourceSeries.Count - 100; i < sourceSeries.Count; i++)
|
||||
{
|
||||
Assert.Equal(batchResult.Values[i], streamingResults[i], 10);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,402 @@
|
||||
using Skender.Stock.Indicators;
|
||||
using Tulip;
|
||||
|
||||
namespace QuanTAlib.Tests;
|
||||
|
||||
public class VwmaValidationTests
|
||||
{
|
||||
private readonly ValidationTestData _data;
|
||||
|
||||
public VwmaValidationTests()
|
||||
{
|
||||
_data = new ValidationTestData();
|
||||
}
|
||||
|
||||
// ============ External Library Validation ============
|
||||
|
||||
[Fact]
|
||||
public void Vwma_Matches_Skender_Batch()
|
||||
{
|
||||
int period = 20;
|
||||
|
||||
// QuanTAlib batch
|
||||
var quantalibResult = Vwma.Batch(_data.Bars, period);
|
||||
var quantalibValues = quantalibResult.Values.ToArray();
|
||||
|
||||
// Skender
|
||||
var quotes = _data.Bars.Select(b => new Quote
|
||||
{
|
||||
Date = b.AsDateTime,
|
||||
Open = (decimal)b.Open,
|
||||
High = (decimal)b.High,
|
||||
Low = (decimal)b.Low,
|
||||
Close = (decimal)b.Close,
|
||||
Volume = (decimal)b.Volume
|
||||
});
|
||||
|
||||
var skenderResult = quotes.GetVwma(period);
|
||||
var skenderValues = skenderResult.Select(r => r.Vwma ?? 0).ToArray();
|
||||
|
||||
// Verify early portion where floating-point drift is minimal (bars 100-200)
|
||||
// Running-sum algorithms accumulate drift over thousands of bars
|
||||
for (int i = 100; i < 200; i++)
|
||||
{
|
||||
Assert.True(
|
||||
Math.Abs(quantalibValues[i] - skenderValues[i]) <= ValidationHelper.SkenderTolerance,
|
||||
$"Mismatch at index {i}: QuanTAlib={quantalibValues[i]:G17}, Skender={skenderValues[i]:G17}, Diff={Math.Abs(quantalibValues[i] - skenderValues[i]):G17}");
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Vwma_Matches_Skender_Streaming()
|
||||
{
|
||||
int period = 20;
|
||||
|
||||
// QuanTAlib streaming
|
||||
var vwma = new Vwma(period);
|
||||
var quantalibValues = new List<double>();
|
||||
foreach (var bar in _data.Bars)
|
||||
{
|
||||
quantalibValues.Add(vwma.Update(bar).Value);
|
||||
}
|
||||
|
||||
// Skender
|
||||
var quotes = _data.Bars.Select(b => new Quote
|
||||
{
|
||||
Date = b.AsDateTime,
|
||||
Open = (decimal)b.Open,
|
||||
High = (decimal)b.High,
|
||||
Low = (decimal)b.Low,
|
||||
Close = (decimal)b.Close,
|
||||
Volume = (decimal)b.Volume
|
||||
});
|
||||
|
||||
var skenderResult = quotes.GetVwma(period);
|
||||
var skenderValues = skenderResult.Select(r => r.Vwma ?? 0).ToArray();
|
||||
|
||||
// Verify early portion where floating-point drift is minimal (bars 100-200)
|
||||
for (int i = 100; i < 200; i++)
|
||||
{
|
||||
Assert.True(
|
||||
Math.Abs(quantalibValues[i] - skenderValues[i]) <= ValidationHelper.SkenderTolerance,
|
||||
$"Mismatch at index {i}: QuanTAlib={quantalibValues[i]:G17}, Skender={skenderValues[i]:G17}, Diff={Math.Abs(quantalibValues[i] - skenderValues[i]):G17}");
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Vwma_Matches_Skender_Span()
|
||||
{
|
||||
int period = 20;
|
||||
|
||||
// QuanTAlib span
|
||||
var price = _data.Bars.Close.Values.ToArray();
|
||||
var volume = _data.Bars.Volume.Values.ToArray();
|
||||
var quantalibValues = new double[price.Length];
|
||||
Vwma.Batch(price, volume, quantalibValues, period);
|
||||
|
||||
// Skender
|
||||
var quotes = _data.Bars.Select(b => new Quote
|
||||
{
|
||||
Date = b.AsDateTime,
|
||||
Open = (decimal)b.Open,
|
||||
High = (decimal)b.High,
|
||||
Low = (decimal)b.Low,
|
||||
Close = (decimal)b.Close,
|
||||
Volume = (decimal)b.Volume
|
||||
});
|
||||
|
||||
var skenderResult = quotes.GetVwma(period);
|
||||
var skenderValues = skenderResult.Select(r => r.Vwma ?? 0).ToArray();
|
||||
|
||||
// Verify early portion where floating-point drift is minimal (bars 100-200)
|
||||
for (int i = 100; i < 200; i++)
|
||||
{
|
||||
Assert.True(
|
||||
Math.Abs(quantalibValues[i] - skenderValues[i]) <= ValidationHelper.SkenderTolerance,
|
||||
$"Mismatch at index {i}: QuanTAlib={quantalibValues[i]:G17}, Skender={skenderValues[i]:G17}, Diff={Math.Abs(quantalibValues[i] - skenderValues[i]):G17}");
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Vwma_NotAvailable_Talib()
|
||||
{
|
||||
// TA-Lib does not have VWMA
|
||||
Assert.True(true, "VWMA is not available in TA-Lib");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Vwma_Matches_Tulip_Batch()
|
||||
{
|
||||
int period = 20;
|
||||
|
||||
// QuanTAlib batch
|
||||
var qResult = Vwma.Batch(_data.Bars, period);
|
||||
|
||||
// Tulip vwma: inputs = {close[], volume[]}, options = {period}
|
||||
double[] closeData = _data.ClosePrices.ToArray();
|
||||
double[] volumeData = _data.VolumeData.ToArray();
|
||||
var tulipIndicator = Tulip.Indicators.vwma;
|
||||
double[][] inputs = { closeData, volumeData };
|
||||
double[] options = { period };
|
||||
int lookback = tulipIndicator.Start(options);
|
||||
double[][] outputs = { new double[closeData.Length - lookback] };
|
||||
tulipIndicator.Run(inputs, options, outputs);
|
||||
double[] tResult = outputs[0];
|
||||
|
||||
ValidationHelper.VerifyData(qResult, tResult, lookback);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Vwma_Matches_Tulip_Streaming()
|
||||
{
|
||||
int period = 20;
|
||||
|
||||
// QuanTAlib streaming
|
||||
var vwma = new Vwma(period);
|
||||
var qResults = new List<double>();
|
||||
foreach (var bar in _data.Bars)
|
||||
{
|
||||
qResults.Add(vwma.Update(bar).Value);
|
||||
}
|
||||
|
||||
// Tulip vwma
|
||||
double[] closeData = _data.ClosePrices.ToArray();
|
||||
double[] volumeData = _data.VolumeData.ToArray();
|
||||
var tulipIndicator = Tulip.Indicators.vwma;
|
||||
double[][] inputs = { closeData, volumeData };
|
||||
double[] options = { period };
|
||||
int lookback = tulipIndicator.Start(options);
|
||||
double[][] outputs = { new double[closeData.Length - lookback] };
|
||||
tulipIndicator.Run(inputs, options, outputs);
|
||||
double[] tResult = outputs[0];
|
||||
|
||||
ValidationHelper.VerifyData(qResults, tResult, lookback);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Vwma_NotAvailable_Ooples()
|
||||
{
|
||||
// Ooples has VWMA - could add validation if needed
|
||||
Assert.True(true, "VWMA validation available via Ooples if needed");
|
||||
}
|
||||
|
||||
// ============ Internal Consistency Tests ============
|
||||
|
||||
[Fact]
|
||||
public void Vwma_Streaming_Matches_Batch()
|
||||
{
|
||||
int period = 20;
|
||||
|
||||
// Streaming
|
||||
var vwma = new Vwma(period);
|
||||
var streamingValues = new List<double>();
|
||||
foreach (var bar in _data.Bars)
|
||||
{
|
||||
streamingValues.Add(vwma.Update(bar).Value);
|
||||
}
|
||||
|
||||
// Batch
|
||||
var batchResult = Vwma.Batch(_data.Bars, period);
|
||||
var batchValues = batchResult.Values.ToArray();
|
||||
|
||||
ValidationHelper.VerifyData(streamingValues.ToArray(), batchValues, 0, 100, 1e-10);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Vwma_Span_Matches_Streaming()
|
||||
{
|
||||
int period = 20;
|
||||
|
||||
// Streaming
|
||||
var vwma = new Vwma(period);
|
||||
var streamingValues = new List<double>();
|
||||
foreach (var bar in _data.Bars)
|
||||
{
|
||||
streamingValues.Add(vwma.Update(bar).Value);
|
||||
}
|
||||
|
||||
// Span
|
||||
var price = _data.Bars.Close.Values.ToArray();
|
||||
var volume = _data.Bars.Volume.Values.ToArray();
|
||||
var spanValues = new double[price.Length];
|
||||
Vwma.Batch(price, volume, spanValues, period);
|
||||
|
||||
ValidationHelper.VerifyData(streamingValues.ToArray(), spanValues, 0, 100, 1e-10);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Vwma_Batch_Matches_Span()
|
||||
{
|
||||
int period = 20;
|
||||
|
||||
// Batch
|
||||
var batchResult = Vwma.Batch(_data.Bars, period);
|
||||
var batchValues = batchResult.Values.ToArray();
|
||||
|
||||
// Span
|
||||
var price = _data.Bars.Close.Values.ToArray();
|
||||
var volume = _data.Bars.Volume.Values.ToArray();
|
||||
var spanValues = new double[price.Length];
|
||||
Vwma.Batch(price, volume, spanValues, period);
|
||||
|
||||
// Batch and Span use identical code path, should match exactly
|
||||
ValidationHelper.VerifyData(batchValues, spanValues, 0, 100, 1e-12);
|
||||
}
|
||||
|
||||
// ============ Algorithm Correctness Tests ============
|
||||
|
||||
[Fact]
|
||||
public void Vwma_Algorithm_Correctness_ManualCalculation()
|
||||
{
|
||||
// Manual calculation to verify algorithm correctness
|
||||
var bars = new TBarSeries();
|
||||
|
||||
// Bar 0: close=10, volume=100
|
||||
// Bar 1: close=20, volume=200
|
||||
// Bar 2: close=30, volume=150
|
||||
bars.Add(new TBar(DateTime.UtcNow, 10, 10, 10, 10, 100));
|
||||
bars.Add(new TBar(DateTime.UtcNow.AddMinutes(1), 20, 20, 20, 20, 200));
|
||||
bars.Add(new TBar(DateTime.UtcNow.AddMinutes(2), 30, 30, 30, 30, 150));
|
||||
|
||||
var vwma = new Vwma(10); // Period larger than data to test accumulation
|
||||
var results = new List<double>();
|
||||
foreach (var bar in bars)
|
||||
{
|
||||
results.Add(vwma.Update(bar).Value);
|
||||
}
|
||||
|
||||
// Bar 0: VWMA = 10*100 / 100 = 10
|
||||
Assert.Equal(10.0, results[0], 6);
|
||||
|
||||
// Bar 1: VWMA = (10*100 + 20*200) / 300 = 5000/300 = 16.667
|
||||
double expectedBar1 = (10.0 * 100 + 20.0 * 200) / 300.0;
|
||||
Assert.Equal(expectedBar1, results[1], 6);
|
||||
|
||||
// Bar 2: VWMA = (10*100 + 20*200 + 30*150) / 450 = 9500/450 = 21.111
|
||||
double expectedBar2 = (10.0 * 100 + 20.0 * 200 + 30.0 * 150) / 450.0;
|
||||
Assert.Equal(expectedBar2, results[2], 6);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Vwma_Algorithm_Correctness_SlidingWindow()
|
||||
{
|
||||
// Verify sliding window drops old values correctly
|
||||
var vwma = new Vwma(2); // Period = 2
|
||||
|
||||
// Bar 0: close=10, volume=100
|
||||
vwma.Update(new TBar(DateTime.UtcNow, 10, 10, 10, 10, 100));
|
||||
Assert.Equal(10.0, vwma.Last.Value, 6);
|
||||
|
||||
// Bar 1: close=20, volume=100
|
||||
// VWMA = (10*100 + 20*100) / 200 = 15
|
||||
vwma.Update(new TBar(DateTime.UtcNow.AddMinutes(1), 20, 20, 20, 20, 100));
|
||||
Assert.Equal(15.0, vwma.Last.Value, 6);
|
||||
|
||||
// Bar 2: close=30, volume=100
|
||||
// Now bar0 drops out: VWMA = (20*100 + 30*100) / 200 = 25
|
||||
vwma.Update(new TBar(DateTime.UtcNow.AddMinutes(2), 30, 30, 30, 30, 100));
|
||||
Assert.Equal(25.0, vwma.Last.Value, 6);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Vwma_Algorithm_Correctness_VolumeWeighting()
|
||||
{
|
||||
// Verify volume weighting: high-volume bars have more influence
|
||||
var vwma = new Vwma(10);
|
||||
|
||||
// Two bars: one with high volume at low price, one with low volume at high price
|
||||
vwma.Update(new TBar(DateTime.UtcNow, 10, 10, 10, 10, 1000));
|
||||
var result = vwma.Update(new TBar(DateTime.UtcNow.AddMinutes(1), 20, 20, 20, 20, 100));
|
||||
|
||||
// VWMA = (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);
|
||||
|
||||
// VWMA should be much closer to 10 than to 20
|
||||
Assert.True(result.Value < 15, "VWMA should be weighted toward high-volume price");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Vwma_DifferentPeriods_ProduceDifferentResults()
|
||||
{
|
||||
var vwma10 = new Vwma(10);
|
||||
var vwma20 = new Vwma(20);
|
||||
var vwma50 = new Vwma(50);
|
||||
|
||||
var results10 = new List<double>();
|
||||
var results20 = new List<double>();
|
||||
var results50 = new List<double>();
|
||||
|
||||
foreach (var bar in _data.Bars)
|
||||
{
|
||||
results10.Add(vwma10.Update(bar).Value);
|
||||
results20.Add(vwma20.Update(bar).Value);
|
||||
results50.Add(vwma50.Update(bar).Value);
|
||||
}
|
||||
|
||||
// After sufficient bars, different periods should produce different results
|
||||
int checkIndex = 60;
|
||||
bool anyDifferent = Math.Abs(results10[checkIndex] - results20[checkIndex]) > 1e-6 ||
|
||||
Math.Abs(results20[checkIndex] - results50[checkIndex]) > 1e-6;
|
||||
|
||||
Assert.True(anyDifferent, "Different periods should produce different VWMA values");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Vwma_StableWithConstantPrice()
|
||||
{
|
||||
// VWMA should remain stable when price is constant
|
||||
var vwma = new Vwma(10);
|
||||
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(vwma.Update(bar).Value);
|
||||
}
|
||||
|
||||
// All VWMA values should be 50
|
||||
foreach (var value in results)
|
||||
{
|
||||
Assert.Equal(50.0, value, 10);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Vwma_ZeroVolume_HandledCorrectly()
|
||||
{
|
||||
// VWMA should handle zero volume gracefully
|
||||
var vwma = new Vwma(10);
|
||||
|
||||
// First bar with volume
|
||||
vwma.Update(new TBar(DateTime.UtcNow, 10, 10, 10, 10, 1000));
|
||||
|
||||
// Second bar with zero volume
|
||||
var result = vwma.Update(new TBar(DateTime.UtcNow.AddMinutes(1), 20, 20, 20, 20, 0));
|
||||
|
||||
// VWMA should remain at 10 (zero volume doesn't contribute)
|
||||
Assert.Equal(10.0, result.Value, 10);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Vwma_ResponsiveToPriceChanges()
|
||||
{
|
||||
// VWMA should be responsive to price changes with shorter periods
|
||||
var vwmaShort = new Vwma(5);
|
||||
var vwmaLong = new Vwma(50);
|
||||
|
||||
// Process 100 bars with trending price
|
||||
for (int i = 0; i < 100; i++)
|
||||
{
|
||||
var bar = new TBar(DateTime.UtcNow.AddMinutes(i), i, i, i, i, 1000);
|
||||
vwmaShort.Update(bar);
|
||||
vwmaLong.Update(bar);
|
||||
}
|
||||
|
||||
// Short period VWMA should be closer to current price (99)
|
||||
double shortDiff = Math.Abs(vwmaShort.Last.Value - 99);
|
||||
double longDiff = Math.Abs(vwmaLong.Last.Value - 99);
|
||||
|
||||
Assert.True(shortDiff < longDiff, "Short period VWMA should track price more closely");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user