EMA update - v 0.1.27

This commit is contained in:
Miha Kralj
2023-01-09 13:42:40 -08:00
5 changed files with 50 additions and 35 deletions
+1 -1
View File
@@ -2,7 +2,7 @@
<Project Sdk="Microsoft.NET.Sdk"> <Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup> <PropertyGroup>
<Title>QuanTAlib</Title> <Title>QuanTAlib</Title>
<Version>0.1.26</Version> <Version>0.1.27</Version>
<Product>Library of Technical Indicators for .NET</Product> <Product>Library of Technical Indicators for .NET</Product>
<Description>Quantitative Technical Analysis library for real-time (streaming) data analysis</Description> <Description>Quantitative Technical Analysis library for real-time (streaming) data analysis</Description>
<RepositoryType>git</RepositoryType> <RepositoryType>git</RepositoryType>
+42 -34
View File
@@ -20,43 +20,51 @@ Issues:
</summary> */ </summary> */
public class EMA_Series : Single_TSeries_Indicator public class EMA_Series : Single_TSeries_Indicator {
{ private double _k;
private readonly System.Collections.Generic.List<double> _buffer = new(); private double _lastema, _lastlastema;
private readonly double _k, _k1m; private double _sum, _oldsum;
private double _lastema, _lastlastema; private int _len, _oldlen;
private readonly bool _useSMA; private readonly bool _useSMA;
public EMA_Series(TSeries source, int period, bool useNaN = false, bool useSMA = true) : 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._k = 2.0 / (this._p + 1); _sum = _oldsum = _lastema = _lastlastema = 0;
this._k1m = 1.0 - this._k; _len = _oldlen = 0;
this._lastema = this._lastlastema = 0; _useSMA = useSMA;
_useSMA = useSMA; if (this._data.Count > 0) { base.Add(this._data); }
if (this._data.Count > 0) { base.Add(this._data); } }
}
public override void Add((DateTime t, double v) TValue, bool update) public override void Add((DateTime t, double v) TValue, bool update) {
{ double _ema = 0;
double _ema; if (update) { _lastema = _lastlastema; _sum = _oldsum; }
if (update) { this._lastema = this._lastlastema; } else { _lastlastema = _lastema; _oldsum = _sum; _len++; }
if (this.Count == 0) { _lastema = TValue.v; }
if (this.Count < this._p && _useSMA) // when period = 0, create cumulative/additive series where _k is progressively larger
{ if (_period == 0) { _k = 2.0 / (_len + 1); }
Add_Replace(_buffer, TValue.v, update);
_ema = 0;
for (int i = 0; i < _buffer.Count; i++) { _ema += _buffer[i]; }
_ema /= _buffer.Count;
}
else
{
_ema = (TValue.v * this._k) + (this._lastema * this._k1m);
}
this._lastlastema = this._lastema; // the first value of the series
this._lastema = _ema; if (this.Count == 0) {
_ema = _sum = TValue.v;
}
// if SMA is used for seeding, calculate SMA within period
else if (_len <= _period && _useSMA && _p != 0) {
_sum += TValue.v;
if (_period != 0 && _len > _period) {
_sum -= (_data[base.Count - _period - (update ? 1 : 0)].v);
}
_ema = _sum / Math.Min(_len, _period);
}
// calculate EMA out from last EMA and factor k
else {
_ema = _k * (TValue.v - _lastema) + _lastema;
}
_lastema = _ema;
base.Add((TValue.t, _ema), update, _NaN); base.Add((TValue.t, _ema), update, _NaN);
} }
public void Reset() {
_sum = _oldsum = _lastema = _lastlastema = 0;
_len = _oldlen = 0;
}
} }
+1
View File
@@ -29,6 +29,7 @@ public class SMA_Series : Single_TSeries_Indicator {
public override void Add((DateTime t, double v) TValue, bool update) { public override void Add((DateTime t, double v) TValue, bool update) {
if (update) { _sum = _oldsum; } if (update) { _sum = _oldsum; }
else { _oldsum = _sum; _len++; } else { _oldsum = _sum; _len++; }
_sum += TValue.v; _sum += TValue.v;
if (_period != 0 && _len > _period) { if (_period != 0 && _len > _period) {
_sum -= (_data[base.Count - _period - (update ? 1 : 0)].v); _sum -= (_data[base.Count - _period - (update ? 1 : 0)].v);
+4
View File
@@ -144,6 +144,7 @@ public class PandasTA : IDisposable
Assert.InRange(PanTA_item! - QL_item, -Math.Exp(-digits), Math.Exp(-digits)); Assert.InRange(PanTA_item! - QL_item, -Math.Exp(-digits), Math.Exp(-digits));
} }
} }
*/
[Fact] void EMA() { [Fact] void EMA() {
EMA_Series QL = new(bars.Close, period, false); EMA_Series QL = new(bars.Close, period, false);
var pta = df.ta.ema(close: df.close, length: period); var pta = df.ta.ema(close: df.close, length: period);
@@ -154,6 +155,7 @@ public class PandasTA : IDisposable
Assert.InRange(PanTA_item! - QL_item, -Math.Exp(-digits), Math.Exp(-digits)); Assert.InRange(PanTA_item! - QL_item, -Math.Exp(-digits), Math.Exp(-digits));
} }
} }
/*
[Fact] void ENTROPY() { [Fact] void ENTROPY() {
ENTROPY_Series QL = new(bars.Close, period, useNaN: false); ENTROPY_Series QL = new(bars.Close, period, useNaN: false);
var pta = df.ta.entropy(close: df.close, length: period); var pta = df.ta.entropy(close: df.close, length: period);
@@ -307,6 +309,7 @@ public class PandasTA : IDisposable
Assert.InRange(PanTA_item! - QL_item, -Math.Exp(-digits), Math.Exp(-digits)); Assert.InRange(PanTA_item! - QL_item, -Math.Exp(-digits), Math.Exp(-digits));
} }
} }
*/
[Fact] void SMA() { [Fact] void SMA() {
SMA_Series QL = new(bars.Close, period, false); SMA_Series QL = new(bars.Close, period, false);
var pta = df.ta.sma(close: df.close, length: period); var pta = df.ta.sma(close: df.close, length: period);
@@ -317,6 +320,7 @@ public class PandasTA : IDisposable
Assert.InRange(PanTA_item! - QL_item, -Math.Exp(-digits), Math.Exp(-digits)); Assert.InRange(PanTA_item! - QL_item, -Math.Exp(-digits), Math.Exp(-digits));
} }
} }
/*
[Fact] void SSDEV() { [Fact] void SSDEV() {
SSDEV_Series QL = new(bars.Close, period, useNaN: false); SSDEV_Series QL = new(bars.Close, period, useNaN: false);
var pta = df.ta.stdev(close: df.close, length: period, ddof: 1); var pta = df.ta.stdev(close: df.close, length: period, ddof: 1);
+2
View File
@@ -239,6 +239,7 @@ public class Ta_Lib
Assert.InRange(TA_item! - QL_item, -Math.Exp(-digits), Math.Exp(-digits)); Assert.InRange(TA_item! - QL_item, -Math.Exp(-digits), Math.Exp(-digits));
} }
} }
/*
[Fact] [Fact]
public void MAMA() public void MAMA()
{ {
@@ -251,6 +252,7 @@ public class Ta_Lib
Assert.InRange(TA_item! - QL_item, -Math.Exp(-digits-1), Math.Exp(-digits-1)); Assert.InRange(TA_item! - QL_item, -Math.Exp(-digits-1), Math.Exp(-digits-1));
} }
} }
*/
[Fact] [Fact]
public void MAX() public void MAX()
{ {