mirror of
https://github.com/mihakralj/QuanTAlib.git
synced 2026-08-21 20:18:05 +00:00
New version merge
This commit is contained in:
@@ -0,0 +1,60 @@
|
||||
namespace QuanTAlib;
|
||||
|
||||
public class Sma : AbstractBase
|
||||
{
|
||||
// inherited _index
|
||||
// inherited _value
|
||||
public readonly int Period;
|
||||
private CircularBuffer _buffer;
|
||||
|
||||
public Sma(int period) : base()
|
||||
{
|
||||
if (period < 1)
|
||||
{
|
||||
throw new ArgumentOutOfRangeException(nameof(period), "Period must be greater than or equal to 1.");
|
||||
}
|
||||
Period = period;
|
||||
WarmupPeriod = period;
|
||||
_buffer = new CircularBuffer(period);
|
||||
Name = "Sma";
|
||||
WarmupPeriod = period;
|
||||
Init();
|
||||
}
|
||||
|
||||
public Sma(object source, int period) : this(period: period)
|
||||
{
|
||||
var pubEvent = source.GetType().GetEvent("Pub");
|
||||
pubEvent?.AddEventHandler(source, new ValueSignal(Sub));
|
||||
}
|
||||
//inhereted public void Sub(object source, in ValueEventArgs args)
|
||||
|
||||
public override void Init()
|
||||
{
|
||||
//_buffer.Clear();
|
||||
base.Init();
|
||||
}
|
||||
|
||||
protected override void ManageState(bool isNew)
|
||||
{
|
||||
if (isNew)
|
||||
{
|
||||
_lastValidValue = Input.Value;
|
||||
_index++;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Core SMA calculation - using _buffer.Average
|
||||
/// </summary>
|
||||
|
||||
protected override double Calculation()
|
||||
{
|
||||
double result;
|
||||
ManageState(IsNew);
|
||||
_buffer.Add(Input.Value, Input.IsNew);
|
||||
result = _buffer.Average();
|
||||
|
||||
IsHot = _index >= WarmupPeriod;
|
||||
return result;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user