Files
QuanTAlib/lib/averages/Epma.cs
T

120 lines
4.0 KiB
C#
Raw Normal View History

2024-10-27 16:11:08 -07:00
using System.Runtime.CompilerServices;
2024-09-22 17:31:24 -07:00
namespace QuanTAlib;
2024-10-27 09:38:53 -07:00
/// <summary>
/// EPMA: Endpoint Moving Average
/// A moving average that uses a specialized convolution kernel to emphasize recent price movements
/// while maintaining a connection to historical data. The weights decrease linearly with a focus
/// on endpoints.
/// </summary>
/// <remarks>
/// The EPMA uses a unique weighting scheme where:
/// - The most recent price gets the highest weight: (2 * period - 1)
/// - Each previous price gets a weight reduced by 3: (2 * period - 1) - 3i
/// - Weights are normalized to sum to 1
///
/// Key characteristics:
/// - Emphasizes recent price movements more than traditional moving averages
/// - Maintains some influence from historical data
/// - Uses convolution for efficient calculation
/// - Provides better endpoint preservation than simple moving averages
///
/// Implementation:
/// Original implementation based on convolution principles
/// </remarks>
2024-09-22 17:31:24 -07:00
public class Epma : AbstractBase
{
private readonly int _period;
private readonly Convolution _convolution;
2024-10-27 09:38:53 -07:00
/// <param name="period">The number of data points used in the EPMA calculation.</param>
/// <exception cref="ArgumentException">Thrown when period is less than 1.</exception>
2024-09-22 17:31:24 -07:00
public Epma(int period)
{
if (period < 1)
{
2024-10-27 16:11:08 -07:00
throw new System.ArgumentException("Period must be greater than or equal to 1.", nameof(period));
2024-09-22 17:31:24 -07:00
}
_period = period;
2024-11-03 09:27:12 -08:00
double[] _baseKernel = GenerateKernel(_period);
2024-10-27 16:11:08 -07:00
_convolution = new Convolution(_baseKernel);
2024-09-22 17:31:24 -07:00
Name = "Epma";
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 data points used in the EPMA calculation.</param>
2024-09-22 17:31:24 -07:00
public Epma(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-22 17:31:24 -07:00
private new void Init()
{
base.Init();
_convolution.Init();
}
2024-10-27 16:11:08 -07:00
[MethodImpl(MethodImplOptions.AggressiveInlining)]
2024-09-22 17:31:24 -07:00
protected override void ManageState(bool isNew)
{
if (isNew)
{
_lastValidValue = Input.Value;
_index++;
}
}
2024-10-27 16:11:08 -07:00
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static double CalculateKernelSum(int period)
{
// Using arithmetic sequence sum formula: n(a1 + an)/2
// where a1 = (2p-1) and an = (2p-1) - 3(n-1)
2024-11-03 23:03:24 +00:00
double firstTerm = (2 * period) - 1;
double lastTerm = firstTerm - (3 * (period - 1));
2024-10-27 16:11:08 -07:00
return period * (firstTerm + lastTerm) * 0.5;
}
2024-09-22 17:31:24 -07:00
protected override double Calculation()
{
ManageState(Input.IsNew);
// Use Convolution for calculation
2024-10-27 16:11:08 -07:00
var convolutionResult = _convolution.Calc(Input);
2024-09-22 17:31:24 -07:00
double result = convolutionResult.Value;
// Adjust for partial periods during warmup
if (_index < _period)
{
2024-10-27 16:11:08 -07:00
result *= CalculateKernelSum(_period) / CalculateKernelSum(_index);
2024-09-22 17:31:24 -07:00
}
IsHot = _index >= WarmupPeriod;
return result;
}
2024-10-27 09:38:53 -07:00
/// <summary>
/// Generates the convolution kernel for the EPMA calculation.
/// </summary>
/// <param name="period">The period for which to generate the kernel.</param>
/// <returns>An array of normalized weights for the convolution operation.</returns>
2024-10-27 16:11:08 -07:00
[MethodImpl(MethodImplOptions.AggressiveInlining)]
2024-09-22 17:31:24 -07:00
public static double[] GenerateKernel(int period)
{
double[] kernel = new double[period];
2024-10-27 16:11:08 -07:00
double weightSum = CalculateKernelSum(period);
double invWeightSum = 1.0 / weightSum;
2024-11-03 23:03:24 +00:00
double baseWeight = (2 * period) - 1;
2024-09-22 17:31:24 -07:00
for (int i = 0; i < period; i++)
{
2024-11-03 23:03:24 +00:00
kernel[i] = (baseWeight - (3 * i)) * invWeightSum;
2024-09-22 17:31:24 -07:00
}
return kernel;
}
2024-10-27 09:38:53 -07:00
}