2026-03-17 13:48:34 -07:00
|
|
|
|
using System.Runtime.CompilerServices;
|
|
|
|
|
|
using System.Runtime.InteropServices;
|
|
|
|
|
|
|
|
|
|
|
|
namespace QuanTAlib;
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// MADH: Ehlers Moving Average Difference with Hann
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <remarks>
|
2026-03-17 14:02:51 -07:00
|
|
|
|
/// A zero-crossing trend oscillator that computes the percentage difference
|
|
|
|
|
|
/// between a short and long Hann-windowed FIR moving average.
|
2026-03-17 13:48:34 -07:00
|
|
|
|
///
|
|
|
|
|
|
/// Calculation:
|
2026-03-17 14:02:51 -07:00
|
|
|
|
/// <c>LongLength = IntPortion(ShortLength + DominantCycle / 2)</c>
|
|
|
|
|
|
/// <c>Filt1 = HannFIR(Close, ShortLength)</c>
|
|
|
|
|
|
/// <c>Filt2 = HannFIR(Close, LongLength)</c>
|
|
|
|
|
|
/// <c>MADH = 100 × (Filt1 / Filt2 - 1)</c>
|
|
|
|
|
|
///
|
|
|
|
|
|
/// Hann coefficients: <c>w(k) = 1 - cos(2π·k / (N + 1))</c>
|
2026-03-17 13:48:34 -07:00
|
|
|
|
/// </remarks>
|
|
|
|
|
|
/// <seealso href="Madh.md">Detailed documentation</seealso>
|
|
|
|
|
|
/// <seealso href="madh.pine">Reference Pine Script implementation</seealso>
|
|
|
|
|
|
[SkipLocalsInit]
|
|
|
|
|
|
public sealed class Madh : AbstractBase
|
|
|
|
|
|
{
|
|
|
|
|
|
[StructLayout(LayoutKind.Auto)]
|
|
|
|
|
|
private record struct State(int Count, double LastValid)
|
|
|
|
|
|
{
|
|
|
|
|
|
public static State New() => new() { Count = 0, LastValid = 0 };
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private readonly int _shortLength;
|
|
|
|
|
|
private readonly int _longLength;
|
2026-03-17 14:02:51 -07:00
|
|
|
|
private readonly double[] _shortCoeffs;
|
|
|
|
|
|
private readonly double[] _longCoeffs;
|
2026-03-17 13:48:34 -07:00
|
|
|
|
|
|
|
|
|
|
private State _s = State.New();
|
|
|
|
|
|
private State _ps = State.New();
|
|
|
|
|
|
|
2026-03-17 14:02:51 -07:00
|
|
|
|
// RingBuffer stores close prices — needs longLength+1 slots
|
2026-03-17 13:48:34 -07:00
|
|
|
|
private readonly RingBuffer _closeBuf;
|
|
|
|
|
|
|
|
|
|
|
|
private const double Epsilon = 1e-10;
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2026-03-17 14:02:51 -07:00
|
|
|
|
/// Creates MADH with specified parameters.
|
2026-03-17 13:48:34 -07:00
|
|
|
|
/// </summary>
|
2026-03-17 14:02:51 -07:00
|
|
|
|
/// <param name="shortLength">Short Hann FIR window length (must be ≥ 1)</param>
|
|
|
|
|
|
/// <param name="dominantCycle">Dominant cycle period (must be ≥ 2)</param>
|
2026-03-17 13:48:34 -07:00
|
|
|
|
public Madh(int shortLength = 8, int dominantCycle = 27)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (shortLength < 1)
|
|
|
|
|
|
{
|
|
|
|
|
|
throw new ArgumentOutOfRangeException(nameof(shortLength), shortLength, "ShortLength must be at least 1.");
|
|
|
|
|
|
}
|
|
|
|
|
|
if (dominantCycle < 2)
|
|
|
|
|
|
{
|
|
|
|
|
|
throw new ArgumentOutOfRangeException(nameof(dominantCycle), dominantCycle, "DominantCycle must be at least 2.");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
_shortLength = shortLength;
|
2026-03-17 14:02:51 -07:00
|
|
|
|
_longLength = shortLength + dominantCycle / 2;
|
2026-03-17 13:48:34 -07:00
|
|
|
|
|
2026-03-17 14:02:51 -07:00
|
|
|
|
// Precompute short Hann coefficients: w(k) = 1 - cos(2π·k / (N+1))
|
|
|
|
|
|
_shortCoeffs = new double[_shortLength];
|
|
|
|
|
|
double shortAngleStep = 2.0 * Math.PI / (_shortLength + 1);
|
|
|
|
|
|
for (int k = 1; k <= _shortLength; k++)
|
2026-03-17 13:48:34 -07:00
|
|
|
|
{
|
2026-03-17 14:02:51 -07:00
|
|
|
|
_shortCoeffs[k - 1] = 1.0 - Math.Cos(shortAngleStep * k);
|
2026-03-17 13:48:34 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-03-17 14:02:51 -07:00
|
|
|
|
// Precompute long Hann coefficients
|
|
|
|
|
|
_longCoeffs = new double[_longLength];
|
|
|
|
|
|
double longAngleStep = 2.0 * Math.PI / (_longLength + 1);
|
2026-03-17 13:48:34 -07:00
|
|
|
|
for (int k = 1; k <= _longLength; k++)
|
|
|
|
|
|
{
|
2026-03-17 14:02:51 -07:00
|
|
|
|
_longCoeffs[k - 1] = 1.0 - Math.Cos(longAngleStep * k);
|
2026-03-17 13:48:34 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-03-17 14:02:51 -07:00
|
|
|
|
_closeBuf = new RingBuffer(_longLength);
|
2026-03-17 13:48:34 -07:00
|
|
|
|
|
|
|
|
|
|
Name = $"Madh({shortLength},{dominantCycle})";
|
2026-03-17 14:02:51 -07:00
|
|
|
|
WarmupPeriod = _longLength;
|
2026-03-17 13:48:34 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2026-03-17 14:02:51 -07:00
|
|
|
|
/// Creates MADH with specified source and parameters.
|
2026-03-17 13:48:34 -07:00
|
|
|
|
/// Subscribes to source.Pub event.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public Madh(ITValuePublisher source, int shortLength = 8, int dominantCycle = 27) : this(shortLength, dominantCycle)
|
|
|
|
|
|
{
|
|
|
|
|
|
source.Pub += Handle;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Creates MADH with a TSeries source, primes from history, then subscribes.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public Madh(TSeries source, int shortLength = 8, int dominantCycle = 27) : this(shortLength, dominantCycle)
|
|
|
|
|
|
{
|
|
|
|
|
|
Prime(source.Values);
|
|
|
|
|
|
if (source.Count > 0)
|
|
|
|
|
|
{
|
|
|
|
|
|
Last = new TValue(source.LastTime, Last.Value);
|
|
|
|
|
|
}
|
|
|
|
|
|
source.Pub += Handle;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-03-17 14:02:51 -07:00
|
|
|
|
public override bool IsHot => _s.Count >= _longLength;
|
2026-03-17 13:48:34 -07:00
|
|
|
|
|
|
|
|
|
|
public override void Prime(ReadOnlySpan<double> source, TimeSpan? step = null)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (source.Length == 0)
|
|
|
|
|
|
{
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
_s = State.New();
|
|
|
|
|
|
_ps = State.New();
|
|
|
|
|
|
_closeBuf.Clear();
|
|
|
|
|
|
|
|
|
|
|
|
int len = source.Length;
|
|
|
|
|
|
for (int i = 0; i < len; i++)
|
|
|
|
|
|
{
|
|
|
|
|
|
double val = source[i];
|
|
|
|
|
|
if (double.IsFinite(val))
|
|
|
|
|
|
{
|
|
|
|
|
|
_s.LastValid = val;
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
val = _s.LastValid;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Step(val);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Last = new TValue(DateTime.MinValue, ComputeResult());
|
|
|
|
|
|
_ps = _s;
|
|
|
|
|
|
_closeBuf.Snapshot();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
|
|
|
|
private void Handle(object? sender, in TValueEventArgs e) => Update(e.Value, e.IsNew);
|
|
|
|
|
|
|
|
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
|
|
|
|
private static double GetValidValue(double input, ref State s)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (double.IsFinite(input))
|
|
|
|
|
|
{
|
|
|
|
|
|
s.LastValid = input;
|
|
|
|
|
|
return input;
|
|
|
|
|
|
}
|
|
|
|
|
|
return s.LastValid;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
|
|
|
|
|
|
public override TValue Update(TValue input, bool isNew = true)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (isNew)
|
|
|
|
|
|
{
|
|
|
|
|
|
_ps = _s;
|
|
|
|
|
|
_closeBuf.Snapshot();
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
_s = _ps;
|
|
|
|
|
|
_closeBuf.Restore();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
double val = GetValidValue(input.Value, ref _s);
|
|
|
|
|
|
Step(val);
|
|
|
|
|
|
double result = ComputeResult();
|
|
|
|
|
|
|
|
|
|
|
|
Last = new TValue(input.Time, result);
|
|
|
|
|
|
PubEvent(Last, isNew);
|
|
|
|
|
|
return Last;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveOptimization)]
|
|
|
|
|
|
public override TSeries Update(TSeries source)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (source.Count == 0)
|
|
|
|
|
|
{
|
|
|
|
|
|
return [];
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int len = source.Count;
|
|
|
|
|
|
var t = new List<long>(len);
|
|
|
|
|
|
var v = new List<double>(len);
|
|
|
|
|
|
CollectionsMarshal.SetCount(t, len);
|
|
|
|
|
|
CollectionsMarshal.SetCount(v, len);
|
|
|
|
|
|
|
|
|
|
|
|
var tSpan = CollectionsMarshal.AsSpan(t);
|
|
|
|
|
|
var vSpan = CollectionsMarshal.AsSpan(v);
|
|
|
|
|
|
|
|
|
|
|
|
source.Times.CopyTo(tSpan);
|
|
|
|
|
|
|
|
|
|
|
|
Reset();
|
|
|
|
|
|
for (int i = 0; i < len; i++)
|
|
|
|
|
|
{
|
|
|
|
|
|
double val = source.Values[i];
|
|
|
|
|
|
if (double.IsFinite(val))
|
|
|
|
|
|
{
|
|
|
|
|
|
_s.LastValid = val;
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
val = _s.LastValid;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Step(val);
|
|
|
|
|
|
vSpan[i] = ComputeResult();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
_ps = _s;
|
|
|
|
|
|
_closeBuf.Snapshot();
|
|
|
|
|
|
Last = new TValue(tSpan[len - 1], vSpan[len - 1]);
|
|
|
|
|
|
|
|
|
|
|
|
return new TSeries(t, v);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Core streaming step: add close price to ring buffer.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
|
|
|
|
private void Step(double input)
|
|
|
|
|
|
{
|
|
|
|
|
|
_s.Count++;
|
|
|
|
|
|
_closeBuf.Add(input);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Computes MADH from the close buffer using dual Hann-weighted FIR averages.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
|
|
|
|
|
|
private double ComputeResult()
|
|
|
|
|
|
{
|
2026-03-17 14:02:51 -07:00
|
|
|
|
int available = Math.Min(_s.Count, _longLength);
|
|
|
|
|
|
if (available < 1)
|
2026-03-17 13:48:34 -07:00
|
|
|
|
{
|
|
|
|
|
|
return 0.0;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-03-17 14:02:51 -07:00
|
|
|
|
// Short Hann FIR
|
|
|
|
|
|
double filt1 = 0.0;
|
|
|
|
|
|
double coef1 = 0.0;
|
|
|
|
|
|
int shortAvail = Math.Min(available, _shortLength);
|
|
|
|
|
|
for (int k = 1; k <= shortAvail; k++)
|
2026-03-17 13:48:34 -07:00
|
|
|
|
{
|
2026-03-17 14:02:51 -07:00
|
|
|
|
double w = _shortCoeffs[k - 1];
|
|
|
|
|
|
filt1 = Math.FusedMultiplyAdd(w, _closeBuf[available - k], filt1);
|
|
|
|
|
|
coef1 += w;
|
2026-03-17 13:48:34 -07:00
|
|
|
|
}
|
2026-03-17 14:02:51 -07:00
|
|
|
|
if (coef1 > Epsilon)
|
2026-03-17 13:48:34 -07:00
|
|
|
|
{
|
2026-03-17 14:02:51 -07:00
|
|
|
|
filt1 /= coef1;
|
2026-03-17 13:48:34 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-03-17 14:02:51 -07:00
|
|
|
|
// Long Hann FIR
|
|
|
|
|
|
double filt2 = 0.0;
|
|
|
|
|
|
double coef2 = 0.0;
|
|
|
|
|
|
for (int k = 1; k <= available; k++)
|
|
|
|
|
|
{
|
|
|
|
|
|
double w = _longCoeffs[k - 1];
|
|
|
|
|
|
filt2 = Math.FusedMultiplyAdd(w, _closeBuf[available - k], filt2);
|
|
|
|
|
|
coef2 += w;
|
|
|
|
|
|
}
|
|
|
|
|
|
if (coef2 > Epsilon)
|
|
|
|
|
|
{
|
|
|
|
|
|
filt2 /= coef2;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// MADH = 100 * (Filt1 / Filt2 - 1)
|
2026-03-17 13:48:34 -07:00
|
|
|
|
return Math.Abs(filt2) > Epsilon ? 100.0 * (filt1 / filt2 - 1.0) : 0.0;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Batch calculation returning a TSeries.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public static TSeries Batch(TSeries source, int shortLength = 8, int dominantCycle = 27)
|
|
|
|
|
|
{
|
|
|
|
|
|
var indicator = new Madh(shortLength, dominantCycle);
|
|
|
|
|
|
return indicator.Update(source);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Batch calculation writing to a pre-allocated output span. Zero-allocation hot path.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public static void Batch(ReadOnlySpan<double> source, Span<double> output, int shortLength = 8, int dominantCycle = 27)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (source.Length != output.Length)
|
|
|
|
|
|
{
|
|
|
|
|
|
throw new ArgumentException("Source and output must have the same length", nameof(output));
|
|
|
|
|
|
}
|
|
|
|
|
|
if (shortLength < 1)
|
|
|
|
|
|
{
|
|
|
|
|
|
throw new ArgumentOutOfRangeException(nameof(shortLength), shortLength, "ShortLength must be at least 1.");
|
|
|
|
|
|
}
|
|
|
|
|
|
if (dominantCycle < 2)
|
|
|
|
|
|
{
|
|
|
|
|
|
throw new ArgumentOutOfRangeException(nameof(dominantCycle), dominantCycle, "DominantCycle must be at least 2.");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (source.Length == 0)
|
|
|
|
|
|
{
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var indicator = new Madh(shortLength, dominantCycle);
|
|
|
|
|
|
for (int i = 0; i < source.Length; i++)
|
|
|
|
|
|
{
|
|
|
|
|
|
double val = source[i];
|
|
|
|
|
|
if (double.IsFinite(val))
|
|
|
|
|
|
{
|
|
|
|
|
|
indicator._s.LastValid = val;
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
val = indicator._s.LastValid;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
indicator.Step(val);
|
|
|
|
|
|
output[i] = indicator.ComputeResult();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Creates a hot indicator from historical data, ready for streaming.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public static (TSeries Results, Madh Indicator) Calculate(TSeries source, int shortLength = 8, int dominantCycle = 27)
|
|
|
|
|
|
{
|
|
|
|
|
|
var indicator = new Madh(shortLength, dominantCycle);
|
|
|
|
|
|
TSeries results = indicator.Update(source);
|
|
|
|
|
|
return (results, indicator);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public override void Reset()
|
|
|
|
|
|
{
|
|
|
|
|
|
_s = State.New();
|
|
|
|
|
|
_ps = _s;
|
|
|
|
|
|
_closeBuf.Clear();
|
|
|
|
|
|
Last = default;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|