Files
QuanTAlib/lib/averages/Fwma.cs
T

114 lines
3.7 KiB
C#
Raw Normal View History

2024-10-27 16:11:08 -07:00
using System.Runtime.CompilerServices;
2024-09-22 17:31:24 -07:00
namespace QuanTAlib;
2024-10-27 09:38:53 -07:00
/// <summary>
/// FWMA: Fibonacci Weighted Moving Average
/// A moving average that uses Fibonacci numbers as weights in its calculation. The weights
/// are arranged in reverse order so that recent prices receive higher weights corresponding
/// to larger Fibonacci numbers.
/// </summary>
/// <remarks>
/// The FWMA calculation process:
/// 1. Generates a Fibonacci sequence up to the specified period
/// 2. Reverses the sequence to give higher weights to recent prices
/// 3. Normalizes the weights to sum to 1
/// 4. Applies the weights through convolution
///
/// Key characteristics:
/// - Uses Fibonacci sequence for weight distribution
/// - Recent prices receive higher weights
/// - Natural progression of weights based on the golden ratio
/// - Implemented using efficient convolution operations
///
/// Implementation:
/// Original implementation based on Fibonacci sequence principles
/// </remarks>
2024-09-22 17:31:24 -07:00
public class Fwma : AbstractBase
{
private readonly Convolution _convolution;
2024-10-27 09:38:53 -07:00
/// <param name="period">The number of data points used in the FWMA calculation.</param>
/// <exception cref="ArgumentException">Thrown when period is less than 1.</exception>
2024-09-22 17:31:24 -07:00
public Fwma(int period)
{
if (period < 1)
{
2024-10-27 16:11:08 -07:00
throw new System.ArgumentException("Period must be greater than or equal to 1.", nameof(period));
2024-09-22 17:31:24 -07:00
}
2024-11-03 09:27:12 -08:00
double[] _kernel = GenerateKernel(period);
2024-10-27 16:11:08 -07:00
_convolution = new Convolution(_kernel);
2024-09-22 17:31:24 -07:00
Name = "Fwma";
WarmupPeriod = period;
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 FWMA calculation.</param>
2024-09-22 17:31:24 -07:00
public Fwma(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 Fibonacci-based convolution kernel for the FWMA calculation.
/// </summary>
/// <param name="period">The period for which to generate the kernel.</param>
/// <returns>An array of normalized Fibonacci-based weights for the convolution operation.</returns>
2024-10-27 16:11:08 -07:00
[MethodImpl(MethodImplOptions.AggressiveInlining)]
2024-09-22 17:31:24 -07:00
public static double[] GenerateKernel(int period)
{
double[] kernel = new double[period];
double[] fibSeries = new double[period];
2024-10-27 16:11:08 -07:00
// Generate Fibonacci series with running sum
2024-09-22 17:31:24 -07:00
fibSeries[0] = fibSeries[1] = 1;
2024-10-27 16:11:08 -07:00
double weightSum = 2.0; // Initial sum for first two Fibonacci numbers
2024-09-22 17:31:24 -07:00
for (int i = 2; i < period; i++)
{
fibSeries[i] = fibSeries[i - 1] + fibSeries[i - 2];
2024-10-27 16:11:08 -07:00
weightSum += fibSeries[i];
2024-09-22 17:31:24 -07:00
}
2024-10-27 16:11:08 -07:00
// Calculate inverse of weight sum for normalization
double invWeightSum = 1.0 / weightSum;
2024-09-22 17:31:24 -07:00
2024-10-27 16:11:08 -07:00
// Reverse and normalize the series in one pass
2024-09-22 17:31:24 -07:00
for (int i = 0; i < period; i++)
{
2024-10-27 16:11:08 -07:00
kernel[i] = fibSeries[period - 1 - i] * invWeightSum;
2024-09-22 17:31:24 -07:00
}
return kernel;
}
2024-10-27 16:11:08 -07:00
[MethodImpl(MethodImplOptions.AggressiveInlining)]
2024-09-22 17:31:24 -07:00
private new void Init()
{
base.Init();
_convolution.Init();
}
2024-10-27 16:11:08 -07:00
[MethodImpl(MethodImplOptions.AggressiveInlining)]
2024-09-22 17:31:24 -07:00
protected override void ManageState(bool isNew)
{
if (isNew)
{
_lastValidValue = Input.Value;
_index++;
}
}
protected override double Calculation()
{
ManageState(Input.IsNew);
// Use Convolution for calculation
2024-10-27 16:11:08 -07:00
var convolutionResult = _convolution.Calc(Input);
2024-09-22 17:31:24 -07:00
IsHot = _index >= WarmupPeriod;
2024-10-27 16:11:08 -07:00
return convolutionResult.Value;
2024-09-22 17:31:24 -07:00
}
2024-10-27 09:38:53 -07:00
}