mirror of
https://github.com/mihakralj/QuanTAlib.git
synced 2026-08-25 22:08:05 +00:00
Refactoring the structure, upgrading to .NET 6.0/7.0/8.0
This commit is contained in:
@@ -0,0 +1,29 @@
|
||||
namespace QuanTAlib;
|
||||
using System;
|
||||
|
||||
/* <summary>
|
||||
ADD - adding TSeries+TSeries together, or TSeries+double, or double+TSeries
|
||||
|
||||
Remarks:
|
||||
Most of scaffolding is packaged in abstracty class Pair_TSeries_Indicator.
|
||||
|
||||
</summary> */
|
||||
|
||||
public class ADD_Series : Pair_TSeries_Indicator
|
||||
{
|
||||
public ADD_Series(TSeries d1, TSeries d2 ) : base(d1, d2) {
|
||||
if (base._d1.Count > 0 && base._d2.Count > 0) { for (int i=0; i< base._d1.Count; i++) { this.Add(base._d1[i], base._d2[i], false); } }
|
||||
}
|
||||
public ADD_Series(TSeries d1, double dd2 ) : base(d1, dd2) {
|
||||
if (base._d1.Count > 0) { for (int i=0; i< base._d1.Count; i++) { this.Add(base._d1[i], (base._d1[i].t, dd2), false); } }
|
||||
}
|
||||
public ADD_Series(double dd1, TSeries d2 ) : base(dd1, d2) {
|
||||
if (base._d2.Count > 0) { for (int i=0; i< base._d2.Count; i++) { this.Add((base._d2[i].t, dd1), base._d2[i], false); } }
|
||||
}
|
||||
|
||||
public override void Add((System.DateTime t, double v)TValue1, (System.DateTime t, double v)TValue2, bool update)
|
||||
{
|
||||
(System.DateTime t, double v) result = ((TValue1.t > TValue2.t) ? TValue1.t : TValue2.t, TValue1.v+TValue2.v);
|
||||
if (update) { base[base.Count - 1] = result; } else { base.Add(result); }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
namespace QuanTAlib;
|
||||
using System;
|
||||
|
||||
/* <summary>
|
||||
DIV - divide TSeries/TSeries , or TSeries/double, or double/TSeries
|
||||
|
||||
Remarks:
|
||||
Most of scaffolding is packaged in abstracty class Pair_TSeries_Indicator.
|
||||
</summary> */
|
||||
|
||||
public class DIV_Series : Pair_TSeries_Indicator
|
||||
{
|
||||
public DIV_Series(TSeries d1, TSeries d2 ) : base(d1, d2) {
|
||||
if (base._d1.Count > 0 && base._d2.Count > 0) { for (int i=0; i< base._d1.Count; i++) { this.Add(base._d1[i], base._d2[i], false); } }
|
||||
}
|
||||
public DIV_Series(TSeries d1, double dd2 ) : base(d1, dd2) {
|
||||
if (base._d1.Count > 0) { for (int i=0; i< base._d1.Count; i++) { this.Add(base._d1[i], (base._d1[i].t, dd2), false); } }
|
||||
}
|
||||
public DIV_Series(double dd1, TSeries d2 ) : base(dd1, d2) {
|
||||
if (base._d2.Count > 0) { for (int i=0; i< base._d2.Count; i++) { this.Add((base._d2[i].t, dd1), base._d2[i], false); } }
|
||||
}
|
||||
|
||||
public override void Add((System.DateTime t, double v)TValue1, (System.DateTime t, double v)TValue2, bool update)
|
||||
{
|
||||
(System.DateTime t, double v) result = ((TValue1.t > TValue2.t) ? TValue1.t : TValue2.t,
|
||||
(TValue2.v is not 0) ? TValue1.v/TValue2.v : Double.PositiveInfinity);
|
||||
if (update) { base[base.Count - 1] = result; } else { base.Add(result); }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
namespace QuanTAlib;
|
||||
using System;
|
||||
using System.Linq;
|
||||
|
||||
/* <summary>
|
||||
MAX - Maximum value in the given period in the series.
|
||||
If period = 0 => period = full length of the series
|
||||
</summary> */
|
||||
|
||||
public class MAX_Series : Single_TSeries_Indicator
|
||||
{
|
||||
public MAX_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();
|
||||
|
||||
public override void Add((DateTime t, double v) TValue, bool update)
|
||||
{
|
||||
Add_Replace_Trim(_buffer, TValue.v, _p, update);
|
||||
double _max = _buffer.Max();
|
||||
|
||||
base.Add((TValue.t, _max), update, _NaN);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
namespace QuanTAlib;
|
||||
using System;
|
||||
|
||||
/* <summary>
|
||||
MIDPOINT: Midpoint value (max+min)/2 in the given period in the series.
|
||||
If period = 0 => period = full length of the series
|
||||
|
||||
Sources:
|
||||
https://thefaqblog.com/what-is-the-midpoint-in-statistics/
|
||||
|
||||
</summary> */
|
||||
|
||||
public class MIDPOINT_Series : Single_TSeries_Indicator
|
||||
{
|
||||
public MIDPOINT_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();
|
||||
|
||||
public override void Add((DateTime t, double v) TValue, bool update)
|
||||
{
|
||||
Add_Replace_Trim(_buffer, TValue.v, _p, update);
|
||||
|
||||
double _max = TValue.v;
|
||||
double _min = TValue.v;
|
||||
for (int i = 0; i < this._buffer.Count; i++)
|
||||
{
|
||||
_max = Math.Max(this._buffer[i], _max);
|
||||
_min = Math.Min(this._buffer[i], _min);
|
||||
}
|
||||
double _mid = (_max + _min) * 0.5;
|
||||
|
||||
base.Add((TValue.t, _mid), update, _NaN);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
namespace QuanTAlib;
|
||||
using System;
|
||||
using System.Linq;
|
||||
|
||||
/* <summary>
|
||||
MIDPRICE: Midpoint price (highhest high + lowest low)/2 in the given period in the series.
|
||||
If period = 0 => period = full length of the series
|
||||
|
||||
</summary> */
|
||||
|
||||
public class MIDPRICE_Series : Single_TBars_Indicator
|
||||
{
|
||||
public MIDPRICE_Series(TBars source, int period, bool useNaN = false) : base(source, period, useNaN)
|
||||
{
|
||||
if (base._bars.Count > 0)
|
||||
{ base.Add(base._bars); }
|
||||
}
|
||||
private readonly System.Collections.Generic.List<double> _bufferhi = new();
|
||||
private readonly System.Collections.Generic.List<double> _bufferlo = new();
|
||||
|
||||
public override void Add((DateTime t, double o, double h, double l, double c, double v) TBar, bool update)
|
||||
{
|
||||
Add_Replace_Trim(_bufferhi, TBar.h, _p, update);
|
||||
Add_Replace_Trim(_bufferlo, TBar.l, _p, update);
|
||||
|
||||
double _max = _bufferhi.Max();
|
||||
double _min = _bufferlo.Min();
|
||||
double _mid = (_max + _min) * 0.5;
|
||||
|
||||
base.Add((TBar.t, _mid), update, _NaN);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
namespace QuanTAlib;
|
||||
using System;
|
||||
using System.Linq;
|
||||
|
||||
/* <summary>
|
||||
MIN - Minimum value in the given period in the series.
|
||||
If period = 0 => period = full length of the series
|
||||
</summary> */
|
||||
|
||||
public class MIN_Series : Single_TSeries_Indicator
|
||||
{
|
||||
public MIN_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();
|
||||
|
||||
public override void Add((System.DateTime t, double v) TValue, bool update)
|
||||
{
|
||||
Add_Replace_Trim(_buffer, TValue.v, _p, update);
|
||||
|
||||
double _min = _buffer.Min();
|
||||
base.Add((TValue.t, _min), update, _NaN);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
namespace QuanTAlib;
|
||||
using System;
|
||||
|
||||
/* <summary>
|
||||
MUL - multiply TSeries*TSeries together, or TSeries*double, or double*TSeries
|
||||
|
||||
</summary> */
|
||||
|
||||
public class MUL_Series : Pair_TSeries_Indicator
|
||||
{
|
||||
public MUL_Series(TSeries d1, TSeries d2 ) : base(d1, d2) {
|
||||
if (base._d1.Count > 0 && base._d2.Count > 0) { for (int i=0; i< base._d1.Count; i++) { this.Add(base._d1[i], base._d2[i], false); } }
|
||||
}
|
||||
public MUL_Series(TSeries d1, double dd2 ) : base(d1, dd2) {
|
||||
if (base._d1.Count > 0) { for (int i=0; i< base._d1.Count; i++) { this.Add(base._d1[i], (base._d1[i].t, dd2), false); } }
|
||||
}
|
||||
public MUL_Series(double dd1, TSeries d2 ) : base(dd1, d2) {
|
||||
if (base._d2.Count > 0) { for (int i=0; i< base._d2.Count; i++) { this.Add((base._d2[i].t, dd1), base._d2[i], false); } }
|
||||
}
|
||||
|
||||
public override void Add((System.DateTime t, double v)TValue1, (System.DateTime t, double v)TValue2, bool update)
|
||||
{
|
||||
(System.DateTime t, double v) result = ((TValue1.t > TValue2.t) ? TValue1.t : TValue2.t,
|
||||
TValue1.v*TValue2.v);
|
||||
if (update) { base[base.Count - 1] = result; } else { base.Add(result); }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,112 @@
|
||||
namespace QuanTAlib;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
/* <summary>
|
||||
Abstract classes with all scaffolding required to build indicators.
|
||||
All abstracts support period, NaN, and all permutations of Add() methods.
|
||||
Indicator classess need to implement:
|
||||
- Chaining constructor (Abstract's constructor executes first)
|
||||
- Default Add(value) class
|
||||
- optional Add(series) bulk insert class (for optimization of historical analysis)
|
||||
|
||||
Single_TSeries_Indicator - one single-value TSeries in, one TSeries out.
|
||||
Pair_TSeries_Indicator - Two TSeries in, one TSeries out. (includes simple semaphoring)
|
||||
Single_TBars_Indicator - One OHLCV TBars in, one TSeries out.
|
||||
|
||||
</summary> */
|
||||
|
||||
|
||||
public abstract class Pair_TSeries_Indicator : TSeries
|
||||
{
|
||||
protected readonly int _p;
|
||||
protected readonly bool _NaN;
|
||||
protected readonly TSeries _d1;
|
||||
protected readonly TSeries _d2;
|
||||
protected readonly double _dd1, _dd2;
|
||||
|
||||
// Chainable Constructors - add them at the end of primary constructors if needed
|
||||
protected Pair_TSeries_Indicator(TSeries source1, TSeries source2, int period, bool useNaN)
|
||||
{
|
||||
this._p = period;
|
||||
this._NaN = useNaN;
|
||||
this._d1 = source1;
|
||||
this._d2 = source2;
|
||||
this._dd1 = double.NaN;
|
||||
this._dd2 = double.NaN;
|
||||
this._d1.Pub += this.Sub;
|
||||
this._d2.Pub += this.Sub;
|
||||
}
|
||||
protected Pair_TSeries_Indicator(TSeries source1, TSeries source2)
|
||||
{
|
||||
this._d1 = source1;
|
||||
this._d2 = source2;
|
||||
this._dd1 = double.NaN;
|
||||
this._dd2 = double.NaN;
|
||||
this._d1.Pub += this.Sub;
|
||||
this._d2.Pub += this.Sub;
|
||||
}
|
||||
protected Pair_TSeries_Indicator(TSeries source1, double dd2)
|
||||
{
|
||||
this._d1 = source1;
|
||||
this._d2 = new();
|
||||
this._dd1 = double.NaN;
|
||||
this._dd2 = dd2;
|
||||
this._d1.Pub += this.Sub;
|
||||
}
|
||||
protected Pair_TSeries_Indicator(double dd1, TSeries source2)
|
||||
{
|
||||
this._d1 = new();
|
||||
this._d2 = source2;
|
||||
this._dd1 = dd1;
|
||||
this._dd2 = double.NaN;
|
||||
this._d2.Pub += this.Sub;
|
||||
}
|
||||
|
||||
// overridable Add(Tvalue, Tvalue) method to add/update a single value at the end of the list
|
||||
public virtual void Add((System.DateTime t, double v)TValue1, (System.DateTime t, double v)TValue2, bool update) => base.Add(TValue: (TValue1.t, 0), update: update); // default inserts zeros
|
||||
|
||||
// potentially overridable Add() bulk variations (could be replaced with faster bulk algos)
|
||||
public virtual void Add(TSeries d1, TSeries d2) { for (int i = 0; i < d1.Count; i++) { this.Add(d1[i], d2[i], update: false); }}
|
||||
public virtual void Add(TSeries d1, double dd2) { for (int i = 0; i < d1.Count; i++) { this.Add(d1[i], (d1[i].t, dd2), update: false); }}
|
||||
public virtual void Add(double dd1, TSeries d2) { for (int i = 0; i < d2.Count; i++) { this.Add((d2[i].t, dd1), d2[i], update: false); }}
|
||||
|
||||
public void Add((System.DateTime t, double v)TValue1, (System.DateTime t, double v)TValue2) => this.Add(TValue1, TValue2, update: false);
|
||||
|
||||
public void Add(bool update)
|
||||
{
|
||||
if ((this._dd1 is double.NaN) && (this._dd2 is double.NaN))
|
||||
{
|
||||
// (Series, Series)
|
||||
if (update || (this._d1.Count > this.Count && this._d2.Count > this.Count))
|
||||
{ this.Add(this._d1[this._d1.Count - 1], this._d2[this._d2.Count - 1], update); }
|
||||
}
|
||||
else if ((this._dd2 is not double.NaN) && (this._dd1 is double.NaN))
|
||||
{
|
||||
// (Series, Double)
|
||||
this.Add(TValue1: this._d1[this._d1.Count - 1], TValue2: (this._d1[this._d1.Count - 1].t, this._dd2), update: update);
|
||||
}
|
||||
else
|
||||
{
|
||||
// (Double, Series)
|
||||
this.Add(TValue1: (this._d2[this._d2.Count - 1].t, this._dd1), TValue2: this._d2[this._d2.Count - 1], update: update);
|
||||
}
|
||||
}
|
||||
|
||||
public void Add() => this.Add(update: false);
|
||||
public new void Sub(object source, TSeriesEventArgs e) => this.Add(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); }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
namespace QuanTAlib;
|
||||
using System;
|
||||
|
||||
/* <summary>
|
||||
SUB - subtracting TSeries-TSeries, or TSeries-double, or double-TSeries
|
||||
|
||||
</summary> */
|
||||
|
||||
|
||||
public class SUB_Series : Pair_TSeries_Indicator
|
||||
{
|
||||
public SUB_Series(TSeries d1, TSeries d2 ) : base(d1, d2) {
|
||||
if (base._d1.Count > 0 && base._d2.Count > 0) { for (int i=0; i< base._d1.Count; i++) { this.Add(base._d1[i], base._d2[i], false); } }
|
||||
}
|
||||
public SUB_Series(TSeries d1, double dd2 ) : base(d1, dd2) {
|
||||
if (base._d1.Count > 0) { for (int i=0; i< base._d1.Count; i++) { this.Add(base._d1[i], (base._d1[i].t, dd2), false); } }
|
||||
}
|
||||
public SUB_Series(double dd1, TSeries d2 ) : base(dd1, d2) {
|
||||
if (base._d2.Count > 0) { for (int i=0; i< base._d2.Count; i++) { this.Add((base._d2[i].t, dd1), base._d2[i], false); } }
|
||||
}
|
||||
|
||||
public override void Add((System.DateTime t, double v)TValue1, (System.DateTime t, double v)TValue2, bool update)
|
||||
{
|
||||
(System.DateTime t, double v) result = ((TValue1.t > TValue2.t) ? TValue1.t : TValue2.t,
|
||||
TValue1.v-TValue2.v);
|
||||
if (update) { base[base.Count - 1] = result; } else { base.Add(result); }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
namespace QuanTAlib;
|
||||
using System;
|
||||
|
||||
/* <summary>
|
||||
SUM: Cumulative Sum (aka Running Total)
|
||||
SUM across a period provides a rolling sum of all values across the period.
|
||||
If SUM values would be divided with period, the output would be SMA()
|
||||
|
||||
Sources:
|
||||
https://en.wikipedia.org/wiki/CUSUM
|
||||
|
||||
</summary> */
|
||||
|
||||
public class SUM_Series : Single_TSeries_Indicator
|
||||
{
|
||||
public SUM_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();
|
||||
|
||||
public override void Add((System.DateTime t, double v) TValue, bool update)
|
||||
{
|
||||
if (update) { _buffer[_buffer.Count - 1] = TValue.v; }
|
||||
else { _buffer.Add(TValue.v); }
|
||||
if (_buffer.Count > this._p && this._p != 0) { _buffer.RemoveAt(0); }
|
||||
|
||||
double _sum = 0;
|
||||
for (int i = 0; i < _buffer.Count; i++) { _sum += _buffer[i]; }
|
||||
|
||||
var result = (TValue.t, (this.Count < this._p - 1 && this._NaN) ? double.NaN : _sum);
|
||||
|
||||
base.Add(result, update);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,67 @@
|
||||
namespace QuanTAlib;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
/* <summary>
|
||||
Abstract classes with all scaffolding required to build indicators.
|
||||
All abstracts support period, NaN, and all permutations of Add() methods.
|
||||
Indicator classess need to implement:
|
||||
- Chaining constructor (Abstract's constructor executes first)
|
||||
- Default Add(value) class
|
||||
- optional Add(series) bulk insert class (for optimization of historical analysis)
|
||||
|
||||
Single_TSeries_Indicator - one single-value TSeries in, one TSeries out.
|
||||
Pair_TSeries_Indicator - Two TSeries in, one TSeries out. (includes simple semaphoring)
|
||||
Single_TBars_Indicator - One OHLCV TBars in, one TSeries out.
|
||||
|
||||
</summary> */
|
||||
|
||||
public abstract class Single_TBars_Indicator : TSeries
|
||||
{
|
||||
protected readonly int _p;
|
||||
protected readonly bool _NaN;
|
||||
protected readonly TBars _bars;
|
||||
|
||||
// Chainable Constructor - add it at the end of primary constructor :base(source: source, period: period, useNaN: useNaN)
|
||||
protected Single_TBars_Indicator(TBars source, int period, bool useNaN)
|
||||
{
|
||||
this._p = period;
|
||||
this._bars = source;
|
||||
this._NaN = useNaN;
|
||||
this._bars.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 o, double h, double l, double c, double v) TBar, bool update) => base.Add((TBar.t, 0.0), update);
|
||||
public virtual void Add((System.DateTime t, double v) TValue, bool update, bool useNaN)
|
||||
{
|
||||
var res = (TValue.t, this.Count < this._p - 1 && this._NaN ? double.NaN : TValue.v);
|
||||
base.Add(res, update);
|
||||
}
|
||||
|
||||
// potentially overridable Add() method for the whole bars or series (could be replaced with faster bulk algo)
|
||||
public virtual void Add(TBars bars) { for (int i = 0; i < bars.Count; i++) { this.Add(TBar: bars[i], update: false); } }
|
||||
public virtual void Add(TSeries data) { for (int i = 0; i < data.Count; i++) { base.Add(TValue: data[i], update: false); } }
|
||||
public void Add((System.DateTime t, double o, double h, double l, double c, double v) TBar) => this.Add(TBar: TBar, update: false);
|
||||
public void Add(bool update) => this.Add(TBar: this._bars[this._bars.Count - 1], update: update);
|
||||
public void Add() => this.Add(TBar: this._bars[this._bars.Count - 1], update: false);
|
||||
public new void Sub(object source, TSeriesEventArgs e) => this.Add(TBar: this._bars[this._bars.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); }
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,70 @@
|
||||
namespace QuanTAlib;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
||||
/* <summary>
|
||||
Abstract classes with all scaffolding required to build indicators.
|
||||
All abstracts support period, NaN, and all permutations of Add() methods.
|
||||
Indicator classess need to implement:
|
||||
- Chaining constructor (Abstract's constructor executes first)
|
||||
- Default Add(value) class
|
||||
- optional Add(series) bulk insert class (for optimization of historical analysis)
|
||||
|
||||
Single_TSeries_Indicator - one single-value TSeries in, one TSeries out.
|
||||
Pair_TSeries_Indicator - Two TSeries in, one TSeries out. (includes simple semaphoring)
|
||||
Single_TBars_Indicator - One OHLCV TBars in, one TSeries out.
|
||||
|
||||
</summary> */
|
||||
public abstract class Single_TSeries_Indicator : TSeries
|
||||
{
|
||||
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) {
|
||||
_data = source;
|
||||
_period = period;
|
||||
_p = _period;
|
||||
_NaN = useNaN;
|
||||
_data.Pub += Sub;
|
||||
}
|
||||
|
||||
// overridable Add() method to add/update a single item at the end of the list
|
||||
|
||||
public virtual void Add((DateTime t, double v) TValue, bool update, bool useNaN) {
|
||||
if (_period == 0) { _p = Length; }
|
||||
var res = (TValue.t, Count < _p - 1 && _NaN ? double.NaN : TValue.v);
|
||||
base.Add(res, update);
|
||||
}
|
||||
public new virtual void Add((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) {
|
||||
foreach (var item in data) { Add(TValue: item, 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)
|
||||
{
|
||||
l.RemoveAt(0);
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,136 @@
|
||||
namespace QuanTAlib;
|
||||
using System;
|
||||
|
||||
/* <summary>
|
||||
TBars class - includes all series for common data used in indicators and other calculations.
|
||||
Has a bit limited overloading and casting (compared to TSeries)
|
||||
Includes Select(int) method to simplify choosing the most optimal data source for indicators
|
||||
Includes the most basic pricing calcs: HL2, OC2, OHL3, HLC3, OHLC4, HLCC4
|
||||
(it is 'cheaper' to calculate them once during data capture than each time during data analysis)
|
||||
|
||||
</summary> */
|
||||
|
||||
public class TBars : System.Collections.Generic.List<(DateTime t, double o, double h, double l, double c, double v)>
|
||||
{
|
||||
private readonly TSeries _open = new();
|
||||
private readonly TSeries _high = new();
|
||||
private readonly TSeries _low = new();
|
||||
private readonly TSeries _close = new();
|
||||
private readonly TSeries _volume = new();
|
||||
private readonly TSeries _hl2 = new();
|
||||
private readonly TSeries _oc2 = new();
|
||||
private readonly TSeries _ohl3 = new();
|
||||
private readonly TSeries _hlc3 = new();
|
||||
private readonly TSeries _ohlc4 = new();
|
||||
private readonly TSeries _hlcc4 = new();
|
||||
|
||||
public TSeries Open => this._open;
|
||||
public TSeries High => this._high;
|
||||
public TSeries Low => this._low;
|
||||
public TSeries Close => this._close;
|
||||
public TSeries Volume => this._volume;
|
||||
public TSeries HL2 => this._hl2;
|
||||
public TSeries OC2 => this._oc2;
|
||||
public TSeries OHL3 => this._ohl3;
|
||||
public TSeries HLC3 => this._hlc3;
|
||||
public TSeries OHLC4 => this._ohlc4;
|
||||
public TSeries HLCC4 => this._hlcc4;
|
||||
|
||||
public TBars Tail(int count = 10)
|
||||
{
|
||||
TBars outBars = new();
|
||||
if (count > this.Count) { count = this.Count; }
|
||||
for (int i = this.Count - count; i < this.Count; i++) { outBars.Add(this[i]); }
|
||||
return outBars;
|
||||
}
|
||||
public TSeries Select(int source)
|
||||
{
|
||||
return source switch
|
||||
{
|
||||
0 => _open,
|
||||
1 => _high,
|
||||
2 => _low,
|
||||
3 => _close,
|
||||
4 => _hl2,
|
||||
5 => _oc2,
|
||||
6 => _ohl3,
|
||||
7 => _hlc3,
|
||||
8 => _ohlc4,
|
||||
_ => _hlcc4,
|
||||
};
|
||||
}
|
||||
public static string SelectStr(int source)
|
||||
{
|
||||
return source switch
|
||||
{
|
||||
0 => "Open",
|
||||
1 => "High",
|
||||
2 => "Low",
|
||||
3 => "Close",
|
||||
4 => "HL2",
|
||||
5 => "OC2",
|
||||
6 => "OHL3",
|
||||
7 => "Typical",
|
||||
8 => "Mean",
|
||||
_ => "Weighted",
|
||||
};
|
||||
}
|
||||
|
||||
public void Add((DateTime t, double o, double h, double l, double c, double v) i, bool update = false)
|
||||
=> Add(i.t, i.o, i.h, i.l, i.c, i.v, update);
|
||||
|
||||
public void Add(DateTime t, decimal o, decimal h, decimal l, decimal c, decimal v, bool update = false)
|
||||
=> Add(t, (double)o, (double)h, (double)l, (double)c, (double)v, update);
|
||||
|
||||
public void Add(DateTime t, double o, double h, double l, double c, double v, bool update = false)
|
||||
{
|
||||
if (update) {
|
||||
this[this.Count - 1] = (t, o, h, l, c, v);
|
||||
}
|
||||
else {
|
||||
base.Add((t, o, h, l, c, v));
|
||||
}
|
||||
_open.Add((t, o),update);
|
||||
_high.Add((t, h), update);
|
||||
_low.Add((t, l), update);
|
||||
_close.Add((t, c), update);
|
||||
_volume.Add((t, v), update);
|
||||
_hl2.Add((t, (h + l) * 0.5), update);
|
||||
_oc2.Add((t, (o + c) * 0.5), update);
|
||||
_ohl3.Add((t, (o + h + l) * 0.333333333333333), update);
|
||||
_hlc3.Add((t, (h + l + c) * 0.333333333333333), update);
|
||||
_ohlc4.Add((t, (o + h + l + c) * 0.25), update);
|
||||
_hlcc4.Add((t, (h + l + c + c) * 0.25), update);
|
||||
|
||||
this.OnEvent(update);
|
||||
}
|
||||
|
||||
// delegate used by event handler + event handler (Pub == publisher)
|
||||
public delegate void NewDataEventHandler(object source, TSeriesEventArgs args);
|
||||
public event NewDataEventHandler Pub;
|
||||
|
||||
// Broadcast handler - only to valid targets
|
||||
protected virtual void OnEvent(bool update = false)
|
||||
{
|
||||
if (Pub != null && Pub.Target != this)
|
||||
{
|
||||
Pub(this, new TSeriesEventArgs { update = update });
|
||||
}
|
||||
}
|
||||
|
||||
public void Sub(object source, TSeriesEventArgs e)
|
||||
{
|
||||
TBars ss = (TBars)source;
|
||||
if (ss.Count > 1)
|
||||
{
|
||||
for (int i = 0; i < ss.Count; i++)
|
||||
{
|
||||
this.Add(ss[i]);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
this.Add(ss[ss.Count - 1], e.update);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
namespace QuanTAlib;
|
||||
using System;
|
||||
|
||||
/* <summary>
|
||||
TR: True Range
|
||||
True Range was introduced by J. Welles Wilder in his book New Concepts in Technical Trading Systems.
|
||||
It measures the daily range plus any gap from the closing price of the preceding day.
|
||||
|
||||
Calculation:
|
||||
d1 = ABS(High - Low)
|
||||
d2 = ABS(High - Previous close)
|
||||
d3 = ABS(Previous close - Low)
|
||||
TR = MAX(d1,d2,d3)
|
||||
|
||||
Sources:
|
||||
https://www.macroption.com/true-range/
|
||||
|
||||
</summary> */
|
||||
|
||||
public class TR_Series : Single_TBars_Indicator
|
||||
{
|
||||
private double _cm1, _cm1_o;
|
||||
public TR_Series(TBars source, bool useNaN = false) : base(source, period:0, useNaN:useNaN) {
|
||||
_cm1 =_cm1_o = double.NaN;
|
||||
if (this._bars.Count > 0) { base.Add(this._bars); }
|
||||
}
|
||||
|
||||
public override void Add((DateTime t, double o, double h, double l, double c, double v) TBar, bool update)
|
||||
{
|
||||
if (update) {_cm1 = _cm1_o; } else { _cm1_o = _cm1; }
|
||||
if (_cm1 is double.NaN) { _cm1 = TBar.c; } //first bar
|
||||
|
||||
double d1 = Math.Abs(TBar.h - TBar.l);
|
||||
double d2 = Math.Abs(_cm1 - TBar.h);
|
||||
double d3 = Math.Abs(_cm1 - TBar.l);
|
||||
var ret = (TBar.t, (base.Count==0 && base._NaN) ? double.NaN : Math.Max(d1,Math.Max(d2,d3)) );
|
||||
base.Add(ret, update);
|
||||
_cm1 = TBar.c;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
namespace QuanTAlib;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
||||
/* <summary>
|
||||
TSeries is the cornerstone of all QuanTAlib classess.
|
||||
TSeries is a single List of tuples (time, value) and contains several operators, casts, overloads
|
||||
and other helpers that simplify usage of library.
|
||||
Think of TSeries as an equivalent of Numpy array.
|
||||
|
||||
- includes Length property (to mimic array's method)
|
||||
- includes publishing and subscribing methods that attach to events
|
||||
|
||||
</summary> */
|
||||
public class TSeries : List<(DateTime t, double v)> {
|
||||
|
||||
public static implicit operator (DateTime t, double v)(TSeries l) => l[^1];
|
||||
public static implicit operator double(TSeries l) => l[^1].v;
|
||||
public static implicit operator DateTime(TSeries l) => l[^1].t;
|
||||
public List<DateTime> t => this.Select(item => item.t).ToList();
|
||||
public List<double> v => this.Select(item => item.v).ToList();
|
||||
public int Length => this.Count;
|
||||
|
||||
public TSeries Tail(int count = 10) {
|
||||
var tailSeries = new TSeries();
|
||||
tailSeries.AddRange(this.Skip(Math.Max(0, this.Count - count)).Take(count));
|
||||
return tailSeries;
|
||||
}
|
||||
public void Add((DateTime t, double v) TValue, bool update = false) {
|
||||
if (update) { this[^1] = TValue; }
|
||||
else { base.Add(TValue); }
|
||||
OnEvent(update);
|
||||
}
|
||||
|
||||
public void Add(DateTime t, double v, bool update = false) => this.Add((t, v), update);
|
||||
public void Add(double v, bool update = false) => this.Add((DateTime.Now, v), update);
|
||||
protected virtual void OnEvent(bool update = false) {
|
||||
Pub?.Invoke(this, new TSeriesEventArgs { update = update }); }
|
||||
|
||||
public delegate void NewDataEventHandler(object source, TSeriesEventArgs args);
|
||||
public event NewDataEventHandler Pub;
|
||||
|
||||
public void Sub(object source, TSeriesEventArgs e) {
|
||||
TSeries ss = (TSeries)source;
|
||||
if (ss.Count > 0) {
|
||||
this.AddRange(ss);
|
||||
} else {
|
||||
Add(ss[^1], e.update);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public class TSeriesEventArgs : EventArgs{
|
||||
public bool update { get; set; }
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
namespace QuanTAlib;
|
||||
using System;
|
||||
|
||||
/* <summary>
|
||||
ZL: Zero Lag
|
||||
Data is de-lagged by removing the data from “lag” days ago, thus removing
|
||||
(or attempting to) the cumulative effect of the moving average.
|
||||
|
||||
Calculation:
|
||||
Lag = (Period-1)/2
|
||||
ZL = Data + (Data - Data(Lag days ago) )
|
||||
|
||||
Sources:
|
||||
https://mudrex.com/blog/zero-lag-ema-trading-strategy/
|
||||
|
||||
</summary> */
|
||||
|
||||
public class ZL_Series : Single_TSeries_Indicator
|
||||
{
|
||||
public ZL_Series(TSeries source, int period, bool useNaN = false) : base(source, period:period, useNaN:useNaN) {
|
||||
if (this._data.Count > 0) { base.Add(this._data); }
|
||||
}
|
||||
|
||||
public override void Add((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);
|
||||
|
||||
var ret = (TValue.t, (base.Count==0 && base._NaN) ? double.NaN : _zl );
|
||||
base.Add(ret, update);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user