Files
QuanTAlib/lib/averages/Dema.cs
T

103 lines
3.0 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;
/// <summary>
/// DEMA: Double Exponential Moving Average
/// DEMA reduces the lag of a traditional EMA by applying a second EMA over EMA.
/// It responds more quickly to price changes than a standard EMA while maintaining
/// smoothness, at the cost of overshooting the signal line.
/// </summary>
/// <remarks>
/// Sources:
2024-10-27 16:11:08 -07:00
/// https://en.wikipedia.org/wiki/Double_exponential_moving_average
2024-09-23 08:34:47 -07:00
/// https://www.investopedia.com/terms/d/double-exponential-moving-average.asp
/// https://www.tradingview.com/support/solutions/43000502589-double-exponential-moving-average-dema/
///
/// Validation:
/// Skender.Stock.Indicators
/// </remarks>
public class Dema : AbstractBase
{
2024-10-27 16:11:08 -07:00
private readonly double _k;
private readonly double _epsilon = 1e-10;
2024-09-23 08:34:47 -07:00
private double _lastEma1, _p_lastEma1;
private double _lastEma2, _p_lastEma2;
2024-10-27 16:11:08 -07:00
private double _e, _p_e;
2024-09-23 08:34:47 -07:00
2024-10-06 14:44:43 -07:00
public Dema(int period)
2024-09-23 08:34:47 -07:00
{
if (period < 1)
{
throw new ArgumentOutOfRangeException(nameof(period), "Period must be greater than or equal to 1.");
}
2024-11-03 09:27:12 -08:00
_k = 2.0 / (period + 1);
2024-09-23 08:34:47 -07:00
Name = "Dema";
double percentile = 0.85; //targeting 85th percentile of correctness of converging EMA
2024-10-27 16:11:08 -07:00
WarmupPeriod = (int)System.Math.Ceiling(-period * System.Math.Log(1 - percentile));
2024-09-23 08:34:47 -07:00
Init();
}
public Dema(object source, int period) : this(period)
{
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();
_e = 1.0;
_lastEma1 = 0;
_lastEma2 = 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_lastEma1 = _lastEma1;
_p_lastEma2 = _lastEma2;
_p_e = _e;
_index++;
}
else
{
_lastEma1 = _p_lastEma1;
_lastEma2 = _p_lastEma2;
_e = _p_e;
}
}
2024-10-27 16:11:08 -07:00
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private double CalculateEma(double input, double lastEma)
{
2024-11-03 23:03:24 +00:00
return (_k * (input - lastEma)) + lastEma;
2024-10-27 16:11:08 -07:00
}
2024-09-23 08:34:47 -07:00
protected override double Calculation()
{
ManageState(Input.IsNew);
2024-10-27 16:11:08 -07:00
// Compensator for early EMA values
_e = (_e > _epsilon) ? (1 - _k) * _e : 0;
double invE = (_e > _epsilon) ? 1 / (1 - _e) : 1;
2024-09-23 08:34:47 -07:00
2024-10-27 16:11:08 -07:00
// Calculate EMAs
double ema1 = CalculateEma(Input.Value, _lastEma1);
double compensatedEma1 = ema1 * invE;
double ema2 = CalculateEma(compensatedEma1, _lastEma2);
2024-09-23 08:34:47 -07:00
2024-10-27 16:11:08 -07:00
// Store values for next iteration
_lastEma1 = ema1;
_lastEma2 = ema2;
2024-09-23 08:34:47 -07:00
2024-10-27 16:11:08 -07:00
// Calculate final DEMA
2024-11-03 23:03:24 +00:00
double result = (2 * compensatedEma1) - (ema2 * invE);
2024-09-23 08:34:47 -07:00
IsHot = _index >= WarmupPeriod;
return result;
}
}