mirror of
https://github.com/mihakralj/QuanTAlib.git
synced 2026-08-17 10:08:05 +00:00
KAMA, SMMA, ZLEMA
This commit is contained in:
@@ -59,7 +59,7 @@ public class HMA_Series : TSeries
|
||||
this._buf1.Add(data.v);
|
||||
this._buf2.Add(data.v);
|
||||
}
|
||||
if (this._buf1.Count > (int)(Math.Ceiling((double)this._p / 2)))
|
||||
if (this._buf1.Count > (int)((double)this._p / 2))
|
||||
{
|
||||
this._buf1.RemoveAt(0);
|
||||
}
|
||||
|
||||
@@ -35,30 +35,28 @@ public class KAMA_Series : Single_TSeries_Indicator
|
||||
_scSlow = 2.0 / (slow+1);
|
||||
if (base._data.Count > 0) { base.Add(base._data); }
|
||||
}
|
||||
public override void Add((System.DateTime t, double v) TValue, bool update)
|
||||
{
|
||||
if (update){
|
||||
_buffer[_buffer.Count - 1] = TValue.v;
|
||||
this._lastkama = this._lastlastkama;
|
||||
} else {
|
||||
_buffer.Add(TValue.v);
|
||||
}
|
||||
if (_buffer.Count > _p + 1) { _buffer.RemoveAt(0); }
|
||||
double _kama = TValue.v;
|
||||
if (this.Count < this._p) {
|
||||
for (int i = 0; i < this._buffer.Count; i++) { _kama += this._buffer[i]; }
|
||||
_kama /= this._buffer.Count;
|
||||
} else {
|
||||
double _change = Math.Abs(_buffer[_buffer.Count - 1] - _buffer[(_buffer.Count > _p + 1) ? 1 : 0]);
|
||||
double _sumpv = 0;
|
||||
for (int i = 1; i < _buffer.Count; i++)
|
||||
{
|
||||
_sumpv += Math.Abs(_buffer[(_buffer.Count > 0) ? i : 0] - _buffer[i - 1]);
|
||||
}
|
||||
double _er = (_sumpv == 0) ? 0 : _change / _sumpv;
|
||||
double _sc = (_er * (_scFast - _scSlow)) + _scSlow;
|
||||
_kama = (_lastkama + (_sc * _sc * (TValue.v - _lastkama)));
|
||||
}
|
||||
public override void Add((System.DateTime t, double v) TValue, bool update)
|
||||
{
|
||||
if (update){
|
||||
_buffer[_buffer.Count - 1] = TValue.v;
|
||||
this._lastkama = this._lastlastkama;
|
||||
} else {
|
||||
_buffer.Add(TValue.v);
|
||||
}
|
||||
if (_buffer.Count > _p + 1) { _buffer.RemoveAt(0); }
|
||||
double _kama = 0;
|
||||
if (this.Count < this._p) {
|
||||
for (int i = 0; i < this._buffer.Count; i++) { _kama += this._buffer[i]; }
|
||||
_kama /= this._buffer.Count;
|
||||
} else {
|
||||
double _change = Math.Abs(_buffer[_buffer.Count - 1] - _buffer[(_buffer.Count > _p + 1) ? 1 : 0]);
|
||||
double _sumpv = 0;
|
||||
for (int i = 1; i < _buffer.Count; i++)
|
||||
{ _sumpv += Math.Abs(_buffer[(_buffer.Count > 0) ? i : 0] - _buffer[i - 1]); }
|
||||
double _er = (_sumpv == 0) ? 0 : _change / _sumpv;
|
||||
double _sc = (_er * (_scFast - _scSlow)) + _scSlow;
|
||||
_kama = (_lastkama + (_sc * _sc * (TValue.v - _lastkama)));
|
||||
}
|
||||
_lastlastkama = _lastkama;
|
||||
_lastkama = _kama;
|
||||
var result = (TValue.t, (this.Count < this._p - 1 && this._NaN) ? double.NaN : _kama);
|
||||
|
||||
@@ -0,0 +1,58 @@
|
||||
namespace QuanTAlib;
|
||||
using System;
|
||||
|
||||
/* <summary>
|
||||
SMMA: Smoothed Moving Average
|
||||
The Smoothed Moving Average (SMMA) is a combination of a SMA and an EMA. It gives the recent prices
|
||||
an equal weighting as the historic prices as it takes all available price data into account.
|
||||
The main advantage of a smoothed moving average is that it removes short-term fluctuations.
|
||||
|
||||
SMMA(i) = (SMMA-1*(N-1) + CLOSE (i)) / N
|
||||
|
||||
Sources:
|
||||
https://blog.earn2trade.com/smoothed-moving-average
|
||||
https://guide.traderevolution.com/traderevolution/mobile-applications/phone/android/technical-indicators/moving-averages/smma-smoothed-moving-average
|
||||
https://www.chartmill.com/documentation/technical-analysis-indicators/217-MOVING-AVERAGES-%7C-The-Smoothed-Moving-Average-%28SMMA%29
|
||||
|
||||
</summary> */
|
||||
|
||||
public class SMMA_Series : Single_TSeries_Indicator
|
||||
{
|
||||
private readonly System.Collections.Generic.List<double> _buffer = new();
|
||||
private double _lastsmma, _lastlastsmma;
|
||||
|
||||
public SMMA_Series(TSeries source, int period, bool useNaN = false) : base(source, period, useNaN)
|
||||
{
|
||||
this._lastsmma = this._lastlastsmma = double.NaN;
|
||||
if (this._data.Count > 0) { base.Add(this._data); }
|
||||
}
|
||||
|
||||
public override void Add((DateTime t, double v) TValue, bool update)
|
||||
{
|
||||
double _smma = 0;
|
||||
if (update) { this._lastsmma = this._lastlastsmma; }
|
||||
|
||||
if (this.Count < this._p)
|
||||
{
|
||||
if (update) { this._buffer[this._buffer.Count - 1] = TValue.v; }
|
||||
else
|
||||
{
|
||||
this._buffer.Add(TValue.v);
|
||||
}
|
||||
if (this._buffer.Count > this._p) { this._buffer.RemoveAt(0); }
|
||||
|
||||
for (int i = 0; i < this._buffer.Count; i++) { _smma += this._buffer[i]; }
|
||||
_smma /= this._buffer.Count;
|
||||
}
|
||||
else
|
||||
{
|
||||
_smma = ((_lastsmma * (_p-1)) + TValue.v) / _p ;
|
||||
}
|
||||
|
||||
this._lastlastsmma = this._lastsmma;
|
||||
this._lastsmma = _smma;
|
||||
|
||||
var ret = (TValue.t, this.Count < this._p - 1 && this._NaN ? double.NaN : _smma);
|
||||
base.Add(ret, update);
|
||||
}
|
||||
}
|
||||
@@ -21,36 +21,51 @@ Remark:
|
||||
|
||||
public class ZLEMA_Series : Single_TSeries_Indicator
|
||||
{
|
||||
private readonly double _k, _k1m;
|
||||
private double _lastema, _lastlastema;
|
||||
private readonly System.Collections.Generic.List<double> _buffer = new();
|
||||
private readonly double _k, _k1m;
|
||||
private double _lastema, _lastlastema;
|
||||
|
||||
public ZLEMA_Series(TSeries source, int period, bool useNaN = false) : base(source, period, useNaN)
|
||||
{
|
||||
this._k = 2.0 / (double)(period + 1);
|
||||
this._k1m = 1.0 - this._k;
|
||||
this._lastema = this._lastlastema = double.NaN;
|
||||
public ZLEMA_Series(TSeries source, int period, bool useNaN = false) : base(source, period, useNaN)
|
||||
{
|
||||
this._k = 2.0 / (this._p + 1);
|
||||
this._k1m = 1.0 - this._k;
|
||||
this._lastema = this._lastlastema = double.NaN;
|
||||
if (base._data.Count > 0)
|
||||
{ base.Add(base._data); }
|
||||
}
|
||||
|
||||
public override void Add((System.DateTime t, double v) TValue, bool update)
|
||||
{
|
||||
int _lag = (int)((_p - 1) * 0.5);
|
||||
_lag = (this.Count - _lag < 0) ? 0 : this.Count - _lag;
|
||||
double _zl = TValue.v + (TValue.v - _data[_lag].v);
|
||||
double _ema = 0;
|
||||
if (update)
|
||||
{ this._lastema = this._lastlastema; }
|
||||
if (this.Count < this._p)
|
||||
{
|
||||
if (update)
|
||||
{ this._buffer[this._buffer.Count - 1] = _zl; }
|
||||
else
|
||||
{
|
||||
this._buffer.Add(_zl);
|
||||
}
|
||||
if (this._buffer.Count > this._p)
|
||||
{ this._buffer.RemoveAt(0); }
|
||||
|
||||
if (base._data.Count > 0) { base.Add(base._data); }
|
||||
}
|
||||
for (int i = 0; i < this._buffer.Count; i++)
|
||||
{ _ema += this._buffer[i]; }
|
||||
_ema /= this._buffer.Count;
|
||||
}
|
||||
else
|
||||
{
|
||||
_ema = TValue.v * this._k + this._lastema * this._k1m;
|
||||
}
|
||||
|
||||
public override void Add((System.DateTime t, double v) TValue, bool update)
|
||||
{
|
||||
if (update)
|
||||
{
|
||||
this._lastema = this._lastlastema;
|
||||
}
|
||||
int _lag = (int)(0.5 * (_p - 1));
|
||||
int _l = Math.Max(this._data.Count - _lag, 0);
|
||||
double _lagdata = 1 * TValue.v - this._data[_l].v;
|
||||
this._lastlastema = this._lastema;
|
||||
this._lastema = _ema;
|
||||
|
||||
double _ema = System.Double.IsNaN(this._lastema) ? _lagdata : _lagdata * this._k + this._lastema * this._k1m;
|
||||
this._lastlastema = this._lastema;
|
||||
this._lastema = _ema;
|
||||
|
||||
(System.DateTime t, double v) result =
|
||||
(TValue.t, (this.Count < this._p - 1 && this._NaN) ? double.NaN : _ema);
|
||||
base.Add(result, update);
|
||||
|
||||
}
|
||||
}
|
||||
var ret = (TValue.t, this.Count < this._p - 1 && this._NaN ? double.NaN : _ema);
|
||||
base.Add(ret, update);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user