pull request changes

This commit is contained in:
Miha Kralj
2024-10-08 10:31:04 -07:00
parent af234594cc
commit 931bbdb6a0
5 changed files with 38 additions and 41 deletions
+25 -26
View File
@@ -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;