mirror of
https://github.com/mihakralj/QuanTAlib.git
synced 2026-08-18 10:38:05 +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,73 @@
|
||||
using TradingPlatform.BusinessLayer;
|
||||
using QuanTAlib;
|
||||
|
||||
namespace QuanTAlib.Tests;
|
||||
|
||||
public class PlusDmIndicatorTests
|
||||
{
|
||||
[Fact]
|
||||
public void PlusDmIndicator_Constructor_SetsDefaults()
|
||||
{
|
||||
var indicator = new PlusDmIndicator();
|
||||
|
||||
Assert.Equal(14, indicator.Period);
|
||||
Assert.True(indicator.ShowColdValues);
|
||||
Assert.Equal("+DM - Plus Directional Movement", indicator.Name);
|
||||
Assert.True(indicator.SeparateWindow);
|
||||
Assert.True(indicator.OnBackGround);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void PlusDmIndicator_MinHistoryDepths_EqualsZero()
|
||||
{
|
||||
var indicator = new PlusDmIndicator { Period = 20 };
|
||||
|
||||
Assert.Equal(0, PlusDmIndicator.MinHistoryDepths);
|
||||
IWatchlistIndicator watchlistIndicator = indicator;
|
||||
Assert.Equal(0, watchlistIndicator.MinHistoryDepths);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void PlusDmIndicator_Initialize_CreatesInternal()
|
||||
{
|
||||
var indicator = new PlusDmIndicator { Period = 14 };
|
||||
|
||||
indicator.Initialize();
|
||||
|
||||
Assert.Single(indicator.LinesSeries);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void PlusDmIndicator_ProcessUpdate_HistoricalBar_ComputesValue()
|
||||
{
|
||||
var indicator = new PlusDmIndicator { Period = 5 };
|
||||
indicator.Initialize();
|
||||
|
||||
var now = DateTime.UtcNow;
|
||||
for (int i = 0; i < 20; i++)
|
||||
{
|
||||
indicator.HistoricalData.AddBar(now.AddMinutes(i), 100 + i, 110 + i, 90 + i, 105 + i);
|
||||
|
||||
var args = new UpdateArgs(UpdateReason.HistoricalBar);
|
||||
indicator.ProcessUpdate(args);
|
||||
}
|
||||
|
||||
double value = indicator.LinesSeries[0].GetValue(0);
|
||||
Assert.True(double.IsFinite(value));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void PlusDmIndicator_ShortName_IsCorrect()
|
||||
{
|
||||
var indicator = new PlusDmIndicator { Period = 20 };
|
||||
Assert.Equal("+DM 20", indicator.ShortName);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void PlusDmIndicator_SourceCodeLink_IsValid()
|
||||
{
|
||||
var indicator = new PlusDmIndicator();
|
||||
Assert.Contains("github.com", indicator.SourceCodeLink, StringComparison.OrdinalIgnoreCase);
|
||||
Assert.Contains("PlusDm.Quantower.cs", indicator.SourceCodeLink, StringComparison.OrdinalIgnoreCase);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,231 @@
|
||||
using OoplesFinance.StockIndicators;
|
||||
using OoplesFinance.StockIndicators.Models;
|
||||
using OoplesFinance.StockIndicators.Enums;
|
||||
using Skender.Stock.Indicators;
|
||||
using TALib;
|
||||
using QuanTAlib.Tests;
|
||||
|
||||
namespace QuanTAlib;
|
||||
|
||||
/// <summary>
|
||||
/// Validation tests for PlusDm (+DM). Cross-validates against TA-Lib,
|
||||
/// OoplesFinance, and internal Dx equivalence with multiple periods.
|
||||
/// Note: Skender does not expose DM values directly; only DI values via GetAdx().
|
||||
/// </summary>
|
||||
public sealed class PlusDmValidationTests : IDisposable
|
||||
{
|
||||
private readonly ValidationTestData _data;
|
||||
|
||||
public PlusDmValidationTests()
|
||||
{
|
||||
_data = new ValidationTestData();
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
_data.Dispose();
|
||||
}
|
||||
|
||||
// ═══════════════════════════════════════════════
|
||||
// TA-Lib Validation
|
||||
// ═══════════════════════════════════════════════
|
||||
|
||||
[Fact]
|
||||
public void MatchesTalib()
|
||||
{
|
||||
var indicator = new PlusDm(14);
|
||||
var results = new List<double>();
|
||||
|
||||
for (int i = 0; i < _data.Bars.Count; i++)
|
||||
{
|
||||
indicator.Update(_data.Bars[i]);
|
||||
results.Add(indicator.Last.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(results, outReal, outRange, lookback);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(7)]
|
||||
[InlineData(21)]
|
||||
[InlineData(28)]
|
||||
public void MatchesTalib_VariousPeriods(int period)
|
||||
{
|
||||
var indicator = new PlusDm(period);
|
||||
var results = new List<double>();
|
||||
|
||||
for (int i = 0; i < _data.Bars.Count; i++)
|
||||
{
|
||||
indicator.Update(_data.Bars[i]);
|
||||
results.Add(indicator.Last.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, period);
|
||||
Assert.Equal(TALib.Core.RetCode.Success, retCode);
|
||||
|
||||
int lookback = Functions.PlusDMLookback(period);
|
||||
ValidationHelper.VerifyData(results, outReal, outRange, lookback);
|
||||
}
|
||||
|
||||
// ═══════════════════════════════════════════════
|
||||
// Dx Equivalence
|
||||
// ═══════════════════════════════════════════════
|
||||
|
||||
[Fact]
|
||||
public void ExactlyMatchesDx_DmPlus()
|
||||
{
|
||||
var indicator = new PlusDm(14);
|
||||
var dx = new Dx(14);
|
||||
|
||||
for (int i = 0; i < _data.Bars.Count; i++)
|
||||
{
|
||||
indicator.Update(_data.Bars[i]);
|
||||
dx.Update(_data.Bars[i]);
|
||||
|
||||
Assert.Equal(dx.DmPlus.Value, indicator.Last.Value, 1e-12);
|
||||
}
|
||||
}
|
||||
|
||||
// ═══════════════════════════════════════════════
|
||||
// OoplesFinance Structural Validation
|
||||
// ═══════════════════════════════════════════════
|
||||
|
||||
[Fact]
|
||||
public void MatchesOoples_Structural()
|
||||
{
|
||||
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 allValues = adxResults.OutputValues.Values.SelectMany(v => v).ToList();
|
||||
int finiteCount = allValues.Count(v => double.IsFinite(v));
|
||||
Assert.True(finiteCount > 100, $"Expected >100 finite Ooples ADX/DI values, got {finiteCount}");
|
||||
}
|
||||
|
||||
// ═══════════════════════════════════════════════
|
||||
// Self-Consistency: Batch == Streaming
|
||||
// ═══════════════════════════════════════════════
|
||||
|
||||
[Fact]
|
||||
public void BatchEqualsStreaming()
|
||||
{
|
||||
var batchResults = PlusDm.Batch(_data.Bars, 14);
|
||||
|
||||
var streaming = new PlusDm(14);
|
||||
var streamResults = new List<double>();
|
||||
for (int i = 0; i < _data.Bars.Count; i++)
|
||||
{
|
||||
streamResults.Add(streaming.Update(_data.Bars[i]).Value);
|
||||
}
|
||||
|
||||
Assert.Equal(streamResults.Count, batchResults.Count);
|
||||
for (int i = 0; i < batchResults.Count; i++)
|
||||
{
|
||||
Assert.Equal(streamResults[i], batchResults.Values[i], 1e-9);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void BatchMatchesTalib()
|
||||
{
|
||||
var batchResults = PlusDm.Batch(_data.Bars, 14);
|
||||
|
||||
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(batchResults.Select(x => x.Value).ToList(), outReal, outRange, lookback);
|
||||
}
|
||||
|
||||
// ═══════════════════════════════════════════════
|
||||
// Determinism
|
||||
// ═══════════════════════════════════════════════
|
||||
|
||||
[Fact]
|
||||
public void ConsistentAcrossMultipleRuns()
|
||||
{
|
||||
var ind1 = new PlusDm(14);
|
||||
var ind2 = new PlusDm(14);
|
||||
var results1 = new List<double>();
|
||||
var results2 = new List<double>();
|
||||
|
||||
for (int i = 0; i < _data.Bars.Count; i++)
|
||||
{
|
||||
ind1.Update(_data.Bars[i]);
|
||||
results1.Add(ind1.Last.Value);
|
||||
}
|
||||
|
||||
for (int i = 0; i < _data.Bars.Count; i++)
|
||||
{
|
||||
ind2.Update(_data.Bars[i]);
|
||||
results2.Add(ind2.Last.Value);
|
||||
}
|
||||
|
||||
for (int i = 0; i < _data.Bars.Count; i++)
|
||||
{
|
||||
Assert.Equal(results1[i], results2[i], 1e-10);
|
||||
}
|
||||
}
|
||||
|
||||
// ═══════════════════════════════════════════════
|
||||
// Output Range Validation
|
||||
// ═══════════════════════════════════════════════
|
||||
|
||||
[Fact]
|
||||
public void OutputIsNonNegative()
|
||||
{
|
||||
var indicator = new PlusDm(14);
|
||||
|
||||
for (int i = 0; i < _data.Bars.Count; i++)
|
||||
{
|
||||
indicator.Update(_data.Bars[i]);
|
||||
Assert.True(indicator.Last.Value >= 0, $"+DM output at bar {i} was {indicator.Last.Value}");
|
||||
}
|
||||
}
|
||||
|
||||
// ═══════════════════════════════════════════════
|
||||
// Different Periods Produce Different Results
|
||||
// ═══════════════════════════════════════════════
|
||||
|
||||
[Fact]
|
||||
public void DifferentPeriods_ProduceDifferentResults()
|
||||
{
|
||||
var short7 = new PlusDm(7);
|
||||
var long28 = new PlusDm(28);
|
||||
|
||||
for (int i = 0; i < _data.Bars.Count; i++)
|
||||
{
|
||||
short7.Update(_data.Bars[i]);
|
||||
long28.Update(_data.Bars[i]);
|
||||
}
|
||||
|
||||
Assert.NotEqual(short7.Last.Value, long28.Last.Value);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user