mirror of
https://github.com/mihakralj/QuanTAlib.git
synced 2026-08-23 04:58:08 +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,65 @@
|
||||
using TradingPlatform.BusinessLayer;
|
||||
using QuanTAlib;
|
||||
|
||||
namespace QuanTAlib.Tests;
|
||||
|
||||
public class AdxIndicatorTests
|
||||
{
|
||||
[Fact]
|
||||
public void AdxIndicator_Constructor_SetsDefaults()
|
||||
{
|
||||
var indicator = new AdxIndicator();
|
||||
|
||||
Assert.Equal(14, indicator.Period);
|
||||
Assert.True(indicator.ShowColdValues);
|
||||
Assert.Equal("ADX - Average Directional Index", indicator.Name);
|
||||
Assert.True(indicator.SeparateWindow);
|
||||
Assert.True(indicator.OnBackGround);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void AdxIndicator_MinHistoryDepths_EqualsZero()
|
||||
{
|
||||
var indicator = new AdxIndicator { Period = 20 };
|
||||
|
||||
Assert.Equal(0, AdxIndicator.MinHistoryDepths);
|
||||
IWatchlistIndicator watchlistIndicator = indicator;
|
||||
Assert.Equal(0, watchlistIndicator.MinHistoryDepths);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void AdxIndicator_Initialize_CreatesInternalAdx()
|
||||
{
|
||||
var indicator = new AdxIndicator { Period = 14 };
|
||||
|
||||
// Initialize should not throw
|
||||
indicator.Initialize();
|
||||
|
||||
// After init, line series should exist (ADX, +DI, -DI)
|
||||
Assert.Equal(3, indicator.LinesSeries.Count);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void AdxIndicator_ProcessUpdate_HistoricalBar_ComputesValue()
|
||||
{
|
||||
var indicator = new AdxIndicator { 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 adx = indicator.LinesSeries[0].GetValue(0);
|
||||
|
||||
Assert.True(double.IsFinite(adx));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,250 @@
|
||||
|
||||
namespace QuanTAlib;
|
||||
|
||||
public class AdxTests
|
||||
{
|
||||
[Fact]
|
||||
public void BasicCalculation_DoesNotCrash()
|
||||
{
|
||||
var adx = new Adx(14);
|
||||
var gbm = new GBM();
|
||||
var bars = gbm.Fetch(1000, DateTime.UtcNow.Ticks, TimeSpan.FromMinutes(1));
|
||||
|
||||
for (int i = 0; i < bars.Count; i++)
|
||||
{
|
||||
adx.Update(bars[i]);
|
||||
}
|
||||
|
||||
Assert.True(double.IsFinite(adx.Last.Value));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void IsNew_Consistency()
|
||||
{
|
||||
var adx = new Adx(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++)
|
||||
{
|
||||
adx.Update(bars[i]);
|
||||
}
|
||||
|
||||
// Update with 100th point (isNew=true is default, so omit it)
|
||||
adx.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 = adx.Update(modifiedBar, isNew: false);
|
||||
|
||||
// Create new instance and feed up to modified
|
||||
var adx2 = new Adx(14);
|
||||
for (int i = 0; i < 99; i++)
|
||||
{
|
||||
adx2.Update(bars[i]);
|
||||
}
|
||||
var val3 = adx2.Update(modifiedBar);
|
||||
|
||||
Assert.Equal(val3.Value, val2.Value, 1e-9);
|
||||
Assert.Equal(adx2.DiPlus.Value, adx.DiPlus.Value, 1e-9);
|
||||
Assert.Equal(adx2.DiMinus.Value, adx.DiMinus.Value, 1e-9);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void IterativeCorrections_RestoreToOriginalState()
|
||||
{
|
||||
var adx = new Adx(14);
|
||||
var gbm = new GBM();
|
||||
var bars = gbm.Fetch(100, DateTime.UtcNow.Ticks, TimeSpan.FromMinutes(1));
|
||||
|
||||
for (int i = 0; i < 50; i++)
|
||||
{
|
||||
adx.Update(bars[i]);
|
||||
}
|
||||
|
||||
var originalValue = adx.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);
|
||||
adx.Update(modified, isNew: false);
|
||||
}
|
||||
|
||||
var restored = adx.Update(bars[49], isNew: false);
|
||||
Assert.Equal(originalValue.Value, restored.Value, 9);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Reset_Works()
|
||||
{
|
||||
var adx = new Adx(14);
|
||||
var gbm = new GBM();
|
||||
var bars = gbm.Fetch(100, DateTime.UtcNow.Ticks, TimeSpan.FromMinutes(1));
|
||||
|
||||
for (int i = 0; i < bars.Count; i++)
|
||||
{
|
||||
adx.Update(bars[i]);
|
||||
}
|
||||
|
||||
adx.Reset();
|
||||
Assert.Equal(0, adx.Last.Value);
|
||||
Assert.False(adx.IsHot);
|
||||
|
||||
// Feed again
|
||||
for (int i = 0; i < bars.Count; i++)
|
||||
{
|
||||
adx.Update(bars[i]);
|
||||
}
|
||||
|
||||
Assert.True(double.IsFinite(adx.Last.Value));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void IsHot_BecomesTrueWhenBufferFull()
|
||||
{
|
||||
var adx = new Adx(14);
|
||||
var gbm = new GBM();
|
||||
var bars = gbm.Fetch(100, DateTime.UtcNow.Ticks, TimeSpan.FromMinutes(1));
|
||||
|
||||
Assert.False(adx.IsHot);
|
||||
|
||||
for (int i = 0; i < bars.Count; i++)
|
||||
{
|
||||
adx.Update(bars[i]);
|
||||
if (adx.IsHot)
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
Assert.True(adx.IsHot);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void NaN_Input_UsesLastValidValue()
|
||||
{
|
||||
var adx = new Adx(14);
|
||||
var gbm = new GBM();
|
||||
var bars = gbm.Fetch(50, DateTime.UtcNow.Ticks, TimeSpan.FromMinutes(1));
|
||||
|
||||
for (int i = 0; i < 40; i++)
|
||||
{
|
||||
adx.Update(bars[i]);
|
||||
}
|
||||
|
||||
var nanBar = new TBar(DateTime.UtcNow, double.NaN, double.NaN, double.NaN, double.NaN, 100);
|
||||
var result = adx.Update(nanBar);
|
||||
|
||||
Assert.True(double.IsFinite(result.Value));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Infinity_Input_UsesLastValidValue()
|
||||
{
|
||||
var adx = new Adx(14);
|
||||
var gbm = new GBM();
|
||||
var bars = gbm.Fetch(50, DateTime.UtcNow.Ticks, TimeSpan.FromMinutes(1));
|
||||
|
||||
for (int i = 0; i < 40; i++)
|
||||
{
|
||||
adx.Update(bars[i]);
|
||||
}
|
||||
|
||||
var infBar = new TBar(DateTime.UtcNow, double.PositiveInfinity, double.PositiveInfinity, 0, 100, 100);
|
||||
var result = adx.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 = Adx.Batch(bars, 14);
|
||||
double expected = batchResult.Last.Value;
|
||||
|
||||
// 2. Streaming Mode
|
||||
var streamAdx = new Adx(14);
|
||||
for (int i = 0; i < bars.Count; i++)
|
||||
{
|
||||
streamAdx.Update(bars[i]);
|
||||
}
|
||||
|
||||
double streamResult = streamAdx.Last.Value;
|
||||
|
||||
Assert.Equal(expected, streamResult, 9);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void TBarSeries_Update_Matches_Streaming()
|
||||
{
|
||||
var adx = new Adx(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(adx.Update(bars[i]).Value);
|
||||
}
|
||||
|
||||
var adx2 = new Adx(14);
|
||||
var seriesResults = adx2.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 adx = new Adx(14);
|
||||
var streamingResults = new List<double>();
|
||||
for (int i = 0; i < bars.Count; i++)
|
||||
{
|
||||
streamingResults.Add(adx.Update(bars[i]).Value);
|
||||
}
|
||||
|
||||
var staticResults = Adx.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 adx = new Adx(14);
|
||||
var gbm = new GBM();
|
||||
var bars = gbm.Fetch(10, DateTime.UtcNow.Ticks, TimeSpan.FromMinutes(1));
|
||||
|
||||
// Test TBarSeries chain
|
||||
var result = adx.Update(bars);
|
||||
Assert.NotNull(result);
|
||||
Assert.IsType<TSeries>(result);
|
||||
|
||||
// Test TBar chain (returns TValue)
|
||||
var result2 = adx.Update(bars[0]);
|
||||
Assert.IsType<TValue>(result2);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Constructor_InvalidParameters_ThrowsArgumentException()
|
||||
{
|
||||
Assert.Throws<ArgumentException>(() => new Adx(0));
|
||||
Assert.Throws<ArgumentException>(() => new Adx(-1));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,197 @@
|
||||
using Skender.Stock.Indicators;
|
||||
using TALib;
|
||||
using OoplesFinance.StockIndicators;
|
||||
using OoplesFinance.StockIndicators.Models;
|
||||
using OoplesFinance.StockIndicators.Enums;
|
||||
using QuanTAlib.Tests;
|
||||
|
||||
namespace QuanTAlib;
|
||||
|
||||
public sealed class AdxValidationTests : IDisposable
|
||||
{
|
||||
private readonly ValidationTestData _data;
|
||||
|
||||
public AdxValidationTests()
|
||||
{
|
||||
_data = new ValidationTestData();
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
_data.Dispose();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void MatchesSkender()
|
||||
{
|
||||
var adx = new Adx(14);
|
||||
var results = new List<double>();
|
||||
|
||||
for (int i = 0; i < _data.Bars.Count; i++)
|
||||
{
|
||||
var res = adx.Update(_data.Bars[i]);
|
||||
results.Add(res.Value);
|
||||
}
|
||||
|
||||
var skenderResults = _data.SkenderQuotes.GetAdx(14).ToList();
|
||||
|
||||
ValidationHelper.VerifyData(results, skenderResults, x => x.Adx);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void MatchesTalib()
|
||||
{
|
||||
var adx = new Adx(14);
|
||||
var results = new List<double>();
|
||||
|
||||
for (int i = 0; i < _data.Bars.Count; i++)
|
||||
{
|
||||
var res = adx.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.Adx(hData, lData, cData, 0..^0, outReal, out var outRange, 14);
|
||||
Assert.Equal(TALib.Core.RetCode.Success, retCode);
|
||||
|
||||
int lookback = Functions.AdxLookback(14);
|
||||
ValidationHelper.VerifyData(results, outReal, outRange, lookback);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void MatchesTulip()
|
||||
{
|
||||
var adx = new Adx(14);
|
||||
var results = new List<double>();
|
||||
|
||||
for (int i = 0; i < _data.Bars.Count; i++)
|
||||
{
|
||||
var res = adx.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 adxInd = Tulip.Indicators.adx;
|
||||
double[][] outputs = { new double[hData.Length - adxInd.Start(options)] };
|
||||
adxInd.Run(inputs, options, outputs);
|
||||
double[] tulipResults = outputs[0];
|
||||
|
||||
// Tulip initializes differently, so we skip the warmup period to verify convergence
|
||||
// We must use the correct offset (lookback) to align the data series
|
||||
int offset = adxInd.Start(options);
|
||||
ValidationHelper.VerifyData(results, tulipResults, lookback: offset);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void DiPlus_MatchesTalib()
|
||||
{
|
||||
var adx = new Adx(14);
|
||||
var diPlusResults = new List<double>();
|
||||
|
||||
for (int i = 0; i < _data.Bars.Count; i++)
|
||||
{
|
||||
adx.Update(_data.Bars[i]);
|
||||
diPlusResults.Add(adx.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 adx = new Adx(14);
|
||||
var diMinusResults = new List<double>();
|
||||
|
||||
for (int i = 0; i < _data.Bars.Count; i++)
|
||||
{
|
||||
adx.Update(_data.Bars[i]);
|
||||
diMinusResults.Add(adx.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 MatchesSkender_DiValues()
|
||||
{
|
||||
var adx = new Adx(14);
|
||||
var diPlusResults = new List<double>();
|
||||
var diMinusResults = new List<double>();
|
||||
|
||||
for (int i = 0; i < _data.Bars.Count; i++)
|
||||
{
|
||||
adx.Update(_data.Bars[i]);
|
||||
diPlusResults.Add(adx.DiPlus.Value);
|
||||
diMinusResults.Add(adx.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);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void MatchesOoples()
|
||||
{
|
||||
var adx = new Adx(14);
|
||||
var results = new List<double>();
|
||||
|
||||
for (int i = 0; i < _data.Bars.Count; i++)
|
||||
{
|
||||
var res = adx.Update(_data.Bars[i]);
|
||||
results.Add(res.Value);
|
||||
}
|
||||
|
||||
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 adxResults = stockData.CalculateAverageDirectionalIndex(MovingAvgType.WildersSmoothingMethod, 14);
|
||||
var ooplesResults = adxResults.OutputValues["Adx"].ToArray();
|
||||
|
||||
// Ooples uses 0-initialization for WWMA, which takes a long time to converge.
|
||||
// We verify only the last 100 bars of the 5000-bar dataset.
|
||||
// Note: Ooples returns full-length array, so lookback is 0.
|
||||
ValidationHelper.VerifyData(results, ooplesResults, lookback: 0, skip: 100, tolerance: ValidationHelper.OoplesTolerance);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user