mirror of
https://github.com/mihakralj/QuanTAlib.git
synced 2026-08-14 00:28:05 +00:00
Fixed ATR and ATRP, improved SMA
This commit is contained in:
@@ -2,9 +2,9 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
<PropertyGroup>
|
||||
<Title>QuanTAlib</Title>
|
||||
<Version>0.1.24</Version>
|
||||
<Version>0.1.25</Version>
|
||||
<Product>Library of Technical Indicators for .NET</Product>
|
||||
<Description>Quantitative Technical Analysis library for both real-time (streaming) and historical data analysis</Description>
|
||||
<Description>Quantitative Technical Analysis library for real-time (streaming) data analysis</Description>
|
||||
<RepositoryType>git</RepositoryType>
|
||||
<RepositoryUrl>https://github.com/mihakralj/QuanTAlib</RepositoryUrl>
|
||||
<PublishRepositoryUrl>true</PublishRepositoryUrl>
|
||||
|
||||
@@ -16,6 +16,30 @@ Remark:
|
||||
|
||||
</summary> */
|
||||
|
||||
public class SMA_Series : Single_TSeries_Indicator {
|
||||
private double _sum, _oldsum;
|
||||
private int _len, _oldlen;
|
||||
|
||||
public SMA_Series(TSeries source, int period = 0, bool useNaN = false) : base(source, period, false) {
|
||||
Reset();
|
||||
if (this._data.Count > 0) { base.Add(this._data); }
|
||||
}
|
||||
|
||||
public override void Add((DateTime t, double v) TValue, bool update) {
|
||||
if (update) { _sum = _oldsum; }
|
||||
else { _oldsum = _sum; _len++; }
|
||||
_sum += TValue.v;
|
||||
if (_period != 0 && _len > _period)
|
||||
_sum -= (_data[base.Count - _period - (update ? 1 : 0)].v);
|
||||
double _div = (_period == 0) ? _len : Math.Min(_len, _period);
|
||||
base.Add((TValue.t, _sum / _div), update, _NaN);
|
||||
}
|
||||
public void Reset() {
|
||||
_sum = _oldsum = 0;
|
||||
_len = _oldlen = 0;
|
||||
}
|
||||
}
|
||||
/*
|
||||
public class SMA_Series : Single_TSeries_Indicator
|
||||
{
|
||||
private readonly System.Collections.Generic.List<double> _buffer = new();
|
||||
@@ -59,4 +83,4 @@ public class SMA_Series : Single_TSeries_Indicator
|
||||
|
||||
base.Add((TValue.t, _sma), update, _NaN);
|
||||
}
|
||||
}
|
||||
}*/
|
||||
|
||||
@@ -11,53 +11,39 @@ Sources:
|
||||
|
||||
</summary> */
|
||||
|
||||
public class ATRP_Series : Single_TBars_Indicator
|
||||
{
|
||||
private readonly System.Collections.Generic.List<double> _buffer = new();
|
||||
private readonly double _k, _k1m;
|
||||
private double _lastema, _lastlastema, _lastcm1;
|
||||
private double _cm1 = double.NaN;
|
||||
public class ATRP_Series : Single_TBars_Indicator {
|
||||
private readonly System.Collections.Generic.List<double> _buffer = new();
|
||||
private readonly double _k;
|
||||
private double _lastatr, _lastlastatr, _cm1, _lastcm1, _sum, _oldsum;
|
||||
private readonly int _period;
|
||||
|
||||
public ATRP_Series(TBars source, int period, bool useNaN = false) : base(source, period, useNaN)
|
||||
{
|
||||
this._k = 1.0 / (double)(this._p);
|
||||
this._k1m = 1.0 - this._k;
|
||||
this._lastema = this._lastlastema = double.NaN;
|
||||
if (_bars.Count > 0) { base.Add(_bars); }
|
||||
}
|
||||
public ATRP_Series(TBars source, int period, bool useNaN = false) : base(source, period, useNaN) {
|
||||
_period = period;
|
||||
_k = 1.0 / (double)(_p);
|
||||
_lastatr = _lastlastatr = _cm1 = _lastcm1 = _sum = _oldsum = 0;
|
||||
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) {
|
||||
this._lastema = this._lastlastema;
|
||||
this._cm1 = this._lastcm1;
|
||||
}
|
||||
public override void Add((DateTime t, double o, double h, double l, double c, double v) TBar, bool update) {
|
||||
if (update) { _lastatr = _lastlastatr; _cm1 = _lastcm1; _sum = _oldsum; }
|
||||
else { _lastlastatr = _lastatr; _lastcm1 = _cm1; _oldsum = _sum; }
|
||||
|
||||
if (_cm1 is double.NaN) { _cm1 = TBar.c; }
|
||||
double d1 = Math.Abs(TBar.h - TBar.l);
|
||||
double d2 = Math.Abs(_cm1 - TBar.h);
|
||||
double d3 = Math.Abs(_cm1 - TBar.l);
|
||||
(DateTime t, double v)d = (TBar.t, Math.Max(d1,Math.Max(d2,d3))); //TR value for RMA below
|
||||
_lastcm1 = _cm1;
|
||||
_cm1 = TBar.c;
|
||||
if (this.Count == 0)
|
||||
_cm1 = TBar.c;
|
||||
double d1 = Math.Abs(TBar.h - TBar.l);
|
||||
double d2 = Math.Abs(_cm1 - TBar.h);
|
||||
double d3 = Math.Abs(_cm1 - TBar.l);
|
||||
(DateTime t, double v) d = (TBar.t, Math.Max(d1, Math.Max(d2, d3)));
|
||||
_cm1 = TBar.c;
|
||||
|
||||
double _ema = 0;
|
||||
if (this.Count < this._p)
|
||||
{
|
||||
if (update) { _buffer[_buffer.Count - 1] = d.v; }
|
||||
else { _buffer.Add(d.v); }
|
||||
if (_buffer.Count > this._p) { _buffer.RemoveAt(0); }
|
||||
for (int i = 0; i < _buffer.Count; i++) { _ema += _buffer[i]; }
|
||||
_ema /= this._buffer.Count;
|
||||
}
|
||||
else { _ema = (d.v * _k) + (_lastema * _k1m); }
|
||||
double _atr = 0;
|
||||
if (this.Count == 0) { _atr = d.v; }
|
||||
else if (this.Count < _p + 1) { _sum += d.v; _atr = _sum / (this.Count); }
|
||||
else { _atr = _k * (d.v - _lastatr) + _lastatr; }
|
||||
_lastatr = _atr;
|
||||
|
||||
this._lastlastema = this._lastema;
|
||||
this._lastema = _ema;
|
||||
|
||||
double _atrp = 100 * (_ema / TBar.c);
|
||||
|
||||
var ret = (d.t, this.Count < this._p - 1 && this._NaN ? double.NaN : _atrp);
|
||||
base.Add(ret, update);
|
||||
}
|
||||
}
|
||||
double _atrp = 100 * (_atr / TBar.c);
|
||||
var ret = (d.t, this.Count < this._p - 1 && this._NaN ? double.NaN : _atrp);
|
||||
base.Add(ret, update);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -13,51 +13,38 @@ Sources:
|
||||
|
||||
</summary> */
|
||||
|
||||
public class ATR_Series : Single_TBars_Indicator
|
||||
{
|
||||
private readonly System.Collections.Generic.List<double> _buffer = new();
|
||||
private readonly double _k, _k1m;
|
||||
private double _lastema, _lastlastema, _lastcm1;
|
||||
private double _cm1;
|
||||
public class ATR_Series : Single_TBars_Indicator {
|
||||
private readonly System.Collections.Generic.List<double> _buffer = new();
|
||||
private readonly double _k;
|
||||
private double _lastatr, _lastlastatr, _cm1, _lastcm1, _sum, _oldsum;
|
||||
private readonly int _period;
|
||||
|
||||
public ATR_Series(TBars source, int period, bool useNaN = false) : base(source, period, useNaN)
|
||||
{
|
||||
this._k = 1.0 / (double)(this._p);
|
||||
this._k1m = 1.0 - this._k;
|
||||
this._lastema = this._lastlastema = double.NaN;
|
||||
if (this._bars.Count > 0) { base.Add(this._bars); }
|
||||
}
|
||||
public ATR_Series(TBars source, int period, bool useNaN = false) : base(source, period, useNaN) {
|
||||
_period = period;
|
||||
_k = 1.0 / (double)(_p);
|
||||
_lastatr = _lastlastatr = _cm1 = _lastcm1 = _sum = _oldsum = 0;
|
||||
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) {
|
||||
this._lastema = this._lastlastema;
|
||||
this._cm1 = this._lastcm1;
|
||||
}
|
||||
public override void Add((DateTime t, double o, double h, double l, double c, double v) TBar, bool update) {
|
||||
if (update) { _lastatr = _lastlastatr; _cm1 = _lastcm1; _sum = _oldsum; }
|
||||
else { _lastlastatr = _lastatr; _lastcm1 = _cm1; _oldsum = _sum; }
|
||||
|
||||
if (this.Count == 0) { this._cm1 = TBar.c; }
|
||||
double d1 = Math.Abs(TBar.h - TBar.l);
|
||||
double d2 = Math.Abs(_cm1 - TBar.h);
|
||||
double d3 = Math.Abs(_cm1 - TBar.l);
|
||||
(DateTime t, double v)d = (TBar.t, Math.Max(d1,Math.Max(d2,d3)));
|
||||
_lastcm1 = _cm1;
|
||||
_cm1 = TBar.c;
|
||||
if (this.Count == 0)
|
||||
_cm1 = TBar.c;
|
||||
double d1 = Math.Abs(TBar.h - TBar.l);
|
||||
double d2 = Math.Abs(_cm1 - TBar.h);
|
||||
double d3 = Math.Abs(_cm1 - TBar.l);
|
||||
(DateTime t, double v) d = (TBar.t, Math.Max(d1, Math.Max(d2, d3)));
|
||||
_cm1 = TBar.c;
|
||||
|
||||
double _ema = 0;
|
||||
if (this.Count < this._p)
|
||||
{
|
||||
if (update) { _buffer[_buffer.Count - 1] = d.v; }
|
||||
else { _buffer.Add(d.v); }
|
||||
if (_buffer.Count > this._p) { _buffer.RemoveAt(0); }
|
||||
for (int i = 0; i < _buffer.Count; i++) { _ema += _buffer[i]; }
|
||||
_ema /= this._buffer.Count;
|
||||
}
|
||||
else { _ema = (d.v * _k) + (_lastema * _k1m); }
|
||||
double _atr = 0;
|
||||
if (this.Count == 0) { _atr = d.v; }
|
||||
else if (this.Count < _p + 1) { _sum += d.v; _atr = _sum / (this.Count); }
|
||||
else { _atr = _k * (d.v - _lastatr) + _lastatr; }
|
||||
_lastatr = _atr;
|
||||
|
||||
this._lastlastema = this._lastema;
|
||||
this._lastema = _ema;
|
||||
|
||||
var ret = (d.t, this.Count < this._p - 1 && this._NaN ? double.NaN : _ema);
|
||||
base.Add(ret, update);
|
||||
}
|
||||
var ret = (d.t, this.Count < this._p - 1 && this._NaN ? double.NaN : _atr);
|
||||
base.Add(ret, update);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user