mirror of
https://github.com/mihakralj/QuanTAlib.git
synced 2026-08-19 19:18:05 +00:00
refactoring
This commit is contained in:
@@ -25,12 +25,14 @@ public class EMA_Series : Single_TSeries_Indicator
|
||||
private readonly System.Collections.Generic.List<double> _buffer = new();
|
||||
private readonly double _k, _k1m;
|
||||
private double _lastema, _lastlastema;
|
||||
private bool _useSMA;
|
||||
|
||||
public EMA_Series(TSeries source, int period, bool useNaN = false) : base(source, period, useNaN)
|
||||
public EMA_Series(TSeries source, int period, bool useNaN = false, bool useSMA = true) : base(source, period, useNaN)
|
||||
{
|
||||
this._k = 2.0 / (this._p + 1);
|
||||
this._k1m = 1.0 - this._k;
|
||||
this._lastema = this._lastlastema = double.NaN;
|
||||
this._lastema = this._lastlastema = 0;
|
||||
_useSMA = useSMA;
|
||||
if (this._data.Count > 0) { base.Add(this._data); }
|
||||
}
|
||||
|
||||
@@ -38,8 +40,9 @@ public class EMA_Series : Single_TSeries_Indicator
|
||||
{
|
||||
double _ema;
|
||||
if (update) { this._lastema = this._lastlastema; }
|
||||
if (this.Count == 0) { _lastema = TValue.v; }
|
||||
|
||||
if (this.Count < this._p)
|
||||
if (this.Count < this._p && _useSMA)
|
||||
{
|
||||
Add_Replace(_buffer, TValue.v, update);
|
||||
_ema = 0;
|
||||
|
||||
+40
-15
@@ -1,6 +1,5 @@
|
||||
namespace QuanTAlib;
|
||||
using System;
|
||||
using System.Linq;
|
||||
|
||||
/* <summary>
|
||||
SMA: Simple Moving Average
|
||||
@@ -19,19 +18,45 @@ Remark:
|
||||
|
||||
public class SMA_Series : Single_TSeries_Indicator
|
||||
{
|
||||
public SMA_Series(TSeries source, int period, bool useNaN = false) : base(source, period, useNaN)
|
||||
{
|
||||
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> _buffer = new();
|
||||
private double _sma, _oldsma;
|
||||
private double _topv, _oldtopv;
|
||||
public SMA_Series(TSeries source, int period, bool useNaN = false) : base(source, period, useNaN)
|
||||
{
|
||||
if (base._data.Count > 0)
|
||||
{ base.Add(base._data); }
|
||||
}
|
||||
public override void Add((System.DateTime t, double v) TValue, bool update)
|
||||
{
|
||||
_topv = Add_Replace_Trim(_buffer, TValue.v, _p, update);
|
||||
|
||||
public override void Add((System.DateTime t, double v) TValue, bool update)
|
||||
{
|
||||
Add_Replace_Trim(_buffer, TValue.v, _p, update);
|
||||
double _sma = 0;
|
||||
for (int i=0; i<_buffer.Count; i++) { _sma+= _buffer[i]; }
|
||||
_sma /= _buffer.Count;
|
||||
// rolling back if update, storing data for potential future update
|
||||
if (update)
|
||||
{
|
||||
_sma = _oldsma;
|
||||
_topv = _oldtopv;
|
||||
}
|
||||
else
|
||||
{
|
||||
_oldsma = _sma;
|
||||
_oldtopv = _topv;
|
||||
}
|
||||
|
||||
base.Add((TValue.t, _sma), update, _NaN);
|
||||
}
|
||||
}
|
||||
// main additive calculation of SMA - for data points that are larger than _p period
|
||||
// this.Count > _p
|
||||
if (this.Count > _p)
|
||||
{
|
||||
_sma += (TValue.v - _topv) / _p;
|
||||
}
|
||||
else
|
||||
{
|
||||
// calculate SMA the traditional way (sum all, divide with _p) for data points within _p period
|
||||
_sma = 0;
|
||||
for (int i = 0; i < _buffer.Count; i++)
|
||||
{ _sma += _buffer[i]; }
|
||||
_sma /= _buffer.Count;
|
||||
}
|
||||
|
||||
base.Add((TValue.t, _sma), update, _NaN);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user