Files
QuanTAlib/lib/averages/Frama.cs
T

147 lines
4.6 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-10-27 09:38:53 -07:00
/// <summary>
/// FRAMA: Fractal Adaptive Moving Average
/// An adaptive moving average that adjusts its smoothing factor based on the fractal dimension
/// of the price series. FRAMA automatically adapts to market conditions, becoming more responsive
/// during trends and more stable during sideways markets.
/// </summary>
/// <remarks>
/// The FRAMA algorithm works by:
/// 1. Calculating the fractal dimension of the price series
/// 2. Using this dimension to determine the optimal alpha (smoothing factor)
/// 3. Applying an EMA with the adaptive alpha
///
/// Key characteristics:
/// - Self-adaptive to market conditions
/// - Reduces lag during trending periods
/// - Increases smoothing during sideways markets
/// - Uses fractal geometry principles for market analysis
///
/// Sources:
/// John Ehlers - "FRAMA: A Trend-Following Indicator"
/// https://www.mesasoftware.com/papers/FRAMA.pdf
/// </remarks>
2024-10-06 14:44:43 -07:00
public class Frama : AbstractBase
2024-09-23 08:34:47 -07:00
{
2024-10-06 14:44:43 -07:00
private readonly int _period;
2024-10-27 16:11:08 -07:00
private readonly int _halfPeriod;
private readonly double _periodRecip;
private readonly double _halfPeriodRecip;
private readonly double _log2 = System.Math.Log(2);
private readonly double _epsilon = double.Epsilon;
2024-10-06 14:44:43 -07:00
private readonly CircularBuffer _buffer;
private double _lastFrama;
private double _prevLastFrama;
2024-10-27 09:38:53 -07:00
/// <param name="period">The number of periods used for fractal dimension calculation. Must be at least 2.</param>
/// <exception cref="ArgumentException">Thrown when period is less than 2.</exception>
2024-10-07 21:41:26 -07:00
public Frama(int period)
2024-09-23 08:34:47 -07:00
{
2024-10-06 14:44:43 -07:00
if (period < 2)
2024-10-27 16:11:08 -07:00
throw new System.ArgumentException("Period must be at least 2", nameof(period));
2024-09-23 08:34:47 -07:00
2024-10-06 14:44:43 -07:00
_period = period;
2024-10-27 16:11:08 -07:00
_halfPeriod = period / 2;
_periodRecip = 1.0 / period;
_halfPeriodRecip = 1.0 / _halfPeriod;
2024-10-06 14:44:43 -07:00
_buffer = new CircularBuffer(period);
WarmupPeriod = period;
}
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 for fractal dimension calculation.</param>
2024-10-07 21:41:26 -07:00
public Frama(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();
_buffer.Clear();
_lastFrama = 0;
_prevLastFrama = 0;
}
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
_prevLastFrama = _lastFrama;
_index++;
}
else
{
_lastFrama = _prevLastFrama;
}
}
2024-09-23 08:34:47 -07:00
2024-10-27 16:11:08 -07:00
[MethodImpl(MethodImplOptions.AggressiveInlining)]
2024-11-04 18:08:33 -08:00
private static void UpdateMinMax(double price, ref double high, ref double low)
2024-10-27 16:11:08 -07:00
{
high = System.Math.Max(high, price);
low = System.Math.Min(low, price);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
2024-11-04 18:08:33 -08:00
private static double CalculateAlpha(double dimension)
2024-10-27 16:11:08 -07:00
{
double alpha = System.Math.Exp(-4.6 * (dimension - 1));
return System.Math.Clamp(alpha, 0.01, 1.0);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
protected override double GetLastValid()
{
return _lastFrama;
}
2024-10-06 14:44:43 -07:00
protected override double Calculation()
{
ManageState(Input.IsNew);
_buffer.Add(Input.Value, Input.IsNew);
if (_buffer.Count < _period)
{
_lastFrama = _buffer.Average();
return _lastFrama;
2024-09-23 08:34:47 -07:00
}
2024-10-06 14:44:43 -07:00
double hh = double.MinValue, ll = double.MaxValue;
double hh1 = double.MinValue, ll1 = double.MaxValue;
double hh2 = double.MinValue, ll2 = double.MaxValue;
2024-09-23 08:34:47 -07:00
2024-10-06 14:44:43 -07:00
for (int i = 0; i < _period; i++)
2024-09-23 08:34:47 -07:00
{
2024-10-06 14:44:43 -07:00
double price = _buffer[i];
2024-10-27 16:11:08 -07:00
UpdateMinMax(price, ref hh, ref ll);
2024-10-06 14:44:43 -07:00
2024-10-27 16:11:08 -07:00
if (i < _halfPeriod)
2024-09-23 08:34:47 -07:00
{
2024-10-27 16:11:08 -07:00
UpdateMinMax(price, ref hh1, ref ll1);
2024-09-23 08:34:47 -07:00
}
else
{
2024-10-27 16:11:08 -07:00
UpdateMinMax(price, ref hh2, ref ll2);
2024-09-23 08:34:47 -07:00
}
}
2024-10-27 16:11:08 -07:00
double n1 = (hh - ll) * _periodRecip;
double n2 = (hh1 - ll1 + hh2 - ll2) * _halfPeriodRecip;
2024-09-23 08:34:47 -07:00
2024-10-27 16:11:08 -07:00
double dimension = (System.Math.Log(n2 + _epsilon) - System.Math.Log(n1 + _epsilon)) / _log2;
double alpha = CalculateAlpha(dimension);
2024-09-23 08:34:47 -07:00
2024-11-03 23:03:24 +00:00
_lastFrama = (alpha * (Input.Value - _lastFrama)) + _lastFrama;
2024-09-23 08:34:47 -07:00
2024-10-06 14:44:43 -07:00
IsHot = _index >= WarmupPeriod;
return _lastFrama;
2024-09-23 08:34:47 -07:00
}
2024-10-27 09:38:53 -07:00
}