2025-12-13 23:17:56 -08:00
|
|
|
|
using System;
|
|
|
|
|
|
using System.Runtime.CompilerServices;
|
2025-12-20 15:08:07 -08:00
|
|
|
|
using System.Collections.Generic;
|
2025-12-21 14:37:44 -08:00
|
|
|
|
using System.Runtime.InteropServices;
|
2025-12-13 23:17:56 -08:00
|
|
|
|
|
|
|
|
|
|
namespace QuanTAlib;
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// DMX – Jurik Directional Movement Index
|
|
|
|
|
|
/// A smoother, lower-lag alternative to Welles Wilder’s DMI/ADX.
|
|
|
|
|
|
/// Uses Jurik Moving Average (JMA) for smoothing directional movement components.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
[SkipLocalsInit]
|
|
|
|
|
|
public sealed class Dmx : ITValuePublisher
|
|
|
|
|
|
{
|
2025-12-21 14:37:44 -08:00
|
|
|
|
private readonly int _period;
|
2025-12-13 23:17:56 -08:00
|
|
|
|
private readonly Jma _jmaDMp;
|
|
|
|
|
|
private readonly Jma _jmaDMm;
|
|
|
|
|
|
private readonly Jma _jmaTR;
|
|
|
|
|
|
|
|
|
|
|
|
private TBar _prevBar;
|
|
|
|
|
|
private TBar _lastInput;
|
|
|
|
|
|
private bool _isInitialized;
|
|
|
|
|
|
|
|
|
|
|
|
public string Name { get; }
|
2025-12-27 15:46:28 -08:00
|
|
|
|
public event TValuePublishedHandler? Pub;
|
2025-12-13 23:17:56 -08:00
|
|
|
|
public TValue Last { get; private set; }
|
2025-12-16 21:16:50 -08:00
|
|
|
|
public int WarmupPeriod { get; }
|
2025-12-13 23:17:56 -08:00
|
|
|
|
|
|
|
|
|
|
public Dmx(int period)
|
|
|
|
|
|
{
|
|
|
|
|
|
Name = $"Dmx({period})";
|
2025-12-16 21:16:50 -08:00
|
|
|
|
WarmupPeriod = period;
|
2025-12-21 14:37:44 -08:00
|
|
|
|
_period = period;
|
2025-12-13 23:17:56 -08:00
|
|
|
|
_jmaDMp = new Jma(period);
|
|
|
|
|
|
_jmaDMm = new Jma(period);
|
|
|
|
|
|
_jmaTR = new Jma(period);
|
|
|
|
|
|
_isInitialized = false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
|
|
|
|
public void Reset()
|
|
|
|
|
|
{
|
|
|
|
|
|
_jmaDMp.Reset();
|
|
|
|
|
|
_jmaDMm.Reset();
|
|
|
|
|
|
_jmaTR.Reset();
|
|
|
|
|
|
_prevBar = default;
|
|
|
|
|
|
_lastInput = default;
|
|
|
|
|
|
_isInitialized = false;
|
|
|
|
|
|
Last = default;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
|
|
|
|
public TValue Update(TBar input, bool isNew = true)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (isNew)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (_isInitialized)
|
|
|
|
|
|
{
|
|
|
|
|
|
_prevBar = _lastInput;
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
_isInitialized = true;
|
|
|
|
|
|
// For the very first bar, _prevBar remains default (all zeros)
|
|
|
|
|
|
// But we want to handle the first bar logic specifically
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-12-16 21:16:50 -08:00
|
|
|
|
|
2025-12-13 23:17:56 -08:00
|
|
|
|
// We always update _lastInput to the current input
|
|
|
|
|
|
_lastInput = input;
|
|
|
|
|
|
|
|
|
|
|
|
double dmPlusRaw = 0;
|
|
|
|
|
|
double dmMinusRaw = 0;
|
|
|
|
|
|
double trRaw = 0;
|
|
|
|
|
|
|
|
|
|
|
|
if (!_isInitialized || _prevBar.Time == 0) // First bar or uninitialized
|
|
|
|
|
|
{
|
|
|
|
|
|
trRaw = input.High - input.Low;
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
double upMove = input.High - _prevBar.High;
|
|
|
|
|
|
double downMove = _prevBar.Low - input.Low;
|
|
|
|
|
|
|
|
|
|
|
|
if (upMove > downMove && upMove > 0)
|
|
|
|
|
|
dmPlusRaw = upMove;
|
2025-12-16 21:16:50 -08:00
|
|
|
|
|
2025-12-13 23:17:56 -08:00
|
|
|
|
if (downMove > upMove && downMove > 0)
|
|
|
|
|
|
dmMinusRaw = downMove;
|
|
|
|
|
|
|
|
|
|
|
|
double tr1 = input.High - input.Low;
|
|
|
|
|
|
double tr2 = Math.Abs(input.High - _prevBar.Close);
|
|
|
|
|
|
double tr3 = Math.Abs(input.Low - _prevBar.Close);
|
2025-12-16 21:16:50 -08:00
|
|
|
|
|
2025-12-13 23:17:56 -08:00
|
|
|
|
trRaw = Math.Max(tr1, Math.Max(tr2, tr3));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Smooth with JMA
|
|
|
|
|
|
// Note: JMA handles NaN and warm-up internally
|
|
|
|
|
|
double dmPlusSmooth = _jmaDMp.Update(new TValue(input.Time, dmPlusRaw), isNew).Value;
|
|
|
|
|
|
double dmMinusSmooth = _jmaDMm.Update(new TValue(input.Time, dmMinusRaw), isNew).Value;
|
|
|
|
|
|
double atrSmooth = _jmaTR.Update(new TValue(input.Time, trRaw), isNew).Value;
|
|
|
|
|
|
|
|
|
|
|
|
double diPlus = 0;
|
|
|
|
|
|
double diMinus = 0;
|
|
|
|
|
|
|
|
|
|
|
|
if (atrSmooth > 1e-12)
|
|
|
|
|
|
{
|
|
|
|
|
|
diPlus = (dmPlusSmooth / atrSmooth) * 100.0;
|
|
|
|
|
|
diMinus = (dmMinusSmooth / atrSmooth) * 100.0;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
double dmxValue = diPlus - diMinus;
|
|
|
|
|
|
|
|
|
|
|
|
Last = new TValue(input.Time, dmxValue);
|
2025-12-27 15:46:28 -08:00
|
|
|
|
Pub?.Invoke(this, new TValueEventArgs { Value = Last, IsNew = true });
|
2025-12-13 23:17:56 -08:00
|
|
|
|
return Last;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public TSeries Update(TBarSeries source)
|
|
|
|
|
|
{
|
2025-12-21 14:37:44 -08:00
|
|
|
|
int count = source.Count;
|
|
|
|
|
|
if (count == 0)
|
|
|
|
|
|
return [];
|
2025-12-13 23:17:56 -08:00
|
|
|
|
|
2025-12-21 14:37:44 -08:00
|
|
|
|
var t = new List<long>(count);
|
|
|
|
|
|
var v = new List<double>(count);
|
|
|
|
|
|
CollectionsMarshal.SetCount(t, count);
|
|
|
|
|
|
CollectionsMarshal.SetCount(v, count);
|
|
|
|
|
|
|
|
|
|
|
|
var tSpan = CollectionsMarshal.AsSpan(t);
|
|
|
|
|
|
var vSpan = CollectionsMarshal.AsSpan(v);
|
|
|
|
|
|
|
|
|
|
|
|
// Span-based batch calculation
|
|
|
|
|
|
Calculate(source.High.Values, source.Low.Values, source.Close.Values, _period, vSpan);
|
|
|
|
|
|
source.Close.Times.CopyTo(tSpan);
|
|
|
|
|
|
|
|
|
|
|
|
// Restore streaming state by replaying the series
|
2025-12-13 23:17:56 -08:00
|
|
|
|
Reset();
|
2025-12-21 14:37:44 -08:00
|
|
|
|
for (int i = 0; i < count; i++)
|
2025-12-13 23:17:56 -08:00
|
|
|
|
{
|
2025-12-21 14:37:44 -08:00
|
|
|
|
Update(source[i], true);
|
2025-12-13 23:17:56 -08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-12-21 14:37:44 -08:00
|
|
|
|
Last = new TValue(tSpan[count - 1], vSpan[count - 1]);
|
2025-12-13 23:17:56 -08:00
|
|
|
|
return new TSeries(t, v);
|
|
|
|
|
|
}
|
2025-12-14 21:55:40 -08:00
|
|
|
|
|
2025-12-21 14:37:44 -08:00
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
|
|
|
|
public static void Calculate(ReadOnlySpan<double> high,
|
|
|
|
|
|
ReadOnlySpan<double> low,
|
|
|
|
|
|
ReadOnlySpan<double> close,
|
|
|
|
|
|
int period,
|
|
|
|
|
|
Span<double> destination)
|
|
|
|
|
|
{
|
|
|
|
|
|
int len = high.Length;
|
|
|
|
|
|
if (len == 0)
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
|
|
if (low.Length != len || close.Length != len || destination.Length != len)
|
2025-12-27 15:46:28 -08:00
|
|
|
|
throw new ArgumentException("All input spans must have the same length", nameof(destination));
|
2025-12-21 14:37:44 -08:00
|
|
|
|
|
|
|
|
|
|
if (period <= 0)
|
|
|
|
|
|
throw new ArgumentException("Period must be greater than zero.", nameof(period));
|
|
|
|
|
|
|
|
|
|
|
|
const int StackallocThreshold = 256;
|
|
|
|
|
|
|
|
|
|
|
|
Span<double> dmPlus = len <= StackallocThreshold
|
|
|
|
|
|
? stackalloc double[len]
|
|
|
|
|
|
: new double[len];
|
|
|
|
|
|
|
|
|
|
|
|
Span<double> dmMinus = len <= StackallocThreshold
|
|
|
|
|
|
? stackalloc double[len]
|
|
|
|
|
|
: new double[len];
|
|
|
|
|
|
|
|
|
|
|
|
Span<double> tr = len <= StackallocThreshold
|
|
|
|
|
|
? stackalloc double[len]
|
|
|
|
|
|
: new double[len];
|
|
|
|
|
|
|
|
|
|
|
|
// First bar: only true range from high-low, no directional movement
|
|
|
|
|
|
tr[0] = high[0] - low[0];
|
|
|
|
|
|
dmPlus[0] = 0.0;
|
|
|
|
|
|
dmMinus[0] = 0.0;
|
|
|
|
|
|
|
|
|
|
|
|
for (int i = 1; i < len; i++)
|
|
|
|
|
|
{
|
|
|
|
|
|
double h = high[i];
|
|
|
|
|
|
double l = low[i];
|
|
|
|
|
|
double ph = high[i - 1];
|
|
|
|
|
|
double pl = low[i - 1];
|
|
|
|
|
|
double pc = close[i - 1];
|
|
|
|
|
|
|
|
|
|
|
|
double upMove = h - ph;
|
|
|
|
|
|
double downMove = pl - l;
|
|
|
|
|
|
|
|
|
|
|
|
double dmPlusRaw = 0.0;
|
|
|
|
|
|
double dmMinusRaw = 0.0;
|
|
|
|
|
|
|
|
|
|
|
|
if (upMove > downMove && upMove > 0.0)
|
|
|
|
|
|
dmPlusRaw = upMove;
|
|
|
|
|
|
|
|
|
|
|
|
if (downMove > upMove && downMove > 0.0)
|
|
|
|
|
|
dmMinusRaw = downMove;
|
|
|
|
|
|
|
|
|
|
|
|
double tr1 = h - l;
|
|
|
|
|
|
double tr2 = Math.Abs(h - pc);
|
|
|
|
|
|
double tr3 = Math.Abs(l - pc);
|
|
|
|
|
|
double trRaw = Math.Max(tr1, Math.Max(tr2, tr3));
|
|
|
|
|
|
|
|
|
|
|
|
dmPlus[i] = dmPlusRaw;
|
|
|
|
|
|
dmMinus[i] = dmMinusRaw;
|
|
|
|
|
|
tr[i] = trRaw;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Span<double> dmPlusSmooth = len <= StackallocThreshold
|
|
|
|
|
|
? stackalloc double[len]
|
|
|
|
|
|
: new double[len];
|
|
|
|
|
|
|
|
|
|
|
|
Span<double> dmMinusSmooth = len <= StackallocThreshold
|
|
|
|
|
|
? stackalloc double[len]
|
|
|
|
|
|
: new double[len];
|
|
|
|
|
|
|
|
|
|
|
|
Span<double> trSmooth = len <= StackallocThreshold
|
|
|
|
|
|
? stackalloc double[len]
|
|
|
|
|
|
: new double[len];
|
|
|
|
|
|
|
|
|
|
|
|
Jma.Calculate(dmPlus, dmPlusSmooth, period);
|
|
|
|
|
|
Jma.Calculate(dmMinus, dmMinusSmooth, period);
|
|
|
|
|
|
Jma.Calculate(tr, trSmooth, period);
|
|
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < len; i++)
|
|
|
|
|
|
{
|
|
|
|
|
|
double atr = trSmooth[i];
|
|
|
|
|
|
double diPlus = 0.0;
|
|
|
|
|
|
double diMinus = 0.0;
|
|
|
|
|
|
|
|
|
|
|
|
if (atr > 1e-12)
|
|
|
|
|
|
{
|
|
|
|
|
|
diPlus = (dmPlusSmooth[i] / atr) * 100.0;
|
|
|
|
|
|
diMinus = (dmMinusSmooth[i] / atr) * 100.0;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
destination[i] = diPlus - diMinus;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-12-16 21:16:50 -08:00
|
|
|
|
public static TSeries Batch(TBarSeries source, int period = 14)
|
2025-12-14 21:55:40 -08:00
|
|
|
|
{
|
|
|
|
|
|
var dmx = new Dmx(period);
|
|
|
|
|
|
return dmx.Update(source);
|
|
|
|
|
|
}
|
2025-12-13 23:17:56 -08:00
|
|
|
|
}
|