Files
QuanTAlib/lib/averages/Rma.cs
T

95 lines
2.4 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-08 13:59:33 -07:00
/// <summary>
/// RMA: Relative Moving Average (also known as Wilder's Moving Average)
/// RMA is similar to EMA but uses a different smoothing factor.
/// </summary>
/// <remarks>
/// Key characteristics:
/// - Uses no buffer, relying only on the previous RMA value.
/// - The weight of new data points (alpha) is calculated as 1 / period.
/// - Provides a smoother curve compared to SMA and EMA, reacting more slowly to price changes.
///
/// Calculation method:
/// RMA = (Previous RMA * (period - 1) + New Data) / period
///
/// Sources:
/// - https://www.tradingview.com/pine-script-reference/v5/#fun_ta{dot}rma
/// - https://www.investopedia.com/terms/w/wilders-smoothing.asp
/// </remarks>
2024-10-06 14:44:43 -07:00
public class Rma : AbstractBase
{
2024-09-23 08:34:47 -07:00
private readonly int _period;
2024-10-08 13:59:33 -07:00
private double _lastRma;
2024-10-06 14:44:43 -07:00
private readonly double _alpha;
2024-10-08 13:59:33 -07:00
private double _savedLastRma;
2024-09-23 08:34:47 -07:00
2024-10-06 14:44:43 -07:00
public Rma(int period)
{
if (period < 1)
{
2024-09-23 08:34:47 -07:00
throw new ArgumentException("Period must be greater than or equal to 1.", nameof(period));
}
_period = period;
WarmupPeriod = period * 2;
_alpha = 1.0 / _period; // Wilder's smoothing factor
Name = $"Rma({_period})";
Init();
2024-10-08 17:31:29 +00:00
}
2024-09-23 08:34:47 -07:00
2024-10-08 17:31:29 +00:00
public Rma(object source, int period) : this(period)
{
2024-09-23 08:34:47 -07:00
var pubEvent = source.GetType().GetEvent("Pub");
pubEvent?.AddEventHandler(source, new ValueSignal(Sub));
2024-10-08 17:31:29 +00:00
}
2024-09-23 08:34:47 -07:00
2024-10-08 17:31:29 +00:00
public override void Init()
{
2024-09-23 08:34:47 -07:00
base.Init();
2024-10-08 13:59:33 -07:00
_lastRma = 0;
_savedLastRma = 0;
2024-10-08 17:31:29 +00:00
}
2024-10-08 09:49:28 -07:00
2024-10-08 17:31:29 +00:00
protected override void ManageState(bool isNew)
{
2024-10-08 13:59:33 -07:00
if (isNew)
2024-10-06 14:44:43 -07:00
{
2024-10-08 13:59:33 -07:00
_savedLastRma = _lastRma;
_lastValidValue = Input.Value;
_index++;
}
else
{
_lastRma = _savedLastRma;
2024-09-23 08:34:47 -07:00
}
2024-10-08 17:31:29 +00:00
}
2024-10-08 09:49:28 -07:00
2024-10-08 17:31:29 +00:00
protected override double Calculation()
{
2024-09-23 08:34:47 -07:00
ManageState(Input.IsNew);
double rma;
2024-10-06 14:44:43 -07:00
if (_index == 1)
{
2024-10-08 13:59:33 -07:00
rma = Input.Value;
2024-10-06 14:44:43 -07:00
}
2024-10-08 13:59:33 -07:00
else if (_index <= _period)
2024-10-06 14:44:43 -07:00
{
2024-09-23 08:34:47 -07:00
// Simple average during initial period
2024-10-08 13:59:33 -07:00
rma = (_lastRma * (_index - 1) + Input.Value) / _index;
}
else
{
// Wilder's smoothing method
2024-10-08 21:00:48 +00:00
rma = _alpha * (_lastRma - Input.Value) + _lastRma;
2024-10-06 14:44:43 -07:00
}
2024-10-08 09:49:28 -07:00
2024-10-08 13:59:33 -07:00
_lastRma = rma;
2024-09-23 08:34:47 -07:00
IsHot = _index >= WarmupPeriod;
2024-10-08 13:59:33 -07:00
return rma;
2024-09-23 08:34:47 -07:00
}
}