Class optimization

This commit is contained in:
Miha
2024-10-27 16:11:08 -07:00
parent b2fcdda785
commit 6c67a0cf31
77 changed files with 2634 additions and 1455 deletions
+27 -28
View File
@@ -1,3 +1,4 @@
using System.Runtime.CompilerServices;
namespace QuanTAlib;
/// <summary>
@@ -7,12 +8,8 @@ namespace QuanTAlib;
/// smoothness, at the cost of overshooting the signal line.
/// </summary>
/// <remarks>
/// Smoothness: ★★★☆☆ (3/5)
/// Sensitivity: ★★★★☆ (4/5)
/// Overshooting: ★★★☆☆ (3/5)
/// Lag: ★★★★☆ (4/5)
///
/// Sources:
/// https://en.wikipedia.org/wiki/Double_exponential_moving_average
/// https://www.investopedia.com/terms/d/double-exponential-moving-average.asp
/// https://www.tradingview.com/support/solutions/43000502589-double-exponential-moving-average-dema/
///
@@ -21,12 +18,12 @@ namespace QuanTAlib;
/// </remarks>
public class Dema : AbstractBase
{
// inherited _index
// inherited _value
private readonly int _period;
private readonly double _k;
private readonly double _epsilon = 1e-10;
private double _lastEma1, _p_lastEma1;
private double _lastEma2, _p_lastEma2;
private double _k, _e, _p_e;
private double _e, _p_e;
public Dema(int period)
{
@@ -35,9 +32,10 @@ public class Dema : AbstractBase
throw new ArgumentOutOfRangeException(nameof(period), "Period must be greater than or equal to 1.");
}
_period = period;
_k = 2.0 / (_period + 1);
Name = "Dema";
double percentile = 0.85; //targeting 85th percentile of correctness of converging EMA
WarmupPeriod = (int)Math.Ceiling(-period * Math.Log(1 - percentile));
WarmupPeriod = (int)System.Math.Ceiling(-period * System.Math.Log(1 - percentile));
Init();
}
@@ -46,17 +44,17 @@ public class Dema : AbstractBase
var pubEvent = source.GetType().GetEvent("Pub");
pubEvent?.AddEventHandler(source, new ValueSignal(Sub));
}
//inhereted public void Sub(object source, in ValueEventArgs args)
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public override void Init()
{
base.Init();
_k = 2.0 / (_period + 1);
_e = 1.0;
_lastEma1 = 0;
_lastEma2 = 0;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
protected override void ManageState(bool isNew)
{
if (isNew)
@@ -74,30 +72,31 @@ public class Dema : AbstractBase
}
}
/// <summary>
/// Core DEMA calculation
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private double CalculateEma(double input, double lastEma)
{
return _k * (input - lastEma) + lastEma;
}
protected override double Calculation()
{
ManageState(Input.IsNew);
double result, _ema1, _ema2;
// compensator for early ema values
_e = (_e > 1e-10) ? (1 - _k) * _e : 0;
double _invE = (_e > 1e-10) ? 1 / (1 - _e) : 1;
// Compensator for early EMA values
_e = (_e > _epsilon) ? (1 - _k) * _e : 0;
double invE = (_e > _epsilon) ? 1 / (1 - _e) : 1;
// Calculate EMA1
_ema1 = _k * (Input.Value - _lastEma1) + _lastEma1;
// Calculate EMAs
double ema1 = CalculateEma(Input.Value, _lastEma1);
double compensatedEma1 = ema1 * invE;
double ema2 = CalculateEma(compensatedEma1, _lastEma2);
// Calculate EMA2 using compensatedEma1
_ema2 = _k * (_ema1 * _invE - _lastEma2) + _lastEma2;
// Store values for next iteration
_lastEma1 = ema1;
_lastEma2 = ema2;
// Calculate DEMA
double _dema = 2 * _ema1 * _invE - (_ema2 * _invE);
result = _dema;
_lastEma1 = _ema1;
_lastEma2 = _ema2;
// Calculate final DEMA
double result = 2 * compensatedEma1 - (ema2 * invE);
IsHot = _index >= WarmupPeriod;
return result;