validation and profiles

This commit is contained in:
Miha Kralj
2026-02-26 22:02:52 -08:00
parent 9ab37c1200
commit 8a1ba95173
317 changed files with 18704 additions and 622 deletions
+15
View File
@@ -44,6 +44,21 @@ $$
## Performance Profile
### Operation Count (Streaming Mode)
ADL computes Money Flow Multiplier (MFM) from bar data, multiplies by volume, and accumulates cumulatively — O(1).
| Operation | Count | Cost (cycles) | Subtotal |
| :--- | :---: | :---: | :---: |
| MFM = ((C-L)-(H-C)) / (H-L) | 1 | 5 cy | ~5 cy |
| MFV = MFM * Volume | 1 | 3 cy | ~3 cy |
| ADL += MFV (cumulative sum) | 1 | 1 cy | ~1 cy |
| Zero guard on H-L | 1 | 2 cy | ~2 cy |
| NaN guard + state update | 1 | 2 cy | ~2 cy |
| **Total** | **O(1)** | — | **~13 cy** |
O(1) cumulative indicator — no window, no buffer. Throughput ~4 ns/bar. Division is the critical path (H-L guard prevents divide-by-zero on doji bars).
| Metric | Score | Notes |
| :--- | :--- | :--- |
| **Throughput** | 10 | High; O(1) calculation with simple arithmetic. |
+15
View File
@@ -36,6 +36,21 @@ Where:
## Performance Profile
### Operation Count (Streaming Mode)
ADOSC = short EMA of ADL minus long EMA of ADL. Two parallel EMA updates per bar — O(1).
| Operation | Count | Cost (cycles) | Subtotal |
| :--- | :---: | :---: | :---: |
| ADL accumulation (MFM * Vol) | 1 | 8 cy | ~8 cy |
| Short EMA update (FMA) | 1 | 1 cy | ~1 cy |
| Long EMA update (FMA) | 1 | 1 cy | ~1 cy |
| ADOSC = shortEMA - longEMA | 1 | 1 cy | ~1 cy |
| NaN guard + state update | 1 | 2 cy | ~2 cy |
| **Total** | **O(1)** | — | **~13 cy** |
O(1) per bar. Two EMA states maintained in parallel. After warmup (longPeriod bars), both EMAs are hot. FMA used for EMA update: new = FMA(prev, decay, alpha*adl).
ADOSC is slightly heavier than ADL because it involves two EMAs.
| Metric | Score | Notes |
+15
View File
@@ -119,4 +119,19 @@ public class CmfValidationTests
ValidationHelper.VerifyData(streamingValues.ToArray(), spanValues, 0, 100, 1e-12);
}
[Fact]
public void Cmf_MatchesOoples_Structural()
{
// CalculateChaikinMoneyFlow — structural validation (already has Skender exact match)
var ooplesData = _data.SkenderQuotes
.Select(q => new TickerData { Date = q.Date, Open = (double)q.Open, High = (double)q.High, Low = (double)q.Low, Close = (double)q.Close, Volume = (double)q.Volume })
.ToList();
var result = new StockData(ooplesData).CalculateChaikinMoneyFlow();
var values = result.CustomValuesList;
int finiteCount = values.Count(v => double.IsFinite(v));
Assert.True(finiteCount > 100, $"Expected >100 finite Ooples CMF values, got {finiteCount}");
}
}
+17
View File
@@ -1,3 +1,5 @@
using OoplesFinance.StockIndicators;
using OoplesFinance.StockIndicators.Models;
using Skender.Stock.Indicators;
using Xunit.Abstractions;
@@ -377,4 +379,19 @@ public sealed class KvoValidationTests : IDisposable
Assert.False(allEqual, "Different periods should produce different results");
}
[Fact]
public void Kvo_MatchesOoples_Structural()
{
// CalculateKlingerVolumeOscillator — structural test (different VF normalization)
var ooplesData = _data.SkenderQuotes
.Select(q => new TickerData { Date = q.Date, Open = (double)q.Open, High = (double)q.High, Low = (double)q.Low, Close = (double)q.Close, Volume = (double)q.Volume })
.ToList();
var result = new StockData(ooplesData).CalculateKlingerVolumeOscillator();
var values = result.CustomValuesList;
int finiteCount = values.Count(v => double.IsFinite(v));
Assert.True(finiteCount > 100, $"Expected >100 finite Ooples KVO values, got {finiteCount}");
}
}
+38 -3
View File
@@ -1,6 +1,7 @@
using Skender.Stock.Indicators;
using OoplesFinance.StockIndicators;
using OoplesFinance.StockIndicators.Models;
using TALib;
namespace QuanTAlib.Tests;
@@ -35,9 +36,43 @@ public class MfiValidationTests
[Fact]
public void Mfi_Matches_Talib()
{
// TA-Lib has MFI but uses different API pattern
// Skip direct comparison - formula is the same
Assert.True(true, "TA-Lib MFI uses different API pattern; formula matches standard MFI");
// TALib MFI = Money Flow Index with the same standard formula as QuanTAlib.
// Both compute: typical price = (H+L+C)/3, raw money flow = TP*Volume,
// then ratio = sum(+MF) / sum(-MF), MFI = 100 - 100/(1+ratio).
// Exact numeric match expected to 1e-9.
const int period = DefaultPeriod;
double[] highData = _data.Bars.High.Values.ToArray();
double[] lowData = _data.Bars.Low.Values.ToArray();
double[] closeData = _data.Bars.Close.Values.ToArray();
double[] volumeData = _data.Bars.Volume.Values.ToArray();
double[] taOut = new double[_data.Bars.Count];
var retCode = Functions.Mfi<double>(
highData, lowData, closeData, volumeData,
0..^0, taOut, out var outRange, period);
Assert.Equal(Core.RetCode.Success, retCode);
(int offset, int length) = outRange.GetOffsetAndLength(taOut.Length);
Assert.True(length > 100, $"TALib MFI produced only {length} values");
// QuanTAlib streaming
var mfi = new Mfi(period);
var qlValues = new double[_data.Bars.Count];
for (int i = 0; i < _data.Bars.Count; i++)
{
qlValues[i] = mfi.Update(_data.Bars[i]).Value;
}
// Compare
for (int j = 0; j < length; j++)
{
int qi = j + offset;
double diff = Math.Abs(qlValues[qi] - taOut[j]);
Assert.True(diff <= 1e-9,
$"MFI mismatch at [{qi}]: QuanTAlib={qlValues[qi]:G17}, TALib={taOut[j]:G17}, diff={diff:E3}");
}
}
[Fact]
+18
View File
@@ -1,3 +1,6 @@
using OoplesFinance.StockIndicators;
using OoplesFinance.StockIndicators.Models;
namespace QuanTAlib.Tests;
public class PvoValidationTests
@@ -227,4 +230,19 @@ public class PvoValidationTests
ValidationHelper.VerifyData(mode1Values.ToArray(), mode3Values, 0, 100, 1e-9);
ValidationHelper.VerifyData(mode1Values.ToArray(), mode4Values, 0, 100, 1e-9);
}
[Fact]
public void Pvo_MatchesOoples_Structural()
{
// CalculatePercentageVolumeOscillator — structural test
var ooplesData = _data.SkenderQuotes
.Select(q => new TickerData { Date = q.Date, Open = (double)q.Open, High = (double)q.High, Low = (double)q.Low, Close = (double)q.Close, Volume = (double)q.Volume })
.ToList();
var result = new StockData(ooplesData).CalculatePercentageVolumeOscillator();
var values = result.CustomValuesList;
int finiteCount = values.Count(v => double.IsFinite(v));
Assert.True(finiteCount > 100, $"Expected >100 finite Ooples PVO values, got {finiteCount}");
}
}
+20
View File
@@ -1,3 +1,6 @@
using OoplesFinance.StockIndicators;
using OoplesFinance.StockIndicators.Models;
namespace QuanTAlib.Tests;
public class TviValidationTests
@@ -173,4 +176,21 @@ public class TviValidationTests
// Values should be non-zero after warmup
Assert.True(values.Skip(10).Any(v => v != 0), "TVI should have non-zero values after warmup");
}
[Fact]
public void Tvi_MatchesOoples_Structural()
{
var gbm = new GBM(startPrice: 100.0, mu: 0.02, sigma: 0.15, seed: 42);
var bars = gbm.Fetch(500, DateTime.UtcNow.Ticks, TimeSpan.FromMinutes(1));
var ooplesData = bars.Select(b => new TickerData
{
Date = new DateTime(b.Time, DateTimeKind.Utc),
Open = b.Open, High = b.High, Low = b.Low,
Close = b.Close, Volume = b.Volume
}).ToList();
var result = new StockData(ooplesData).CalculateTradeVolumeIndex();
var values = result.CustomValuesList;
int finiteCount = values.Count(v => double.IsFinite(v));
Assert.True(finiteCount > 100, $"Expected >100 finite values, got {finiteCount}");
}
}
+61
View File
@@ -3,6 +3,8 @@
// No standard external library equivalents with matching implementation.
// Validation uses mathematical property testing.
using Tulip;
namespace QuanTAlib.Tests;
using Xunit;
@@ -195,4 +197,63 @@ public class VoValidationTests
vo.Update(finalBar, isNew: true);
Assert.True(vo.IsHot, "Should be hot after longPeriod bars");
}
// === Tulip Cross-Validation ===
/// <summary>
/// Structural validation against Tulip <c>vosc</c> (volume oscillator).
/// Algorithm variant: Tulip <c>vosc</c> takes one input (volume only) with two options
/// (short_period, long_period) and computes <c>(sma_short - sma_long) / sma_long × 100</c>.
/// QuanTAlib Vo also adds an optional signal EMA. With <c>signalPeriod=1</c> the signal
/// equals Vo itself, so raw Vo output is directly comparable to Tulip vosc.
/// </summary>
[Fact]
public void Vo_Matches_Tulip_Vosc_Batch()
{
const int shortPeriod = 5;
const int longPeriod = 10;
var bars = new GBM(sigma: 0.3, seed: 42).Fetch(300, DateTime.UtcNow.Ticks, TimeSpan.FromMinutes(1));
double[] volumeData = new double[bars.Count];
for (int i = 0; i < bars.Count; i++) { volumeData[i] = bars[i].Volume; }
// QuanTAlib Vo batch
var qResult = Vo.Batch(bars, shortPeriod, longPeriod, signalPeriod: 1);
// Tulip vosc — volume only, no signal period
var tulipIndicator = Tulip.Indicators.vosc;
double[][] inputs = { volumeData };
double[] options = { shortPeriod, longPeriod };
int lookback = tulipIndicator.Start(options);
double[][] outputs = { new double[volumeData.Length - lookback] };
tulipIndicator.Run(inputs, options, outputs);
double[] tResult = outputs[0];
ValidationHelper.VerifyData(qResult, tResult, lookback);
}
[Fact]
public void Vo_Matches_Tulip_Vosc_Streaming()
{
const int shortPeriod = 5;
const int longPeriod = 10;
var bars = new GBM(sigma: 0.3, seed: 42).Fetch(300, DateTime.UtcNow.Ticks, TimeSpan.FromMinutes(1));
double[] volumeData = new double[bars.Count];
for (int i = 0; i < bars.Count; i++) { volumeData[i] = bars[i].Volume; }
// QuanTAlib Vo streaming (signalPeriod=1 → signal equals Vo)
var vo = new Vo(shortPeriod, longPeriod, signalPeriod: 1);
var qResults = new List<double>();
foreach (var bar in bars) { qResults.Add(vo.Update(bar).Value); }
// Tulip vosc
var tulipIndicator = Tulip.Indicators.vosc;
double[][] inputs = { volumeData };
double[] options = { shortPeriod, longPeriod };
int lookback = tulipIndicator.Start(options);
double[][] outputs = { new double[volumeData.Length - lookback] };
tulipIndicator.Run(inputs, options, outputs);
double[] tResult = outputs[0];
ValidationHelper.VerifyData(qResults, tResult, lookback);
}
}
+46 -3
View File
@@ -1,4 +1,5 @@
using Skender.Stock.Indicators;
using Tulip;
namespace QuanTAlib.Tests;
@@ -124,10 +125,52 @@ public class VwmaValidationTests
}
[Fact]
public void Vwma_NotAvailable_Tulip()
public void Vwma_Matches_Tulip_Batch()
{
// Tulip has VWMA but named differently - verify manually
Assert.True(true, "VWMA validation requires manual verification for Tulip");
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]
+15
View File
@@ -65,6 +65,21 @@ No price movement detected; no volume impact on WAD.
## Performance Profile
### Operation Count (Streaming Mode)
Williams Accumulation/Distribution uses directional price comparison to select a TrueRange component, then accumulates — O(1).
| Operation | Count | Cost (cycles) | Subtotal |
| :--- | :---: | :---: | :---: |
| Previous close comparison | 1 | 2 cy | ~2 cy |
| TrueHigh / TrueLow conditional select | 1 | 3 cy | ~3 cy |
| WAD_bar = C - TrueRange selected | 1 | 1 cy | ~1 cy |
| WAD cumulative += WAD_bar | 1 | 1 cy | ~1 cy |
| NaN guard + state update | 1 | 2 cy | ~2 cy |
| **Total** | **O(1)** | — | **~9 cy** |
O(1) cumulative. No window, no smoothing. The conditional branch (up day vs down day vs unchanged) is predicted by the CPU after a few bars.
| Metric | Score | Notes |
| :--- | :--- | :--- |
| **Throughput** | 10 | High; O(1) calculation with simple comparisons. |