mirror of
https://github.com/mihakralj/QuanTAlib.git
synced 2026-08-22 20:48:04 +00:00
volume category touchup
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,54 @@
|
||||
using System.Drawing;
|
||||
using System.Runtime.CompilerServices;
|
||||
using TradingPlatform.BusinessLayer;
|
||||
|
||||
namespace QuanTAlib;
|
||||
|
||||
/// <summary>
|
||||
/// Quantower adapter for VWAP (Volume Weighted Average Price).
|
||||
/// </summary>
|
||||
[SkipLocalsInit]
|
||||
public sealed class VwapIndicator : Indicator, IWatchlistIndicator
|
||||
{
|
||||
[InputParameter("Period (0 = no reset)", sortIndex: 10, 0, 10000, 1, 0)]
|
||||
public int Period { get; set; }
|
||||
|
||||
[InputParameter("Show cold values", sortIndex: 21)]
|
||||
public bool ShowColdValues { get; set; } = true;
|
||||
|
||||
private Vwap _vwap = null!;
|
||||
private readonly LineSeries _series;
|
||||
|
||||
public int MinHistoryDepths => Period > 0 ? Period : 1;
|
||||
int IWatchlistIndicator.MinHistoryDepths => MinHistoryDepths;
|
||||
|
||||
public override string ShortName => Period > 0 ? $"VWAP({Period})" : "VWAP";
|
||||
public override string SourceCodeLink => "https://github.com/mihakralj/QuanTAlib/blob/main/lib/volume/vwap/Vwap.Quantower.cs";
|
||||
|
||||
public VwapIndicator()
|
||||
{
|
||||
OnBackGround = true;
|
||||
SeparateWindow = false;
|
||||
Name = "VWAP - Volume Weighted Average Price";
|
||||
Description = "Volume Weighted Average Price calculates the average price weighted by volume";
|
||||
|
||||
_series = new LineSeries(name: "VWAP", color: Color.Yellow, width: 2, style: LineStyle.Solid);
|
||||
AddLineSeries(_series);
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
protected override void OnInit()
|
||||
{
|
||||
_vwap = new Vwap(Period);
|
||||
base.OnInit();
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
protected override void OnUpdate(UpdateArgs args)
|
||||
{
|
||||
TBar bar = this.GetInputBar(args);
|
||||
TValue result = _vwap.Update(bar, args.IsNewBar());
|
||||
|
||||
_series.SetValue(result.Value, _vwap.IsHot, ShowColdValues);
|
||||
}
|
||||
}
|
||||
@@ -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.Calculate(_bars);
|
||||
|
||||
Assert.NotNull(result);
|
||||
Assert.Equal(_bars.Count, result.Count);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Calculate_Static_WithPeriod_ShouldWork()
|
||||
{
|
||||
var result = Vwap.Calculate(_bars, 100);
|
||||
|
||||
Assert.NotNull(result);
|
||||
Assert.Equal(_bars.Count, result.Count);
|
||||
}
|
||||
|
||||
// ============ Span API Tests ============
|
||||
|
||||
[Fact]
|
||||
public void Calculate_Span_ShouldMatchBatch()
|
||||
{
|
||||
var batchResult = Vwap.Calculate(_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.Calculate(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.Calculate(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.Calculate(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.Calculate(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.Calculate(_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.Calculate(_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.Calculate(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.Calculate(_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.Calculate(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);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,371 @@
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace QuanTAlib;
|
||||
|
||||
/// <summary>
|
||||
/// Computes the Volume Weighted Average Price (VWAP) with optional periodic reset.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// VWAP uses the typical price <c>(High + Low + Close) / 3</c> weighted by volume:
|
||||
/// <c>VWAP = Σ(typicalPrice × volume) / Σ(volume)</c>.
|
||||
///
|
||||
/// This implementation supports cumulative mode (<c>period=0</c>) or periodic reset
|
||||
/// for session-based analysis. Commonly used by institutional traders for execution benchmarking.
|
||||
/// Non-finite inputs (NaN/±Inf) are sanitized by substituting the last finite value observed.
|
||||
///
|
||||
/// For the authoritative algorithm reference, full rationale, and behavioral contracts, see the
|
||||
/// companion files in the same directory.
|
||||
/// </remarks>
|
||||
/// <seealso href="Vwap.md">Detailed documentation</seealso>
|
||||
/// <seealso href="vwap.pine">Reference Pine Script implementation</seealso>
|
||||
[SkipLocalsInit]
|
||||
public sealed class Vwap : ITValuePublisher
|
||||
{
|
||||
[StructLayout(LayoutKind.Auto)]
|
||||
private record struct State(double SumPV, double SumVol, int Index, int BarsSinceReset)
|
||||
{
|
||||
public static State New() => new() { SumPV = 0, SumVol = 0, Index = 0, BarsSinceReset = 0 };
|
||||
}
|
||||
|
||||
private readonly int _period;
|
||||
private State _state = State.New();
|
||||
private State _p_state = State.New();
|
||||
private double _lastValidHigh;
|
||||
private double _lastValidLow;
|
||||
private double _lastValidClose;
|
||||
private double _lastValidVolume;
|
||||
private double _p_lastValidHigh;
|
||||
private double _p_lastValidLow;
|
||||
private double _p_lastValidClose;
|
||||
private double _p_lastValidVolume;
|
||||
|
||||
/// <summary>
|
||||
/// Display name for the indicator.
|
||||
/// </summary>
|
||||
public string Name { get; }
|
||||
|
||||
public event TValuePublishedHandler? Pub;
|
||||
|
||||
/// <summary>
|
||||
/// Current VWAP value.
|
||||
/// </summary>
|
||||
public TValue Last { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// True if the indicator has processed at least one bar.
|
||||
/// </summary>
|
||||
public bool IsHot => _state.Index > 0;
|
||||
|
||||
/// <summary>
|
||||
/// Warmup period: 1 bar needed for first valid value.
|
||||
/// </summary>
|
||||
// S2325 suppressed: Instance property required for interface consistency across all indicators,
|
||||
// even when value is constant. All QuanTAlib indicators expose WarmupPeriod as instance property.
|
||||
#pragma warning disable S2325
|
||||
public int WarmupPeriod => 1;
|
||||
#pragma warning restore S2325
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new VWAP indicator with period-based reset.
|
||||
/// </summary>
|
||||
/// <param name="period">Period for VWAP reset (0 = no reset/cumulative). Default: 390 (typical trading day in minutes)</param>
|
||||
/// <exception cref="ArgumentException">Thrown when period is negative.</exception>
|
||||
public Vwap(int period = 0)
|
||||
{
|
||||
if (period < 0)
|
||||
{
|
||||
throw new ArgumentException("Period must be >= 0 (0 = no reset)", nameof(period));
|
||||
}
|
||||
|
||||
_period = period;
|
||||
Name = period == 0 ? "VWAP" : $"VWAP({period})";
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Resets the indicator state.
|
||||
/// </summary>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public void Reset()
|
||||
{
|
||||
_state = State.New();
|
||||
_p_state = State.New();
|
||||
_lastValidHigh = 0;
|
||||
_lastValidLow = 0;
|
||||
_lastValidClose = 0;
|
||||
_lastValidVolume = 0;
|
||||
_p_lastValidHigh = 0;
|
||||
_p_lastValidLow = 0;
|
||||
_p_lastValidClose = 0;
|
||||
_p_lastValidVolume = 0;
|
||||
Last = default;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
private static double GetValidValue(double input, ref double lastValid)
|
||||
{
|
||||
if (double.IsFinite(input))
|
||||
{
|
||||
lastValid = input;
|
||||
return input;
|
||||
}
|
||||
return lastValid;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
|
||||
public TValue Update(TBar input, bool isNew = true)
|
||||
{
|
||||
if (isNew)
|
||||
{
|
||||
_p_state = _state;
|
||||
_p_lastValidHigh = _lastValidHigh;
|
||||
_p_lastValidLow = _lastValidLow;
|
||||
_p_lastValidClose = _lastValidClose;
|
||||
_p_lastValidVolume = _lastValidVolume;
|
||||
}
|
||||
else
|
||||
{
|
||||
_state = _p_state;
|
||||
_lastValidHigh = _p_lastValidHigh;
|
||||
_lastValidLow = _p_lastValidLow;
|
||||
_lastValidClose = _p_lastValidClose;
|
||||
_lastValidVolume = _p_lastValidVolume;
|
||||
}
|
||||
|
||||
// Get valid OHLCV values
|
||||
double high = GetValidValue(input.High, ref _lastValidHigh);
|
||||
double low = GetValidValue(input.Low, ref _lastValidLow);
|
||||
double close = GetValidValue(input.Close, ref _lastValidClose);
|
||||
double volume = GetValidValue(input.Volume, ref _lastValidVolume);
|
||||
|
||||
// Calculate typical price (hlc3)
|
||||
double typicalPrice = (high + low + close) / 3.0;
|
||||
|
||||
// Local copy for struct promotion
|
||||
var s = _state;
|
||||
|
||||
// Check for period reset
|
||||
bool shouldReset = _period > 0 && s.BarsSinceReset >= _period;
|
||||
if (shouldReset)
|
||||
{
|
||||
s.SumPV = 0;
|
||||
s.SumVol = 0;
|
||||
s.BarsSinceReset = 0;
|
||||
}
|
||||
|
||||
// Update cumulative sums
|
||||
if (volume > 0)
|
||||
{
|
||||
s.SumPV += typicalPrice * volume;
|
||||
s.SumVol += volume;
|
||||
}
|
||||
|
||||
// Calculate VWAP
|
||||
double vwap = s.SumVol > double.Epsilon ? s.SumPV / s.SumVol : typicalPrice;
|
||||
|
||||
if (isNew)
|
||||
{
|
||||
s.Index++;
|
||||
s.BarsSinceReset++;
|
||||
}
|
||||
|
||||
_state = s;
|
||||
|
||||
Last = new TValue(input.Time, vwap);
|
||||
Pub?.Invoke(this, new TValueEventArgs { Value = Last, IsNew = isNew });
|
||||
return Last;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Updates VWAP with a TValue input (uses value as both price and assumes volume=1).
|
||||
/// </summary>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
|
||||
public TValue Update(TValue input, bool isNew = true)
|
||||
{
|
||||
// Create synthetic bar: price as close, high, low; volume = 1
|
||||
var bar = new TBar(input.Time, input.Value, input.Value, input.Value, input.Value, 1.0);
|
||||
return Update(bar, isNew);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Calculates VWAP for an entire bar series.
|
||||
/// </summary>
|
||||
/// <param name="source">Source bar series</param>
|
||||
/// <returns>TSeries containing VWAP values</returns>
|
||||
public TSeries Update(TBarSeries source)
|
||||
{
|
||||
if (source.Count == 0)
|
||||
{
|
||||
return [];
|
||||
}
|
||||
|
||||
var t = new List<long>(source.Count);
|
||||
var v = new List<double>(source.Count);
|
||||
|
||||
Reset();
|
||||
|
||||
for (int i = 0; i < source.Count; i++)
|
||||
{
|
||||
var val = Update(source[i], isNew: true);
|
||||
t.Add(val.Time);
|
||||
v.Add(val.Value);
|
||||
}
|
||||
|
||||
return new TSeries(t, v);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Static calculation returning TSeries.
|
||||
/// </summary>
|
||||
/// <param name="source">Source bar series</param>
|
||||
/// <param name="period">Period for VWAP reset (0 = no reset)</param>
|
||||
/// <returns>TSeries containing VWAP values</returns>
|
||||
public static TSeries Calculate(TBarSeries source, int period = 0)
|
||||
{
|
||||
if (source.Count == 0)
|
||||
{
|
||||
return [];
|
||||
}
|
||||
|
||||
var t = source.Open.Times.ToArray();
|
||||
var v = new double[source.Count];
|
||||
|
||||
Calculate(source.High.Values, source.Low.Values, source.Close.Values, source.Volume.Values, v, period);
|
||||
|
||||
return new TSeries(t, v);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Zero-allocation span-based calculation.
|
||||
/// </summary>
|
||||
/// <param name="high">High prices</param>
|
||||
/// <param name="low">Low prices</param>
|
||||
/// <param name="close">Close prices</param>
|
||||
/// <param name="volume">Volume values</param>
|
||||
/// <param name="output">Output span for VWAP values</param>
|
||||
/// <param name="period">Period for VWAP reset (0 = no reset)</param>
|
||||
[MethodImpl(MethodImplOptions.AggressiveOptimization)]
|
||||
public static void Calculate(ReadOnlySpan<double> high, ReadOnlySpan<double> low, ReadOnlySpan<double> close, ReadOnlySpan<double> volume, Span<double> output, int period = 0)
|
||||
{
|
||||
if (high.Length != low.Length)
|
||||
{
|
||||
throw new ArgumentException("High and Low spans must be of the same length", nameof(low));
|
||||
}
|
||||
|
||||
if (high.Length != close.Length)
|
||||
{
|
||||
throw new ArgumentException("High and Close spans must be of the same length", nameof(close));
|
||||
}
|
||||
|
||||
if (high.Length != volume.Length)
|
||||
{
|
||||
throw new ArgumentException("High and Volume spans must be of the same length", nameof(volume));
|
||||
}
|
||||
|
||||
if (high.Length != output.Length)
|
||||
{
|
||||
throw new ArgumentException("Output span must be of the same length as input", nameof(output));
|
||||
}
|
||||
|
||||
if (period < 0)
|
||||
{
|
||||
throw new ArgumentException("Period must be >= 0 (0 = no reset)", nameof(period));
|
||||
}
|
||||
|
||||
int len = high.Length;
|
||||
if (len == 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
double sumPV = 0;
|
||||
double sumVol = 0;
|
||||
double lastValidHigh = 0;
|
||||
double lastValidLow = 0;
|
||||
double lastValidClose = 0;
|
||||
double lastValidVolume = 0;
|
||||
int barsSinceReset = 0;
|
||||
|
||||
// Find first valid values
|
||||
for (int k = 0; k < len; k++)
|
||||
{
|
||||
if (double.IsFinite(high[k]))
|
||||
{
|
||||
lastValidHigh = high[k];
|
||||
break;
|
||||
}
|
||||
}
|
||||
for (int k = 0; k < len; k++)
|
||||
{
|
||||
if (double.IsFinite(low[k]))
|
||||
{
|
||||
lastValidLow = low[k];
|
||||
break;
|
||||
}
|
||||
}
|
||||
for (int k = 0; k < len; k++)
|
||||
{
|
||||
if (double.IsFinite(close[k]))
|
||||
{
|
||||
lastValidClose = close[k];
|
||||
break;
|
||||
}
|
||||
}
|
||||
for (int k = 0; k < len; k++)
|
||||
{
|
||||
if (double.IsFinite(volume[k]))
|
||||
{
|
||||
lastValidVolume = volume[k];
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
for (int i = 0; i < len; i++)
|
||||
{
|
||||
// Get valid values with NaN substitution
|
||||
double h = double.IsFinite(high[i]) ? high[i] : lastValidHigh;
|
||||
double l = double.IsFinite(low[i]) ? low[i] : lastValidLow;
|
||||
double c = double.IsFinite(close[i]) ? close[i] : lastValidClose;
|
||||
double vol = double.IsFinite(volume[i]) ? volume[i] : lastValidVolume;
|
||||
|
||||
if (double.IsFinite(high[i]))
|
||||
{
|
||||
lastValidHigh = high[i];
|
||||
}
|
||||
if (double.IsFinite(low[i]))
|
||||
{
|
||||
lastValidLow = low[i];
|
||||
}
|
||||
if (double.IsFinite(close[i]))
|
||||
{
|
||||
lastValidClose = close[i];
|
||||
}
|
||||
if (double.IsFinite(volume[i]))
|
||||
{
|
||||
lastValidVolume = volume[i];
|
||||
}
|
||||
|
||||
// Calculate typical price (hlc3)
|
||||
double typicalPrice = (h + l + c) / 3.0;
|
||||
|
||||
// Check for period reset
|
||||
if (period > 0 && barsSinceReset >= period)
|
||||
{
|
||||
sumPV = 0;
|
||||
sumVol = 0;
|
||||
barsSinceReset = 0;
|
||||
}
|
||||
|
||||
// Update cumulative sums
|
||||
if (vol > 0)
|
||||
{
|
||||
sumPV += typicalPrice * vol;
|
||||
sumVol += vol;
|
||||
}
|
||||
|
||||
// Calculate VWAP
|
||||
output[i] = sumVol > double.Epsilon ? sumPV / sumVol : typicalPrice;
|
||||
barsSinceReset++;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,170 @@
|
||||
# VWAP: Volume Weighted Average Price
|
||||
|
||||
> "VWAP doesn't predict where price will go—it reveals where institutional money has already committed."
|
||||
|
||||
VWAP (Volume Weighted Average Price) calculates the cumulative average price weighted by trading volume, typically reset at session boundaries. It represents the true average price at which a security has traded throughout the period, giving more weight to prices where higher volume occurred. This implementation supports flexible period-based resets rather than traditional session-based anchoring.
|
||||
|
||||
## Historical Context
|
||||
|
||||
VWAP emerged in the 1980s as institutional traders sought benchmarks for execution quality. Before electronic trading, large orders moved markets significantly, and traders needed a way to measure whether their executions were favorable relative to the day's overall trading activity.
|
||||
|
||||
The concept gained prominence with the rise of algorithmic trading in the 1990s. Portfolio managers began using VWAP as a benchmark for their brokers—if you bought shares at a price below VWAP, you outperformed the average buyer that day. This created an entire industry of "VWAP execution algorithms" designed to spread large orders across time to minimize market impact.
|
||||
|
||||
Traditional implementations anchor VWAP to market session boundaries (daily, weekly, monthly). This QuanTAlib implementation extends the concept with configurable period-based resets, enabling intraday applications and backtesting scenarios where session boundaries aren't meaningful.
|
||||
|
||||
## Architecture & Physics
|
||||
|
||||
VWAP operates as a cumulative weighted average with optional periodic resets.
|
||||
|
||||
### 1. Typical Price Calculation
|
||||
|
||||
The typical price (HLC3) represents the central tendency of each bar:
|
||||
|
||||
$$
|
||||
TP_t = \frac{High_t + Low_t + Close_t}{3}
|
||||
$$
|
||||
|
||||
HLC3 is preferred over close-only pricing because it captures intrabar price discovery, particularly important for high-volume bars where significant trading occurred across the price range.
|
||||
|
||||
### 2. Cumulative Sums
|
||||
|
||||
VWAP maintains two running totals:
|
||||
|
||||
$$
|
||||
\sum PV_t = \sum_{i=start}^{t} (TP_i \times V_i)
|
||||
$$
|
||||
|
||||
$$
|
||||
\sum V_t = \sum_{i=start}^{t} V_i
|
||||
$$
|
||||
|
||||
where $start$ is either the beginning of the series or the last reset point.
|
||||
|
||||
### 3. VWAP Calculation
|
||||
|
||||
$$
|
||||
VWAP_t = \frac{\sum PV_t}{\sum V_t}
|
||||
$$
|
||||
|
||||
When $\sum V_t = 0$ (no volume), VWAP returns the current typical price as a fallback.
|
||||
|
||||
### 4. Period Reset Mechanism
|
||||
|
||||
When period > 0, resets occur every N bars:
|
||||
|
||||
$$
|
||||
\text{if } (barsSinceReset \geq period) \rightarrow \text{Reset } \sum PV, \sum V
|
||||
$$
|
||||
|
||||
This enables:
|
||||
- Intraday VWAP (e.g., period=78 for hourly on 5-min chart)
|
||||
- Rolling VWAP windows for regime detection
|
||||
- Backtesting without session boundary dependencies
|
||||
|
||||
## Mathematical Foundation
|
||||
|
||||
### Weighted Average Property
|
||||
|
||||
VWAP is mathematically equivalent to:
|
||||
|
||||
$$
|
||||
VWAP = \frac{\sum_{i=1}^{n} w_i \cdot P_i}{\sum_{i=1}^{n} w_i}
|
||||
$$
|
||||
|
||||
where weights $w_i = V_i$. This makes VWAP a proper weighted arithmetic mean, inheriting all standard properties:
|
||||
- **Bounded**: $\min(TP) \leq VWAP \leq \max(TP)$
|
||||
- **Linear**: VWAP scales proportionally with prices
|
||||
- **Volume-invariant**: Doubling all volumes produces identical VWAP
|
||||
|
||||
### Incremental Update
|
||||
|
||||
For streaming calculation, the incremental form avoids recomputation:
|
||||
|
||||
$$
|
||||
\sum PV_t = \sum PV_{t-1} + TP_t \cdot V_t
|
||||
$$
|
||||
|
||||
$$
|
||||
\sum V_t = \sum V_{t-1} + V_t
|
||||
$$
|
||||
|
||||
This yields O(1) time complexity per bar regardless of history length.
|
||||
|
||||
### Zero-Volume Handling
|
||||
|
||||
When $V_t = 0$:
|
||||
- Bar contributes nothing to cumulative sums
|
||||
- VWAP remains unchanged from previous value
|
||||
- If all volume is zero, VWAP defaults to typical price
|
||||
|
||||
## Performance Profile
|
||||
|
||||
### Operation Count (Streaming Mode)
|
||||
|
||||
| Operation | Count | Cost (cycles) | Subtotal |
|
||||
| :--- | :---: | :---: | :---: |
|
||||
| ADD | 5 | 1 | 5 |
|
||||
| MUL | 1 | 3 | 3 |
|
||||
| DIV | 2 | 15 | 30 |
|
||||
| CMP | 3 | 1 | 3 |
|
||||
| **Total** | **11** | — | **~41 cycles** |
|
||||
|
||||
Division dominates the cost profile (73% of cycles).
|
||||
|
||||
### Batch Mode (SIMD Potential)
|
||||
|
||||
VWAP's cumulative nature limits SIMD parallelization. However, the typical price calculation can be vectorized:
|
||||
|
||||
| Operation | Scalar Ops | SIMD Ops (AVX2) | Speedup |
|
||||
| :--- | :---: | :---: | :---: |
|
||||
| TP calculation | 3N | N/4 | 12× |
|
||||
| Cumulative sum | N | N | 1× |
|
||||
|
||||
**Net improvement**: ~15% for batch mode due to cumulative dependency limiting parallelism.
|
||||
|
||||
### Memory Footprint
|
||||
|
||||
- **Streaming**: 64 bytes (State struct + 4 lastValid doubles)
|
||||
- **No buffer required**: Cumulative nature eliminates sliding window storage
|
||||
- **Period tracking**: +4 bytes for barsSinceReset counter
|
||||
|
||||
### Quality Metrics
|
||||
|
||||
| Metric | Score | Notes |
|
||||
| :--- | :---: | :--- |
|
||||
| **Accuracy** | 10/10 | Exact weighted average, no approximation |
|
||||
| **Timeliness** | 8/10 | Lags during trends (by design) |
|
||||
| **Stability** | 9/10 | Smooth; resets can cause jumps |
|
||||
| **Interpretability** | 10/10 | Clear economic meaning |
|
||||
|
||||
## Validation
|
||||
|
||||
| Library | Status | Notes |
|
||||
| :--- | :---: | :--- |
|
||||
| **TA-Lib** | N/A | Not implemented |
|
||||
| **Skender** | ⚠️ | Session-anchored, different reset model |
|
||||
| **Tulip** | N/A | Not implemented |
|
||||
| **Ooples** | ⚠️ | Implementation may differ |
|
||||
| **Self-consistency** | ✅ | Streaming/Batch/Span modes match |
|
||||
|
||||
VWAP implementations vary primarily in reset behavior. This implementation uses period-based resets for maximum flexibility, while most others use calendar-based session anchoring.
|
||||
|
||||
## Common Pitfalls
|
||||
|
||||
1. **Session vs Period Confusion**: Traditional VWAP resets at market open. This implementation uses bar-count periods. For session VWAP, set period to match your session length in bars (e.g., 390 for US equities on 1-minute data).
|
||||
|
||||
2. **Cumulative Error Accumulation**: While mathematically exact, floating-point arithmetic accumulates error over thousands of bars. Difference of ~1e-10 per 5000 bars is typical and acceptable.
|
||||
|
||||
3. **Zero Volume Bars**: Bars with zero volume don't affect VWAP. This is correct behavior—no trades means no price discovery contribution.
|
||||
|
||||
4. **Intraday Interpretation**: VWAP is most meaningful when reset at consistent intervals. Comparing VWAP values across different reset periods is not meaningful.
|
||||
|
||||
5. **Reset Timing**: Reset occurs BEFORE processing the bar that triggers it. Bar at index `period` starts fresh accumulation.
|
||||
|
||||
6. **TValue API Limitation**: When using `Update(TValue)`, a synthetic bar is created with the value as all OHLC prices and volume=1. This works for simple averaging but loses volume weighting benefits.
|
||||
|
||||
## References
|
||||
|
||||
- Berkowitz, S., Logue, D., & Noser, E. (1988). "The Total Cost of Transactions on the NYSE." *Journal of Finance*.
|
||||
- Madhavan, A. (2002). "VWAP Strategies." *Trading*, Spring 2002.
|
||||
- Kissell, R. (2006). "The Science of Algorithmic Trading and Portfolio Management." *Academic Press*.
|
||||
Reference in New Issue
Block a user