mirror of
https://github.com/mihakralj/QuanTAlib.git
synced 2026-08-22 20:48:04 +00:00
Quantower adaptation
This commit is contained in:
+109
-126
@@ -1,127 +1,110 @@
|
||||
namespace QuanTAlib;
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Numerics;
|
||||
|
||||
/* <summary>
|
||||
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://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 double k, a;
|
||||
private double c1, c2, c3, c4;
|
||||
private double o_c1, o_c2, o_c3, o_c4;
|
||||
|
||||
private double e1, e2, e3, e4, e5, e6;
|
||||
private double o_e1, o_e2, o_e3, o_e4, o_e5, o_e6;
|
||||
|
||||
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 = 0.7, bool useNaN = false) : base(source, period, useNaN)
|
||||
{
|
||||
k = 2.0 / (_p + 1);
|
||||
a = vfactor;
|
||||
c1 = -a * a * a;
|
||||
c2 = (3 * a * a) + (3 * a * a * a);
|
||||
c3 = (-6 * a * a) - (3 * a) - (3 * a * a * a);
|
||||
c4 = 1 + (3 * a) + (3 * a * a) + (a * a * a) ;
|
||||
e1 = e2 = e3 = e4 = e5 = e6 = 0;
|
||||
sum1 = sum2 = sum3 = sum4 = sum5 = sum6 = 0;
|
||||
|
||||
if (_data.Count > 0) { base.Add(data: _data); }
|
||||
}
|
||||
|
||||
public override void Add((DateTime t, double v) TValue, bool update)
|
||||
{
|
||||
if (update) {
|
||||
// roll back (x = oldx)
|
||||
c1 = o_c1; c2 = o_c2; c3 = o_c3; c4 = o_c4;
|
||||
e1 = o_e1; e2 = o_e2; e3 = o_e3; e4 = o_e4; e5 = o_e5; e6 = o_e6;
|
||||
sum1 = o_sum1; sum2 = o_sum2; sum3 = o_sum3; sum4 = o_sum4; sum5 = o_sum5; sum6 = o_sum6;
|
||||
} else {
|
||||
// roll forward (oldx = x)
|
||||
o_c1 = c1; o_c2 = c2; o_c3 = c3; o_c4 = c4;
|
||||
o_e1 = e1; o_e2 = e2; o_e3 = e3; o_e4 = e4; o_e5 = e5; o_e6 = e6;
|
||||
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)) {
|
||||
e2 += k * (e1 - e2);
|
||||
if (i > 3 * (_p - 1)) {
|
||||
e3 += k * (e2 - e3);
|
||||
if (i > 4 * (_p - 1)) {
|
||||
e4 += k * (e3 - e4);
|
||||
if (i > 5 * (_p - 1)) {
|
||||
e5 += k * (e4 - e5);
|
||||
if (i > 6 * (_p - 1)) {
|
||||
e6 += k * (e5 - e6);
|
||||
}
|
||||
else {
|
||||
sum6 += e5;
|
||||
if (i == 6 * (_p - 1)) {
|
||||
e6 = sum6 / Math.Max(_p, base.Count);
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
sum5 += e4;
|
||||
if (i == 5 * (_p - 1)) {
|
||||
sum6 = e5 = sum5 / Math.Max(_p, base.Count);
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
sum4 += e3;
|
||||
if (i == 4 * (_p - 1)) {
|
||||
sum5 = e4 = sum4 / Math.Max(_p, base.Count);
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
sum3 += e2;
|
||||
if (i == 3 * (_p - 1)) {
|
||||
sum4 = e3 = sum3 / Math.Max(_p, base.Count);
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
sum2 += e1;
|
||||
if (i == 2 * (_p - 1)) {
|
||||
sum3 = e2 = sum2 / Math.Max(_p, base.Count);
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
sum1 += v;
|
||||
if (i == _p - 1) {
|
||||
sum2 = e1 = sum1 / Math.Max(_p, base.Count);
|
||||
}
|
||||
}
|
||||
|
||||
double t3 = (c1 * e6) + (c2 * e5) + (c3 * e4) + (c4 * e3);
|
||||
base.Add(TValue: (TValue.t, t3), update: update, useNaN: _NaN);
|
||||
}
|
||||
namespace QuanTAlib;
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Numerics;
|
||||
|
||||
/* <summary>
|
||||
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://technicalindicators.net/indicators-technical-analysis/150-t3-moving-average
|
||||
http://www.binarytribune.com/forex-trading-indicators/t3-moving-average-indicator/
|
||||
|
||||
Calculation:
|
||||
Volume Factor is typically 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 readonly double _k, _k1m, _c1, _c2, _c3, _c4;
|
||||
private readonly System.Collections.Generic.List<double> _buffer1 = new();
|
||||
private readonly System.Collections.Generic.List<double> _buffer2 = new();
|
||||
private readonly System.Collections.Generic.List<double> _buffer3 = new();
|
||||
private readonly System.Collections.Generic.List<double> _buffer4 = new();
|
||||
private readonly System.Collections.Generic.List<double> _buffer5 = new();
|
||||
private readonly System.Collections.Generic.List<double> _buffer6 = new();
|
||||
|
||||
private double _lastema1, _lastema2, _lastema3, _lastema4, _lastema5, _lastema6;
|
||||
private double _llastema1, _llastema2, _llastema3, _llastema4, _llastema5, _llastema6;
|
||||
private bool _useSMA;
|
||||
|
||||
public T3_Series(TSeries source, int period, double vfactor = 0.7, bool useNaN = false, bool useSMA = true) : base(source, period, useNaN) {
|
||||
double _a = vfactor; //0.7; //0.618
|
||||
_c1 = -_a * _a * _a;
|
||||
_c2 = 3 * _a * _a + 3 * _a * _a * _a;
|
||||
_c3 = -6 * _a * _a - 3 * _a - 3 * _a * _a * _a;
|
||||
_c4 = 1 + 3 * _a + _a * _a * _a + 3 * _a * _a;
|
||||
|
||||
_k = 2.0 / (_p + 1);
|
||||
_k1m = 1.0 - _k;
|
||||
_lastema1 = _llastema1 = _lastema2 = _llastema2 = _lastema3 = _llastema3 = _lastema4 = _llastema4 = _lastema5 = _llastema5 = _lastema5 = _llastema5 = 0;
|
||||
_useSMA = useSMA;
|
||||
if (this._data.Count > 0) { base.Add(this._data); }
|
||||
}
|
||||
|
||||
public override void Add((DateTime t, double v) TValue, bool update) {
|
||||
double _ema1, _ema2, _ema3, _ema4, _ema5, _ema6;
|
||||
if (update) { _lastema1 = _llastema1; _lastema2 = _llastema2; _lastema3 = _llastema3; _lastema4 = _llastema4; _lastema5 = _llastema5; _lastema6 = _llastema6; }
|
||||
else { _llastema1 = _lastema1; _llastema2 = _lastema2; _llastema3 = _lastema3; _llastema4 = _lastema4; _llastema5 = _lastema5; _llastema6 = _lastema6; }
|
||||
|
||||
if (this.Count == 0) { _lastema1 = _lastema2 = _lastema3 = _lastema4 = _lastema5 = _lastema6 = TValue.v; }
|
||||
|
||||
if ((this.Count < _p) && _useSMA) {
|
||||
Add_Replace(_buffer1, TValue.v, update);
|
||||
_ema1 = 0;
|
||||
for (int i = 0; i < _buffer1.Count; i++) { _ema1 += _buffer1[i]; }
|
||||
_ema1 /= _buffer1.Count;
|
||||
|
||||
Add_Replace(_buffer2, _ema1, update);
|
||||
_ema2 = 0;
|
||||
for (int i = 0; i < _buffer2.Count; i++) { _ema2 += _buffer2[i]; }
|
||||
_ema2 /= _buffer2.Count;
|
||||
|
||||
Add_Replace(_buffer3, _ema2, update);
|
||||
_ema3 = 0;
|
||||
for (int i = 0; i < _buffer3.Count; i++) { _ema3 += _buffer3[i]; }
|
||||
_ema3 /= _buffer3.Count;
|
||||
|
||||
Add_Replace(_buffer4, _ema3, update);
|
||||
_ema4 = 0;
|
||||
for (int i = 0; i < _buffer4.Count; i++) { _ema4 += _buffer4[i]; }
|
||||
_ema4 /= _buffer4.Count;
|
||||
|
||||
Add_Replace(_buffer5, _ema4, update);
|
||||
_ema5 = 0;
|
||||
for (int i = 0; i < _buffer5.Count; i++) { _ema5 += _buffer5[i]; }
|
||||
_ema5 /= _buffer5.Count;
|
||||
|
||||
Add_Replace(_buffer6, _ema5, update);
|
||||
_ema6 = 0;
|
||||
for (int i = 0; i < _buffer6.Count; i++) { _ema6 += _buffer6[i]; }
|
||||
_ema6 /= _buffer6.Count;
|
||||
}
|
||||
else {
|
||||
_ema1 = (TValue.v * this._k) + (this._lastema1 * this._k1m);
|
||||
_ema2 = (_ema1 * this._k) + (this._lastema2 * this._k1m);
|
||||
_ema3 = (_ema2 * this._k) + (this._lastema3 * this._k1m);
|
||||
_ema4 = (_ema3 * this._k) + (this._lastema4 * this._k1m);
|
||||
_ema5 = (_ema4 * this._k) + (this._lastema5 * this._k1m);
|
||||
_ema6 = (_ema5 * this._k) + (this._lastema6 * this._k1m);
|
||||
}
|
||||
_lastema1 = _ema1;
|
||||
_lastema2 = _ema2;
|
||||
_lastema3 = _ema3;
|
||||
_lastema4 = _ema4;
|
||||
_lastema5 = _ema5;
|
||||
_lastema6 = _ema6;
|
||||
|
||||
double _T3 = _c1 * _ema6 + _c2 * _ema5 + _c3 * _ema4 + _c4 * _ema3;
|
||||
base.Add((TValue.t, _T3), update, _NaN);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user