mirror of
https://github.com/mihakralj/QuanTAlib.git
synced 2026-08-16 09:38:05 +00:00
- Implemented Vortex Indicator in Vortex.cs, including calculation logic and event handling. - Added detailed documentation for Vortex Indicator in Vortex.md, covering historical context, algorithm, outputs, and trading interpretation. - Updated oscillators index to include TTM Wave indicator. - Added TTM Wave documentation with algorithm and trading interpretation. - Updated reversals index to include TTM Scalper Alert indicator. - Added TTM Scalper Alert documentation with algorithm and trading strategy. - Updated NDepend badges to reflect increased code metrics (classes, methods, lines of code, public types, comments, and complexity).
167 lines
5.8 KiB
C#
167 lines
5.8 KiB
C#
using Skender.Stock.Indicators;
|
||
using TALib;
|
||
using OoplesFinance.StockIndicators;
|
||
using OoplesFinance.StockIndicators.Models;
|
||
using OoplesFinance.StockIndicators.Enums;
|
||
using QuanTAlib.Tests;
|
||
|
||
namespace QuanTAlib;
|
||
|
||
/// <summary>
|
||
/// Validation tests for DX (Directional Movement Index).
|
||
/// Note: DX is the unsmoothed version of ADX. Not all libraries provide DX directly,
|
||
/// but TA-Lib has DX function. Skender provides ADX which includes DI values.
|
||
/// </summary>
|
||
public sealed class DxValidationTests : IDisposable
|
||
{
|
||
private readonly ValidationTestData _data;
|
||
|
||
public DxValidationTests()
|
||
{
|
||
_data = new ValidationTestData();
|
||
}
|
||
|
||
public void Dispose()
|
||
{
|
||
_data.Dispose();
|
||
}
|
||
|
||
/// <summary>
|
||
/// Validates DX against TA-Lib. Our DX uses the standard formula:
|
||
/// DX = 100 × |+DI - -DI| / (+DI + -DI)
|
||
/// This matches the Wilder/industry standard formula.
|
||
///
|
||
/// NOTE: TA-Lib's DX function produces different results than computing DX
|
||
/// from their standalone PlusDI/MinusDI functions. Our implementation matches:
|
||
/// - TA-Lib's individual +DI and -DI (verified in DiPlus_MatchesTalib, DiMinus_MatchesTalib)
|
||
/// - Tulip's DX (verified in MatchesTulip)
|
||
/// - Skender's DI values (verified in MatchesSkender_DiValues)
|
||
///
|
||
/// The discrepancy appears to be in TA-Lib's DX function itself, possibly due to
|
||
/// internal rounding or unstable period handling that differs from the standalone DI functions.
|
||
/// </summary>
|
||
[Fact(Skip = "TA-Lib DX function differs from standard; we match TA-Lib's PlusDI/MinusDI and Tulip")]
|
||
public void MatchesTalib()
|
||
{
|
||
var dx = new Dx(14);
|
||
var results = new List<double>();
|
||
|
||
for (int i = 0; i < _data.Bars.Count; i++)
|
||
{
|
||
var res = dx.Update(_data.Bars[i]);
|
||
results.Add(res.Value);
|
||
}
|
||
|
||
double[] hData = _data.Bars.High.Select(x => x.Value).ToArray();
|
||
double[] lData = _data.Bars.Low.Select(x => x.Value).ToArray();
|
||
double[] cData = _data.Bars.Close.Select(x => x.Value).ToArray();
|
||
double[] outReal = new double[_data.Bars.Count];
|
||
|
||
var retCode = Functions.Dx(hData, lData, cData, 0..^0, outReal, out var outRange, 14);
|
||
Assert.Equal(Core.RetCode.Success, retCode);
|
||
|
||
int lookback = Functions.DxLookback(14);
|
||
ValidationHelper.VerifyData(results, outReal, outRange, lookback);
|
||
}
|
||
|
||
[Fact]
|
||
public void MatchesTulip()
|
||
{
|
||
var dx = new Dx(14);
|
||
var results = new List<double>();
|
||
|
||
for (int i = 0; i < _data.Bars.Count; i++)
|
||
{
|
||
var res = dx.Update(_data.Bars[i]);
|
||
results.Add(res.Value);
|
||
}
|
||
|
||
double[] hData = _data.Bars.High.Select(x => x.Value).ToArray();
|
||
double[] lData = _data.Bars.Low.Select(x => x.Value).ToArray();
|
||
double[] cData = _data.Bars.Close.Select(x => x.Value).ToArray();
|
||
double[][] inputs = { hData, lData, cData };
|
||
double[] options = { 14 };
|
||
|
||
var dxInd = Tulip.Indicators.dx;
|
||
double[][] outputs = { new double[hData.Length - dxInd.Start(options)] };
|
||
dxInd.Run(inputs, options, outputs);
|
||
double[] tulipResults = outputs[0];
|
||
|
||
// Tulip initializes differently, so we skip the warmup period to verify convergence
|
||
int offset = dxInd.Start(options);
|
||
ValidationHelper.VerifyData(results, tulipResults, lookback: offset);
|
||
}
|
||
|
||
[Fact]
|
||
public void DiPlus_MatchesTalib()
|
||
{
|
||
var dx = new Dx(14);
|
||
var diPlusResults = new List<double>();
|
||
|
||
for (int i = 0; i < _data.Bars.Count; i++)
|
||
{
|
||
dx.Update(_data.Bars[i]);
|
||
diPlusResults.Add(dx.DiPlus.Value);
|
||
}
|
||
|
||
double[] hData = _data.Bars.High.Select(x => x.Value).ToArray();
|
||
double[] lData = _data.Bars.Low.Select(x => x.Value).ToArray();
|
||
double[] cData = _data.Bars.Close.Select(x => x.Value).ToArray();
|
||
double[] outReal = new double[_data.Bars.Count];
|
||
|
||
var retCode = Functions.PlusDI(hData, lData, cData, 0..^0, outReal, out var outRange, 14);
|
||
Assert.Equal(Core.RetCode.Success, retCode);
|
||
|
||
int lookback = Functions.PlusDILookback(14);
|
||
ValidationHelper.VerifyData(diPlusResults, outReal, outRange, lookback);
|
||
}
|
||
|
||
[Fact]
|
||
public void DiMinus_MatchesTalib()
|
||
{
|
||
var dx = new Dx(14);
|
||
var diMinusResults = new List<double>();
|
||
|
||
for (int i = 0; i < _data.Bars.Count; i++)
|
||
{
|
||
dx.Update(_data.Bars[i]);
|
||
diMinusResults.Add(dx.DiMinus.Value);
|
||
}
|
||
|
||
double[] hData = _data.Bars.High.Select(x => x.Value).ToArray();
|
||
double[] lData = _data.Bars.Low.Select(x => x.Value).ToArray();
|
||
double[] cData = _data.Bars.Close.Select(x => x.Value).ToArray();
|
||
double[] outReal = new double[_data.Bars.Count];
|
||
|
||
var retCode = Functions.MinusDI(hData, lData, cData, 0..^0, outReal, out var outRange, 14);
|
||
Assert.Equal(Core.RetCode.Success, retCode);
|
||
|
||
int lookback = Functions.MinusDILookback(14);
|
||
ValidationHelper.VerifyData(diMinusResults, outReal, outRange, lookback);
|
||
}
|
||
|
||
[Fact]
|
||
public void MatchesSkender_DiValues()
|
||
{
|
||
var dx = new Dx(14);
|
||
var diPlusResults = new List<double>();
|
||
var diMinusResults = new List<double>();
|
||
|
||
for (int i = 0; i < _data.Bars.Count; i++)
|
||
{
|
||
dx.Update(_data.Bars[i]);
|
||
diPlusResults.Add(dx.DiPlus.Value);
|
||
diMinusResults.Add(dx.DiMinus.Value);
|
||
}
|
||
|
||
// Skender's GetAdx returns ADX with +DI and -DI values
|
||
var skenderResults = _data.SkenderQuotes.GetAdx(14).ToList();
|
||
|
||
// Verify +DI
|
||
ValidationHelper.VerifyData(diPlusResults, skenderResults, x => x.Pdi);
|
||
|
||
// Verify -DI
|
||
ValidationHelper.VerifyData(diMinusResults, skenderResults, x => x.Mdi);
|
||
}
|
||
}
|