mirror of
https://github.com/mihakralj/QuanTAlib.git
synced 2026-08-17 10:08:05 +00:00
Class optimization
This commit is contained in:
+7
-9
@@ -1,4 +1,4 @@
|
||||
using System;
|
||||
using System.Runtime.CompilerServices;
|
||||
namespace QuanTAlib;
|
||||
|
||||
/// <summary>
|
||||
@@ -27,9 +27,8 @@ namespace QuanTAlib;
|
||||
|
||||
public class Sma : AbstractBase
|
||||
{
|
||||
// inherited _index
|
||||
// inherited _value
|
||||
private readonly CircularBuffer _buffer;
|
||||
private readonly int _period;
|
||||
|
||||
/// <param name="period">The number of data points used in the SMA calculation.</param>
|
||||
/// <exception cref="ArgumentOutOfRangeException">Thrown when period is less than 1.</exception>
|
||||
@@ -37,9 +36,9 @@ public class Sma : AbstractBase
|
||||
{
|
||||
if (period < 1)
|
||||
{
|
||||
throw new ArgumentOutOfRangeException(nameof(period), "Period must be greater than or equal to 1.");
|
||||
throw new System.ArgumentOutOfRangeException(nameof(period), "Period must be greater than or equal to 1.");
|
||||
}
|
||||
WarmupPeriod = period;
|
||||
_period = period;
|
||||
_buffer = new CircularBuffer(period);
|
||||
Name = "Sma";
|
||||
WarmupPeriod = period;
|
||||
@@ -48,12 +47,13 @@ public class Sma : AbstractBase
|
||||
|
||||
/// <param name="source">The data source object that publishes updates.</param>
|
||||
/// <param name="period">The number of data points used in the SMA calculation.</param>
|
||||
public Sma(object source, int period) : this(period: period)
|
||||
public Sma(object source, int period) : this(period)
|
||||
{
|
||||
var pubEvent = source.GetType().GetEvent("Pub");
|
||||
pubEvent?.AddEventHandler(source, new ValueSignal(Sub));
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
protected override void ManageState(bool isNew)
|
||||
{
|
||||
if (isNew)
|
||||
@@ -69,12 +69,10 @@ public class Sma : AbstractBase
|
||||
/// <returns>The calculated SMA value.</returns>
|
||||
protected override double Calculation()
|
||||
{
|
||||
double result;
|
||||
ManageState(IsNew);
|
||||
_buffer.Add(Input.Value, Input.IsNew);
|
||||
result = _buffer.Average();
|
||||
|
||||
IsHot = _index >= WarmupPeriod;
|
||||
return result;
|
||||
return _buffer.Average();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user