Files
QuanTAlib/Source/Trends/WMA_Series.cs
T

35 lines
1.3 KiB
C#
Raw Normal View History

2022-11-12 19:00:38 -08:00
namespace QuanTAlib;
using System;
/* <summary>
WMA: (linearly) Weighted Moving Average
The weights are linearly decreasing over the period and the most recent data has
the heaviest weight.
Sources:
https://corporatefinanceinstitute.com/resources/knowledge/trading-investing/weighted-moving-average-wma/
https://www.technicalindicators.net/indicators-technical-analysis/83-moving-averages-simple-exponential-weighted
</summary> */
public class WMA_Series : Single_TSeries_Indicator
{
public WMA_Series(TSeries source, int period, bool useNaN = false) : base(source, period, useNaN)
{
for (int i = 0; i < this._p; i++) { this._weights.Add(i + 1); }
if (base._data.Count > 0) { base.Add(base._data); }
}
private readonly System.Collections.Generic.List<double> _buffer = new();
private readonly System.Collections.Generic.List<double> _weights = new();
public override void Add((System.DateTime t, double v) TValue, bool update)
{
2022-11-17 07:53:45 -08:00
Add_Replace_Trim(_buffer, TValue.v, _p, update);
2022-11-12 19:00:38 -08:00
double _wma = 0;
for (int i = 0; i < _buffer.Count; i++) { _wma += _buffer[i] * this._weights[i]; }
_wma /= (this._buffer.Count * (this._buffer.Count + 1)) * 0.5;
2022-11-17 07:53:45 -08:00
base.Add((TValue.t, _wma), update, _NaN);
2022-11-12 19:00:38 -08:00
}
2022-04-19 14:35:10 -07:00
}