mirror of
https://github.com/mihakralj/QuanTAlib.git
synced 2026-08-14 00:28:05 +00:00
feat(dynamics): add PlusDI, MinusDI, PlusDM, MinusDM indicators
Complete thin Dx-composition wrapper indicators with full test coverage: - PlusDi/MinusDi: Directional Indicator wrappers (DiPlus/DiMinus from Dx) - PlusDm/MinusDm: Directional Movement wrappers (DmPlus/DmMinus from Dx) - Individual validation tests per indicator directory (TALib, Skender, bounds) - Combined unit tests (DiDm.Tests.cs) and validation tests (DiDm.Validation.Tests.cs) - Quantower wrappers + tests for all 4 indicators - PineScript v6 implementations with compensated RMA - Normalized .md documentation for all indicators and categories - 182 tests passing, 0 failures
This commit is contained in:
@@ -0,0 +1,917 @@
|
||||
|
||||
namespace QuanTAlib;
|
||||
|
||||
/// <summary>
|
||||
/// Combined unit tests for PlusDi, MinusDi, PlusDm, MinusDm.
|
||||
/// All four are thin Dx-composition wrappers extracting a single property.
|
||||
/// </summary>
|
||||
public class DiDmTests
|
||||
{
|
||||
// ═══════════════════════════════════════════════
|
||||
// A. Constructor / Parameter Tests
|
||||
// ═══════════════════════════════════════════════
|
||||
|
||||
[Fact]
|
||||
public void PlusDi_Constructor_InvalidPeriod_Throws()
|
||||
{
|
||||
Assert.Throws<ArgumentException>(() => new PlusDi(0));
|
||||
Assert.Throws<ArgumentException>(() => new PlusDi(-1));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void MinusDi_Constructor_InvalidPeriod_Throws()
|
||||
{
|
||||
Assert.Throws<ArgumentException>(() => new MinusDi(0));
|
||||
Assert.Throws<ArgumentException>(() => new MinusDi(-1));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void PlusDm_Constructor_InvalidPeriod_Throws()
|
||||
{
|
||||
Assert.Throws<ArgumentException>(() => new PlusDm(0));
|
||||
Assert.Throws<ArgumentException>(() => new PlusDm(-1));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void MinusDm_Constructor_InvalidPeriod_Throws()
|
||||
{
|
||||
Assert.Throws<ArgumentException>(() => new MinusDm(0));
|
||||
Assert.Throws<ArgumentException>(() => new MinusDm(-1));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void PlusDi_DefaultPeriod_Is14()
|
||||
{
|
||||
var indicator = new PlusDi();
|
||||
Assert.Equal(14, indicator.Period);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void MinusDi_DefaultPeriod_Is14()
|
||||
{
|
||||
var indicator = new MinusDi();
|
||||
Assert.Equal(14, indicator.Period);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void PlusDm_DefaultPeriod_Is14()
|
||||
{
|
||||
var indicator = new PlusDm();
|
||||
Assert.Equal(14, indicator.Period);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void MinusDm_DefaultPeriod_Is14()
|
||||
{
|
||||
var indicator = new MinusDm();
|
||||
Assert.Equal(14, indicator.Period);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void PlusDi_Name_ContainsPeriod()
|
||||
{
|
||||
var indicator = new PlusDi(20);
|
||||
Assert.Contains("20", indicator.Name, StringComparison.Ordinal);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void MinusDi_Name_ContainsPeriod()
|
||||
{
|
||||
var indicator = new MinusDi(20);
|
||||
Assert.Contains("20", indicator.Name, StringComparison.Ordinal);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void PlusDm_Name_ContainsPeriod()
|
||||
{
|
||||
var indicator = new PlusDm(20);
|
||||
Assert.Contains("20", indicator.Name, StringComparison.Ordinal);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void MinusDm_Name_ContainsPeriod()
|
||||
{
|
||||
var indicator = new MinusDm(20);
|
||||
Assert.Contains("20", indicator.Name, StringComparison.Ordinal);
|
||||
}
|
||||
|
||||
// ═══════════════════════════════════════════════
|
||||
// B. Basic Calculation Tests
|
||||
// ═══════════════════════════════════════════════
|
||||
|
||||
[Fact]
|
||||
public void PlusDi_BasicCalculation_DoesNotCrash()
|
||||
{
|
||||
var indicator = new PlusDi(14);
|
||||
var gbm = new GBM();
|
||||
var bars = gbm.Fetch(1000, DateTime.UtcNow.Ticks, TimeSpan.FromMinutes(1));
|
||||
|
||||
for (int i = 0; i < bars.Count; i++)
|
||||
{
|
||||
indicator.Update(bars[i]);
|
||||
}
|
||||
|
||||
Assert.True(double.IsFinite(indicator.Last.Value));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void MinusDi_BasicCalculation_DoesNotCrash()
|
||||
{
|
||||
var indicator = new MinusDi(14);
|
||||
var gbm = new GBM();
|
||||
var bars = gbm.Fetch(1000, DateTime.UtcNow.Ticks, TimeSpan.FromMinutes(1));
|
||||
|
||||
for (int i = 0; i < bars.Count; i++)
|
||||
{
|
||||
indicator.Update(bars[i]);
|
||||
}
|
||||
|
||||
Assert.True(double.IsFinite(indicator.Last.Value));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void PlusDm_BasicCalculation_DoesNotCrash()
|
||||
{
|
||||
var indicator = new PlusDm(14);
|
||||
var gbm = new GBM();
|
||||
var bars = gbm.Fetch(1000, DateTime.UtcNow.Ticks, TimeSpan.FromMinutes(1));
|
||||
|
||||
for (int i = 0; i < bars.Count; i++)
|
||||
{
|
||||
indicator.Update(bars[i]);
|
||||
}
|
||||
|
||||
Assert.True(double.IsFinite(indicator.Last.Value));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void MinusDm_BasicCalculation_DoesNotCrash()
|
||||
{
|
||||
var indicator = new MinusDm(14);
|
||||
var gbm = new GBM();
|
||||
var bars = gbm.Fetch(1000, DateTime.UtcNow.Ticks, TimeSpan.FromMinutes(1));
|
||||
|
||||
for (int i = 0; i < bars.Count; i++)
|
||||
{
|
||||
indicator.Update(bars[i]);
|
||||
}
|
||||
|
||||
Assert.True(double.IsFinite(indicator.Last.Value));
|
||||
}
|
||||
|
||||
// ═══════════════════════════════════════════════
|
||||
// C. IsHot / WarmupPeriod Tests
|
||||
// ═══════════════════════════════════════════════
|
||||
|
||||
[Fact]
|
||||
public void PlusDi_IsHot_BecomesTrueAfterWarmup()
|
||||
{
|
||||
var indicator = new PlusDi(14);
|
||||
var gbm = new GBM();
|
||||
var bars = gbm.Fetch(100, DateTime.UtcNow.Ticks, TimeSpan.FromMinutes(1));
|
||||
|
||||
Assert.False(indicator.IsHot);
|
||||
|
||||
for (int i = 0; i < bars.Count; i++)
|
||||
{
|
||||
indicator.Update(bars[i]);
|
||||
if (indicator.IsHot)
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
Assert.True(indicator.IsHot);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void MinusDi_IsHot_BecomesTrueAfterWarmup()
|
||||
{
|
||||
var indicator = new MinusDi(14);
|
||||
var gbm = new GBM();
|
||||
var bars = gbm.Fetch(100, DateTime.UtcNow.Ticks, TimeSpan.FromMinutes(1));
|
||||
|
||||
Assert.False(indicator.IsHot);
|
||||
|
||||
for (int i = 0; i < bars.Count; i++)
|
||||
{
|
||||
indicator.Update(bars[i]);
|
||||
if (indicator.IsHot)
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
Assert.True(indicator.IsHot);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void PlusDm_IsHot_BecomesTrueAfterWarmup()
|
||||
{
|
||||
var indicator = new PlusDm(14);
|
||||
var gbm = new GBM();
|
||||
var bars = gbm.Fetch(100, DateTime.UtcNow.Ticks, TimeSpan.FromMinutes(1));
|
||||
|
||||
Assert.False(indicator.IsHot);
|
||||
|
||||
for (int i = 0; i < bars.Count; i++)
|
||||
{
|
||||
indicator.Update(bars[i]);
|
||||
if (indicator.IsHot)
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
Assert.True(indicator.IsHot);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void MinusDm_IsHot_BecomesTrueAfterWarmup()
|
||||
{
|
||||
var indicator = new MinusDm(14);
|
||||
var gbm = new GBM();
|
||||
var bars = gbm.Fetch(100, DateTime.UtcNow.Ticks, TimeSpan.FromMinutes(1));
|
||||
|
||||
Assert.False(indicator.IsHot);
|
||||
|
||||
for (int i = 0; i < bars.Count; i++)
|
||||
{
|
||||
indicator.Update(bars[i]);
|
||||
if (indicator.IsHot)
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
Assert.True(indicator.IsHot);
|
||||
}
|
||||
|
||||
// ═══════════════════════════════════════════════
|
||||
// D. Bar Correction (isNew=false) Tests
|
||||
// ═══════════════════════════════════════════════
|
||||
|
||||
[Fact]
|
||||
public void PlusDi_BarCorrection_MatchesFreshInstance()
|
||||
{
|
||||
var indicator = new PlusDi(14);
|
||||
var gbm = new GBM();
|
||||
var bars = gbm.Fetch(100, DateTime.UtcNow.Ticks, TimeSpan.FromMinutes(1));
|
||||
|
||||
for (int i = 0; i < 99; i++)
|
||||
{
|
||||
indicator.Update(bars[i]);
|
||||
}
|
||||
indicator.Update(bars[99]);
|
||||
|
||||
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 = indicator.Update(modifiedBar, isNew: false);
|
||||
|
||||
var fresh = new PlusDi(14);
|
||||
for (int i = 0; i < 99; i++)
|
||||
{
|
||||
fresh.Update(bars[i]);
|
||||
}
|
||||
var val3 = fresh.Update(modifiedBar);
|
||||
|
||||
Assert.Equal(val3.Value, val2.Value, 1e-9);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void MinusDi_BarCorrection_MatchesFreshInstance()
|
||||
{
|
||||
var indicator = new MinusDi(14);
|
||||
var gbm = new GBM();
|
||||
var bars = gbm.Fetch(100, DateTime.UtcNow.Ticks, TimeSpan.FromMinutes(1));
|
||||
|
||||
for (int i = 0; i < 99; i++)
|
||||
{
|
||||
indicator.Update(bars[i]);
|
||||
}
|
||||
indicator.Update(bars[99]);
|
||||
|
||||
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 = indicator.Update(modifiedBar, isNew: false);
|
||||
|
||||
var fresh = new MinusDi(14);
|
||||
for (int i = 0; i < 99; i++)
|
||||
{
|
||||
fresh.Update(bars[i]);
|
||||
}
|
||||
var val3 = fresh.Update(modifiedBar);
|
||||
|
||||
Assert.Equal(val3.Value, val2.Value, 1e-9);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void PlusDm_BarCorrection_MatchesFreshInstance()
|
||||
{
|
||||
var indicator = new PlusDm(14);
|
||||
var gbm = new GBM();
|
||||
var bars = gbm.Fetch(100, DateTime.UtcNow.Ticks, TimeSpan.FromMinutes(1));
|
||||
|
||||
for (int i = 0; i < 99; i++)
|
||||
{
|
||||
indicator.Update(bars[i]);
|
||||
}
|
||||
indicator.Update(bars[99]);
|
||||
|
||||
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 = indicator.Update(modifiedBar, isNew: false);
|
||||
|
||||
var fresh = new PlusDm(14);
|
||||
for (int i = 0; i < 99; i++)
|
||||
{
|
||||
fresh.Update(bars[i]);
|
||||
}
|
||||
var val3 = fresh.Update(modifiedBar);
|
||||
|
||||
Assert.Equal(val3.Value, val2.Value, 1e-9);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void MinusDm_BarCorrection_MatchesFreshInstance()
|
||||
{
|
||||
var indicator = new MinusDm(14);
|
||||
var gbm = new GBM();
|
||||
var bars = gbm.Fetch(100, DateTime.UtcNow.Ticks, TimeSpan.FromMinutes(1));
|
||||
|
||||
for (int i = 0; i < 99; i++)
|
||||
{
|
||||
indicator.Update(bars[i]);
|
||||
}
|
||||
indicator.Update(bars[99]);
|
||||
|
||||
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 = indicator.Update(modifiedBar, isNew: false);
|
||||
|
||||
var fresh = new MinusDm(14);
|
||||
for (int i = 0; i < 99; i++)
|
||||
{
|
||||
fresh.Update(bars[i]);
|
||||
}
|
||||
var val3 = fresh.Update(modifiedBar);
|
||||
|
||||
Assert.Equal(val3.Value, val2.Value, 1e-9);
|
||||
}
|
||||
|
||||
// ═══════════════════════════════════════════════
|
||||
// E. Reset Tests
|
||||
// ═══════════════════════════════════════════════
|
||||
|
||||
[Fact]
|
||||
public void PlusDi_Reset_ClearsState()
|
||||
{
|
||||
var indicator = new PlusDi(14);
|
||||
var gbm = new GBM();
|
||||
var bars = gbm.Fetch(100, DateTime.UtcNow.Ticks, TimeSpan.FromMinutes(1));
|
||||
|
||||
for (int i = 0; i < bars.Count; i++)
|
||||
{
|
||||
indicator.Update(bars[i]);
|
||||
}
|
||||
|
||||
indicator.Reset();
|
||||
Assert.Equal(0, indicator.Last.Value);
|
||||
Assert.False(indicator.IsHot);
|
||||
|
||||
for (int i = 0; i < bars.Count; i++)
|
||||
{
|
||||
indicator.Update(bars[i]);
|
||||
}
|
||||
|
||||
Assert.True(double.IsFinite(indicator.Last.Value));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void MinusDi_Reset_ClearsState()
|
||||
{
|
||||
var indicator = new MinusDi(14);
|
||||
var gbm = new GBM();
|
||||
var bars = gbm.Fetch(100, DateTime.UtcNow.Ticks, TimeSpan.FromMinutes(1));
|
||||
|
||||
for (int i = 0; i < bars.Count; i++)
|
||||
{
|
||||
indicator.Update(bars[i]);
|
||||
}
|
||||
|
||||
indicator.Reset();
|
||||
Assert.Equal(0, indicator.Last.Value);
|
||||
Assert.False(indicator.IsHot);
|
||||
|
||||
for (int i = 0; i < bars.Count; i++)
|
||||
{
|
||||
indicator.Update(bars[i]);
|
||||
}
|
||||
|
||||
Assert.True(double.IsFinite(indicator.Last.Value));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void PlusDm_Reset_ClearsState()
|
||||
{
|
||||
var indicator = new PlusDm(14);
|
||||
var gbm = new GBM();
|
||||
var bars = gbm.Fetch(100, DateTime.UtcNow.Ticks, TimeSpan.FromMinutes(1));
|
||||
|
||||
for (int i = 0; i < bars.Count; i++)
|
||||
{
|
||||
indicator.Update(bars[i]);
|
||||
}
|
||||
|
||||
indicator.Reset();
|
||||
Assert.Equal(0, indicator.Last.Value);
|
||||
Assert.False(indicator.IsHot);
|
||||
|
||||
for (int i = 0; i < bars.Count; i++)
|
||||
{
|
||||
indicator.Update(bars[i]);
|
||||
}
|
||||
|
||||
Assert.True(double.IsFinite(indicator.Last.Value));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void MinusDm_Reset_ClearsState()
|
||||
{
|
||||
var indicator = new MinusDm(14);
|
||||
var gbm = new GBM();
|
||||
var bars = gbm.Fetch(100, DateTime.UtcNow.Ticks, TimeSpan.FromMinutes(1));
|
||||
|
||||
for (int i = 0; i < bars.Count; i++)
|
||||
{
|
||||
indicator.Update(bars[i]);
|
||||
}
|
||||
|
||||
indicator.Reset();
|
||||
Assert.Equal(0, indicator.Last.Value);
|
||||
Assert.False(indicator.IsHot);
|
||||
|
||||
for (int i = 0; i < bars.Count; i++)
|
||||
{
|
||||
indicator.Update(bars[i]);
|
||||
}
|
||||
|
||||
Assert.True(double.IsFinite(indicator.Last.Value));
|
||||
}
|
||||
|
||||
// ═══════════════════════════════════════════════
|
||||
// F. Batch / Streaming Consistency Tests
|
||||
// ═══════════════════════════════════════════════
|
||||
|
||||
[Fact]
|
||||
public void PlusDi_Batch_MatchesStreaming()
|
||||
{
|
||||
var gbm = new GBM(seed: 123);
|
||||
var bars = gbm.Fetch(200, DateTime.UtcNow.Ticks, TimeSpan.FromMinutes(1));
|
||||
|
||||
var batchResult = PlusDi.Batch(bars, 14);
|
||||
|
||||
var streaming = new PlusDi(14);
|
||||
for (int i = 0; i < bars.Count; i++)
|
||||
{
|
||||
streaming.Update(bars[i]);
|
||||
}
|
||||
|
||||
Assert.Equal(batchResult.Last.Value, streaming.Last.Value, 9);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void MinusDi_Batch_MatchesStreaming()
|
||||
{
|
||||
var gbm = new GBM(seed: 123);
|
||||
var bars = gbm.Fetch(200, DateTime.UtcNow.Ticks, TimeSpan.FromMinutes(1));
|
||||
|
||||
var batchResult = MinusDi.Batch(bars, 14);
|
||||
|
||||
var streaming = new MinusDi(14);
|
||||
for (int i = 0; i < bars.Count; i++)
|
||||
{
|
||||
streaming.Update(bars[i]);
|
||||
}
|
||||
|
||||
Assert.Equal(batchResult.Last.Value, streaming.Last.Value, 9);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void PlusDm_Batch_MatchesStreaming()
|
||||
{
|
||||
var gbm = new GBM(seed: 123);
|
||||
var bars = gbm.Fetch(200, DateTime.UtcNow.Ticks, TimeSpan.FromMinutes(1));
|
||||
|
||||
var batchResult = PlusDm.Batch(bars, 14);
|
||||
|
||||
var streaming = new PlusDm(14);
|
||||
for (int i = 0; i < bars.Count; i++)
|
||||
{
|
||||
streaming.Update(bars[i]);
|
||||
}
|
||||
|
||||
Assert.Equal(batchResult.Last.Value, streaming.Last.Value, 9);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void MinusDm_Batch_MatchesStreaming()
|
||||
{
|
||||
var gbm = new GBM(seed: 123);
|
||||
var bars = gbm.Fetch(200, DateTime.UtcNow.Ticks, TimeSpan.FromMinutes(1));
|
||||
|
||||
var batchResult = MinusDm.Batch(bars, 14);
|
||||
|
||||
var streaming = new MinusDm(14);
|
||||
for (int i = 0; i < bars.Count; i++)
|
||||
{
|
||||
streaming.Update(bars[i]);
|
||||
}
|
||||
|
||||
Assert.Equal(batchResult.Last.Value, streaming.Last.Value, 9);
|
||||
}
|
||||
|
||||
// ═══════════════════════════════════════════════
|
||||
// G. Event / Pub Tests
|
||||
// ═══════════════════════════════════════════════
|
||||
|
||||
[Fact]
|
||||
public void PlusDi_Pub_FiresOnNewBar()
|
||||
{
|
||||
var indicator = new PlusDi(14);
|
||||
var gbm = new GBM();
|
||||
var bars = gbm.Fetch(20, DateTime.UtcNow.Ticks, TimeSpan.FromMinutes(1));
|
||||
int fireCount = 0;
|
||||
|
||||
indicator.Pub += (object? _, in TValueEventArgs _e) => { fireCount++; };
|
||||
|
||||
for (int i = 0; i < bars.Count; i++)
|
||||
{
|
||||
indicator.Update(bars[i]);
|
||||
}
|
||||
|
||||
Assert.Equal(bars.Count, fireCount);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void MinusDi_Pub_DoesNotFireOnCorrection()
|
||||
{
|
||||
var indicator = new MinusDi(14);
|
||||
var gbm = new GBM();
|
||||
var bars = gbm.Fetch(20, DateTime.UtcNow.Ticks, TimeSpan.FromMinutes(1));
|
||||
int fireCount = 0;
|
||||
|
||||
indicator.Pub += (object? _, in TValueEventArgs _e) => { fireCount++; };
|
||||
|
||||
for (int i = 0; i < bars.Count; i++)
|
||||
{
|
||||
indicator.Update(bars[i]);
|
||||
}
|
||||
|
||||
int countAfterNewBars = fireCount;
|
||||
|
||||
// Bar correction should not fire
|
||||
var modifiedBar = new TBar(bars[^1].Time, bars[^1].Open, bars[^1].High + 1.0, bars[^1].Low - 1.0, bars[^1].Close, bars[^1].Volume);
|
||||
indicator.Update(modifiedBar, isNew: false);
|
||||
|
||||
Assert.Equal(countAfterNewBars, fireCount);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void PlusDm_Pub_FiresOnNewBar()
|
||||
{
|
||||
var indicator = new PlusDm(14);
|
||||
var gbm = new GBM();
|
||||
var bars = gbm.Fetch(20, DateTime.UtcNow.Ticks, TimeSpan.FromMinutes(1));
|
||||
int fireCount = 0;
|
||||
|
||||
indicator.Pub += (object? _, in TValueEventArgs _e) => { fireCount++; };
|
||||
|
||||
for (int i = 0; i < bars.Count; i++)
|
||||
{
|
||||
indicator.Update(bars[i]);
|
||||
}
|
||||
|
||||
Assert.Equal(bars.Count, fireCount);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void MinusDm_Pub_DoesNotFireOnCorrection()
|
||||
{
|
||||
var indicator = new MinusDm(14);
|
||||
var gbm = new GBM();
|
||||
var bars = gbm.Fetch(20, DateTime.UtcNow.Ticks, TimeSpan.FromMinutes(1));
|
||||
int fireCount = 0;
|
||||
|
||||
indicator.Pub += (object? _, in TValueEventArgs _e) => { fireCount++; };
|
||||
|
||||
for (int i = 0; i < bars.Count; i++)
|
||||
{
|
||||
indicator.Update(bars[i]);
|
||||
}
|
||||
|
||||
int countAfterNewBars = fireCount;
|
||||
|
||||
var modifiedBar = new TBar(bars[^1].Time, bars[^1].Open, bars[^1].High + 1.0, bars[^1].Low - 1.0, bars[^1].Close, bars[^1].Volume);
|
||||
indicator.Update(modifiedBar, isNew: false);
|
||||
|
||||
Assert.Equal(countAfterNewBars, fireCount);
|
||||
}
|
||||
|
||||
// ═══════════════════════════════════════════════
|
||||
// H. Prime / Calculate / Chainability Tests
|
||||
// ═══════════════════════════════════════════════
|
||||
|
||||
[Fact]
|
||||
public void PlusDi_Prime_SetsState()
|
||||
{
|
||||
var indicator = new PlusDi(14);
|
||||
var gbm = new GBM();
|
||||
var bars = gbm.Fetch(100, DateTime.UtcNow.Ticks, TimeSpan.FromMinutes(1));
|
||||
|
||||
indicator.Prime(bars);
|
||||
|
||||
Assert.True(indicator.IsHot);
|
||||
Assert.True(double.IsFinite(indicator.Last.Value));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void MinusDi_Prime_SetsState()
|
||||
{
|
||||
var indicator = new MinusDi(14);
|
||||
var gbm = new GBM();
|
||||
var bars = gbm.Fetch(100, DateTime.UtcNow.Ticks, TimeSpan.FromMinutes(1));
|
||||
|
||||
indicator.Prime(bars);
|
||||
|
||||
Assert.True(indicator.IsHot);
|
||||
Assert.True(double.IsFinite(indicator.Last.Value));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void PlusDm_Calculate_ReturnsTupleWithIndicator()
|
||||
{
|
||||
var gbm = new GBM();
|
||||
var bars = gbm.Fetch(200, DateTime.UtcNow.Ticks, TimeSpan.FromMinutes(1));
|
||||
|
||||
var (results, ind) = PlusDm.Calculate(bars, 14);
|
||||
|
||||
Assert.Equal(bars.Count, results.Count);
|
||||
Assert.True(ind.IsHot);
|
||||
Assert.Equal(results[^1].Value, ind.Last.Value, 1e-9);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void MinusDm_Calculate_ReturnsTupleWithIndicator()
|
||||
{
|
||||
var gbm = new GBM();
|
||||
var bars = gbm.Fetch(200, DateTime.UtcNow.Ticks, TimeSpan.FromMinutes(1));
|
||||
|
||||
var (results, ind) = MinusDm.Calculate(bars, 14);
|
||||
|
||||
Assert.Equal(bars.Count, results.Count);
|
||||
Assert.True(ind.IsHot);
|
||||
Assert.Equal(results[^1].Value, ind.Last.Value, 1e-9);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void PlusDi_TBarSeriesConstructor_Works()
|
||||
{
|
||||
var gbm = new GBM();
|
||||
var bars = gbm.Fetch(200, DateTime.UtcNow.Ticks, TimeSpan.FromMinutes(1));
|
||||
|
||||
var indicator = new PlusDi(bars, 14);
|
||||
|
||||
Assert.True(double.IsFinite(indicator.Last.Value));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void MinusDi_TBarSeriesConstructor_Works()
|
||||
{
|
||||
var gbm = new GBM();
|
||||
var bars = gbm.Fetch(200, DateTime.UtcNow.Ticks, TimeSpan.FromMinutes(1));
|
||||
|
||||
var indicator = new MinusDi(bars, 14);
|
||||
|
||||
Assert.True(double.IsFinite(indicator.Last.Value));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void PlusDi_ScalarUpdate_ReturnsLastUnchanged()
|
||||
{
|
||||
var indicator = new PlusDi(14);
|
||||
var gbm = new GBM();
|
||||
var bars = gbm.Fetch(50, DateTime.UtcNow.Ticks, TimeSpan.FromMinutes(1));
|
||||
|
||||
for (int i = 0; i < bars.Count; i++)
|
||||
{
|
||||
indicator.Update(bars[i]);
|
||||
}
|
||||
|
||||
var before = indicator.Last;
|
||||
var result = indicator.Update(new TValue(DateTime.UtcNow, 42.0));
|
||||
|
||||
Assert.Equal(before.Value, result.Value);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void MinusDm_ScalarUpdate_ReturnsLastUnchanged()
|
||||
{
|
||||
var indicator = new MinusDm(14);
|
||||
var gbm = new GBM();
|
||||
var bars = gbm.Fetch(50, DateTime.UtcNow.Ticks, TimeSpan.FromMinutes(1));
|
||||
|
||||
for (int i = 0; i < bars.Count; i++)
|
||||
{
|
||||
indicator.Update(bars[i]);
|
||||
}
|
||||
|
||||
var before = indicator.Last;
|
||||
var result = indicator.Update(new TValue(DateTime.UtcNow, 42.0));
|
||||
|
||||
Assert.Equal(before.Value, result.Value);
|
||||
}
|
||||
|
||||
// ═══════════════════════════════════════════════
|
||||
// Range / Value Constraint Tests
|
||||
// ═══════════════════════════════════════════════
|
||||
|
||||
[Fact]
|
||||
public void PlusDi_OutputRange_0to100()
|
||||
{
|
||||
var indicator = new PlusDi(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 = indicator.Update(bars[i]);
|
||||
if (indicator.IsHot)
|
||||
{
|
||||
Assert.InRange(result.Value, 0, 100);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void MinusDi_OutputRange_0to100()
|
||||
{
|
||||
var indicator = new MinusDi(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 = indicator.Update(bars[i]);
|
||||
if (indicator.IsHot)
|
||||
{
|
||||
Assert.InRange(result.Value, 0, 100);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void PlusDm_NonNegative()
|
||||
{
|
||||
var indicator = new PlusDm(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 = indicator.Update(bars[i]);
|
||||
if (indicator.IsHot)
|
||||
{
|
||||
Assert.True(result.Value >= 0, $"+DM should be non-negative, got {result.Value}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void MinusDm_NonNegative()
|
||||
{
|
||||
var indicator = new MinusDm(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 = indicator.Update(bars[i]);
|
||||
if (indicator.IsHot)
|
||||
{
|
||||
Assert.True(result.Value >= 0, $"-DM should be non-negative, got {result.Value}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ═══════════════════════════════════════════════
|
||||
// Dx Equivalence Tests
|
||||
// ═══════════════════════════════════════════════
|
||||
|
||||
[Fact]
|
||||
public void PlusDi_MatchesDx_DiPlus()
|
||||
{
|
||||
var gbm = new GBM(seed: 42);
|
||||
var bars = gbm.Fetch(200, DateTime.UtcNow.Ticks, TimeSpan.FromMinutes(1));
|
||||
|
||||
var plusDi = new PlusDi(14);
|
||||
var dx = new Dx(14);
|
||||
|
||||
for (int i = 0; i < bars.Count; i++)
|
||||
{
|
||||
plusDi.Update(bars[i]);
|
||||
dx.Update(bars[i]);
|
||||
|
||||
Assert.Equal(dx.DiPlus.Value, plusDi.Last.Value, 1e-12);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void MinusDi_MatchesDx_DiMinus()
|
||||
{
|
||||
var gbm = new GBM(seed: 42);
|
||||
var bars = gbm.Fetch(200, DateTime.UtcNow.Ticks, TimeSpan.FromMinutes(1));
|
||||
|
||||
var minusDi = new MinusDi(14);
|
||||
var dx = new Dx(14);
|
||||
|
||||
for (int i = 0; i < bars.Count; i++)
|
||||
{
|
||||
minusDi.Update(bars[i]);
|
||||
dx.Update(bars[i]);
|
||||
|
||||
Assert.Equal(dx.DiMinus.Value, minusDi.Last.Value, 1e-12);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void PlusDm_MatchesDx_DmPlus()
|
||||
{
|
||||
var gbm = new GBM(seed: 42);
|
||||
var bars = gbm.Fetch(200, DateTime.UtcNow.Ticks, TimeSpan.FromMinutes(1));
|
||||
|
||||
var plusDm = new PlusDm(14);
|
||||
var dx = new Dx(14);
|
||||
|
||||
for (int i = 0; i < bars.Count; i++)
|
||||
{
|
||||
plusDm.Update(bars[i]);
|
||||
dx.Update(bars[i]);
|
||||
|
||||
Assert.Equal(dx.DmPlus.Value, plusDm.Last.Value, 1e-12);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void MinusDm_MatchesDx_DmMinus()
|
||||
{
|
||||
var gbm = new GBM(seed: 42);
|
||||
var bars = gbm.Fetch(200, DateTime.UtcNow.Ticks, TimeSpan.FromMinutes(1));
|
||||
|
||||
var minusDm = new MinusDm(14);
|
||||
var dx = new Dx(14);
|
||||
|
||||
for (int i = 0; i < bars.Count; i++)
|
||||
{
|
||||
minusDm.Update(bars[i]);
|
||||
dx.Update(bars[i]);
|
||||
|
||||
Assert.Equal(dx.DmMinus.Value, minusDm.Last.Value, 1e-12);
|
||||
}
|
||||
}
|
||||
|
||||
// ═══════════════════════════════════════════════
|
||||
// Determinism Tests
|
||||
// ═══════════════════════════════════════════════
|
||||
|
||||
[Fact]
|
||||
public void AllFour_Deterministic_SameSeedSameResult()
|
||||
{
|
||||
var gbm1 = new GBM(seed: 99);
|
||||
var bars1 = gbm1.Fetch(200, DateTime.UtcNow.Ticks, TimeSpan.FromMinutes(1));
|
||||
var gbm2 = new GBM(seed: 99);
|
||||
var bars2 = gbm2.Fetch(200, DateTime.UtcNow.Ticks, TimeSpan.FromMinutes(1));
|
||||
|
||||
var pdi1 = new PlusDi(14);
|
||||
var pdi2 = new PlusDi(14);
|
||||
var mdi1 = new MinusDi(14);
|
||||
var mdi2 = new MinusDi(14);
|
||||
var pdm1 = new PlusDm(14);
|
||||
var pdm2 = new PlusDm(14);
|
||||
var mdm1 = new MinusDm(14);
|
||||
var mdm2 = new MinusDm(14);
|
||||
|
||||
for (int i = 0; i < bars1.Count; i++)
|
||||
{
|
||||
pdi1.Update(bars1[i]);
|
||||
pdi2.Update(bars2[i]);
|
||||
mdi1.Update(bars1[i]);
|
||||
mdi2.Update(bars2[i]);
|
||||
pdm1.Update(bars1[i]);
|
||||
pdm2.Update(bars2[i]);
|
||||
mdm1.Update(bars1[i]);
|
||||
mdm2.Update(bars2[i]);
|
||||
}
|
||||
|
||||
Assert.Equal(pdi1.Last.Value, pdi2.Last.Value, 1e-12);
|
||||
Assert.Equal(mdi1.Last.Value, mdi2.Last.Value, 1e-12);
|
||||
Assert.Equal(pdm1.Last.Value, pdm2.Last.Value, 1e-12);
|
||||
Assert.Equal(mdm1.Last.Value, mdm2.Last.Value, 1e-12);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,818 @@
|
||||
using OoplesFinance.StockIndicators;
|
||||
using OoplesFinance.StockIndicators.Models;
|
||||
using OoplesFinance.StockIndicators.Enums;
|
||||
using Skender.Stock.Indicators;
|
||||
using TALib;
|
||||
using QuanTAlib.Tests;
|
||||
|
||||
namespace QuanTAlib;
|
||||
|
||||
/// <summary>
|
||||
/// Combined validation tests for PlusDi, MinusDi, PlusDm, MinusDm.
|
||||
/// Cross-validates against TA-Lib, Skender, Ooples, and Dx equivalence.
|
||||
/// </summary>
|
||||
public sealed class DiDmValidationTests : IDisposable
|
||||
{
|
||||
private readonly ValidationTestData _data;
|
||||
|
||||
public DiDmValidationTests()
|
||||
{
|
||||
_data = new ValidationTestData();
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
_data.Dispose();
|
||||
}
|
||||
|
||||
// ═══════════════════════════════════════════════
|
||||
// TA-Lib Validation
|
||||
// ═══════════════════════════════════════════════
|
||||
|
||||
[Fact]
|
||||
public void PlusDi_MatchesTalib()
|
||||
{
|
||||
var indicator = new PlusDi(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[] 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(results, outReal, outRange, lookback);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void MinusDi_MatchesTalib()
|
||||
{
|
||||
var indicator = new MinusDi(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[] 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(results, outReal, outRange, lookback);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void PlusDm_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);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void MinusDm_MatchesTalib()
|
||||
{
|
||||
var indicator = new MinusDm(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.MinusDM(hData, lData, 0..^0, outReal, out var outRange, 14);
|
||||
Assert.Equal(TALib.Core.RetCode.Success, retCode);
|
||||
|
||||
int lookback = Functions.MinusDMLookback(14);
|
||||
ValidationHelper.VerifyData(results, outReal, outRange, lookback);
|
||||
}
|
||||
|
||||
// ═══════════════════════════════════════════════
|
||||
// Skender Validation
|
||||
// ═══════════════════════════════════════════════
|
||||
|
||||
[Fact]
|
||||
public void PlusDi_MatchesSkender()
|
||||
{
|
||||
var indicator = new PlusDi(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);
|
||||
}
|
||||
|
||||
var skenderResults = _data.SkenderQuotes.GetAdx(14).ToList();
|
||||
|
||||
ValidationHelper.VerifyData(results, skenderResults, x => x.Pdi);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void MinusDi_MatchesSkender()
|
||||
{
|
||||
var indicator = new MinusDi(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);
|
||||
}
|
||||
|
||||
var skenderResults = _data.SkenderQuotes.GetAdx(14).ToList();
|
||||
|
||||
ValidationHelper.VerifyData(results, skenderResults, x => x.Mdi);
|
||||
}
|
||||
|
||||
// ═══════════════════════════════════════════════
|
||||
// Dx Equivalence
|
||||
// ═══════════════════════════════════════════════
|
||||
|
||||
[Fact]
|
||||
public void PlusDi_ExactlyMatchesDx_DiPlus()
|
||||
{
|
||||
var indicator = new PlusDi(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.DiPlus.Value, indicator.Last.Value, 1e-12);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void MinusDi_ExactlyMatchesDx_DiMinus()
|
||||
{
|
||||
var indicator = new MinusDi(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.DiMinus.Value, indicator.Last.Value, 1e-12);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void PlusDm_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);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void MinusDm_ExactlyMatchesDx_DmMinus()
|
||||
{
|
||||
var indicator = new MinusDm(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.DmMinus.Value, indicator.Last.Value, 1e-12);
|
||||
}
|
||||
}
|
||||
|
||||
// ═══════════════════════════════════════════════
|
||||
// Self-Consistency: Batch == Streaming
|
||||
// ═══════════════════════════════════════════════
|
||||
|
||||
[Fact]
|
||||
public void PlusDi_BatchEqualsStreaming()
|
||||
{
|
||||
var batchResults = PlusDi.Batch(_data.Bars, 14);
|
||||
|
||||
var streaming = new PlusDi(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 MinusDi_BatchEqualsStreaming()
|
||||
{
|
||||
var batchResults = MinusDi.Batch(_data.Bars, 14);
|
||||
|
||||
var streaming = new MinusDi(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 PlusDm_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 MinusDm_BatchEqualsStreaming()
|
||||
{
|
||||
var batchResults = MinusDm.Batch(_data.Bars, 14);
|
||||
|
||||
var streaming = new MinusDm(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);
|
||||
}
|
||||
}
|
||||
|
||||
// ═══════════════════════════════════════════════
|
||||
// Multi-Period TALib Validation
|
||||
// ═══════════════════════════════════════════════
|
||||
|
||||
[Theory]
|
||||
[InlineData(7)]
|
||||
[InlineData(21)]
|
||||
[InlineData(28)]
|
||||
public void PlusDi_MatchesTalib_VariousPeriods(int period)
|
||||
{
|
||||
var indicator = new PlusDi(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[] 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, period);
|
||||
Assert.Equal(TALib.Core.RetCode.Success, retCode);
|
||||
|
||||
int lookback = Functions.PlusDILookback(period);
|
||||
ValidationHelper.VerifyData(results, outReal, outRange, lookback);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(7)]
|
||||
[InlineData(21)]
|
||||
[InlineData(28)]
|
||||
public void MinusDi_MatchesTalib_VariousPeriods(int period)
|
||||
{
|
||||
var indicator = new MinusDi(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[] 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, period);
|
||||
Assert.Equal(TALib.Core.RetCode.Success, retCode);
|
||||
|
||||
int lookback = Functions.MinusDILookback(period);
|
||||
ValidationHelper.VerifyData(results, outReal, outRange, lookback);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(7)]
|
||||
[InlineData(21)]
|
||||
[InlineData(28)]
|
||||
public void PlusDm_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);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(7)]
|
||||
[InlineData(21)]
|
||||
[InlineData(28)]
|
||||
public void MinusDm_MatchesTalib_VariousPeriods(int period)
|
||||
{
|
||||
var indicator = new MinusDm(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.MinusDM(hData, lData, 0..^0, outReal, out var outRange, period);
|
||||
Assert.Equal(TALib.Core.RetCode.Success, retCode);
|
||||
|
||||
int lookback = Functions.MinusDMLookback(period);
|
||||
ValidationHelper.VerifyData(results, outReal, outRange, lookback);
|
||||
}
|
||||
|
||||
// ═══════════════════════════════════════════════
|
||||
// Multi-Period Skender Validation
|
||||
// ═══════════════════════════════════════════════
|
||||
|
||||
[Theory]
|
||||
[InlineData(7)]
|
||||
[InlineData(21)]
|
||||
[InlineData(28)]
|
||||
public void PlusDi_MatchesSkender_VariousPeriods(int period)
|
||||
{
|
||||
var indicator = new PlusDi(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);
|
||||
}
|
||||
|
||||
var skenderResults = _data.SkenderQuotes.GetAdx(period).ToList();
|
||||
ValidationHelper.VerifyData(results, skenderResults, x => x.Pdi);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(7)]
|
||||
[InlineData(21)]
|
||||
[InlineData(28)]
|
||||
public void MinusDi_MatchesSkender_VariousPeriods(int period)
|
||||
{
|
||||
var indicator = new MinusDi(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);
|
||||
}
|
||||
|
||||
var skenderResults = _data.SkenderQuotes.GetAdx(period).ToList();
|
||||
ValidationHelper.VerifyData(results, skenderResults, x => x.Mdi);
|
||||
}
|
||||
|
||||
// ═══════════════════════════════════════════════
|
||||
// Determinism: Consistent Across Multiple Runs
|
||||
// ═══════════════════════════════════════════════
|
||||
|
||||
[Fact]
|
||||
public void PlusDi_ConsistentAcrossMultipleRuns()
|
||||
{
|
||||
var ind1 = new PlusDi(14);
|
||||
var ind2 = new PlusDi(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);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void MinusDi_ConsistentAcrossMultipleRuns()
|
||||
{
|
||||
var ind1 = new MinusDi(14);
|
||||
var ind2 = new MinusDi(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);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void PlusDm_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);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void MinusDm_ConsistentAcrossMultipleRuns()
|
||||
{
|
||||
var ind1 = new MinusDm(14);
|
||||
var ind2 = new MinusDm(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);
|
||||
}
|
||||
}
|
||||
|
||||
// ═══════════════════════════════════════════════
|
||||
// Non-Negative Output Validation
|
||||
// ═══════════════════════════════════════════════
|
||||
|
||||
[Fact]
|
||||
public void PlusDi_OutputIsNonNegative()
|
||||
{
|
||||
var indicator = new PlusDi(14);
|
||||
|
||||
for (int i = 0; i < _data.Bars.Count; i++)
|
||||
{
|
||||
indicator.Update(_data.Bars[i]);
|
||||
Assert.True(indicator.Last.Value >= 0, $"PlusDi output at bar {i} was {indicator.Last.Value}");
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void MinusDi_OutputIsNonNegative()
|
||||
{
|
||||
var indicator = new MinusDi(14);
|
||||
|
||||
for (int i = 0; i < _data.Bars.Count; i++)
|
||||
{
|
||||
indicator.Update(_data.Bars[i]);
|
||||
Assert.True(indicator.Last.Value >= 0, $"MinusDi output at bar {i} was {indicator.Last.Value}");
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void PlusDm_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, $"PlusDm output at bar {i} was {indicator.Last.Value}");
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void MinusDm_OutputIsNonNegative()
|
||||
{
|
||||
var indicator = new MinusDm(14);
|
||||
|
||||
for (int i = 0; i < _data.Bars.Count; i++)
|
||||
{
|
||||
indicator.Update(_data.Bars[i]);
|
||||
Assert.True(indicator.Last.Value >= 0, $"MinusDm output at bar {i} was {indicator.Last.Value}");
|
||||
}
|
||||
}
|
||||
|
||||
// ═══════════════════════════════════════════════
|
||||
// DI values bounded 0-100
|
||||
// ═══════════════════════════════════════════════
|
||||
|
||||
[Fact]
|
||||
public void PlusDi_OutputBounded0To100()
|
||||
{
|
||||
var indicator = new PlusDi(14);
|
||||
|
||||
for (int i = 0; i < _data.Bars.Count; i++)
|
||||
{
|
||||
indicator.Update(_data.Bars[i]);
|
||||
double val = indicator.Last.Value;
|
||||
if (i >= 14)
|
||||
{
|
||||
Assert.True(val >= 0 && val <= 100, $"PlusDi at bar {i} was {val}, expected [0,100]");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void MinusDi_OutputBounded0To100()
|
||||
{
|
||||
var indicator = new MinusDi(14);
|
||||
|
||||
for (int i = 0; i < _data.Bars.Count; i++)
|
||||
{
|
||||
indicator.Update(_data.Bars[i]);
|
||||
double val = indicator.Last.Value;
|
||||
if (i >= 14)
|
||||
{
|
||||
Assert.True(val >= 0 && val <= 100, $"MinusDi at bar {i} was {val}, expected [0,100]");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ═══════════════════════════════════════════════
|
||||
// Different Periods Produce Different Results
|
||||
// ═══════════════════════════════════════════════
|
||||
|
||||
[Fact]
|
||||
public void PlusDi_DifferentPeriods_ProduceDifferentResults()
|
||||
{
|
||||
var short14 = new PlusDi(7);
|
||||
var long28 = new PlusDi(28);
|
||||
|
||||
for (int i = 0; i < _data.Bars.Count; i++)
|
||||
{
|
||||
short14.Update(_data.Bars[i]);
|
||||
long28.Update(_data.Bars[i]);
|
||||
}
|
||||
|
||||
Assert.NotEqual(short14.Last.Value, long28.Last.Value);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void MinusDi_DifferentPeriods_ProduceDifferentResults()
|
||||
{
|
||||
var short14 = new MinusDi(7);
|
||||
var long28 = new MinusDi(28);
|
||||
|
||||
for (int i = 0; i < _data.Bars.Count; i++)
|
||||
{
|
||||
short14.Update(_data.Bars[i]);
|
||||
long28.Update(_data.Bars[i]);
|
||||
}
|
||||
|
||||
Assert.NotEqual(short14.Last.Value, long28.Last.Value);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void PlusDm_DifferentPeriods_ProduceDifferentResults()
|
||||
{
|
||||
var short14 = new PlusDm(7);
|
||||
var long28 = new PlusDm(28);
|
||||
|
||||
for (int i = 0; i < _data.Bars.Count; i++)
|
||||
{
|
||||
short14.Update(_data.Bars[i]);
|
||||
long28.Update(_data.Bars[i]);
|
||||
}
|
||||
|
||||
Assert.NotEqual(short14.Last.Value, long28.Last.Value);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void MinusDm_DifferentPeriods_ProduceDifferentResults()
|
||||
{
|
||||
var short14 = new MinusDm(7);
|
||||
var long28 = new MinusDm(28);
|
||||
|
||||
for (int i = 0; i < _data.Bars.Count; i++)
|
||||
{
|
||||
short14.Update(_data.Bars[i]);
|
||||
long28.Update(_data.Bars[i]);
|
||||
}
|
||||
|
||||
Assert.NotEqual(short14.Last.Value, long28.Last.Value);
|
||||
}
|
||||
|
||||
// ═══════════════════════════════════════════════
|
||||
// OoplesFinance Structural Validation
|
||||
// ═══════════════════════════════════════════════
|
||||
|
||||
[Fact]
|
||||
public void DiDm_MatchesOoples_Structural()
|
||||
{
|
||||
// OoplesFinance.CalculateAverageDirectionalIndex produces Di+/Di- as part of ADX
|
||||
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);
|
||||
|
||||
// Verify the Ooples ADX calculation produces finite DI values
|
||||
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 DI/DM values, got {finiteCount}");
|
||||
}
|
||||
|
||||
// ═══════════════════════════════════════════════
|
||||
// Batch Matches TALib
|
||||
// ═══════════════════════════════════════════════
|
||||
|
||||
[Fact]
|
||||
public void PlusDi_BatchMatchesTalib()
|
||||
{
|
||||
var batchResults = PlusDi.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[] 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(batchResults.Select(x => x.Value).ToList(), outReal, outRange, lookback);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void MinusDi_BatchMatchesTalib()
|
||||
{
|
||||
var batchResults = MinusDi.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[] 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(batchResults.Select(x => x.Value).ToList(), outReal, outRange, lookback);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void PlusDm_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);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void MinusDm_BatchMatchesTalib()
|
||||
{
|
||||
var batchResults = MinusDm.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.MinusDM(hData, lData, 0..^0, outReal, out var outRange, 14);
|
||||
Assert.Equal(TALib.Core.RetCode.Success, retCode);
|
||||
|
||||
int lookback = Functions.MinusDMLookback(14);
|
||||
ValidationHelper.VerifyData(batchResults.Select(x => x.Value).ToList(), outReal, outRange, lookback);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,73 @@
|
||||
using TradingPlatform.BusinessLayer;
|
||||
using QuanTAlib;
|
||||
|
||||
namespace QuanTAlib.Tests;
|
||||
|
||||
public class PlusDiIndicatorTests
|
||||
{
|
||||
[Fact]
|
||||
public void PlusDiIndicator_Constructor_SetsDefaults()
|
||||
{
|
||||
var indicator = new PlusDiIndicator();
|
||||
|
||||
Assert.Equal(14, indicator.Period);
|
||||
Assert.True(indicator.ShowColdValues);
|
||||
Assert.Equal("+DI - Plus Directional Indicator", indicator.Name);
|
||||
Assert.True(indicator.SeparateWindow);
|
||||
Assert.True(indicator.OnBackGround);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void PlusDiIndicator_MinHistoryDepths_EqualsZero()
|
||||
{
|
||||
var indicator = new PlusDiIndicator { Period = 20 };
|
||||
|
||||
Assert.Equal(0, PlusDiIndicator.MinHistoryDepths);
|
||||
IWatchlistIndicator watchlistIndicator = indicator;
|
||||
Assert.Equal(0, watchlistIndicator.MinHistoryDepths);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void PlusDiIndicator_Initialize_CreatesInternal()
|
||||
{
|
||||
var indicator = new PlusDiIndicator { Period = 14 };
|
||||
|
||||
indicator.Initialize();
|
||||
|
||||
Assert.Single(indicator.LinesSeries);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void PlusDiIndicator_ProcessUpdate_HistoricalBar_ComputesValue()
|
||||
{
|
||||
var indicator = new PlusDiIndicator { 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 PlusDiIndicator_ShortName_IsCorrect()
|
||||
{
|
||||
var indicator = new PlusDiIndicator { Period = 20 };
|
||||
Assert.Equal("+DI 20", indicator.ShortName);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void PlusDiIndicator_SourceCodeLink_IsValid()
|
||||
{
|
||||
var indicator = new PlusDiIndicator();
|
||||
Assert.Contains("github.com", indicator.SourceCodeLink, StringComparison.OrdinalIgnoreCase);
|
||||
Assert.Contains("PlusDi.Quantower.cs", indicator.SourceCodeLink, StringComparison.OrdinalIgnoreCase);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
using System.Drawing;
|
||||
using System.Runtime.CompilerServices;
|
||||
using TradingPlatform.BusinessLayer;
|
||||
|
||||
namespace QuanTAlib;
|
||||
|
||||
[SkipLocalsInit]
|
||||
public sealed class PlusDiIndicator : Indicator, IWatchlistIndicator
|
||||
{
|
||||
[InputParameter("Period", sortIndex: 1, 1, 1000, 1, 0)]
|
||||
public int Period { get; set; } = 14;
|
||||
|
||||
[InputParameter("Show cold values", sortIndex: 21)]
|
||||
public bool ShowColdValues { get; set; } = true;
|
||||
|
||||
private PlusDi _plusDi = null!;
|
||||
private readonly LineSeries _plusDiSeries;
|
||||
|
||||
public static int MinHistoryDepths => 0;
|
||||
int IWatchlistIndicator.MinHistoryDepths => MinHistoryDepths;
|
||||
|
||||
public override string ShortName => $"+DI {Period}";
|
||||
public override string SourceCodeLink => "https://github.com/mihakralj/QuanTAlib/blob/main/lib/dynamics/plusdi/PlusDi.Quantower.cs";
|
||||
|
||||
public PlusDiIndicator()
|
||||
{
|
||||
OnBackGround = true;
|
||||
SeparateWindow = true;
|
||||
Name = "+DI - Plus Directional Indicator";
|
||||
Description = "Measures upward directional movement as a percentage of true range";
|
||||
|
||||
_plusDiSeries = new LineSeries(name: "+DI", color: Color.Green, width: 2, style: LineStyle.Solid);
|
||||
|
||||
AddLineSeries(_plusDiSeries);
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
protected override void OnInit()
|
||||
{
|
||||
_plusDi = new PlusDi(Period);
|
||||
base.OnInit();
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
protected override void OnUpdate(UpdateArgs args)
|
||||
{
|
||||
TValue result = _plusDi.Update(this.GetInputBar(args), args.IsNewBar());
|
||||
|
||||
_plusDiSeries.SetValue(result.Value, _plusDi.IsHot, ShowColdValues);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,288 @@
|
||||
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 PlusDi (+DI). Cross-validates against TA-Lib, Skender,
|
||||
/// OoplesFinance, and internal Dx equivalence with multiple periods.
|
||||
/// </summary>
|
||||
public sealed class PlusDiValidationTests : IDisposable
|
||||
{
|
||||
private readonly ValidationTestData _data;
|
||||
|
||||
public PlusDiValidationTests()
|
||||
{
|
||||
_data = new ValidationTestData();
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
_data.Dispose();
|
||||
}
|
||||
|
||||
// ═══════════════════════════════════════════════
|
||||
// TA-Lib Validation
|
||||
// ═══════════════════════════════════════════════
|
||||
|
||||
[Fact]
|
||||
public void MatchesTalib()
|
||||
{
|
||||
var indicator = new PlusDi(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[] 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(results, outReal, outRange, lookback);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(7)]
|
||||
[InlineData(21)]
|
||||
[InlineData(28)]
|
||||
public void MatchesTalib_VariousPeriods(int period)
|
||||
{
|
||||
var indicator = new PlusDi(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[] 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, period);
|
||||
Assert.Equal(TALib.Core.RetCode.Success, retCode);
|
||||
|
||||
int lookback = Functions.PlusDILookback(period);
|
||||
ValidationHelper.VerifyData(results, outReal, outRange, lookback);
|
||||
}
|
||||
|
||||
// ═══════════════════════════════════════════════
|
||||
// Skender Validation
|
||||
// ═══════════════════════════════════════════════
|
||||
|
||||
[Fact]
|
||||
public void MatchesSkender()
|
||||
{
|
||||
var indicator = new PlusDi(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);
|
||||
}
|
||||
|
||||
var skenderResults = _data.SkenderQuotes.GetAdx(14).ToList();
|
||||
ValidationHelper.VerifyData(results, skenderResults, x => x.Pdi);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(7)]
|
||||
[InlineData(21)]
|
||||
[InlineData(28)]
|
||||
public void MatchesSkender_VariousPeriods(int period)
|
||||
{
|
||||
var indicator = new PlusDi(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);
|
||||
}
|
||||
|
||||
var skenderResults = _data.SkenderQuotes.GetAdx(period).ToList();
|
||||
ValidationHelper.VerifyData(results, skenderResults, x => x.Pdi);
|
||||
}
|
||||
|
||||
// ═══════════════════════════════════════════════
|
||||
// Dx Equivalence
|
||||
// ═══════════════════════════════════════════════
|
||||
|
||||
[Fact]
|
||||
public void ExactlyMatchesDx_DiPlus()
|
||||
{
|
||||
var indicator = new PlusDi(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.DiPlus.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 DI values, got {finiteCount}");
|
||||
}
|
||||
|
||||
// ═══════════════════════════════════════════════
|
||||
// Self-Consistency: Batch == Streaming
|
||||
// ═══════════════════════════════════════════════
|
||||
|
||||
[Fact]
|
||||
public void BatchEqualsStreaming()
|
||||
{
|
||||
var batchResults = PlusDi.Batch(_data.Bars, 14);
|
||||
|
||||
var streaming = new PlusDi(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 = PlusDi.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[] 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(batchResults.Select(x => x.Value).ToList(), outReal, outRange, lookback);
|
||||
}
|
||||
|
||||
// ═══════════════════════════════════════════════
|
||||
// Determinism
|
||||
// ═══════════════════════════════════════════════
|
||||
|
||||
[Fact]
|
||||
public void ConsistentAcrossMultipleRuns()
|
||||
{
|
||||
var ind1 = new PlusDi(14);
|
||||
var ind2 = new PlusDi(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 PlusDi(14);
|
||||
|
||||
for (int i = 0; i < _data.Bars.Count; i++)
|
||||
{
|
||||
indicator.Update(_data.Bars[i]);
|
||||
Assert.True(indicator.Last.Value >= 0, $"+DI output at bar {i} was {indicator.Last.Value}");
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void OutputBounded0To100()
|
||||
{
|
||||
var indicator = new PlusDi(14);
|
||||
|
||||
for (int i = 0; i < _data.Bars.Count; i++)
|
||||
{
|
||||
indicator.Update(_data.Bars[i]);
|
||||
double val = indicator.Last.Value;
|
||||
if (i >= 14)
|
||||
{
|
||||
Assert.True(val >= 0 && val <= 100, $"+DI at bar {i} was {val}, expected [0,100]");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ═══════════════════════════════════════════════
|
||||
// Different Periods Produce Different Results
|
||||
// ═══════════════════════════════════════════════
|
||||
|
||||
[Fact]
|
||||
public void DifferentPeriods_ProduceDifferentResults()
|
||||
{
|
||||
var short7 = new PlusDi(7);
|
||||
var long28 = new PlusDi(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);
|
||||
}
|
||||
}
|
||||
@@ -1,30 +1,100 @@
|
||||
# PLUS_DI: Plus Directional Indicator
|
||||
|
||||
Measures upward directional movement strength as a percentage (0-100).
|
||||
> *+DI isolates upward directional thrust as a fraction of true range — the bullish arm of Wilder's directional system.*
|
||||
|
||||
## Introduction
|
||||
The Plus Directional Indicator (+DI) measures the strength of upward price movement relative to the true range. It is one of the components of the Directional Movement System developed by J. Welles Wilder Jr.
|
||||
| Property | Value |
|
||||
| ---------------- | -------------------------------- |
|
||||
| **Category** | Dynamic |
|
||||
| **Inputs** | OHLCV bar (TBar) |
|
||||
| **Parameters** | `period` (default 14) |
|
||||
| **Outputs** | Single series |
|
||||
| **Output range** | 0 to 100 |
|
||||
| **Warmup** | `period` bars |
|
||||
| **PineScript** | [plusdi.pine](plusdi.pine) |
|
||||
|
||||
When +DI is rising, upward price pressure is increasing. When +DI crosses above -DI, it signals a potential bullish trend. The +DI line is commonly plotted alongside -DI to visualize directional balance.
|
||||
- The Plus Directional Indicator measures the strength of upward price movement relative to true range.
|
||||
- Parameterized by `period` (default 14).
|
||||
- Output range: 0 to 100.
|
||||
- Requires `period` bars of warmup before first valid output (IsHot = true).
|
||||
- Validated against TA-Lib, Skender, and Dx equivalence.
|
||||
|
||||
## Calculation
|
||||
+DI = Smoothed(+DM) / Smoothed(TR) × 100
|
||||
The Plus Directional Indicator (+DI) is one component of J. Welles Wilder Jr.'s Directional Movement System. It quantifies the fraction of recent true range attributable to upward price extension. The computation smooths both +DM (plus directional movement) and TR (true range) with Wilder's RMA ($\alpha = 1/N$), then divides: $+DI = 100 \times \text{Smooth}(+DM) / \text{Smooth}(TR)$. When +DI rises, upward price pressure is increasing. When +DI crosses above -DI, it signals a potential bullish trend. The +DI line is commonly plotted alongside -DI to visualize directional balance.
|
||||
|
||||
Where:
|
||||
- +DM (Plus Directional Movement) = max(High - PrevHigh, 0) when High - PrevHigh > PrevLow - Low, else 0
|
||||
- TR (True Range) = max(High - Low, |High - PrevClose|, |Low - PrevClose|)
|
||||
- Smoothing uses Wilder's method: Smooth = Smooth - Smooth/N + Input
|
||||
## Historical Context
|
||||
|
||||
## Parameters
|
||||
| Parameter | Default | Range | Description |
|
||||
| :--- | :--- | :--- | :--- |
|
||||
| Period | 14 | 2-∞ | Wilder smoothing period |
|
||||
J. Welles Wilder Jr. introduced the Directional Movement System in *New Concepts in Technical Trading Systems* (1978). The system decomposes price range into directional components. +DI and -DI are the normalized indicators from which DX and ADX are derived. While most traders focus on ADX for trend strength, +DI and -DI remain essential for determining trend *direction* — a bullish signal occurs when +DI crosses above -DI, bearish when -DI crosses above +DI.
|
||||
|
||||
## Interpretation
|
||||
- **Rising +DI:** Strengthening upward movement
|
||||
- **+DI > -DI:** Bulls dominate; potential uptrend
|
||||
- **+DI crossover above -DI:** Bullish signal
|
||||
- **High +DI (>40):** Strong upward momentum
|
||||
## Architecture & Physics
|
||||
|
||||
## References
|
||||
- Wilder, J. Welles Jr. "New Concepts in Technical Trading Systems" (1978)
|
||||
### 1. Plus Directional Movement
|
||||
|
||||
$$\text{UpMove} = H_t - H_{t-1}, \quad \text{DownMove} = L_{t-1} - L_t$$
|
||||
|
||||
$$+DM = \begin{cases} \text{UpMove} & \text{if UpMove} > \text{DownMove and UpMove} > 0 \\ 0 & \text{otherwise} \end{cases}$$
|
||||
|
||||
### 2. True Range
|
||||
|
||||
$$TR = \max(H_t - L_t,\; |H_t - C_{t-1}|,\; |L_t - C_{t-1}|)$$
|
||||
|
||||
### 3. Wilder Smoothing (RMA)
|
||||
|
||||
$$+DM_{\text{smooth}} = \text{RMA}(+DM, N), \quad TR_{\text{smooth}} = \text{RMA}(TR, N)$$
|
||||
|
||||
### 4. Plus Directional Indicator
|
||||
|
||||
$$+DI = 100 \times \frac{+DM_{\text{smooth}}}{TR_{\text{smooth}}}$$
|
||||
|
||||
When $TR_{\text{smooth}} = 0$ (no price movement), +DI = 0.
|
||||
|
||||
### 5. Complexity
|
||||
|
||||
- **Time:** $O(1)$ per bar — all RMA updates are recursive
|
||||
- **Space:** $O(1)$ — scalar state only (delegates to Dx)
|
||||
- **Warmup:** $N$ bars
|
||||
|
||||
## Mathematical Foundation
|
||||
|
||||
### Parameters
|
||||
|
||||
| Symbol | Parameter | Default | Constraint |
|
||||
|--------|-----------|---------|------------|
|
||||
| $N$ | period | 14 | $N \geq 2$ |
|
||||
|
||||
### Interpretation
|
||||
|
||||
| +DI Value | Signal |
|
||||
|-----------|--------|
|
||||
| Rising +DI | Strengthening upward movement |
|
||||
| +DI > -DI | Bulls dominate; potential uptrend |
|
||||
| +DI crossover above -DI | Bullish signal |
|
||||
| High +DI (>40) | Strong upward momentum |
|
||||
|
||||
+DI measures directional *strength*, not absolute direction. Compare +DI vs -DI for directional bias: if $+DI > -DI$, the trend is up.
|
||||
|
||||
## Performance Profile
|
||||
|
||||
### Operation Count (Streaming Mode)
|
||||
|
||||
+DI is a thin wrapper around Dx. The per-bar cost is identical to Dx (one property extraction after Dx completes its update).
|
||||
|
||||
**Post-warmup steady state (per bar):**
|
||||
|
||||
| Operation | Count | Cost (cycles) | Subtotal |
|
||||
| :--- | :---: | :---: | :---: |
|
||||
| Dx.Update (full pipeline) | 1 | 75 | 75 |
|
||||
| Property extraction | 1 | 1 | 1 |
|
||||
| **Total** | **2** | — | **~76 cycles** |
|
||||
|
||||
### Quality Metrics
|
||||
|
||||
| Metric | Score | Notes |
|
||||
| :--- | :---: | :--- |
|
||||
| **Accuracy** | 9/10 | Exact Dx delegation; FMA-precise RMA smoothing |
|
||||
| **Timeliness** | 7/10 | N-bar warmup; responds to bar-level changes |
|
||||
| **Smoothness** | 7/10 | Single RMA layer; moderate noise suppression |
|
||||
| **Noise Rejection** | 7/10 | Wilder smoothing filters transient spikes |
|
||||
|
||||
## Resources
|
||||
|
||||
- Wilder, J.W. — *New Concepts in Technical Trading Systems* (Trend Research, 1978)
|
||||
- PineScript reference: `plusdi.pine` in indicator directory
|
||||
|
||||
@@ -0,0 +1,55 @@
|
||||
// Licensed under the Apache License, Version 2.0
|
||||
// © mihakralj
|
||||
//@version=6
|
||||
indicator("Plus Directional Indicator (+DI)", "+DI", overlay=false)
|
||||
|
||||
//@function Calculates +DI using Wilder's smoothing with compensated RMA
|
||||
//@param period Number of bars used in the calculation
|
||||
//@returns +DI value (0-100)
|
||||
//@optimized Uses Wilder's smoothing (RMA) with warmup compensation for accurate values from bar 1
|
||||
plusdi(simple int period) =>
|
||||
if period <= 0
|
||||
runtime.error("Period must be greater than 0")
|
||||
float alpha = 1.0 / period
|
||||
float beta = 1.0 - alpha
|
||||
float tr = 0.0
|
||||
float plus_dm = 0.0
|
||||
if na(close[1])
|
||||
tr := high - low
|
||||
else
|
||||
tr := math.max(high - low, math.max(math.abs(high - close[1]), math.abs(low - close[1])))
|
||||
float upMove = high - high[1]
|
||||
float downMove = low[1] - low
|
||||
if upMove > downMove and upMove > 0
|
||||
plus_dm := upMove
|
||||
var bool warmup = true
|
||||
var float e = 1.0
|
||||
var float tr_ema = 0.0
|
||||
var float tr_result = tr
|
||||
var float plus_dm_ema = 0.0
|
||||
var float plus_dm_result = plus_dm
|
||||
tr_ema := alpha * (tr - tr_ema) + tr_ema
|
||||
plus_dm_ema := alpha * (plus_dm - plus_dm_ema) + plus_dm_ema
|
||||
if warmup
|
||||
e *= beta
|
||||
float c = 1.0 / (1.0 - e)
|
||||
tr_result := c * tr_ema
|
||||
plus_dm_result := c * plus_dm_ema
|
||||
warmup := e > 1e-10
|
||||
else
|
||||
tr_result := tr_ema
|
||||
plus_dm_result := plus_dm_ema
|
||||
float plus_di = tr_result != 0.0 ? 100.0 * plus_dm_result / tr_result : 0.0
|
||||
plus_di
|
||||
|
||||
// ---------- Main loop ----------
|
||||
|
||||
// Inputs
|
||||
i_period = input.int(14, "Period", minval=1, tooltip="Number of bars used in the calculation")
|
||||
|
||||
// Calculation
|
||||
plus_di = plusdi(i_period)
|
||||
|
||||
// Plot
|
||||
plot(plus_di, "+DI", color=color.green, linewidth=2)
|
||||
hline(25, "Threshold", color=color.gray, linestyle=hline.style_dashed)
|
||||
Reference in New Issue
Block a user