mirror of
https://github.com/mihakralj/QuanTAlib.git
synced 2026-08-22 04:28:04 +00:00
docs: remove C# Implementation Considerations sections, clean up temp scripts, reorganize test files
- Remove 'C# Implementation Considerations' sections from 34 indicator .md files - Delete 29 temp PowerShell scripts (_fix_mojibake.ps1, _hex_scan.ps1, etc.) - Move test files into tests/ subdirectories for consistent project structure - Add trader-focused bullet points to indicator documentation
This commit is contained in:
@@ -0,0 +1,80 @@
|
||||
using TradingPlatform.BusinessLayer;
|
||||
using QuanTAlib;
|
||||
|
||||
namespace QuanTAlib.Tests;
|
||||
|
||||
public class DxIndicatorTests
|
||||
{
|
||||
[Fact]
|
||||
public void DxIndicator_Constructor_SetsDefaults()
|
||||
{
|
||||
var indicator = new DxIndicator();
|
||||
|
||||
Assert.Equal(14, indicator.Period);
|
||||
Assert.True(indicator.ShowColdValues);
|
||||
Assert.Equal("DX - Directional Movement Index", indicator.Name);
|
||||
Assert.True(indicator.SeparateWindow);
|
||||
Assert.True(indicator.OnBackGround);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void DxIndicator_MinHistoryDepths_EqualsZero()
|
||||
{
|
||||
var indicator = new DxIndicator { Period = 20 };
|
||||
|
||||
Assert.Equal(0, DxIndicator.MinHistoryDepths);
|
||||
IWatchlistIndicator watchlistIndicator = indicator;
|
||||
Assert.Equal(0, watchlistIndicator.MinHistoryDepths);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void DxIndicator_Initialize_CreatesInternalDx()
|
||||
{
|
||||
var indicator = new DxIndicator { Period = 14 };
|
||||
|
||||
// Initialize should not throw
|
||||
indicator.Initialize();
|
||||
|
||||
// After init, line series should exist (DX, +DI, -DI)
|
||||
Assert.Equal(3, indicator.LinesSeries.Count);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void DxIndicator_ProcessUpdate_HistoricalBar_ComputesValue()
|
||||
{
|
||||
var indicator = new DxIndicator { Period = 5 };
|
||||
indicator.Initialize();
|
||||
|
||||
// Add historical data
|
||||
var now = DateTime.UtcNow;
|
||||
// Need enough bars for Period
|
||||
for (int i = 0; i < 20; i++)
|
||||
{
|
||||
indicator.HistoricalData.AddBar(now.AddMinutes(i), 100 + i, 110 + i, 90 + i, 105 + i);
|
||||
|
||||
// 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 dx = indicator.LinesSeries[0].GetValue(0);
|
||||
|
||||
Assert.True(double.IsFinite(dx));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void DxIndicator_ShortName_IsCorrect()
|
||||
{
|
||||
var indicator = new DxIndicator { Period = 20 };
|
||||
Assert.Equal("DX 20", indicator.ShortName);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void DxIndicator_SourceCodeLink_IsValid()
|
||||
{
|
||||
var indicator = new DxIndicator();
|
||||
Assert.Contains("github.com", indicator.SourceCodeLink, StringComparison.OrdinalIgnoreCase);
|
||||
Assert.Contains("Dx.Quantower.cs", indicator.SourceCodeLink, StringComparison.OrdinalIgnoreCase);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,299 @@
|
||||
|
||||
namespace QuanTAlib;
|
||||
|
||||
public class DxTests
|
||||
{
|
||||
[Fact]
|
||||
public void BasicCalculation_DoesNotCrash()
|
||||
{
|
||||
var dx = new Dx(14);
|
||||
var gbm = new GBM();
|
||||
var bars = gbm.Fetch(1000, DateTime.UtcNow.Ticks, TimeSpan.FromMinutes(1));
|
||||
|
||||
for (int i = 0; i < bars.Count; i++)
|
||||
{
|
||||
dx.Update(bars[i]);
|
||||
}
|
||||
|
||||
Assert.True(double.IsFinite(dx.Last.Value));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void IsNew_Consistency()
|
||||
{
|
||||
var dx = new Dx(14);
|
||||
var gbm = new GBM();
|
||||
var bars = gbm.Fetch(100, DateTime.UtcNow.Ticks, TimeSpan.FromMinutes(1));
|
||||
|
||||
// Feed first 99
|
||||
for (int i = 0; i < 99; i++)
|
||||
{
|
||||
dx.Update(bars[i]);
|
||||
}
|
||||
|
||||
// Update with 100th point (isNew=true is default, so omit it)
|
||||
dx.Update(bars[99]);
|
||||
|
||||
// Update with modified 100th point (isNew=false)
|
||||
var modifiedBar = new TBar(bars[99].Time, bars[99].Open, bars[99].High + 1.0, bars[99].Low - 1.0, bars[99].Close, bars[99].Volume);
|
||||
var val2 = dx.Update(modifiedBar, isNew: false);
|
||||
|
||||
// Create new instance and feed up to modified
|
||||
var dx2 = new Dx(14);
|
||||
for (int i = 0; i < 99; i++)
|
||||
{
|
||||
dx2.Update(bars[i]);
|
||||
}
|
||||
var val3 = dx2.Update(modifiedBar);
|
||||
|
||||
Assert.Equal(val3.Value, val2.Value, 1e-9);
|
||||
Assert.Equal(dx2.DiPlus.Value, dx.DiPlus.Value, 1e-9);
|
||||
Assert.Equal(dx2.DiMinus.Value, dx.DiMinus.Value, 1e-9);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void IterativeCorrections_RestoreToOriginalState()
|
||||
{
|
||||
var dx = new Dx(14);
|
||||
var gbm = new GBM();
|
||||
var bars = gbm.Fetch(100, DateTime.UtcNow.Ticks, TimeSpan.FromMinutes(1));
|
||||
|
||||
for (int i = 0; i < 50; i++)
|
||||
{
|
||||
dx.Update(bars[i]);
|
||||
}
|
||||
|
||||
var originalValue = dx.Last;
|
||||
|
||||
for (int m = 0; m < 5; m++)
|
||||
{
|
||||
var modified = new TBar(bars[49].Time, bars[49].Open, bars[49].High + m, bars[49].Low - m, bars[49].Close, bars[49].Volume);
|
||||
dx.Update(modified, isNew: false);
|
||||
}
|
||||
|
||||
var restored = dx.Update(bars[49], isNew: false);
|
||||
Assert.Equal(originalValue.Value, restored.Value, 9);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Reset_Works()
|
||||
{
|
||||
var dx = new Dx(14);
|
||||
var gbm = new GBM();
|
||||
var bars = gbm.Fetch(100, DateTime.UtcNow.Ticks, TimeSpan.FromMinutes(1));
|
||||
|
||||
for (int i = 0; i < bars.Count; i++)
|
||||
{
|
||||
dx.Update(bars[i]);
|
||||
}
|
||||
|
||||
dx.Reset();
|
||||
Assert.Equal(0, dx.Last.Value);
|
||||
Assert.False(dx.IsHot);
|
||||
|
||||
// Feed again
|
||||
for (int i = 0; i < bars.Count; i++)
|
||||
{
|
||||
dx.Update(bars[i]);
|
||||
}
|
||||
|
||||
Assert.True(double.IsFinite(dx.Last.Value));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void IsHot_BecomesTrueWhenBufferFull()
|
||||
{
|
||||
var dx = new Dx(14);
|
||||
var gbm = new GBM();
|
||||
var bars = gbm.Fetch(100, DateTime.UtcNow.Ticks, TimeSpan.FromMinutes(1));
|
||||
|
||||
Assert.False(dx.IsHot);
|
||||
|
||||
for (int i = 0; i < bars.Count; i++)
|
||||
{
|
||||
dx.Update(bars[i]);
|
||||
if (dx.IsHot)
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
Assert.True(dx.IsHot);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void NaN_Input_UsesLastValidValue()
|
||||
{
|
||||
var dx = new Dx(14);
|
||||
var gbm = new GBM();
|
||||
var bars = gbm.Fetch(50, DateTime.UtcNow.Ticks, TimeSpan.FromMinutes(1));
|
||||
|
||||
for (int i = 0; i < 40; i++)
|
||||
{
|
||||
dx.Update(bars[i]);
|
||||
}
|
||||
|
||||
var nanBar = new TBar(DateTime.UtcNow, double.NaN, double.NaN, double.NaN, double.NaN, 100);
|
||||
var result = dx.Update(nanBar);
|
||||
|
||||
Assert.True(double.IsFinite(result.Value));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Infinity_Input_UsesLastValidValue()
|
||||
{
|
||||
var dx = new Dx(14);
|
||||
var gbm = new GBM();
|
||||
var bars = gbm.Fetch(50, DateTime.UtcNow.Ticks, TimeSpan.FromMinutes(1));
|
||||
|
||||
for (int i = 0; i < 40; i++)
|
||||
{
|
||||
dx.Update(bars[i]);
|
||||
}
|
||||
|
||||
var infBar = new TBar(DateTime.UtcNow, double.PositiveInfinity, double.PositiveInfinity, 0, 100, 100);
|
||||
var result = dx.Update(infBar);
|
||||
|
||||
Assert.True(double.IsFinite(result.Value));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void AllModes_ProduceSameResult()
|
||||
{
|
||||
var gbm = new GBM(seed: 123);
|
||||
var bars = gbm.Fetch(200, DateTime.UtcNow.Ticks, TimeSpan.FromMinutes(1));
|
||||
|
||||
// 1. Batch Mode
|
||||
var batchResult = Dx.Batch(bars, 14);
|
||||
double expected = batchResult.Last.Value;
|
||||
|
||||
// 2. Streaming Mode
|
||||
var streamDx = new Dx(14);
|
||||
for (int i = 0; i < bars.Count; i++)
|
||||
{
|
||||
streamDx.Update(bars[i]);
|
||||
}
|
||||
|
||||
double streamResult = streamDx.Last.Value;
|
||||
|
||||
Assert.Equal(expected, streamResult, 9);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void TBarSeries_Update_Matches_Streaming()
|
||||
{
|
||||
var dx = new Dx(14);
|
||||
var gbm = new GBM();
|
||||
var bars = gbm.Fetch(200, DateTime.UtcNow.Ticks, TimeSpan.FromMinutes(1));
|
||||
|
||||
var streamingResults = new List<double>();
|
||||
for (int i = 0; i < bars.Count; i++)
|
||||
{
|
||||
streamingResults.Add(dx.Update(bars[i]).Value);
|
||||
}
|
||||
|
||||
var dx2 = new Dx(14);
|
||||
var seriesResults = dx2.Update(bars);
|
||||
|
||||
Assert.Equal(streamingResults.Count, seriesResults.Count);
|
||||
for (int i = 0; i < seriesResults.Count; i++)
|
||||
{
|
||||
Assert.Equal(streamingResults[i], seriesResults.Values[i], 1e-9);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void StaticCalculate_Matches_Streaming()
|
||||
{
|
||||
var gbm = new GBM();
|
||||
var bars = gbm.Fetch(200, DateTime.UtcNow.Ticks, TimeSpan.FromMinutes(1));
|
||||
|
||||
var dx = new Dx(14);
|
||||
var streamingResults = new List<double>();
|
||||
for (int i = 0; i < bars.Count; i++)
|
||||
{
|
||||
streamingResults.Add(dx.Update(bars[i]).Value);
|
||||
}
|
||||
|
||||
var staticResults = Dx.Batch(bars, 14);
|
||||
|
||||
Assert.Equal(streamingResults.Count, staticResults.Count);
|
||||
for (int i = 0; i < staticResults.Count; i++)
|
||||
{
|
||||
Assert.Equal(streamingResults[i], staticResults.Values[i], 1e-9);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Chainability_Works()
|
||||
{
|
||||
var dx = new Dx(14);
|
||||
var gbm = new GBM();
|
||||
var bars = gbm.Fetch(10, DateTime.UtcNow.Ticks, TimeSpan.FromMinutes(1));
|
||||
|
||||
// Test TBarSeries chain
|
||||
var result = dx.Update(bars);
|
||||
Assert.NotNull(result);
|
||||
Assert.IsType<TSeries>(result);
|
||||
|
||||
// Test TBar chain (returns TValue)
|
||||
var result2 = dx.Update(bars[0]);
|
||||
Assert.IsType<TValue>(result2);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Constructor_InvalidParameters_ThrowsArgumentException()
|
||||
{
|
||||
Assert.Throws<ArgumentException>(() => new Dx(0));
|
||||
Assert.Throws<ArgumentException>(() => new Dx(-1));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void DiPlus_DiMinus_AreValid()
|
||||
{
|
||||
var dx = new Dx(14);
|
||||
var gbm = new GBM();
|
||||
var bars = gbm.Fetch(100, DateTime.UtcNow.Ticks, TimeSpan.FromMinutes(1));
|
||||
|
||||
for (int i = 0; i < bars.Count; i++)
|
||||
{
|
||||
dx.Update(bars[i]);
|
||||
}
|
||||
|
||||
// +DI and -DI should be between 0 and 100
|
||||
Assert.InRange(dx.DiPlus.Value, 0, 100);
|
||||
Assert.InRange(dx.DiMinus.Value, 0, 100);
|
||||
Assert.InRange(dx.Last.Value, 0, 100);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void DX_Range_IsBetween0And100()
|
||||
{
|
||||
var dx = new Dx(14);
|
||||
var gbm = new GBM();
|
||||
var bars = gbm.Fetch(500, DateTime.UtcNow.Ticks, TimeSpan.FromMinutes(1));
|
||||
|
||||
for (int i = 0; i < bars.Count; i++)
|
||||
{
|
||||
var result = dx.Update(bars[i]);
|
||||
if (dx.IsHot)
|
||||
{
|
||||
Assert.InRange(result.Value, 0, 100);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void DefaultPeriod_Is14()
|
||||
{
|
||||
var dx = new Dx();
|
||||
Assert.Equal(14, dx.Period);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void WarmupPeriod_EqualsPeriod()
|
||||
{
|
||||
var dx = new Dx(20);
|
||||
Assert.Equal(20, dx.WarmupPeriod);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,174 @@
|
||||
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();
|
||||
}
|
||||
|
||||
[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(TALib.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(TALib.Core.RetCode.Success, retCode);
|
||||
|
||||
int lookback = Functions.MinusDILookback(14);
|
||||
ValidationHelper.VerifyData(diMinusResults, outReal, outRange, lookback);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void DmPlus_MatchesTalib()
|
||||
{
|
||||
var dx = new Dx(14);
|
||||
var dmPlusResults = new List<double>();
|
||||
|
||||
for (int i = 0; i < _data.Bars.Count; i++)
|
||||
{
|
||||
dx.Update(_data.Bars[i]);
|
||||
dmPlusResults.Add(dx.DmPlus.Value);
|
||||
}
|
||||
|
||||
double[] hData = _data.Bars.High.Select(x => x.Value).ToArray();
|
||||
double[] lData = _data.Bars.Low.Select(x => x.Value).ToArray();
|
||||
double[] outReal = new double[_data.Bars.Count];
|
||||
|
||||
var retCode = Functions.PlusDM(hData, lData, 0..^0, outReal, out var outRange, 14);
|
||||
Assert.Equal(TALib.Core.RetCode.Success, retCode);
|
||||
|
||||
int lookback = Functions.PlusDMLookback(14);
|
||||
ValidationHelper.VerifyData(dmPlusResults, outReal, outRange, lookback);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void DmMinus_MatchesTalib()
|
||||
{
|
||||
var dx = new Dx(14);
|
||||
var dmMinusResults = new List<double>();
|
||||
|
||||
for (int i = 0; i < _data.Bars.Count; i++)
|
||||
{
|
||||
dx.Update(_data.Bars[i]);
|
||||
dmMinusResults.Add(dx.DmMinus.Value);
|
||||
}
|
||||
|
||||
double[] hData = _data.Bars.High.Select(x => x.Value).ToArray();
|
||||
double[] lData = _data.Bars.Low.Select(x => x.Value).ToArray();
|
||||
double[] outReal = new double[_data.Bars.Count];
|
||||
|
||||
var retCode = Functions.MinusDM(hData, lData, 0..^0, outReal, out var outRange, 14);
|
||||
Assert.Equal(TALib.Core.RetCode.Success, retCode);
|
||||
|
||||
int lookback = Functions.MinusDMLookback(14);
|
||||
ValidationHelper.VerifyData(dmMinusResults, 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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user