Files
QuanTAlib/lib/averages/Mgdi.cs
T

114 lines
3.7 KiB
C#
Raw Normal View History

2024-10-27 16:11:08 -07:00
using System.Runtime.CompilerServices;
2024-09-23 08:34:47 -07:00
namespace QuanTAlib;
2024-10-27 09:38:53 -07:00
/// <summary>
/// MGDI: Modified Geometric Decay Index
/// A moving average that uses geometric decay with a ratio-based adjustment factor.
/// The decay rate is modified based on the ratio between current and previous values,
/// allowing for adaptive smoothing based on price movement magnitude.
/// </summary>
/// <remarks>
/// The MGDI calculation process:
/// 1. Calculates ratio between current and previous values
/// 2. Uses ratio to modify the geometric decay rate
/// 3. Applies modified decay to smooth the data
/// 4. Adjusts smoothing based on K-factor parameter
///
/// Key characteristics:
/// - Geometric decay-based smoothing
/// - Adaptive to price movement magnitude
/// - Adjustable smoothing via K-factor
/// - More responsive to large price changes
/// - Maintains smoothness during small fluctuations
///
/// Implementation:
/// Based on geometric decay principles with ratio-based modification
/// </remarks>
2024-09-23 08:34:47 -07:00
public class Mgdi : AbstractBase
{
private readonly int _period;
2024-10-27 16:11:08 -07:00
private readonly double _kFactorPeriod; // Precalculated k * period
2024-09-23 08:34:47 -07:00
private double _prevMd, _p_prevMd;
2024-10-27 09:38:53 -07:00
/// <param name="period">The number of periods used in the MGDI calculation.</param>
/// <param name="kFactor">The K-factor controlling the decay rate adjustment (default 0.6).</param>
/// <exception cref="ArgumentOutOfRangeException">Thrown when period or kFactor is less than or equal to 0.</exception>
2024-10-06 14:44:43 -07:00
public Mgdi(int period, double kFactor = 0.6)
2024-09-23 08:34:47 -07:00
{
if (period <= 0)
{
2024-10-27 16:11:08 -07:00
throw new System.ArgumentOutOfRangeException(nameof(period), "Period must be greater than 0.");
2024-09-23 08:34:47 -07:00
}
if (kFactor <= 0)
{
2024-10-27 16:11:08 -07:00
throw new System.ArgumentOutOfRangeException(nameof(kFactor), "K-Factor must be greater than 0.");
2024-09-23 08:34:47 -07:00
}
_period = period;
2024-10-27 16:11:08 -07:00
_kFactorPeriod = kFactor * period;
2024-09-23 08:34:47 -07:00
Name = "Mgdi";
WarmupPeriod = period;
Init();
}
2024-10-27 09:38:53 -07:00
/// <param name="source">The data source object that publishes updates.</param>
/// <param name="period">The number of periods used in the MGDI calculation.</param>
/// <param name="kFactor">The K-factor controlling the decay rate adjustment (default 0.6).</param>
2024-10-08 19:51:33 -07:00
public Mgdi(object source, int period, double kFactor = 0.6) : this(period, kFactor)
2024-09-23 08:34:47 -07:00
{
var pubEvent = source.GetType().GetEvent("Pub");
pubEvent?.AddEventHandler(source, new ValueSignal(Sub));
}
2024-10-27 16:11:08 -07:00
[MethodImpl(MethodImplOptions.AggressiveInlining)]
2024-09-23 08:34:47 -07:00
public override void Init()
{
base.Init();
_prevMd = _p_prevMd = 0;
}
2024-10-27 16:11:08 -07:00
[MethodImpl(MethodImplOptions.AggressiveInlining)]
2024-09-23 08:34:47 -07:00
protected override void ManageState(bool isNew)
{
if (isNew)
{
_p_prevMd = _prevMd;
_index++;
2024-10-08 17:31:29 +00:00
}
else
{
2024-09-23 08:34:47 -07:00
_prevMd = _p_prevMd;
}
}
2024-10-27 16:11:08 -07:00
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private double CalculateRatio(double value)
{
2024-11-04 18:08:33 -08:00
return _prevMd >= double.Epsilon ? value / _prevMd : 1;
2024-10-27 16:11:08 -07:00
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private double CalculateMd(double value, double ratio)
{
return _prevMd + ((value - _prevMd) / (_kFactorPeriod * System.Math.Pow(ratio, 4)));
}
2024-09-23 08:34:47 -07:00
protected override double Calculation()
{
ManageState(Input.IsNew);
double value = Input.Value;
2024-10-08 17:31:29 +00:00
if (_index < 2)
{
2024-09-23 08:34:47 -07:00
_prevMd = value;
}
else
{
2024-10-27 16:11:08 -07:00
double ratio = CalculateRatio(value);
_prevMd = CalculateMd(value, ratio);
2024-09-23 08:34:47 -07:00
}
IsHot = _index >= _period;
return _prevMd;
}
2024-10-27 09:38:53 -07:00
}