From 09a014f6ed8ad1bdbabe436a99944784a9d360c6 Mon Sep 17 00:00:00 2001 From: Miha Kralj Date: Sun, 6 Nov 2022 17:23:33 -0800 Subject: [PATCH] Revert "for dev branch" This reverts commit 32615af18bf5ad9412c8c2448dc573f04beba68c. --- Quantower/Indicators/PSDEV_chart.cs | 2 +- Quantower/Indicators/SDEV_chart.cs | 2 +- Source/Feeds/Alphavantage_Feed.cs | 2 +- Source/Statistics/PSDEV_Series.cs | 44 ----------------------------- Source/Statistics/SDEV_Series.cs | 32 ++++++++++----------- Source/Statistics/SSDEV_Series.cs | 44 +++++++++++++++++++++++++++++ Tests/Statistics/PSDEV_Test.cs | 4 +-- Tests/Statistics/SDEV_Test .cs | 4 +-- Tests/Validations/TA_LIB.cs | 11 +++++++- 9 files changed, 77 insertions(+), 68 deletions(-) delete mode 100644 Source/Statistics/PSDEV_Series.cs create mode 100644 Source/Statistics/SSDEV_Series.cs diff --git a/Quantower/Indicators/PSDEV_chart.cs b/Quantower/Indicators/PSDEV_chart.cs index 91b45426..d3cc5bec 100644 --- a/Quantower/Indicators/PSDEV_chart.cs +++ b/Quantower/Indicators/PSDEV_chart.cs @@ -19,7 +19,7 @@ public class PSDEV_chart : Indicator private TBars bars; ///////dotnet - private PSDEV_Series indicator; + private SDEV_Series indicator; /////// public PSDEV_chart() diff --git a/Quantower/Indicators/SDEV_chart.cs b/Quantower/Indicators/SDEV_chart.cs index d920c6fa..55e267f2 100644 --- a/Quantower/Indicators/SDEV_chart.cs +++ b/Quantower/Indicators/SDEV_chart.cs @@ -19,7 +19,7 @@ public class SDEV_chart : Indicator private TBars bars; ///////dotnet - private SDEV_Series indicator; + private SSDEV_Series indicator; /////// public SDEV_chart() diff --git a/Source/Feeds/Alphavantage_Feed.cs b/Source/Feeds/Alphavantage_Feed.cs index 046b9fa3..deb18696 100644 --- a/Source/Feeds/Alphavantage_Feed.cs +++ b/Source/Feeds/Alphavantage_Feed.cs @@ -113,7 +113,7 @@ public class Alphavantage_Feed : TBars { Interval.Month => "_MONTHLY", Interval.Week => "_WEEKLY", - Interval.Day => "_DAILY", + Interval.Day => "_DAILY_ADJUSTED", Interval.Hour => "_INTRADAY&interval=60min", Interval.Min30 => "_INTRADAY&interval=30min", Interval.Min15 => "_INTRADAY&interval=15min", diff --git a/Source/Statistics/PSDEV_Series.cs b/Source/Statistics/PSDEV_Series.cs deleted file mode 100644 index 454c9231..00000000 --- a/Source/Statistics/PSDEV_Series.cs +++ /dev/null @@ -1,44 +0,0 @@ -namespace QuanTAlib; -using System; - -/* -PSDEV: Population Standard Deviation - Population Standard Deviation is the square root of the biased variance, also knons as - Uncorrected Sample Standard Deviation - -Sources: - https://en.wikipedia.org/wiki/Standard_deviation#Uncorrected_sample_standard_deviation - -Remark: - PSDEV (Population Standard Deviation) is also known as a biased/uncorrected Standard Deviation. - For unbiased version that uses Bessel's correction, use SDEV instead. - - */ - -public class PSDEV_Series : Single_TSeries_Indicator -{ - public PSDEV_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 _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 _sma = 0; - for (int i = 0; i < _buffer.Count; i++) { _sma += _buffer[i]; } - _sma /= this._buffer.Count; - - double _pvar = 0; - for (int i = 0; i < _buffer.Count; i++) { _pvar += (_buffer[i] - _sma) * (_buffer[i] - _sma); } - _pvar /= this._buffer.Count; - double _psdev = Math.Sqrt(_pvar); - - var result = (TValue.t, (this.Count < this._p - 1 && this._NaN) ? double.NaN : _psdev); - base.Add(result, update); - } -} \ No newline at end of file diff --git a/Source/Statistics/SDEV_Series.cs b/Source/Statistics/SDEV_Series.cs index e58ff2dd..0a0675e6 100644 --- a/Source/Statistics/SDEV_Series.cs +++ b/Source/Statistics/SDEV_Series.cs @@ -2,17 +2,17 @@ using System; /* -SDEV: (Corrected) Sample Standard Deviation - Sample Standard Deviaton uses Bessel's correction to correct the bias in the variance. +SDEV: Population Standard Deviation + Population Standard Deviation is the square root of the biased variance, also knons as + Uncorrected Sample Standard Deviation Sources: - https://en.wikipedia.org/wiki/Standard_deviation#Corrected_sample_standard_deviation - Bessel's correction: https://en.wikipedia.org/wiki/Bessel%27s_correction + https://en.wikipedia.org/wiki/Standard_deviation#Uncorrected_sample_standard_deviation Remark: - SSDEV (Sample Standard Deviation) is also known as a unbiased/corrected Standard Deviation. - For a population/biased/uncorrected Standard Deviation, use PSDEV instead - + SDEV (Population Standard Deviation) is also known as a biased/uncorrected Standard Deviation. + For unbiased version that uses Bessel's correction, use SDEV instead. + */ public class SDEV_Series : Single_TSeries_Indicator @@ -25,20 +25,20 @@ public class SDEV_Series : Single_TSeries_Indicator public override void Add((System.DateTime t, double v) TValue, bool update) { - if (update) { this._buffer[this._buffer.Count - 1] = TValue.v; } - else { this._buffer.Add(TValue.v); } - if (this._buffer.Count > this._p && this._p != 0) { this._buffer.RemoveAt(0); } + 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 _sma = 0; - for (int i = 0; i < this._buffer.Count; i++) { _sma += this._buffer[i]; } + for (int i = 0; i < _buffer.Count; i++) { _sma += _buffer[i]; } _sma /= this._buffer.Count; - double _svar = 0; - for (int i = 0; i < this._buffer.Count; i++) { _svar += (this._buffer[i] - _sma) * (this._buffer[i] - _sma); } - _svar /= (this._buffer.Count > 1) ? this._buffer.Count - 1 : 1; // Bessel's correction - double _ssdev = Math.Sqrt(_svar); + double _pvar = 0; + for (int i = 0; i < _buffer.Count; i++) { _pvar += (_buffer[i] - _sma) * (_buffer[i] - _sma); } + _pvar /= this._buffer.Count; + double _psdev = Math.Sqrt(_pvar); - var result = (TValue.t, (this.Count < this._p - 1 && this._NaN) ? double.NaN : _ssdev); + var result = (TValue.t, (this.Count < this._p - 1 && this._NaN) ? double.NaN : _psdev); base.Add(result, update); } } \ No newline at end of file diff --git a/Source/Statistics/SSDEV_Series.cs b/Source/Statistics/SSDEV_Series.cs new file mode 100644 index 00000000..376ffa79 --- /dev/null +++ b/Source/Statistics/SSDEV_Series.cs @@ -0,0 +1,44 @@ +namespace QuanTAlib; +using System; + +/* +SSDEV: (Corrected) Sample Standard Deviation + Sample Standard Deviaton uses Bessel's correction to correct the bias in the variance. + +Sources: + https://en.wikipedia.org/wiki/Standard_deviation#Corrected_sample_standard_deviation + Bessel's correction: https://en.wikipedia.org/wiki/Bessel%27s_correction + +Remark: + SSDEV (Sample Standard Deviation) is also known as a unbiased/corrected Standard Deviation. + For a population/biased/uncorrected Standard Deviation, use PSDEV instead + + */ + +public class SSDEV_Series : Single_TSeries_Indicator +{ + public SSDEV_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 _buffer = new(); + + public override void Add((System.DateTime t, double v) TValue, bool update) + { + if (update) { this._buffer[this._buffer.Count - 1] = TValue.v; } + else { this._buffer.Add(TValue.v); } + if (this._buffer.Count > this._p && this._p != 0) { this._buffer.RemoveAt(0); } + + double _sma = 0; + for (int i = 0; i < this._buffer.Count; i++) { _sma += this._buffer[i]; } + _sma /= this._buffer.Count; + + double _svar = 0; + for (int i = 0; i < this._buffer.Count; i++) { _svar += (this._buffer[i] - _sma) * (this._buffer[i] - _sma); } + _svar /= (this._buffer.Count > 1) ? this._buffer.Count - 1 : 1; // Bessel's correction + double _ssdev = Math.Sqrt(_svar); + + var result = (TValue.t, (this.Count < this._p - 1 && this._NaN) ? double.NaN : _ssdev); + base.Add(result, update); + } +} \ No newline at end of file diff --git a/Tests/Statistics/PSDEV_Test.cs b/Tests/Statistics/PSDEV_Test.cs index 29afa83a..503f9a6d 100644 --- a/Tests/Statistics/PSDEV_Test.cs +++ b/Tests/Statistics/PSDEV_Test.cs @@ -9,7 +9,7 @@ public class PSDEV_Test public void Add_Test() { TSeries a = new() { 0, 1, 2, 3, 4, 5 }; - PSDEV_Series c = new(a, 3); + SDEV_Series c = new(a, 3); Assert.Equal(6, c.Count); a.Add(5); Assert.Equal(a.Count, c.Count); @@ -21,7 +21,7 @@ public class PSDEV_Test public void Edge_Test() { TSeries a = new() { double.NaN, double.Epsilon, double.PositiveInfinity, double.MaxValue }; - PSDEV_Series c = new(a, 3); + SDEV_Series c = new(a, 3); Assert.Equal(a.Count, c.Count); a.Add(double.NaN); Assert.Equal(a.Count, c.Count); diff --git a/Tests/Statistics/SDEV_Test .cs b/Tests/Statistics/SDEV_Test .cs index c1f02a34..9b50f719 100644 --- a/Tests/Statistics/SDEV_Test .cs +++ b/Tests/Statistics/SDEV_Test .cs @@ -9,7 +9,7 @@ public class SDEV_Test public void Add_Test() { TSeries a = new() { 0, 1, 2, 3, 4, 5 }; - SDEV_Series c = new(a, 3); + SSDEV_Series c = new(a, 3); Assert.Equal(6, c.Count); a.Add(5); Assert.Equal(a.Count, c.Count); @@ -21,7 +21,7 @@ public class SDEV_Test public void Edge_Test() { TSeries a = new() { double.NaN, double.Epsilon, double.PositiveInfinity, double.MaxValue }; - SDEV_Series c = new(a, 3); + SSDEV_Series c = new(a, 3); Assert.Equal(a.Count, c.Count); a.Add(double.NaN); Assert.Equal(a.Count, c.Count); diff --git a/Tests/Validations/TA_LIB.cs b/Tests/Validations/TA_LIB.cs index ebfc1a68..0442d662 100644 --- a/Tests/Validations/TA_LIB.cs +++ b/Tests/Validations/TA_LIB.cs @@ -28,7 +28,16 @@ public class TA_LIB this.involume = this.bars.Volume.v.ToArray(); } -///////////////////////////////////////// + ///////////////////////////////////////// + + [Fact] + public void SDEV() + { + SDEV_Series QL = new(this.bars.Close, this.period, false); + Core.StdDev(this.inclose, 0, this.bars.Count - 1, this.TALIB, out int outBegIdx, out _, this.period); + + Assert.Equal(Math.Round(this.TALIB[this.TALIB.Length - outBegIdx - 1], 8), Math.Round(QL.Last().v, 8)); + } [Fact] public void SMA()