Files
QuanTAlib/lib/averages/Frama.cs
T

130 lines
3.8 KiB
C#
Raw Normal View History

2024-09-23 08:34:47 -07:00
using System;
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;
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)
throw new 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;
_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-06 14:44:43 -07:00
public override void Init()
{
base.Init();
_buffer.Clear();
_lastFrama = 0;
_prevLastFrama = 0;
}
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-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
int half = _period / 2;
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];
hh = Math.Max(hh, price);
ll = Math.Min(ll, price);
if (i < half)
2024-09-23 08:34:47 -07:00
{
2024-10-06 14:44:43 -07:00
hh1 = Math.Max(hh1, price);
ll1 = Math.Min(ll1, price);
2024-09-23 08:34:47 -07:00
}
else
{
2024-10-06 14:44:43 -07:00
hh2 = Math.Max(hh2, price);
ll2 = Math.Min(ll2, price);
2024-09-23 08:34:47 -07:00
}
}
2024-10-06 14:44:43 -07:00
double n1 = (hh - ll) / _period;
double n2 = (hh1 - ll1 + hh2 - ll2) / (_period / 2);
2024-09-23 08:34:47 -07:00
2024-10-06 14:44:43 -07:00
double d = (Math.Log(n2 + double.Epsilon) - Math.Log(n1 + double.Epsilon)) / Math.Log(2);
2024-09-23 08:34:47 -07:00
2024-10-06 14:44:43 -07:00
double alpha = Math.Exp(-4.6 * (d - 1));
alpha = Math.Max(Math.Min(alpha, 1), 0.01); // Ensure alpha is between 0.01 and 1
2024-09-23 08:34:47 -07:00
2024-10-06 14:44:43 -07: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-06 14:44:43 -07:00
protected override double GetLastValid()
{
return _lastFrama;
2024-09-23 08:34:47 -07:00
}
2024-10-27 09:38:53 -07:00
}