normalization of methods

This commit is contained in:
Miha Kralj
2026-02-10 21:33:16 -08:00
parent 915d7a007b
commit 6d6259a47d
527 changed files with 10525 additions and 2123 deletions
+10 -10
View File
@@ -228,7 +228,7 @@ public class VwadTests
bars.Add(new TBar(time.AddMinutes(1), 10, 12, 8, 12, 200));
bars.Add(new TBar(time.AddMinutes(2), 12, 12, 8, 8, 100));
var result = Vwad.Calculate(bars, 3);
var result = Vwad.Batch(bars, 3);
Assert.Equal(3, result.Count);
}
@@ -242,7 +242,7 @@ public class VwadTests
double[] volume = [100, 200, 100];
double[] output = new double[3];
Vwad.Calculate(high, low, close, volume, output, 3);
Vwad.Batch(high, low, close, volume, output, 3);
// Bar 0: MFM=0, Vol=100, SumVol=100, VolWeight=1, WeightedMFV=0
Assert.Equal(0, output[0]);
@@ -268,7 +268,7 @@ public class VwadTests
double[] output = new double[2];
Assert.Throws<ArgumentException>(() =>
Vwad.Calculate(high, low, close, volume, output, 3));
Vwad.Batch(high, low, close, volume, output, 3));
}
[Fact]
@@ -281,14 +281,14 @@ public class VwadTests
double[] output = new double[1];
Assert.Throws<ArgumentException>(() =>
Vwad.Calculate(high, low, close, volume, output, 0));
Vwad.Batch(high, low, close, volume, output, 0));
}
[Fact]
public void Vwad_Calculate_EmptySeries_ReturnsEmpty()
{
var bars = new TBarSeries();
var result = Vwad.Calculate(bars);
var result = Vwad.Batch(bars);
Assert.Empty(result);
}
@@ -312,7 +312,7 @@ public class VwadTests
}
// Batch
var batchResult = Vwad.Calculate(bars, 20);
var batchResult = Vwad.Batch(bars, 20);
// Compare all values
for (int i = 0; i < 100; i++)
@@ -366,7 +366,7 @@ public class VwadTests
double[] volume = [100, 200, 100, double.PositiveInfinity, 100];
double[] output = new double[5];
Vwad.Calculate(high, low, close, volume, output, 3);
Vwad.Batch(high, low, close, volume, output, 3);
// All outputs should be finite
foreach (var val in output)
@@ -386,7 +386,7 @@ public class VwadTests
bars.Add(gbm.Next());
}
var result = Vwad.Calculate(bars, 10);
var result = Vwad.Batch(bars, 10);
// In a bullish trend, VWAD should generally be positive and growing
// (this is a statistical expectation, not a guarantee)
@@ -406,12 +406,12 @@ public class VwadTests
var bars = gbm.Fetch(100, DateTime.UtcNow.Ticks, TimeSpan.FromMinutes(1));
// 1. Batch Mode
var batchSeries = Vwad.Calculate(bars, period);
var batchSeries = Vwad.Batch(bars, period);
double expected = batchSeries.Last.Value;
// 2. Span Mode
var spanOutput = new double[bars.Count];
Vwad.Calculate(bars.High.Values, bars.Low.Values, bars.Close.Values, bars.Volume.Values, spanOutput, period);
Vwad.Batch(bars.High.Values, bars.Low.Values, bars.Close.Values, bars.Volume.Values, spanOutput, period);
double spanResult = spanOutput[^1];
// 3. Streaming Mode
+4 -4
View File
@@ -50,7 +50,7 @@ public class VwadValidationTests
}
// Batch
var batchResult = Vwad.Calculate(_data.Bars, DefaultPeriod);
var batchResult = Vwad.Batch(_data.Bars, DefaultPeriod);
var batchValues = batchResult.Values.ToArray();
// Cumulative indicators accumulate floating-point errors over many bars
@@ -76,7 +76,7 @@ public class VwadValidationTests
var volume = _data.Bars.Volume.Values.ToArray();
var spanValues = new double[high.Length];
Vwad.Calculate(high, low, close, volume, spanValues, DefaultPeriod);
Vwad.Batch(high, low, close, volume, spanValues, DefaultPeriod);
// Cumulative indicators accumulate floating-point errors over many bars
// 1e-10 tolerance is appropriate for ~5000 bar cumulative calculations
@@ -87,7 +87,7 @@ public class VwadValidationTests
public void Vwad_Batch_Matches_Span()
{
// Batch
var batchResult = Vwad.Calculate(_data.Bars, DefaultPeriod);
var batchResult = Vwad.Batch(_data.Bars, DefaultPeriod);
var batchValues = batchResult.Values.ToArray();
// Span
@@ -97,7 +97,7 @@ public class VwadValidationTests
var volume = _data.Bars.Volume.Values.ToArray();
var spanValues = new double[high.Length];
Vwad.Calculate(high, low, close, volume, spanValues, DefaultPeriod);
Vwad.Batch(high, low, close, volume, spanValues, DefaultPeriod);
// Batch and Span use identical code path, should match exactly
ValidationHelper.VerifyData(batchValues, spanValues, 0, 100, 1e-12);
+10 -3
View File
@@ -229,7 +229,7 @@ public sealed class Vwad : ITValuePublisher
/// <param name="source">Source bar series</param>
/// <param name="period">Lookback period for volume weighting</param>
/// <returns>TSeries containing VWAD values</returns>
public static TSeries Calculate(TBarSeries source, int period = 20)
public static TSeries Batch(TBarSeries source, int period = 20)
{
if (source.Count == 0)
{
@@ -239,7 +239,7 @@ public sealed class Vwad : ITValuePublisher
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);
Batch(source.High.Values, source.Low.Values, source.Close.Values, source.Volume.Values, v, period);
return new TSeries(t, v);
}
@@ -254,7 +254,7 @@ public sealed class Vwad : ITValuePublisher
/// <param name="output">Output span for VWAD values</param>
/// <param name="period">Lookback period for volume weighting</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 = 20)
public static void Batch(ReadOnlySpan<double> high, ReadOnlySpan<double> low, ReadOnlySpan<double> close, ReadOnlySpan<double> volume, Span<double> output, int period = 20)
{
if (high.Length != low.Length)
{
@@ -378,4 +378,11 @@ public sealed class Vwad : ITValuePublisher
output[i] = cumulativeVwad;
}
}
public static (TSeries Results, Vwad Indicator) Calculate(TBarSeries source, int period = 20)
{
var indicator = new Vwad(period);
TSeries results = indicator.Update(source);
return (results, indicator);
}
}