Files
QuanTAlib/lib/averages/Wma.cs
T
Miha Kralj 58d72c06ca Squash
dotcover

s1

.sln

s1

s1

s2

s3

s4

s5

s1

s2

x

x2

x3

x4

x5

x6

x1

sonarcube cleanup1

sonarcube cleanup2

sonarcube cleanup 3

fixes

q

q

q

q

q

q

q

q

q1

q2

q

q1

codacy 1
2024-09-23 22:08:40 -07:00

67 lines
1.5 KiB
C#

namespace QuanTAlib;
//TODO fix WMA - passing Talib test
public class Wma : AbstractBase
{
private readonly Convolution _convolution;
public Wma(int period)
{
if (period < 1)
{
throw new ArgumentException("Period must be greater than or equal to 1.", nameof(period));
}
_convolution = new Convolution(GenerateWmaKernel(period));
Name = "Wma";
WarmupPeriod = period;
Init();
}
public Wma(object source, int period) : this(period)
{
var pubEvent = source.GetType().GetEvent("Pub");
pubEvent?.AddEventHandler(source, new ValueSignal(Sub));
}
private static double[] GenerateWmaKernel(int period)
{
double[] kernel = new double[period];
double weightSum = period * (period + 1) / 2.0;
for (int i = 0; i < period; i++)
{
kernel[i] = (period - i) / weightSum;
}
return kernel;
}
private new void Init()
{
base.Init();
_convolution.Init();
}
protected override void ManageState(bool isNew)
{
if (isNew)
{
_lastValidValue = Input.Value;
_index++;
}
}
protected override double Calculation()
{
ManageState(Input.IsNew);
// Use Convolution for calculation
TValue convolutionResult = _convolution.Calc(Input);
double result = convolutionResult.Value;
IsHot = _index >= WarmupPeriod;
return result;
}
}