This commit is contained in:
Miha Kralj
2024-10-08 13:59:33 -07:00
parent 46f5ffe9e9
commit 0bc41854aa
19 changed files with 48 additions and 218 deletions
-8
View File
@@ -81,16 +81,8 @@ public class Frama : AbstractBase
}
}
<<<<<<< HEAD
protected override double Calculation()
{
ManageState(Input.IsNew);
_buffer.Add(Input.Value, Input.IsNew);
=======
double n1 = (hh - ll) / _period;
double n2 = (hh1 - ll1 + hh2 - ll2) / (_period / 2);
>>>>>>> dev
double d = (Math.Log(n2 + double.Epsilon) - Math.Log(n1 + double.Epsilon)) / Math.Log(2);
+3 -12
View File
@@ -2,33 +2,24 @@ namespace QuanTAlib;
public class Qema : AbstractBase
{
private readonly double _k1, _k2, _k3, _k4;
private readonly Ema _ema1, _ema2, _ema3, _ema4;
private double _lastQema, _p_lastQema;
<<<<<<< HEAD
public Qema(double k1 = 0.2, double k2 = 0.2, double k3 = 0.2, double k4 = 0.2) : base()
=======
public Qema(double k1 = 0.2, double k2 = 0.2, double k3 = 0.2, double k4 = 0.2)
>>>>>>> dev
{
if (k1 <= 0 || k2 <= 0 || k3 <= 0 || k4 <= 0)
{
throw new ArgumentOutOfRangeException("All k values must be in the range (0, 1].");
throw new ArgumentOutOfRangeException(nameof(k1), "All k values must be in the range (0, 1].");
}
_k1 = k1;
_k2 = k2;
_k3 = k3;
_k4 = k4;
_ema1 = new Ema(k1);
_ema2 = new Ema(k2);
_ema3 = new Ema(k3);
_ema4 = new Ema(k4);
Name = $"QEMA ({k1:F2},{k2:F2},{k3:F2},{k4:F2})";
double smK = Math.Min(Math.Min(_k1, _k2), Math.Min(_k3, _k4));
double smK = Math.Min(Math.Min(k1, k2), Math.Min(k3, k4));
WarmupPeriod = (int)((2 - smK) / smK);
Init();
+39 -105
View File
@@ -1,45 +1,29 @@
using System;
using System.Runtime.CompilerServices;
<<<<<<< HEAD
namespace QuanTAlib
{
public class Rma : AbstractBase
{
private readonly int _period;
private double _alpha;
private double _lastRMA;
private double _savedLastRMA;
public Rma(int period) : base()
{
if (period < 1)
{
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();
}
public Rma(object source, int period) : this(period)
{
var pubEvent = source.GetType().GetEvent("Pub");
pubEvent?.AddEventHandler(source, new ValueSignal(Sub));
}
=======
namespace QuanTAlib;
/// <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>
public class Rma : AbstractBase
{
private readonly int _period;
private double _lastRma;
private readonly double _alpha;
private double _lastRMA;
private double _savedLastRMA;
private double _savedLastRma;
public Rma(int period)
{
@@ -63,21 +47,22 @@ public class Rma : AbstractBase
public override void Init()
{
base.Init();
_lastRMA = 0;
_savedLastRMA = 0;
_lastRma = 0;
_savedLastRma = 0;
}
protected override void ManageState(bool isNew)
{
if (!isNew)
if (isNew)
{
_lastRMA = _savedLastRMA;
return;
_savedLastRma = _lastRma;
_lastValidValue = Input.Value;
_index++;
}
else
{
_lastRma = _savedLastRma;
}
_savedLastRMA = _lastRMA;
_lastValidValue = Input.Value;
_index++;
}
protected override double Calculation()
@@ -88,73 +73,22 @@ public class Rma : AbstractBase
if (_index == 1)
{
return Input.Value;
rma = Input.Value;
}
if (_index <= _period)
else if (_index <= _period)
{
// Simple average during initial period
return (_lastRMA * (_index - 1) + Input.Value) / _index;
rma = (_lastRma * (_index - 1) + Input.Value) / _index;
}
else
{
// Wilder's smoothing method
rma = _alpha * (_lastRma - Input.Value) + _lastRma;
}
// Wilder's smoothing method
return _alpha * (Input.Value - _lastRMA) + _lastRMA;
}
_lastRMA = rma;
_lastRma = rma;
IsHot = _index >= WarmupPeriod;
>>>>>>> dev
public override void Init()
{
base.Init();
_lastRMA = 0;
_savedLastRMA = 0;
}
protected override void ManageState(bool isNew)
{
if (isNew)
{
_savedLastRMA = _lastRMA;
_lastValidValue = Input.Value;
_index++;
}
else
{
_lastRMA = _savedLastRMA;
}
}
protected override double Calculation()
{
ManageState(Input.IsNew);
double rma;
if (_index == 1)
{
rma = Input.Value;
}
else if (_index <= _period)
{
// Simple average during initial period
rma = (_lastRMA * (_index - 1) + Input.Value) / _index;
}
else
{
// Wilder's smoothing method
rma = _alpha * (Input.Value - _lastRMA) + _lastRMA;
}
_lastRMA = rma;
IsHot = _index >= WarmupPeriod;
return rma;
}
return rma;
}
<<<<<<< HEAD
}
=======
}
>>>>>>> dev