Files
QuanTAlib/lib/averages/Tema.cs
T

126 lines
4.1 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>
/// TEMA: Triple Exponential Moving Average
/// A sophisticated moving average that applies three EMAs in sequence with a specific
/// combination formula to reduce lag while maintaining smoothness. The formula
/// 3*EMA1 - 3*EMA2 + EMA3 helps eliminate lag in trending markets.
/// </summary>
/// <remarks>
/// The TEMA calculation process:
/// 1. Calculates first EMA of the price
/// 2. Calculates second EMA of the first EMA
/// 3. Calculates third EMA of the second EMA
/// 4. Combines using formula: 3*EMA1 - 3*EMA2 + EMA3
///
/// Key characteristics:
/// - Significantly reduced lag compared to single EMA
/// - Better response to trends than standard EMAs
/// - Maintains smoothness despite reduced lag
/// - More responsive than double EMA (DEMA)
/// - Uses compensator for early values
///
/// Sources:
/// Patrick Mulloy - "Smoothing Data with Faster Moving Averages"
/// Technical Analysis of Stocks and Commodities, 1994
/// </remarks>
2024-09-23 08:34:47 -07:00
public class Tema : AbstractBase
{
2024-10-27 16:11:08 -07:00
private readonly double _k;
private readonly double _oneMinusK;
private readonly double _epsilon = 1e-10;
2024-09-23 08:34:47 -07:00
private double _lastEma1, _p_lastEma1;
private double _lastEma2, _p_lastEma2;
private double _lastEma3, _p_lastEma3;
2024-10-27 16:11:08 -07:00
private double _e, _p_e;
2024-09-23 08:34:47 -07:00
2024-10-27 09:38:53 -07:00
/// <param name="period">The number of periods used in each EMA calculation.</param>
/// <exception cref="ArgumentOutOfRangeException">Thrown when period is less than 1.</exception>
2024-10-06 14:44:43 -07:00
public Tema(int period)
2024-09-23 08:34:47 -07:00
{
if (period < 1)
{
2024-10-27 16:11:08 -07:00
throw new System.ArgumentOutOfRangeException(nameof(period), "Period must be greater than or equal to 1.");
2024-09-23 08:34:47 -07:00
}
2024-11-04 18:08:33 -08:00
_k = 2.0 / (period + 1);
2024-10-27 16:11:08 -07:00
_oneMinusK = 1.0 - _k;
2024-09-23 08:34:47 -07:00
Name = "Tema";
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();
}
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 each EMA calculation.</param>
2024-09-23 08:34:47 -07:00
public Tema(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 = _lastEma2 = _lastEma3 = 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_lastEma3 = _lastEma3;
_p_e = _e;
_index++;
}
else
{
_lastEma1 = _p_lastEma1;
_lastEma2 = _p_lastEma2;
_lastEma3 = _p_lastEma3;
_e = _p_e;
}
}
2024-10-27 16:11:08 -07:00
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private double CalculateEma(double input, double lastEma, double invE)
{
2024-11-03 23:03:24 +00:00
return (_k * ((input * invE) - lastEma)) + lastEma;
2024-10-27 16:11:08 -07:00
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private double UpdateCompensator()
{
_e = (_e > _epsilon) ? _oneMinusK * _e : 0;
return (_e > _epsilon) ? 1.0 / (1.0 - _e) : 1.0;
}
2024-09-23 08:34:47 -07:00
protected override double Calculation()
{
ManageState(Input.IsNew);
2024-10-06 14:44:43 -07:00
2024-10-27 16:11:08 -07:00
double invE = UpdateCompensator();
2024-09-23 08:34:47 -07:00
2024-10-27 16:11:08 -07:00
// 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);
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;
_lastEma3 = ema3;
2024-09-23 08:34:47 -07:00
2024-10-27 16:11:08 -07:00
// Calculate final TEMA with compensation
2024-11-03 23:03:24 +00:00
double result = ((3.0 * ema1) - (3.0 * ema2) + ema3) * invE;
2024-09-23 08:34:47 -07:00
IsHot = _index >= WarmupPeriod;
return result;
}
2024-10-27 09:38:53 -07:00
}