Files
QuanTAlib/lib/averages/Wma.cs
T

104 lines
3.2 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>
/// WMA: Weighted Moving Average
/// A moving average that assigns linearly decreasing weights to older data points.
/// The most recent price has the highest weight, and each older price receives
/// linearly less weight, creating a more responsive average than SMA.
/// </summary>
/// <remarks>
/// The WMA calculation process:
/// 1. Assigns weights linearly decreasing with age
/// 2. Most recent price gets weight of period
/// 3. Each older price gets decremented weight
/// 4. Normalizes weights by sum of weights
/// 5. Applies weights through convolution
///
/// Key characteristics:
/// - Linear weight distribution
/// - More responsive than SMA
/// - Less lag than SMA
/// - Emphasizes recent prices
/// - Implemented using efficient convolution operations
///
/// Sources:
/// https://www.investopedia.com/articles/technical/060401.asp
/// https://stockcharts.com/school/doku.php?id=chart_school:technical_indicators:weighted_moving_average
/// </remarks>
2024-09-22 17:31:24 -07:00
public class Wma : AbstractBase
{
2024-09-24 16:41:26 -07:00
private readonly int _period;
2024-09-22 17:31:24 -07:00
private readonly Convolution _convolution;
2024-10-27 09:38:53 -07:00
/// <param name="period">The number of data points used in the WMA calculation.</param>
/// <exception cref="ArgumentException">Thrown when period is less than 1.</exception>
2024-09-22 17:31:24 -07:00
public Wma(int period)
{
if (period < 1)
{
throw new ArgumentException("Period must be greater than or equal to 1.", nameof(period));
}
2024-09-24 16:41:26 -07:00
_period = period;
_convolution = new Convolution(GenerateWmaKernel(_period));
2024-09-22 17:31:24 -07:00
Name = "Wma";
2024-09-24 16:41:26 -07:00
WarmupPeriod = _period;
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 WMA calculation.</param>
2024-09-22 17:31:24 -07:00
public Wma(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 linearly weighted convolution kernel for the WMA calculation.
/// </summary>
/// <param name="period">The period for which to generate the kernel.</param>
/// <returns>An array of normalized linearly decreasing weights 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();
_convolution.Init();
}
protected override void ManageState(bool isNew)
{
if (isNew)
{
_lastValidValue = Input.Value;
_index++;
}
}
protected override double Calculation()
{
ManageState(Input.IsNew);
// Use Convolution for calculation
TValue convolutionResult = _convolution.Calc(Input);
double result = convolutionResult.Value;
IsHot = _index >= WarmupPeriod;
return result;
}
2024-10-27 09:38:53 -07:00
}