mirror of
https://github.com/mihakralj/QuanTAlib.git
synced 2026-08-24 13:38:05 +00:00
validation and profiles
This commit is contained in:
@@ -1,7 +1,10 @@
|
||||
// PSAR Validation Tests - Parabolic Stop And Reverse
|
||||
// Cross-validated against Skender.Stock.Indicators GetParabolicSar()
|
||||
// Cross-validated against Skender.Stock.Indicators GetParabolicSar(), TALib SAR, and OoplesFinance CalculateParabolicSAR.
|
||||
|
||||
using OoplesFinance.StockIndicators;
|
||||
using OoplesFinance.StockIndicators.Models;
|
||||
using Skender.Stock.Indicators;
|
||||
using TALib;
|
||||
|
||||
namespace QuanTAlib.Tests;
|
||||
|
||||
@@ -185,4 +188,113 @@ public sealed class PsarValidationTests
|
||||
Assert.True(reversals > 5, $"Expected > 5 reversals, got {reversals}");
|
||||
Assert.True(reversals < 250, $"Expected < 250 reversals, got {reversals}");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void StreamingMatchesTalib()
|
||||
{
|
||||
/* TALib SAR uses the same Wilder parabolic SAR formula as QuanTAlib.
|
||||
Parameters: accelerationFactor=0.02 (step), maximum=0.20 (cap).
|
||||
Initialization differences produce a short divergence; values converge after first reversal.
|
||||
We accept up to 2% mismatch for edge-of-reversal rounding at period boundaries. */
|
||||
|
||||
var _data = new ValidationTestData();
|
||||
|
||||
double[] highData = _data.Bars.High.Values.ToArray();
|
||||
double[] lowData = _data.Bars.Low.Values.ToArray();
|
||||
double[] taOut = new double[_data.Bars.Count];
|
||||
|
||||
const double afStep = 0.02;
|
||||
const double afMax = 0.20;
|
||||
|
||||
var retCode = Functions.Sar<double>(
|
||||
highData, lowData,
|
||||
0..^0, taOut, out var outRange,
|
||||
afStep, afMax);
|
||||
Assert.Equal(Core.RetCode.Success, retCode);
|
||||
|
||||
(int offset, int length) = outRange.GetOffsetAndLength(taOut.Length);
|
||||
Assert.True(length > 100, $"TALib SAR produced only {length} values");
|
||||
|
||||
// QuanTAlib streaming
|
||||
var psar = new Psar(afStart: afStep, afIncrement: afStep, afMax: afMax);
|
||||
var qlSar = new double[_data.Bars.Count];
|
||||
for (int i = 0; i < _data.Bars.Count; i++)
|
||||
{
|
||||
_ = psar.Update(_data.Bars[i], isNew: true);
|
||||
qlSar[i] = psar.Sar;
|
||||
}
|
||||
|
||||
// Skip the first ~5 bars (initialization divergence), then require exact match.
|
||||
int skipBars = 5;
|
||||
int compared = 0;
|
||||
int matched = 0;
|
||||
for (int j = skipBars; j < length; j++)
|
||||
{
|
||||
int qi = j + offset;
|
||||
if (!double.IsFinite(qlSar[qi]) || !double.IsFinite(taOut[j])) { continue; }
|
||||
compared++;
|
||||
double diff = Math.Abs(qlSar[qi] - taOut[j]);
|
||||
if (diff <= 1e-9) { matched++; }
|
||||
}
|
||||
|
||||
// After initialization, QuanTAlib and TALib SAR should converge fully.
|
||||
// Accept up to 2% mismatch for edge-of-reversal rounding at period boundaries.
|
||||
double matchRate = compared > 0 ? (double)matched / compared : 0;
|
||||
Assert.True(matchRate >= 0.98,
|
||||
$"TALib SAR match rate {matchRate:P1} ({matched}/{compared}) < 98% — unexpected divergence");
|
||||
|
||||
_data.Dispose();
|
||||
}
|
||||
|
||||
// ── Cross-library: OoplesFinance ────────────────────────────────────
|
||||
|
||||
/// <summary>
|
||||
/// Structural validation against Ooples <c>CalculateParabolicSAR</c>.
|
||||
/// Ooples PSAR uses the same Wilder acceleration factor algorithm (start=0.02, increment=0.02, max=0.2).
|
||||
/// Cross-library numeric equality is not asserted because reversal-point initialization
|
||||
/// diverges across implementations when the very first bar direction is ambiguous.
|
||||
/// Both must produce finite, positive output on the same OHLCV data.
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public void Psar_MatchesOoples_Structural()
|
||||
{
|
||||
var _data = new ValidationTestData();
|
||||
|
||||
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 stockData = new StockData(ooplesData);
|
||||
var oResult = stockData.CalculateParabolicSAR(start: 0.02, increment: 0.02, maximum: 0.2);
|
||||
var oValues = oResult.OutputValues.Values.First();
|
||||
|
||||
var psar = new Psar(afStart: 0.02, afIncrement: 0.02, afMax: 0.20);
|
||||
var qValues = new System.Collections.Generic.List<double>();
|
||||
foreach (var bar in _data.Data)
|
||||
{
|
||||
qValues.Add(psar.Update(bar).Value);
|
||||
}
|
||||
|
||||
Assert.True(oValues.Count > 0, "Ooples PSAR must produce output");
|
||||
|
||||
int finiteCount = 0;
|
||||
int warmup = 5;
|
||||
for (int i = warmup; i < Math.Min(oValues.Count, qValues.Count); i++)
|
||||
{
|
||||
if (double.IsFinite(oValues[i]) && double.IsFinite(qValues[i]) && qValues[i] > 0)
|
||||
{
|
||||
finiteCount++;
|
||||
}
|
||||
}
|
||||
|
||||
Assert.True(finiteCount > 100, $"Expected >100 finite positive PSAR pairs, got {finiteCount}");
|
||||
|
||||
_data.Dispose();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -76,6 +76,22 @@ At AF = 0.20 (maximum), SAR covers 20% of the EP-SAR gap per bar.
|
||||
|
||||
## Performance Profile
|
||||
|
||||
### Operation Count (Streaming Mode)
|
||||
|
||||
Parabolic SAR uses an adaptive acceleration factor with trend-reversal detection — O(1) per bar.
|
||||
|
||||
| Operation | Count | Cost (cycles) | Subtotal |
|
||||
| :--- | :---: | :---: | :---: |
|
||||
| Trend direction check | 1 | 2 cy | ~2 cy |
|
||||
| EP (extreme point) update | 1 | 2 cy | ~2 cy |
|
||||
| AF increment (conditional) | 1 | 2 cy | ~2 cy |
|
||||
| SAR = SAR + AF*(EP - SAR) via FMA | 1 | 1 cy | ~1 cy |
|
||||
| Reversal detection + reset | 1 | 3 cy | ~3 cy |
|
||||
| NaN guard + state update | 1 | 2 cy | ~2 cy |
|
||||
| **Total** | **O(1)** | — | **~12 cy** |
|
||||
|
||||
O(1) per bar. FMA computes SAR update in a single instruction. Reversal branching adds ~3 cy amortized. No SIMD in streaming — trend state is sequential.
|
||||
|
||||
| Operation | Complexity | Notes |
|
||||
|-----------|-----------|-------|
|
||||
| Update (streaming) | O(1) | State machine: constant work per bar |
|
||||
|
||||
Reference in New Issue
Block a user