Files
QuanTAlib/lib/averages/Hma.cs
T

111 lines
3.6 KiB
C#
Raw Normal View History

2024-10-27 09:38:53 -07:00
using System;
2024-09-22 17:31:24 -07:00
namespace QuanTAlib;
2024-10-27 09:38:53 -07:00
/// <summary>
/// HMA: Hull Moving Average
/// A moving average designed by Alan Hull to reduce lag while maintaining smoothness.
/// It combines weighted moving averages of different periods to achieve better
/// responsiveness to price changes while minimizing noise.
/// </summary>
/// <remarks>
/// The HMA calculation process:
/// 1. Calculate WMA with period n/2
/// 2. Calculate WMA with period n
/// 3. Calculate difference: 2*WMA(n/2) - WMA(n)
/// 4. Apply final WMA with period sqrt(n) to the difference
///
/// Key characteristics:
/// - Significantly reduced lag compared to traditional moving averages
/// - Maintains smoothness despite the reduced lag
/// - Responds more quickly to price changes
/// - Better at identifying trend changes
/// - Uses weighted moving averages for all calculations
///
/// Sources:
/// Alan Hull - "Better Trading with Hull Moving Average"
/// https://alanhull.com/hull-moving-average
/// </remarks>
2024-09-22 17:31:24 -07:00
public class Hma : AbstractBase
{
private readonly Convolution _wmaHalf, _wmaFull, _wmaFinal;
2024-10-27 09:38:53 -07:00
/// <param name="period">The number of data points used in the HMA calculation. Must be at least 2.</param>
/// <exception cref="ArgumentException">Thrown when period is less than 2.</exception>
2024-09-22 17:31:24 -07:00
public Hma(int period)
{
if (period < 2)
{
throw new ArgumentException("Period must be greater than or equal to 2.", nameof(period));
}
2024-09-30 07:30:27 -07:00
int _sqrtPeriod = (int)Math.Sqrt(period);
2024-09-22 17:31:24 -07:00
_wmaHalf = new Convolution(GenerateWmaKernel(period / 2));
_wmaFull = new Convolution(GenerateWmaKernel(period));
_wmaFinal = new Convolution(GenerateWmaKernel(_sqrtPeriod));
Name = "Hma";
2024-09-30 07:30:27 -07:00
WarmupPeriod = period + _sqrtPeriod - 1;
2024-09-22 17:31:24 -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 data points used in the HMA calculation.</param>
2024-09-22 17:31:24 -07:00
public Hma(object source, int period) : this(period)
{
var pubEvent = source.GetType().GetEvent("Pub");
pubEvent?.AddEventHandler(source, new ValueSignal(Sub));
}
2024-10-27 09:38:53 -07:00
/// <summary>
/// Generates the weighted moving average kernel for the HMA calculation.
/// </summary>
/// <param name="period">The period for which to generate the kernel.</param>
/// <returns>An array of linearly weighted values for the convolution operation.</returns>
2024-09-22 17:31:24 -07:00
private static double[] GenerateWmaKernel(int period)
{
double[] kernel = new double[period];
double weightSum = period * (period + 1) / 2.0;
for (int i = 0; i < period; i++)
{
kernel[i] = (period - i) / weightSum;
}
return kernel;
}
private new void Init()
{
base.Init();
_wmaHalf.Init();
_wmaFull.Init();
_wmaFinal.Init();
}
protected override void ManageState(bool isNew)
{
if (isNew)
{
_lastValidValue = Input.Value;
_index++;
}
}
protected override double Calculation()
{
ManageState(Input.IsNew);
// Calculate WMA(n/2) and WMA(n)
double wmaHalfResult = _wmaHalf.Calc(Input).Value;
double wmaFullResult = _wmaFull.Calc(Input).Value;
// Calculate 2*WMA(n/2) - WMA(n)
double intermediateResult = 2 * wmaHalfResult - wmaFullResult;
// Calculate final WMA
double result = _wmaFinal.Calc(new TValue(Input.Time, intermediateResult, Input.IsNew)).Value;
IsHot = _index >= WarmupPeriod;
return result;
}
2024-10-27 09:38:53 -07:00
}