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
+35 -16
View File
@@ -1,4 +1,4 @@
using System;
using System.Runtime.CompilerServices;
namespace QuanTAlib;
/// <summary>
@@ -29,10 +29,13 @@ namespace QuanTAlib;
public class Tema : AbstractBase
{
private readonly int _period;
private readonly double _k;
private readonly double _oneMinusK;
private readonly double _epsilon = 1e-10;
private double _lastEma1, _p_lastEma1;
private double _lastEma2, _p_lastEma2;
private double _lastEma3, _p_lastEma3;
private double _k, _e, _p_e;
private double _e, _p_e;
/// <param name="period">The number of periods used in each EMA calculation.</param>
/// <exception cref="ArgumentOutOfRangeException">Thrown when period is less than 1.</exception>
@@ -40,12 +43,14 @@ public class Tema : AbstractBase
{
if (period < 1)
{
throw new ArgumentOutOfRangeException(nameof(period), "Period must be greater than or equal to 1.");
throw new System.ArgumentOutOfRangeException(nameof(period), "Period must be greater than or equal to 1.");
}
_period = period;
_k = 2.0 / (_period + 1);
_oneMinusK = 1.0 - _k;
Name = "Tema";
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();
}
@@ -57,14 +62,15 @@ public class Tema : AbstractBase
pubEvent?.AddEventHandler(source, new ValueSignal(Sub));
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public override void Init()
{
base.Init();
_k = 2.0 / (_period + 1);
_e = 1.0;
_lastEma1 = _lastEma2 = _lastEma3 = 0;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
protected override void ManageState(bool isNew)
{
if (isNew)
@@ -84,24 +90,37 @@ public class Tema : AbstractBase
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private double CalculateEma(double input, double lastEma, double invE)
{
return _k * (input * invE - lastEma) + lastEma;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private double UpdateCompensator()
{
_e = (_e > _epsilon) ? _oneMinusK * _e : 0;
return (_e > _epsilon) ? 1.0 / (1.0 - _e) : 1.0;
}
protected override double Calculation()
{
double result, _ema1, _ema2, _ema3;
ManageState(Input.IsNew);
_e = (_e > 1e-10) ? (1 - _k) * _e : 0;
double _invE = (_e > 1e-10) ? 1 / (1 - _e) : 1;
double invE = UpdateCompensator();
_ema1 = _k * (Input.Value - _lastEma1) + _lastEma1;
_ema2 = _k * (_ema1 * _invE - _lastEma2) + _lastEma2;
_ema3 = _k * (_ema2 * _invE - _lastEma3) + _lastEma3;
// Calculate EMAs with compensation
double ema1 = CalculateEma(Input.Value, _lastEma1, 1.0); // First EMA doesn't need compensation
double ema2 = CalculateEma(ema1, _lastEma2, invE);
double ema3 = CalculateEma(ema2, _lastEma3, invE);
double _tema = 3 * _ema1 * _invE - 3 * _ema2 * _invE + _ema3 * _invE;
// Store values for next iteration
_lastEma1 = ema1;
_lastEma2 = ema2;
_lastEma3 = ema3;
result = _tema;
_lastEma1 = _ema1;
_lastEma2 = _ema2;
_lastEma3 = _ema3;
// Calculate final TEMA with compensation
double result = (3.0 * ema1 - 3.0 * ema2 + ema3) * invE;
IsHot = _index >= WarmupPeriod;
return result;