Files
QuanTAlib/lib/averages/Maaf.cs
T

142 lines
4.3 KiB
C#
Raw Normal View History

2024-10-27 09:38:53 -07:00
using System;
using System.Linq;
2024-09-23 08:34:47 -07:00
namespace QuanTAlib;
2024-10-27 09:38:53 -07:00
/// <summary>
/// MAAF: Median Adaptive Average Filter
/// A sophisticated moving average that combines median filtering with adaptive smoothing
/// to provide robust noise reduction while maintaining signal fidelity. The filter
/// automatically adjusts its length based on market conditions.
/// </summary>
/// <remarks>
/// The MAAF calculation process:
/// 1. Applies initial smoothing using weighted moving average
/// 2. Uses median filtering to remove outliers
/// 3. Adaptively adjusts filter length based on price deviation
/// 4. Applies final EMA smoothing with adaptive period
///
/// Key characteristics:
/// - Combines median and exponential filtering
/// - Adaptive period adjustment
/// - Robust noise reduction
/// - Preserves significant price movements
/// - Reduces impact of outliers
///
/// Sources:
/// John F. Ehlers - "The Secret Behind The Filter"
/// https://efs.kb.esignal.com/hc/en-us/articles/6362791434395-2005-Mar-The-Secret-Behind-The-Filter-MedianAdaptiveFilter-efs
///
/// Note: Initial values handling is currently under development.
/// </remarks>
2024-10-13 11:19:27 -07:00
2024-09-23 08:34:47 -07:00
public class Maaf : AbstractBase
{
private readonly CircularBuffer _priceBuffer;
private readonly CircularBuffer _smoothBuffer;
2024-10-06 14:44:43 -07:00
private double _prevFilter, _prevValue2;
private readonly double _threshold;
2024-09-23 08:34:47 -07:00
private double _p_prevFilter, _p_prevValue2;
private readonly int _period;
2024-10-27 09:38:53 -07:00
/// <param name="period">The initial period for the filter (default 39).</param>
/// <param name="threshold">The threshold for adaptive adjustment (default 0.002).</param>
2024-10-11 18:02:09 -07:00
public Maaf(int period = 39, double threshold = 0.002)
2024-09-23 08:34:47 -07:00
{
2024-10-11 18:02:09 -07:00
_period = period;
_threshold = threshold;
2024-09-23 08:34:47 -07:00
_priceBuffer = new CircularBuffer(4);
2024-10-11 18:02:09 -07:00
_smoothBuffer = new CircularBuffer(period);
2024-09-23 08:34:47 -07:00
Name = "MAAF";
2024-10-11 18:02:09 -07:00
WarmupPeriod = period;
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 initial period for the filter (default 39).</param>
/// <param name="threshold">The threshold for adaptive adjustment (default 0.002).</param>
2024-10-11 18:02:09 -07:00
public Maaf(object source, int period = 39, double threshold = 0.002) : this(period, threshold)
2024-09-23 08:34:47 -07:00
{
var pubEvent = source.GetType().GetEvent("Pub");
pubEvent?.AddEventHandler(source, new ValueSignal(Sub));
}
public override void Init()
{
_priceBuffer.Clear();
_smoothBuffer.Clear();
_prevFilter = 0;
_prevValue2 = 0;
base.Init();
}
protected override void ManageState(bool isNew)
{
if (isNew)
{
_lastValidValue = Input.Value;
_index++;
_p_prevFilter = _prevFilter;
_p_prevValue2 = _prevValue2;
}
else
{
_prevFilter = _p_prevFilter;
_prevValue2 = _p_prevValue2;
}
}
protected override double Calculation()
{
ManageState(IsNew);
_priceBuffer.Add(Input.Value, Input.IsNew);
if (_priceBuffer.Count < 4)
{
return Input.Value;
}
double smooth = (_priceBuffer[^1] + (2 * _priceBuffer[^2]) + (2 * _priceBuffer[^3]) + _priceBuffer[^4]) / 6;
_smoothBuffer.Add(smooth, Input.IsNew);
if (_smoothBuffer.Count < _period)
{
return smooth;
}
int length = _period;
double value3 = 0.2;
double value2 = _prevValue2;
while (value3 > _threshold && length > 0)
{
double alpha = 2.0 / (length + 1);
var sortedValues = _smoothBuffer.TakeLast(length).OrderBy(x => x).ToList();
double value1 = sortedValues[length / 2];
value2 = alpha * (smooth - _prevValue2) + _prevValue2;
if (value1 != 0)
{
value3 = Math.Abs(value1 - value2) / value1;
}
length -= 2;
}
2024-09-24 16:41:26 -07:00
if (length < 3) length = 3;
2024-09-23 08:34:47 -07:00
double finalAlpha = 2.0 / (length + 1);
double filter = finalAlpha * (smooth - _prevFilter) + _prevFilter;
_p_prevFilter = _prevFilter;
_prevFilter = filter;
_p_prevValue2 = _prevValue2;
_prevValue2 = value2;
IsHot = _index >= WarmupPeriod;
return filter;
}
}