mirror of
https://github.com/mihakralj/QuanTAlib.git
synced 2026-08-17 10:08:05 +00:00
pull request changes
This commit is contained in:
+25
-26
@@ -22,54 +22,53 @@ public class Rma : AbstractBase
|
||||
_alpha = 1.0 / _period; // Wilder's smoothing factor
|
||||
Name = $"Rma({_period})";
|
||||
Init();
|
||||
}
|
||||
}
|
||||
|
||||
public Rma(object source, int period) : this(period)
|
||||
{
|
||||
public Rma(object source, int period) : this(period)
|
||||
{
|
||||
var pubEvent = source.GetType().GetEvent("Pub");
|
||||
pubEvent?.AddEventHandler(source, new ValueSignal(Sub));
|
||||
}
|
||||
}
|
||||
|
||||
public override void Init()
|
||||
{
|
||||
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
|
||||
|
||||
protected override void ManageState(bool isNew)
|
||||
{
|
||||
if (!isNew)
|
||||
{
|
||||
_lastRMA = _savedLastRMA;
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
protected override double Calculation()
|
||||
{
|
||||
_savedLastRMA = _lastRMA;
|
||||
_lastValidValue = Input.Value;
|
||||
_index++;
|
||||
}
|
||||
|
||||
protected override double Calculation()
|
||||
{
|
||||
ManageState(Input.IsNew);
|
||||
|
||||
double rma;
|
||||
|
||||
if (_index == 1)
|
||||
{
|
||||
rma = Input.Value;
|
||||
return Input.Value;
|
||||
}
|
||||
else if (_index <= _period)
|
||||
|
||||
if (_index <= _period)
|
||||
{
|
||||
// Simple average during initial period
|
||||
rma = (_lastRMA * (_index - 1) + Input.Value) / _index;
|
||||
return (_lastRMA * (_index - 1) + Input.Value) / _index;
|
||||
}
|
||||
else
|
||||
{
|
||||
// Wilder's smoothing method
|
||||
rma = _alpha * (Input.Value - _lastRMA) + _lastRMA;
|
||||
|
||||
// Wilder's smoothing method
|
||||
return _alpha * (Input.Value - _lastRMA) + _lastRMA;
|
||||
}
|
||||
|
||||
_lastRMA = rma;
|
||||
|
||||
Reference in New Issue
Block a user