mirror of
https://github.com/mihakralj/QuanTAlib.git
synced 2026-08-18 10:38:05 +00:00
better EMA
This commit is contained in:
@@ -17,15 +17,17 @@ Abstract classes with all scaffolding required to build indicators.
|
||||
</summary> */
|
||||
public abstract class Single_TSeries_Indicator : TSeries
|
||||
{
|
||||
protected readonly int _p;
|
||||
protected readonly int _period;
|
||||
protected readonly bool _NaN;
|
||||
protected readonly TSeries _data;
|
||||
protected int _p;
|
||||
|
||||
// Chainable Constructor - add it at the end of primary constructor :base(source: source, period: period, useNaN: useNaN)
|
||||
protected Single_TSeries_Indicator(TSeries source, int period, bool useNaN)
|
||||
{
|
||||
this._data = source;
|
||||
this._p = period;
|
||||
this._period = period;
|
||||
this._p = _period;
|
||||
this._NaN = useNaN;
|
||||
this._data.Pub += this.Sub;
|
||||
}
|
||||
@@ -34,6 +36,7 @@ public abstract class Single_TSeries_Indicator : TSeries
|
||||
|
||||
public virtual void Add((System.DateTime t, double v) TValue, bool update, bool useNaN)
|
||||
{
|
||||
if (_period == 0) { _p = this.Length; }
|
||||
var res = (TValue.t, this.Count < this._p - 1 && this._NaN ? double.NaN : TValue.v);
|
||||
base.Add(res, update);
|
||||
}
|
||||
|
||||
@@ -66,7 +66,6 @@
|
||||
<Visible>False</Visible>
|
||||
<PackagePath></PackagePath>
|
||||
</None>
|
||||
<PackageReference Include="System.Collections" Version="4.3.0" />
|
||||
<PackageReference Include="System.Text.Json" Version="7.0.0" />
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
@@ -21,12 +21,10 @@ public class MAMA_Series : Single_TSeries_Indicator
|
||||
{
|
||||
fastl = fastlimit;
|
||||
slowl = slowlimit;
|
||||
i = 0;
|
||||
Fama = new();
|
||||
if (base._data.Count > 0) { base.Add(base._data); }
|
||||
}
|
||||
|
||||
private int i;
|
||||
private double sumPr, jI, jQ, fastl, slowl;
|
||||
private (double i, double i1, double i2, double i3, double i4, double i5, double i6, double io) pr, i1, q1, sm, dt;
|
||||
private (double i, double i1, double io) i2, q2, re, im, pd, ph, mama, fama;
|
||||
@@ -51,7 +49,7 @@ public class MAMA_Series : Single_TSeries_Indicator
|
||||
mama.io = mama.i1; mama.i1 = mama.i;
|
||||
fama.io = fama.i1; fama.i1 = fama.i;
|
||||
}
|
||||
|
||||
int i = base.Count;
|
||||
pr.i = TValue.v;
|
||||
if (i > 5) {
|
||||
double adj = (0.075 * pd.i1) + 0.54;
|
||||
@@ -113,7 +111,6 @@ public class MAMA_Series : Single_TSeries_Indicator
|
||||
mama.i = fama.i = sumPr / (i+1);
|
||||
}
|
||||
|
||||
if (!update) { i++; }
|
||||
base.Add((TValue.t, mama.i), update, _NaN);
|
||||
var result = (TValue.t, this.Count < this._p - 1 && this._NaN ? double.NaN : fama.i);
|
||||
Fama.Add(result, update);
|
||||
|
||||
+24
-14
@@ -4,19 +4,30 @@ using System.Linq;
|
||||
using System.Numerics;
|
||||
|
||||
/* <summary>
|
||||
T3: Triple Exponential Moving Average
|
||||
TEMA uses EMA(EMA(EMA())) to calculate less laggy Exponential moving average.
|
||||
T3: Tillson T3 Moving Average
|
||||
Tim Tillson described it in "Technical Analysis of Stocks and Commodities", January 1998 in the
|
||||
article "Better Moving Averages". Tillson’s moving average becomes a popular indicator of
|
||||
technical analysis as it gets less lag with the price chart and its curve is considerably smoother.
|
||||
|
||||
Sources:
|
||||
https://www.tradingtechnologies.com/help/x-study/technical-indicator-definitions/triple-exponential-moving-average-tema/
|
||||
https://technicalindicators.net/indicators-technical-analysis/150-t3-moving-average
|
||||
http://www.binarytribune.com/forex-trading-indicators/t3-moving-average-indicator/
|
||||
|
||||
Calculation:
|
||||
a = 0.7 (but also 0.618);
|
||||
Ema1 = Ema (Close);
|
||||
Ema2 = Ema (Ema1);
|
||||
Ema3 = Ema (Ema2);
|
||||
Ema4 = Ema (Ema3);
|
||||
Ema5 = Ema (Ema4);
|
||||
Ema6 = Ema (Ema5);
|
||||
T3 = –(a*a*a) * Ema6 + (3*a*a + 3*a*a*a) * Ema5 + (–6*a*a – 3*a – 3*a*a*a) * Ema4 + (1 + 3*a + a*a*a + 3*a*a) * Ema3
|
||||
|
||||
</summary> */
|
||||
|
||||
public class T3_Series : Single_TSeries_Indicator
|
||||
{
|
||||
private int i;
|
||||
private double k, a;
|
||||
private double k, a;
|
||||
private double c1, c2, c3, c4;
|
||||
private double o_c1, o_c2, o_c3, o_c4;
|
||||
|
||||
@@ -26,9 +37,8 @@ public class T3_Series : Single_TSeries_Indicator
|
||||
private double sum1, sum2, sum3, sum4, sum5, sum6;
|
||||
private double o_sum1, o_sum2, o_sum3, o_sum4, o_sum5, o_sum6;
|
||||
|
||||
public T3_Series(TSeries source, int period, double vfactor, bool useNaN = false) : base(source, period, useNaN)
|
||||
public T3_Series(TSeries source, int period, double vfactor = 0.7, bool useNaN = false) : base(source, period, useNaN)
|
||||
{
|
||||
i = 0;
|
||||
k = 2.0 / (_p + 1);
|
||||
a = vfactor;
|
||||
c1 = -a * a * a;
|
||||
@@ -55,6 +65,7 @@ public class T3_Series : Single_TSeries_Indicator
|
||||
o_sum1 = sum1; o_sum2 = sum2; o_sum3 = sum3; o_sum4 = sum4; o_sum5 = sum5; o_sum6 = sum6;
|
||||
}
|
||||
double v = TValue.v;
|
||||
int i = base.Count;
|
||||
if (i > _p - 1) {
|
||||
e1 += k * (v - e1);
|
||||
if (i > 2 * (_p - 1)) {
|
||||
@@ -71,45 +82,44 @@ public class T3_Series : Single_TSeries_Indicator
|
||||
else {
|
||||
sum6 += e5;
|
||||
if (i == 6 * (_p - 1)) {
|
||||
e6 = sum6 / _p;
|
||||
e6 = sum6 / Math.Max(_p, base.Count);
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
sum5 += e4;
|
||||
if (i == 5 * (_p - 1)) {
|
||||
sum6 = e5 = sum5 / _p;
|
||||
sum6 = e5 = sum5 / Math.Max(_p, base.Count);
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
sum4 += e3;
|
||||
if (i == 4 * (_p - 1)) {
|
||||
sum5 = e4 = sum4 / _p;
|
||||
sum5 = e4 = sum4 / Math.Max(_p, base.Count);
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
sum3 += e2;
|
||||
if (i == 3 * (_p - 1)) {
|
||||
sum4 = e3 = sum3 / _p;
|
||||
sum4 = e3 = sum3 / Math.Max(_p, base.Count);
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
sum2 += e1;
|
||||
if (i == 2 * (_p - 1)) {
|
||||
sum3 = e2 = sum2 / _p;
|
||||
sum3 = e2 = sum2 / Math.Max(_p, base.Count);
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
sum1 += v;
|
||||
if (i == _p - 1) {
|
||||
sum2 = e1 = sum1 / _p;
|
||||
sum2 = e1 = sum1 / Math.Max(_p, base.Count);
|
||||
}
|
||||
}
|
||||
if (!update) { i++; }
|
||||
|
||||
double t3 = (c1 * e6) + (c2 * e5) + (c3 * e4) + (c4 * e3);
|
||||
base.Add(TValue: (TValue.t, t3), update: update, useNaN: _NaN);
|
||||
|
||||
@@ -5,7 +5,7 @@ using System;
|
||||
ADL: Chaikin Accumulation/Distribution Line
|
||||
ADL is a volume-based indicator that measures the cumulative Money Flow Volume:
|
||||
|
||||
1. Money Flow Multiplier = [(Close - Low) - (High - Close)] /(High - Low)
|
||||
1. Money Flow Multiplier = [(Close - Low) - (High - Close)] /(High - Low)
|
||||
2. Money Flow Volume = Money Flow Multiplier x Volume for the Period
|
||||
3. ADL = Previous ADL + Current Period's Money Flow Volume
|
||||
|
||||
@@ -20,19 +20,17 @@ public class ADL_Series : Single_TBars_Indicator
|
||||
|
||||
public ADL_Series(TBars source, bool useNaN = false) : base(source, 0, useNaN)
|
||||
{
|
||||
this._lastadl = this._lastlastadl = 0;
|
||||
if (_bars.Count > 0)
|
||||
{ base.Add(_bars); }
|
||||
_lastadl = _lastlastadl = 0;
|
||||
if (_bars.Count > 0) { base.Add(_bars); }
|
||||
}
|
||||
|
||||
public override void Add((DateTime t, double o, double h, double l, double c, double v) TBar, bool update)
|
||||
{
|
||||
if (update)
|
||||
{ this._lastadl = this._lastlastadl; }
|
||||
if (update) { this._lastadl = this._lastlastadl; }
|
||||
|
||||
double _mfm = ((TBar.c - TBar.l) - (TBar.h - TBar.c)) / (TBar.h - TBar.l);
|
||||
double _mfv = _mfm * TBar.v;
|
||||
double _adl = this._lastadl + _mfv;
|
||||
double _adl = 0;
|
||||
double tmp = TBar.h - TBar.l;
|
||||
if (tmp > 0.0 ) { _adl = _lastadl + ((2*TBar.c - TBar.l - TBar.h) / tmp * TBar.v); }
|
||||
|
||||
this._lastlastadl = this._lastadl;
|
||||
this._lastadl = _adl;
|
||||
|
||||
Reference in New Issue
Block a user