mirror of
https://github.com/mihakralj/QuanTAlib.git
synced 2026-08-16 17:48:05 +00:00
refactoring
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
namespace QuanTAlib;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
||||
/* <summary>
|
||||
Abstract classes with all scaffolding required to build indicators.
|
||||
@@ -17,50 +18,54 @@ Abstract classes with all scaffolding required to build indicators.
|
||||
</summary> */
|
||||
public abstract class Single_TSeries_Indicator : TSeries
|
||||
{
|
||||
protected readonly int _period;
|
||||
protected readonly bool _NaN;
|
||||
protected readonly TSeries _data;
|
||||
protected 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)
|
||||
// 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._period = period;
|
||||
this._p = _period;
|
||||
this._NaN = useNaN;
|
||||
this._data.Pub += this.Sub;
|
||||
}
|
||||
|
||||
// overridable Add() method to add/update a single item at the end of the list
|
||||
|
||||
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);
|
||||
}
|
||||
public new virtual void Add((System.DateTime t, double v) TValue, bool update) => base.Add(TValue, update);
|
||||
|
||||
// potentially overridable Add() method for the whole series (could be replaced with faster bulk algo)
|
||||
public virtual void Add(TSeries data) { for (int i = 0; i < data.Count; i++) { this.Add(TValue: data[i], update: false); } }
|
||||
|
||||
public new void Add((System.DateTime t, double v) TValue) => this.Add(TValue: TValue, update: false);
|
||||
public void Add(bool update) => this.Add(TValue: this._data[this._data.Count - 1], update: update);
|
||||
public void Add() => this.Add(TValue: this._data[this._data.Count - 1], update: false);
|
||||
public new void Sub(object source, TSeriesEventArgs e) => this.Add(TValue: this._data[this._data.Count - 1], update: e.update);
|
||||
|
||||
protected static void Add_Replace(List<double> l, double v, bool update)
|
||||
{
|
||||
if (update)
|
||||
{ l[l.Count - 1] = v; }
|
||||
else
|
||||
{ l.Add(v); }
|
||||
}
|
||||
protected static double Add_Replace_Trim(List<double> l, double v, int p, bool update)
|
||||
{
|
||||
Add_Replace(l, v, update);
|
||||
double ret = (l.Count > 0) ? l.First() : 0;
|
||||
if (l.Count > p && p != 0)
|
||||
{
|
||||
this._data = source;
|
||||
this._period = period;
|
||||
this._p = _period;
|
||||
this._NaN = useNaN;
|
||||
this._data.Pub += this.Sub;
|
||||
}
|
||||
|
||||
// overridable Add() method to add/update a single item at the end of the list
|
||||
|
||||
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);
|
||||
}
|
||||
public new virtual void Add((System.DateTime t, double v) TValue, bool update) => base.Add(TValue, update);
|
||||
|
||||
// potentially overridable Add() method for the whole series (could be replaced with faster bulk algo)
|
||||
public virtual void Add(TSeries data) { for (int i = 0; i < data.Count; i++) { this.Add(TValue: data[i], update: false); }}
|
||||
|
||||
public new void Add((System.DateTime t, double v) TValue) => this.Add(TValue: TValue, update: false);
|
||||
public void Add(bool update) => this.Add(TValue: this._data[this._data.Count - 1], update: update);
|
||||
public void Add() => this.Add(TValue: this._data[this._data.Count - 1], update: false);
|
||||
public new void Sub(object source, TSeriesEventArgs e) => this.Add(TValue: this._data[this._data.Count - 1], update: e.update);
|
||||
|
||||
protected static void Add_Replace(List<double> l, double v, bool update)
|
||||
{
|
||||
if (update)
|
||||
{ l[l.Count - 1] = v; }
|
||||
else
|
||||
{ l.Add(v); }
|
||||
}
|
||||
protected static void Add_Replace_Trim(List<double> l, double v, int p, bool update)
|
||||
{
|
||||
Add_Replace(l, v, update);
|
||||
if (l.Count > p && p!=0)
|
||||
{ l.RemoveAt(0); }
|
||||
l.RemoveAt(0);
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -20,10 +20,10 @@ public class ADOSC_Series : Single_TBars_Indicator
|
||||
private double _lastema1, _lastlastema1, _lastema2, _lastlastema2;
|
||||
private double _lastadl, _lastlastadl;
|
||||
|
||||
public ADOSC_Series(TBars source, bool useNaN = false) : base(source, period: 0, useNaN)
|
||||
public ADOSC_Series(TBars source, int shortPeriod = 3, int longPeriod =10, bool useNaN = false) : base(source, period: 0, useNaN)
|
||||
{
|
||||
_k1 = 2.0 / (3 + 1);
|
||||
_k2 = 2.0 / (10 + 1);
|
||||
_k1 = 2.0 / (shortPeriod + 1);
|
||||
_k2 = 2.0 / (longPeriod + 1);
|
||||
_lastadl = _lastlastadl = _lastema1 = _lastlastema1 = _lastema2 = _lastlastema2 = 0;
|
||||
if (_bars.Count > 0) { base.Add(_bars); }
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user