Files
QuanTAlib/lib/averages/Mma.cs
T

118 lines
3.7 KiB
C#
Raw Normal View History

2024-10-27 16:11:08 -07:00
using System.Runtime.CompilerServices;
2024-10-06 14:44:43 -07:00
namespace QuanTAlib;
2024-09-23 08:34:47 -07:00
2024-10-27 09:38:53 -07:00
/// <summary>
/// MMA: Modified Moving Average
/// A moving average that combines a simple moving average with a weighted component
/// to provide a balanced smoothing effect. The weighting scheme emphasizes central
/// values while maintaining overall data representation.
/// </summary>
/// <remarks>
/// The MMA calculation process:
/// 1. Calculates the simple moving average component (T/period)
/// 2. Calculates a weighted sum with symmetric weights around the center
/// 3. Combines both components using the formula: SMA + 6*WeightedSum/((period+1)*period)
///
/// Key characteristics:
/// - Combines simple and weighted moving averages
/// - Symmetric weighting around the center
/// - Better balance between smoothing and responsiveness
/// - Reduces lag compared to simple moving average
/// - Maintains stability through dual-component approach
///
/// Implementation:
/// Based on modified moving average principles combining
/// simple and weighted components for optimal smoothing
/// </remarks>
2024-10-06 14:44:43 -07:00
public class Mma : AbstractBase
2024-09-23 08:34:47 -07:00
{
2024-10-06 14:44:43 -07:00
private readonly int _period;
private readonly CircularBuffer _buffer;
2024-10-27 16:11:08 -07:00
private readonly double _periodRecip; // 1/period
private readonly double _combinedRecip; // 6/((period+1)*period)
private readonly double[] _weights; // Precalculated weights
2024-10-06 14:44:43 -07:00
private double _lastMma;
2024-10-27 09:38:53 -07:00
/// <param name="period">The number of periods used in the MMA calculation. Must be at least 2.</param>
/// <exception cref="ArgumentOutOfRangeException">Thrown when period is less than 2.</exception>
2024-10-06 14:44:43 -07:00
public Mma(int period)
2024-09-23 08:34:47 -07:00
{
2024-10-06 14:44:43 -07:00
if (period < 2)
2024-09-23 08:34:47 -07:00
{
2024-10-27 16:11:08 -07:00
throw new System.ArgumentOutOfRangeException(nameof(period), "Period must be greater than or equal to 2.");
2024-09-23 08:34:47 -07:00
}
2024-10-06 14:44:43 -07:00
_period = period;
_buffer = new CircularBuffer(period);
2024-10-27 16:11:08 -07:00
_periodRecip = 1.0 / period;
_combinedRecip = 6.0 / ((period + 1) * period);
// Precalculate weights
_weights = new double[period];
for (int i = 0; i < period; i++)
{
2024-11-03 23:03:24 +00:00
_weights[i] = (period - ((2 * i) + 1)) * 0.5;
2024-10-27 16:11:08 -07:00
}
2024-10-06 14:44:43 -07:00
Name = "Mma";
WarmupPeriod = period;
Init();
}
2024-09-23 08:34:47 -07:00
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 the MMA calculation.</param>
2024-10-06 14:44:43 -07:00
public Mma(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-10-06 14:44:43 -07:00
public override void Init()
{
base.Init();
_lastMma = 0;
_buffer.Clear();
}
2024-10-27 16:11:08 -07:00
[MethodImpl(MethodImplOptions.AggressiveInlining)]
2024-10-06 14:44:43 -07:00
protected override void ManageState(bool isNew)
{
if (isNew)
2024-09-23 08:34:47 -07:00
{
2024-10-06 14:44:43 -07:00
_index++;
2024-09-23 08:34:47 -07:00
}
}
2024-10-06 14:44:43 -07:00
2024-10-27 16:11:08 -07:00
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private double CalculateWeightedSum()
{
double sum = 0;
for (int i = 0; i < _period; i++)
{
sum += _weights[i] * _buffer[^(i + 1)];
}
return sum;
}
2024-10-06 14:44:43 -07:00
protected override double Calculation()
{
ManageState(Input.IsNew);
_buffer.Add(Input.Value, Input.IsNew);
if (_index >= _period)
{
double T = _buffer.Sum();
double S = CalculateWeightedSum();
2024-10-27 16:11:08 -07:00
_lastMma = (T * _periodRecip) + (S * _combinedRecip);
2024-10-06 14:44:43 -07:00
}
else
{
// Use simple average until we have enough data points
_lastMma = _buffer.Average();
}
IsHot = _index >= _period;
return _lastMma;
}
}