Files
QuanTAlib/lib/averages/Mgdi.cs
T

103 lines
3.2 KiB
C#
Raw Normal View History

2024-10-27 09:38:53 -07:00
using System;
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;
private readonly double _kFactor;
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)
{
throw new ArgumentOutOfRangeException(nameof(period), "Period must be greater than 0.");
}
if (kFactor <= 0)
{
throw new ArgumentOutOfRangeException(nameof(kFactor), "K-Factor must be greater than 0.");
}
_period = period;
_kFactor = kFactor;
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));
}
public override void Init()
{
base.Init();
_prevMd = _p_prevMd = 0;
}
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;
}
}
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-08 19:51:33 -07:00
double ratio = _prevMd != 0 ? value / _prevMd : 1;
2024-09-23 08:34:47 -07:00
double md = _prevMd + ((value - _prevMd) /
2024-10-08 19:51:33 -07:00
(_kFactor * _period * Math.Pow(ratio, 4)));
2024-09-23 08:34:47 -07:00
_prevMd = md;
}
IsHot = _index >= _period;
return _prevMd;
}
2024-10-27 09:38:53 -07:00
}