From 86d0adc1d46182a9256dc579611d816a18047072 Mon Sep 17 00:00:00 2001 From: Miha Kralj Date: Sun, 24 Apr 2022 20:15:11 -0700 Subject: [PATCH 01/11] Sonar changes --- Docs/coverage.md | 314 ++++++++++++++--------------- Quantower/Indicators/ZLMA_chart.cs | 8 +- Source/Indicators/JMA_Series.cs | 2 - Source/Indicators/ZLEMA_Series.cs | 140 ++++++------- Tests/Validations/Pandas_TA.cstemp | 238 +++++++++++----------- 5 files changed, 350 insertions(+), 352 deletions(-) diff --git a/Docs/coverage.md b/Docs/coverage.md index 7d8b9e97..665c170e 100644 --- a/Docs/coverage.md +++ b/Docs/coverage.md @@ -1,157 +1,157 @@ -# Coverage of indicators - -| Indicator | QuanTAlib | TA-LIB | Skender | Pandas-TA | -|--|:--:|:--:|:--:|:--:| -| **Basics** ||||| -| OC2 - (Open+Close)/2 |✔️|||✔️| -| HL2 - (High+Low)/2 |✔️|||✔️| -| HLC3 - Typical Price |✔️|||✔️| -| OHL3 - (Open+High+Low)/3 |✔️|||✔️| -| OHLC4 - (O+H+L+C)/4 |✔️|||✔️| -| HLCC4 - Weighted Price |✔️||✔️|✔️| -| ZL - Zero Lag - De-lagged price |✔️|||✔️| -| ADD - Addition |✔️|✔️||| -| SUB - Subtraction |✔️|✔️||| -| MUL - Multiplication |✔️|✔️||| -| DIV - Division |✔️|✔️||| -|||||| -| **Statistics** ||||| -| BETA - Beta coefficient |||✔️|| -| BIAS - Bias |✔️|||✔️| -| ENTR - Entropy |✔️|||✔️| -| KUR - Kurtosis |✔️|||✔️| -| LINREG - Linear Regression ||✔️|✔️|| -| MAD - Mean Absolute Deviation |✔️||✔️|✔️| -| MAPE - Mean Absolute Percent Error |✔️||✔️|| -| MAX - Max value |✔️|✔️||| -| MIN - Min value |✔️|✔️||| -| MED - Median value |✔️|✔️||✔️| -| MSE - Mean Squared Error |✔️||✔️|| -| PSDEV - Population Standard Deviation |✔️|||| -| PVAR - Population Variance |✔️|||| -| QUANTILE ||||✔️| -| RS - R-Squared Coefficient |||✔️|| -| SKEW - Skewness ||||✔️| -| SLOPE - Slope |||✔️|| -| SMAPE - Symmetric Mean Absolute Percent Error |✔️|||| -| SDEV - Sample Standard Deviation |✔️|✔️|✔️|✔️| -| VAR - Sample Variance |✔️|||✔️| -| WMAPE - Weighted Mean Absolute Percent Error |✔️|||| -| ZSCORE |||✔️|✔️| -|||||| -| **Moving Averages** ||||| -| AFIRMA - Autoregressive Finite Impulse Response Moving Average ||||| -| ALMA - Arnaud Legoux Moving Average |||✔️|✔️| -| ARIMA - Autoregressive Integrated Moving Average ||||| -| ATR - Average True Range |✔️|✔️|✔️|✔️| -| ATRP - Average True Range Percent |✔️||✔️|| -| DEMA - Double EMA |✔️|✔️|✔️|✔️| -| EMA - Exponential Moving Average |✔️|✔️|✔️|✔️| -| EPMA - Endpoint Moving Average |||✔️|| -| FWMA - Fibonacci's Weighted Moving Average ||||✔️| -| HEMA - Hull Exponential Moving Average |✔️|||| -| HMA - Hull Moving Average |✔️||✔️|✔️| -| HWMA - Holt-Winter Moving Average ||||✔️| -| JMA - Jurik Moving Average |✔️|||✔️| -| KAMA - Kaufman's Adaptive Moving Average |✔️|✔️|✔️|✔️| -| LSMA - Least Squares Moving Average |||✔️|| -| MACD - Moving Average Convergence/Divergence ||✔️|✔️|✔️| -| MAMA - MESA Adaptive Moving Average ||✔️|✔️|| -| MMA - Modified Moving Average |||✔️|| -| NATR - Normalized Average True Range ||✔️|✔️|✔️| -| PPMA - Pivot Point Moving Average |||✔️|| -| PWMA - Pascal's Weighted Moving Average ||||✔️| -| RMA - WildeR's Moving Average |✔️|||✔️| -| SINWMA - Sine Weighted Moving Average ||||✔️| -| SMA - Simple Moving Average |✔️|✔️|✔️|✔️| -| SMMA - Smoothed Moving Average |✔️||✔️|| -| STOCH - Stochastic Oscillator ||✔️|✔️|✔️| -| SSF - Ehler's Super Smoother Filter ||||✔️| -| SUP - Supertrend |||✔️|✔️| -| SWMA - Symmetric Weighted Moving Average ||||✔️| -| T3 - Tillson T3 Moving Average ||✔️|✔️|✔️| -| TEMA - Triple EMA |✔️|✔️|✔️|✔️| -| TRIMA - Triangular Moving Average ||✔️||✔️| -| VIDYA - Variable Index Dynamic Average ||||✔️| -| VWAP - Volume Weighted Average Price |||✔️|✔️| -| VWMA - Volume Weighted Moving Average |||✔️|✔️| -| WMA - Weighted Moving Average |✔️|✔️|✔️|✔️| -| ZLEMA - Zero Lag EMA |✔️|||✔️| -|||||| -| **Oscillators and Indices** ||||| -| AC - Acceleration Oscillator ||||✔️| -| AD - Chaikin Accumulation Distribution ||✔️|✔️|✔️| -| ADOSC - Chaikin Accumulation Distribution Oscillator ||✔️|✔️|| -| ADX - Average Directional Movement Index ||✔️|✔️|✔️| -| ADXR - Average Directional Movement Index Rating ||✔️|✔️|| -| AO - Awesome Oscillator |||✔️|✔️| -| APO - Absolute Price Oscillator ||✔️||✔️| -| AROON - Aroon oscillator ||✔️|✔️|✔️| -| BBANDS - Bollinger Bands ||✔️|✔️|✔️| -| BOP - Balance of Power ||✔️|✔️|✔️| -| CCI - Commodity Channel Index ||✔️|✔️|✔️| -| CFO - Chande Forcast Oscillator ||||✔️| -| CMF - Chaikin Money Flow |||✔️|✔️| -| CMO - Chande Momentum Oscillator ||✔️||✔️| -| COG - Center of Gravity ||||✔️| -| CRSI - Connor RSI |||✔️|| -| CTI - Ehler's Correlation Trend Indicator ||||✔️| -| DMI - Directional Movement Index ||✔️|✔️|✔️| -| EFI - Elder Ray's Force Index |||✔️|✔️| -| GAT - Alligator oscillator |||✔️|| -| KRI - Kairi Relative Index ||||| -| KVO - Klinger Volume Oscillator |||✔️|✔️| -| MFI - Money Flow Index ||✔️|✔️|✔️| -| MOM - Momentum |||✔️|✔️| -| NVI - Negative Volume Index ||||✔️| -| PO - Price Oscillator ||||✔️| -| PPO - Percentage Price Oscillator ||✔️||✔️| -| PVI - Positive Volume Index ||||✔️| -| RSI - Relative Strength Index ||✔️|✔️|✔️| -| RVGI - Relative Vigor Index ||||✔️| -| SRSI - Stochastic RSI |||✔️|✔️| -| TRIX - 1-day ROC of TEMA ||✔️|✔️|✔️| -| TSI - True Strength Index |||✔️|✔️| -| UI - Ulcer Index |||✔️|✔️| -| UO - Ultimate Oscillator ||✔️|✔️|✔️| -| WGAT - Williams Alligator |||✔️|| -|||||| -| **Volume** ||||| -| AOBV - Archer On-Balance Volume ||||✔️| -| OBV - On-Balance Volume ||✔️|✔️|✔️| -| PRS - Price Relative Strength |||✔️|| -| PVOL - Price-Volume ||||| -| PVR - Price Volume Rank ||||✔️| -| PVT - Price Volume Trend ||||✔️| -| VP - Volume Profile ||||✔️| -|||||| -|**Unsorted**||||| -| CHN - Price Channel |||✔️|| -| COPPOCK - Coppock Curve ||||✔️| -| CORREL - Pearson's Correlation Coefficient ||✔️|✔️|| -| EOM - Ease of Movement ||||✔️| -| HILO - Gann High-Low Activator ||||✔️| -| HV - Historical Volatility |||✔️|| -| HT - HT Trendline |||✔️|| -| ICH - Ichimoku |||✔️|✔️| -| MCGD - McGinley Dynamic ||||✔️| -| ROC - Rate of Change ||✔️|✔️|✔️| -| SAR - Parabolic Stop and Reverse ||✔️|✔️|✔️| -| STC - Schaff Trend Cycle |||✔️|✔️| -| TR - True Range ||✔️|✔️|✔️| -| WILLR - Larry Williams' %R ||✔️|✔️|✔️| -| HURST - Hurst Exponent |||✔️|| -| VOR - Vortex Indicator |||✔️|✔️| -| DON - Donchian Channels |||✔️|✔️| -| FCB - Fractal Chaos Bands |||✔️|| -| KEL - Keltner Channels |||✔️|✔️| -| PVT - Pivot Points |||✔️|| -| STARC - Starc Bands |||✔️|| -| DPO - De-trended Price Oscillator |||✔️|✔️| -| KDJ - KDJ Index |||✔️|✔️| -| SMI - Stochastic Momentum Index |||✔️|✔️| -| CHAND - Chandelier Exit |||✔️|| -| VSTOP - Volatility Stop |||✔️|| -| PVO - Percentage Volume Oscillator |||✔️|✔️| -| Hilbert Transform Instantaneous Trendline ||||| -| PMO - Price Momentum Oscillator |||✔️|| +# Coverage of indicators + +| Indicator | QuanTAlib | TA-LIB | Skender | Pandas-TA | +|--|:--:|:--:|:--:|:--:| +| **Basics** ||||| +| OC2 - (Open+Close)/2 |✔️|||✔️| +| HL2 - (High+Low)/2 |✔️|||✔️| +| HLC3 - Typical Price |✔️|||✔️| +| OHL3 - (Open+High+Low)/3 |✔️|||✔️| +| OHLC4 - (O+H+L+C)/4 |✔️|||✔️| +| HLCC4 - Weighted Price |✔️||✔️|✔️| +| ZL - Zero Lag - De-lagged price |✔️|||✔️| +| ADD - Addition |✔️|✔️||| +| SUB - Subtraction |✔️|✔️||| +| MUL - Multiplication |✔️|✔️||| +| DIV - Division |✔️|✔️||| +|||||| +| **Statistics** ||||| +| BETA - Beta coefficient |||✔️|| +| BIAS - Bias |✔️|||✔️| +| ENTR - Entropy |✔️|||✔️| +| KUR - Kurtosis |✔️|||✔️| +| LINREG - Linear Regression ||✔️|✔️|| +| MAD - Mean Absolute Deviation |✔️||✔️|✔️| +| MAPE - Mean Absolute Percent Error |✔️||✔️|| +| MAX - Max value |✔️|✔️||| +| MIN - Min value |✔️|✔️||| +| MED - Median value |✔️|✔️||✔️| +| MSE - Mean Squared Error |✔️||✔️|| +| PSDEV - Population Standard Deviation |✔️|||| +| PVAR - Population Variance |✔️|||| +| QUANTILE ||||✔️| +| RS - R-Squared Coefficient |||✔️|| +| SKEW - Skewness ||||✔️| +| SLOPE - Slope |||✔️|| +| SMAPE - Symmetric Mean Absolute Percent Error |✔️|||| +| SDEV - Sample Standard Deviation |✔️|✔️|✔️|✔️| +| VAR - Sample Variance |✔️|||✔️| +| WMAPE - Weighted Mean Absolute Percent Error |✔️|||| +| ZSCORE |||✔️|✔️| +|||||| +| **Moving Averages** ||||| +| AFIRMA - Autoregressive Finite Impulse Response Moving Average ||||| +| ALMA - Arnaud Legoux Moving Average |||✔️|✔️| +| ARIMA - Autoregressive Integrated Moving Average ||||| +| ATR - Average True Range |✔️|✔️|✔️|✔️| +| ATRP - Average True Range Percent |✔️||✔️|| +| DEMA - Double EMA |✔️|✔️|✔️|✔️| +| EMA - Exponential Moving Average |✔️|✔️|✔️|✔️| +| EPMA - Endpoint Moving Average |||✔️|| +| FWMA - Fibonacci's Weighted Moving Average ||||✔️| +| HEMA - Hull Exponential Moving Average |✔️|||| +| HMA - Hull Moving Average |✔️||✔️|✔️| +| HWMA - Holt-Winter Moving Average ||||✔️| +| JMA - Jurik Moving Average |✔️|||✔️| +| KAMA - Kaufman's Adaptive Moving Average |✔️|✔️|✔️|✔️| +| LSMA - Least Squares Moving Average |||✔️|| +| MACD - Moving Average Convergence/Divergence ||✔️|✔️|✔️| +| MAMA - MESA Adaptive Moving Average ||✔️|✔️|| +| MMA - Modified Moving Average |||✔️|| +| NATR - Normalized Average True Range ||✔️|✔️|✔️| +| PPMA - Pivot Point Moving Average |||✔️|| +| PWMA - Pascal's Weighted Moving Average ||||✔️| +| RMA - WildeR's Moving Average |✔️|||✔️| +| SINWMA - Sine Weighted Moving Average ||||✔️| +| SMA - Simple Moving Average |✔️|✔️|✔️|✔️| +| SMMA - Smoothed Moving Average |✔️||✔️|| +| STOCH - Stochastic Oscillator ||✔️|✔️|✔️| +| SSF - Ehler's Super Smoother Filter ||||✔️| +| SUP - Supertrend |||✔️|✔️| +| SWMA - Symmetric Weighted Moving Average ||||✔️| +| T3 - Tillson T3 Moving Average ||✔️|✔️|✔️| +| TEMA - Triple EMA |✔️|✔️|✔️|✔️| +| TRIMA - Triangular Moving Average ||✔️||✔️| +| VIDYA - Variable Index Dynamic Average ||||✔️| +| VWAP - Volume Weighted Average Price |||✔️|✔️| +| VWMA - Volume Weighted Moving Average |||✔️|✔️| +| WMA - Weighted Moving Average |✔️|✔️|✔️|✔️| +| ZLEMA - Zero Lag EMA |✔️|||✔️| +|||||| +| **Oscillators and Indices** ||||| +| AC - Acceleration Oscillator ||||✔️| +| AD - Chaikin Accumulation Distribution ||✔️|✔️|✔️| +| ADOSC - Chaikin Accumulation Distribution Oscillator ||✔️|✔️|| +| ADX - Average Directional Movement Index ||✔️|✔️|✔️| +| ADXR - Average Directional Movement Index Rating ||✔️|✔️|| +| AO - Awesome Oscillator |||✔️|✔️| +| APO - Absolute Price Oscillator ||✔️||✔️| +| AROON - Aroon oscillator ||✔️|✔️|✔️| +| BBANDS - Bollinger Bands ||✔️|✔️|✔️| +| BOP - Balance of Power ||✔️|✔️|✔️| +| CCI - Commodity Channel Index ||✔️|✔️|✔️| +| CFO - Chande Forcast Oscillator ||||✔️| +| CMF - Chaikin Money Flow |||✔️|✔️| +| CMO - Chande Momentum Oscillator ||✔️||✔️| +| COG - Center of Gravity ||||✔️| +| CRSI - Connor RSI |||✔️|| +| CTI - Ehler's Correlation Trend Indicator ||||✔️| +| DMI - Directional Movement Index ||✔️|✔️|✔️| +| EFI - Elder Ray's Force Index |||✔️|✔️| +| GAT - Alligator oscillator |||✔️|| +| KRI - Kairi Relative Index ||||| +| KVO - Klinger Volume Oscillator |||✔️|✔️| +| MFI - Money Flow Index ||✔️|✔️|✔️| +| MOM - Momentum |||✔️|✔️| +| NVI - Negative Volume Index ||||✔️| +| PO - Price Oscillator ||||✔️| +| PPO - Percentage Price Oscillator ||✔️||✔️| +| PVI - Positive Volume Index ||||✔️| +| RSI - Relative Strength Index ||✔️|✔️|✔️| +| RVGI - Relative Vigor Index ||||✔️| +| SRSI - Stochastic RSI |||✔️|✔️| +| TRIX - 1-day ROC of TEMA ||✔️|✔️|✔️| +| TSI - True Strength Index |||✔️|✔️| +| UI - Ulcer Index |||✔️|✔️| +| UO - Ultimate Oscillator ||✔️|✔️|✔️| +| WGAT - Williams Alligator |||✔️|| +|||||| +| **Volume** ||||| +| AOBV - Archer On-Balance Volume ||||✔️| +| OBV - On-Balance Volume ||✔️|✔️|✔️| +| PRS - Price Relative Strength |||✔️|| +| PVOL - Price-Volume ||||| +| PVR - Price Volume Rank ||||✔️| +| PVT - Price Volume Trend ||||✔️| +| VP - Volume Profile ||||✔️| +|||||| +|**Unsorted**||||| +| CHN - Price Channel |||✔️|| +| COPPOCK - Coppock Curve ||||✔️| +| CORREL - Pearson's Correlation Coefficient ||✔️|✔️|| +| EOM - Ease of Movement ||||✔️| +| HILO - Gann High-Low Activator ||||✔️| +| HV - Historical Volatility |||✔️|| +| HT - HT Trendline |||✔️|| +| ICH - Ichimoku |||✔️|✔️| +| MCGD - McGinley Dynamic ||||✔️| +| ROC - Rate of Change ||✔️|✔️|✔️| +| SAR - Parabolic Stop and Reverse ||✔️|✔️|✔️| +| STC - Schaff Trend Cycle |||✔️|✔️| +| TR - True Range ||✔️|✔️|✔️| +| WILLR - Larry Williams' %R ||✔️|✔️|✔️| +| HURST - Hurst Exponent |||✔️|| +| VOR - Vortex Indicator |||✔️|✔️| +| DON - Donchian Channels |||✔️|✔️| +| FCB - Fractal Chaos Bands |||✔️|| +| KEL - Keltner Channels |||✔️|✔️| +| PVT - Pivot Points |||✔️|| +| STARC - Starc Bands |||✔️|| +| DPO - De-trended Price Oscillator |||✔️|✔️| +| KDJ - KDJ Index |||✔️|✔️| +| SMI - Stochastic Momentum Index |||✔️|✔️| +| CHAND - Chandelier Exit |||✔️|| +| VSTOP - Volatility Stop |||✔️|| +| PVO - Percentage Volume Oscillator |||✔️|✔️| +| Hilbert Transform Instantaneous Trendline ||||| +| PMO - Price Momentum Oscillator |||✔️|| diff --git a/Quantower/Indicators/ZLMA_chart.cs b/Quantower/Indicators/ZLMA_chart.cs index c8a23b28..61020e38 100644 --- a/Quantower/Indicators/ZLMA_chart.cs +++ b/Quantower/Indicators/ZLMA_chart.cs @@ -30,12 +30,12 @@ public class ZLMA_chart : Indicator private int matype = 2; #endregion Parameters - + private TBars bars; - /////// + /////// private TSeries indicator; - /////// - + /////// + public ZLMA_chart() { this.SeparateWindow = false; diff --git a/Source/Indicators/JMA_Series.cs b/Source/Indicators/JMA_Series.cs index 7359c4d4..dfe2bf9a 100644 --- a/Source/Indicators/JMA_Series.cs +++ b/Source/Indicators/JMA_Series.cs @@ -41,8 +41,6 @@ public class JMA_Series : Single_TSeries_Indicator this.rvolty = Math.Exp((1 / this.pow1) * Math.Log(len1)); this.len2 = Math.Sqrt(0.5 * (_p - 1)) * len1; this.beta = 0.45 * (_p - 1) / (0.45 * (_p - 1) + 2); - //this._l = (int)Math.Round(this._p - 1 * 0.5); - if (base._data.Count > 0) { base.Add(base._data); } } diff --git a/Source/Indicators/ZLEMA_Series.cs b/Source/Indicators/ZLEMA_Series.cs index 5642cf8f..be2913f7 100644 --- a/Source/Indicators/ZLEMA_Series.cs +++ b/Source/Indicators/ZLEMA_Series.cs @@ -1,71 +1,71 @@ -namespace QuanTAlib; -using System; - -/* -ZLEMA: Zero Lag Exponential Moving Average - The Zero lag exponential moving average (ZLEMA) indicator was created by John - Ehlers and Ric Way. - -The formula for a given N-Day period and for a given Data series is: - Lag = (Period-1)/2 - Ema Data = {Data+(Data-Data(Lag days ago)) - ZLEMA = EMA (EmaData,Period) - -Remark: - The idea is do a regular exponential moving average (EMA) calculation but on a - de-lagged data instead of doing it on the regular data. Data is de-lagged by - removing the data from "lag" days ago thus removing (or attempting to remove) - the cumulative lag effect of the moving average. - - */ - -public class ZLEMA_Series : Single_TSeries_Indicator -{ - private readonly System.Collections.Generic.List _buffer = new(); - private readonly double _k, _k1m; - private double _lastema, _lastlastema; - - public ZLEMA_Series(TSeries source, int period, bool useNaN = false) : base(source, period, useNaN) - { - this._k = 2.0 / (this._p + 1); - this._k1m = 1.0 - this._k; - this._lastema = this._lastlastema = double.NaN; - if (base._data.Count > 0) - { base.Add(base._data); } - } - - public override void Add((System.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); - double _ema = 0; - if (update) - { this._lastema = this._lastlastema; } - if (this.Count < this._p) - { - if (update) - { this._buffer[this._buffer.Count - 1] = _zl; } - else - { - this._buffer.Add(_zl); - } - if (this._buffer.Count > this._p) - { this._buffer.RemoveAt(0); } - - for (int i = 0; i < this._buffer.Count; i++) - { _ema += this._buffer[i]; } - _ema /= this._buffer.Count; - } - else - { - _ema = TValue.v * this._k + this._lastema * this._k1m; - } - - this._lastlastema = this._lastema; - this._lastema = _ema; - - var ret = (TValue.t, this.Count < this._p - 1 && this._NaN ? double.NaN : _ema); - base.Add(ret, update); - } +namespace QuanTAlib; +using System; + +/* +ZLEMA: Zero Lag Exponential Moving Average + The Zero lag exponential moving average (ZLEMA) indicator was created by John + Ehlers and Ric Way. + +The formula for a given N-Day period and for a given Data series is: + Lag = (Period-1)/2 + Ema Data = {Data+(Data-Data(Lag days ago)) + ZLEMA = EMA (EmaData,Period) + +Remark: + The idea is do a regular exponential moving average (EMA) calculation but on a + de-lagged data instead of doing it on the regular data. Data is de-lagged by + removing the data from "lag" days ago thus removing (or attempting to remove) + the cumulative lag effect of the moving average. + + */ + +public class ZLEMA_Series : Single_TSeries_Indicator +{ + private readonly System.Collections.Generic.List _buffer = new(); + private readonly double _k, _k1m; + private double _lastema, _lastlastema; + + public ZLEMA_Series(TSeries source, int period, bool useNaN = false) : base(source, period, useNaN) + { + this._k = 2.0 / (this._p + 1); + this._k1m = 1.0 - this._k; + this._lastema = this._lastlastema = double.NaN; + if (base._data.Count > 0) + { base.Add(base._data); } + } + + public override void Add((System.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); + double _ema = 0; + if (update) + { this._lastema = this._lastlastema; } + if (this.Count < this._p) + { + if (update) + { this._buffer[this._buffer.Count - 1] = _zl; } + else + { + this._buffer.Add(_zl); + } + if (this._buffer.Count > this._p) + { this._buffer.RemoveAt(0); } + + for (int i = 0; i < this._buffer.Count; i++) + { _ema += this._buffer[i]; } + _ema /= this._buffer.Count; + } + else + { + _ema = TValue.v * this._k + this._lastema * this._k1m; + } + + this._lastlastema = this._lastema; + this._lastema = _ema; + + var ret = (TValue.t, this.Count < this._p - 1 && this._NaN ? double.NaN : _ema); + base.Add(ret, update); + } } \ No newline at end of file diff --git a/Tests/Validations/Pandas_TA.cstemp b/Tests/Validations/Pandas_TA.cstemp index 27c2e9b8..0aa769bd 100644 --- a/Tests/Validations/Pandas_TA.cstemp +++ b/Tests/Validations/Pandas_TA.cstemp @@ -1,120 +1,120 @@ -using Xunit; -using System; -using QuanTAlib; -using Python.Runtime; -using Python.Included; - -namespace Validation; -public class PandasTA -{ - private readonly RND_Feed bars; - private readonly Random rnd = new(); - private readonly int period; - private readonly dynamic ta; - private readonly dynamic df; - - public PandasTA() - { - this.bars = new(1000); - this.period = this.rnd.Next(28) + 3; - - Installer.SetupPython().Wait(); - Installer.TryInstallPip(); - Installer.PipInstallModule("numpy"); - Installer.PipInstallModule("pandas"); - Installer.PipInstallModule("pandas-ta"); - PythonEngine.Initialize(); - this.ta = Py.Import("pandas_ta"); - this.df = this.ta.DataFrame(this.bars.Close.v); - } - - ~PandasTA() - { - PythonEngine.Shutdown(); - } - - [Fact] - void SMA() - { - SMA_Series QL = new(this.bars.Close, this.period, false); - var pta = this.ta.sma(close: this.df[0], length: this.period); - - Assert.Equal(System.Math.Round((double)pta.tail(1), 7), Math.Round(QL.Last().v, 7)); - } - -/* - [Fact] - void EMA() - { - EMA_Series QL = new(this.bars.Close, this.period, false); - var pta = this.ta.ema(close: this.df[0], length: this.period); - - Assert.Equal(System.Math.Round((double)pta.tail(1), 7), Math.Round(QL.Last().v, 7)); - } - - [Fact] - void TEMA() - { - TEMA_Series QL = new(this.bars.Close, this.period, false); - var pta = this.ta.tema(close: this.df[0], length: this.period); - - Assert.Equal(System.Math.Round((double)pta.tail(1), 7), Math.Round(QL.Last().v, 7)); - } - - [Fact] - void ENTP() - { - ENTP_Series QL = new(this.bars.Close, this.period, useNaN:false); - var pta = this.ta.entropy(close: this.df[0], length: this.period); - - Assert.Equal(System.Math.Round((double)pta.tail(1), 7), Math.Round(QL.Last().v, 7)); - } - - - [Fact] - void WMA() - { - WMA_Series QL = new(this.bars.Close, this.period, false); - var pta = this.ta.wma(close: this.df[0], length: this.period); - - Assert.Equal(System.Math.Round((double)pta.tail(1), 7), Math.Round(QL.Last().v, 7)); - } - - [Fact] - void DEMA() - { - DEMA_Series QL = new(this.bars.Close, this.period, false); - var pta = this.ta.dema(close: this.df[0], length: this.period); - - Assert.Equal(System.Math.Round((double)pta.tail(1), 7), Math.Round(QL.Last().v, 7)); - } - - [Fact] - void BIAS() - { - BIAS_Series QL = new(this.bars.Close, this.period, false); - var pta = this.ta.bias(close: this.df[0], length: this.period); - - Assert.Equal(System.Math.Round((double)pta.tail(1), 7), Math.Round(QL.Last().v, 7)); - } - - [Fact] - void KURT() - { - KURT_Series QL = new(this.bars.Close, this.period, useNaN: false); - var pta = this.ta.kurtosis(close: this.df[0], length: this.period); - - Assert.Equal(System.Math.Round((double)pta.tail(1), 4), Math.Round(QL.Last().v, 4)); - } - - [Fact] - void MAD() - { - MAD_Series QL = new(this.bars.Close, this.period, useNaN: false); - var pta = this.ta.mad(close: this.df[0], length: this.period); - - Assert.Equal(System.Math.Round((double)pta.tail(1), 7), Math.Round(QL.Last().v, 7)); - } -*/ - +using Xunit; +using System; +using QuanTAlib; +using Python.Runtime; +using Python.Included; + +namespace Validation; +public class PandasTA +{ + private readonly RND_Feed bars; + private readonly Random rnd = new(); + private readonly int period; + private readonly dynamic ta; + private readonly dynamic df; + + public PandasTA() + { + this.bars = new(1000); + this.period = this.rnd.Next(28) + 3; + + Installer.SetupPython().Wait(); + Installer.TryInstallPip(); + Installer.PipInstallModule("numpy"); + Installer.PipInstallModule("pandas"); + Installer.PipInstallModule("pandas-ta"); + PythonEngine.Initialize(); + this.ta = Py.Import("pandas_ta"); + this.df = this.ta.DataFrame(this.bars.Close.v); + } + + ~PandasTA() + { + PythonEngine.Shutdown(); + } + + [Fact] + void SMA() + { + SMA_Series QL = new(this.bars.Close, this.period, false); + var pta = this.ta.sma(close: this.df[0], length: this.period); + + Assert.Equal(System.Math.Round((double)pta.tail(1), 7), Math.Round(QL.Last().v, 7)); + } + +/* + [Fact] + void EMA() + { + EMA_Series QL = new(this.bars.Close, this.period, false); + var pta = this.ta.ema(close: this.df[0], length: this.period); + + Assert.Equal(System.Math.Round((double)pta.tail(1), 7), Math.Round(QL.Last().v, 7)); + } + + [Fact] + void TEMA() + { + TEMA_Series QL = new(this.bars.Close, this.period, false); + var pta = this.ta.tema(close: this.df[0], length: this.period); + + Assert.Equal(System.Math.Round((double)pta.tail(1), 7), Math.Round(QL.Last().v, 7)); + } + + [Fact] + void ENTP() + { + ENTP_Series QL = new(this.bars.Close, this.period, useNaN:false); + var pta = this.ta.entropy(close: this.df[0], length: this.period); + + Assert.Equal(System.Math.Round((double)pta.tail(1), 7), Math.Round(QL.Last().v, 7)); + } + + + [Fact] + void WMA() + { + WMA_Series QL = new(this.bars.Close, this.period, false); + var pta = this.ta.wma(close: this.df[0], length: this.period); + + Assert.Equal(System.Math.Round((double)pta.tail(1), 7), Math.Round(QL.Last().v, 7)); + } + + [Fact] + void DEMA() + { + DEMA_Series QL = new(this.bars.Close, this.period, false); + var pta = this.ta.dema(close: this.df[0], length: this.period); + + Assert.Equal(System.Math.Round((double)pta.tail(1), 7), Math.Round(QL.Last().v, 7)); + } + + [Fact] + void BIAS() + { + BIAS_Series QL = new(this.bars.Close, this.period, false); + var pta = this.ta.bias(close: this.df[0], length: this.period); + + Assert.Equal(System.Math.Round((double)pta.tail(1), 7), Math.Round(QL.Last().v, 7)); + } + + [Fact] + void KURT() + { + KURT_Series QL = new(this.bars.Close, this.period, useNaN: false); + var pta = this.ta.kurtosis(close: this.df[0], length: this.period); + + Assert.Equal(System.Math.Round((double)pta.tail(1), 4), Math.Round(QL.Last().v, 4)); + } + + [Fact] + void MAD() + { + MAD_Series QL = new(this.bars.Close, this.period, useNaN: false); + var pta = this.ta.mad(close: this.df[0], length: this.period); + + Assert.Equal(System.Math.Round((double)pta.tail(1), 7), Math.Round(QL.Last().v, 7)); + } +*/ + } \ No newline at end of file From d74e28b6bd491b874f6d737d75290ba0571050dc Mon Sep 17 00:00:00 2001 From: Miha Kralj Date: Sun, 24 Apr 2022 20:15:11 -0700 Subject: [PATCH 02/11] Sonar changes --- Quantower/Indicators/ZLMA_chart.cs | 9 +- Source/Indicators/JMA_Series.cs | 2 - Source/Indicators/ZLEMA_Series.cs | 140 ++++++++--------- Tests/Validations/Pandas_TA.cstemp | 238 ++++++++++++++--------------- 4 files changed, 193 insertions(+), 196 deletions(-) diff --git a/Quantower/Indicators/ZLMA_chart.cs b/Quantower/Indicators/ZLMA_chart.cs index e8370495..0b556b0a 100644 --- a/Quantower/Indicators/ZLMA_chart.cs +++ b/Quantower/Indicators/ZLMA_chart.cs @@ -30,13 +30,12 @@ public class ZLMA_chart : Indicator private int matype = 2; #endregion Parameters - + private TBars bars; - /////// - private ZL_Series zerolag; + /////// private TSeries indicator; - /////// - + /////// + public ZLMA_chart() { this.SeparateWindow = false; diff --git a/Source/Indicators/JMA_Series.cs b/Source/Indicators/JMA_Series.cs index 4a9068c2..d1639a09 100644 --- a/Source/Indicators/JMA_Series.cs +++ b/Source/Indicators/JMA_Series.cs @@ -41,8 +41,6 @@ public class JMA_Series : Single_TSeries_Indicator this.rvolty = Math.Exp((1 / this.pow1) * Math.Log(len1)); this.len2 = Math.Sqrt(0.5 * (_p - 1)) * len1; this.beta = 0.45 * (_p - 1) / (0.45 * (_p - 1) + 2); - this._l = (int)Math.Round(this._p - 1 * 0.5); - if (base._data.Count > 0) { base.Add(base._data); } } diff --git a/Source/Indicators/ZLEMA_Series.cs b/Source/Indicators/ZLEMA_Series.cs index 5642cf8f..be2913f7 100644 --- a/Source/Indicators/ZLEMA_Series.cs +++ b/Source/Indicators/ZLEMA_Series.cs @@ -1,71 +1,71 @@ -namespace QuanTAlib; -using System; - -/* -ZLEMA: Zero Lag Exponential Moving Average - The Zero lag exponential moving average (ZLEMA) indicator was created by John - Ehlers and Ric Way. - -The formula for a given N-Day period and for a given Data series is: - Lag = (Period-1)/2 - Ema Data = {Data+(Data-Data(Lag days ago)) - ZLEMA = EMA (EmaData,Period) - -Remark: - The idea is do a regular exponential moving average (EMA) calculation but on a - de-lagged data instead of doing it on the regular data. Data is de-lagged by - removing the data from "lag" days ago thus removing (or attempting to remove) - the cumulative lag effect of the moving average. - - */ - -public class ZLEMA_Series : Single_TSeries_Indicator -{ - private readonly System.Collections.Generic.List _buffer = new(); - private readonly double _k, _k1m; - private double _lastema, _lastlastema; - - public ZLEMA_Series(TSeries source, int period, bool useNaN = false) : base(source, period, useNaN) - { - this._k = 2.0 / (this._p + 1); - this._k1m = 1.0 - this._k; - this._lastema = this._lastlastema = double.NaN; - if (base._data.Count > 0) - { base.Add(base._data); } - } - - public override void Add((System.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); - double _ema = 0; - if (update) - { this._lastema = this._lastlastema; } - if (this.Count < this._p) - { - if (update) - { this._buffer[this._buffer.Count - 1] = _zl; } - else - { - this._buffer.Add(_zl); - } - if (this._buffer.Count > this._p) - { this._buffer.RemoveAt(0); } - - for (int i = 0; i < this._buffer.Count; i++) - { _ema += this._buffer[i]; } - _ema /= this._buffer.Count; - } - else - { - _ema = TValue.v * this._k + this._lastema * this._k1m; - } - - this._lastlastema = this._lastema; - this._lastema = _ema; - - var ret = (TValue.t, this.Count < this._p - 1 && this._NaN ? double.NaN : _ema); - base.Add(ret, update); - } +namespace QuanTAlib; +using System; + +/* +ZLEMA: Zero Lag Exponential Moving Average + The Zero lag exponential moving average (ZLEMA) indicator was created by John + Ehlers and Ric Way. + +The formula for a given N-Day period and for a given Data series is: + Lag = (Period-1)/2 + Ema Data = {Data+(Data-Data(Lag days ago)) + ZLEMA = EMA (EmaData,Period) + +Remark: + The idea is do a regular exponential moving average (EMA) calculation but on a + de-lagged data instead of doing it on the regular data. Data is de-lagged by + removing the data from "lag" days ago thus removing (or attempting to remove) + the cumulative lag effect of the moving average. + + */ + +public class ZLEMA_Series : Single_TSeries_Indicator +{ + private readonly System.Collections.Generic.List _buffer = new(); + private readonly double _k, _k1m; + private double _lastema, _lastlastema; + + public ZLEMA_Series(TSeries source, int period, bool useNaN = false) : base(source, period, useNaN) + { + this._k = 2.0 / (this._p + 1); + this._k1m = 1.0 - this._k; + this._lastema = this._lastlastema = double.NaN; + if (base._data.Count > 0) + { base.Add(base._data); } + } + + public override void Add((System.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); + double _ema = 0; + if (update) + { this._lastema = this._lastlastema; } + if (this.Count < this._p) + { + if (update) + { this._buffer[this._buffer.Count - 1] = _zl; } + else + { + this._buffer.Add(_zl); + } + if (this._buffer.Count > this._p) + { this._buffer.RemoveAt(0); } + + for (int i = 0; i < this._buffer.Count; i++) + { _ema += this._buffer[i]; } + _ema /= this._buffer.Count; + } + else + { + _ema = TValue.v * this._k + this._lastema * this._k1m; + } + + this._lastlastema = this._lastema; + this._lastema = _ema; + + var ret = (TValue.t, this.Count < this._p - 1 && this._NaN ? double.NaN : _ema); + base.Add(ret, update); + } } \ No newline at end of file diff --git a/Tests/Validations/Pandas_TA.cstemp b/Tests/Validations/Pandas_TA.cstemp index 27c2e9b8..0aa769bd 100644 --- a/Tests/Validations/Pandas_TA.cstemp +++ b/Tests/Validations/Pandas_TA.cstemp @@ -1,120 +1,120 @@ -using Xunit; -using System; -using QuanTAlib; -using Python.Runtime; -using Python.Included; - -namespace Validation; -public class PandasTA -{ - private readonly RND_Feed bars; - private readonly Random rnd = new(); - private readonly int period; - private readonly dynamic ta; - private readonly dynamic df; - - public PandasTA() - { - this.bars = new(1000); - this.period = this.rnd.Next(28) + 3; - - Installer.SetupPython().Wait(); - Installer.TryInstallPip(); - Installer.PipInstallModule("numpy"); - Installer.PipInstallModule("pandas"); - Installer.PipInstallModule("pandas-ta"); - PythonEngine.Initialize(); - this.ta = Py.Import("pandas_ta"); - this.df = this.ta.DataFrame(this.bars.Close.v); - } - - ~PandasTA() - { - PythonEngine.Shutdown(); - } - - [Fact] - void SMA() - { - SMA_Series QL = new(this.bars.Close, this.period, false); - var pta = this.ta.sma(close: this.df[0], length: this.period); - - Assert.Equal(System.Math.Round((double)pta.tail(1), 7), Math.Round(QL.Last().v, 7)); - } - -/* - [Fact] - void EMA() - { - EMA_Series QL = new(this.bars.Close, this.period, false); - var pta = this.ta.ema(close: this.df[0], length: this.period); - - Assert.Equal(System.Math.Round((double)pta.tail(1), 7), Math.Round(QL.Last().v, 7)); - } - - [Fact] - void TEMA() - { - TEMA_Series QL = new(this.bars.Close, this.period, false); - var pta = this.ta.tema(close: this.df[0], length: this.period); - - Assert.Equal(System.Math.Round((double)pta.tail(1), 7), Math.Round(QL.Last().v, 7)); - } - - [Fact] - void ENTP() - { - ENTP_Series QL = new(this.bars.Close, this.period, useNaN:false); - var pta = this.ta.entropy(close: this.df[0], length: this.period); - - Assert.Equal(System.Math.Round((double)pta.tail(1), 7), Math.Round(QL.Last().v, 7)); - } - - - [Fact] - void WMA() - { - WMA_Series QL = new(this.bars.Close, this.period, false); - var pta = this.ta.wma(close: this.df[0], length: this.period); - - Assert.Equal(System.Math.Round((double)pta.tail(1), 7), Math.Round(QL.Last().v, 7)); - } - - [Fact] - void DEMA() - { - DEMA_Series QL = new(this.bars.Close, this.period, false); - var pta = this.ta.dema(close: this.df[0], length: this.period); - - Assert.Equal(System.Math.Round((double)pta.tail(1), 7), Math.Round(QL.Last().v, 7)); - } - - [Fact] - void BIAS() - { - BIAS_Series QL = new(this.bars.Close, this.period, false); - var pta = this.ta.bias(close: this.df[0], length: this.period); - - Assert.Equal(System.Math.Round((double)pta.tail(1), 7), Math.Round(QL.Last().v, 7)); - } - - [Fact] - void KURT() - { - KURT_Series QL = new(this.bars.Close, this.period, useNaN: false); - var pta = this.ta.kurtosis(close: this.df[0], length: this.period); - - Assert.Equal(System.Math.Round((double)pta.tail(1), 4), Math.Round(QL.Last().v, 4)); - } - - [Fact] - void MAD() - { - MAD_Series QL = new(this.bars.Close, this.period, useNaN: false); - var pta = this.ta.mad(close: this.df[0], length: this.period); - - Assert.Equal(System.Math.Round((double)pta.tail(1), 7), Math.Round(QL.Last().v, 7)); - } -*/ - +using Xunit; +using System; +using QuanTAlib; +using Python.Runtime; +using Python.Included; + +namespace Validation; +public class PandasTA +{ + private readonly RND_Feed bars; + private readonly Random rnd = new(); + private readonly int period; + private readonly dynamic ta; + private readonly dynamic df; + + public PandasTA() + { + this.bars = new(1000); + this.period = this.rnd.Next(28) + 3; + + Installer.SetupPython().Wait(); + Installer.TryInstallPip(); + Installer.PipInstallModule("numpy"); + Installer.PipInstallModule("pandas"); + Installer.PipInstallModule("pandas-ta"); + PythonEngine.Initialize(); + this.ta = Py.Import("pandas_ta"); + this.df = this.ta.DataFrame(this.bars.Close.v); + } + + ~PandasTA() + { + PythonEngine.Shutdown(); + } + + [Fact] + void SMA() + { + SMA_Series QL = new(this.bars.Close, this.period, false); + var pta = this.ta.sma(close: this.df[0], length: this.period); + + Assert.Equal(System.Math.Round((double)pta.tail(1), 7), Math.Round(QL.Last().v, 7)); + } + +/* + [Fact] + void EMA() + { + EMA_Series QL = new(this.bars.Close, this.period, false); + var pta = this.ta.ema(close: this.df[0], length: this.period); + + Assert.Equal(System.Math.Round((double)pta.tail(1), 7), Math.Round(QL.Last().v, 7)); + } + + [Fact] + void TEMA() + { + TEMA_Series QL = new(this.bars.Close, this.period, false); + var pta = this.ta.tema(close: this.df[0], length: this.period); + + Assert.Equal(System.Math.Round((double)pta.tail(1), 7), Math.Round(QL.Last().v, 7)); + } + + [Fact] + void ENTP() + { + ENTP_Series QL = new(this.bars.Close, this.period, useNaN:false); + var pta = this.ta.entropy(close: this.df[0], length: this.period); + + Assert.Equal(System.Math.Round((double)pta.tail(1), 7), Math.Round(QL.Last().v, 7)); + } + + + [Fact] + void WMA() + { + WMA_Series QL = new(this.bars.Close, this.period, false); + var pta = this.ta.wma(close: this.df[0], length: this.period); + + Assert.Equal(System.Math.Round((double)pta.tail(1), 7), Math.Round(QL.Last().v, 7)); + } + + [Fact] + void DEMA() + { + DEMA_Series QL = new(this.bars.Close, this.period, false); + var pta = this.ta.dema(close: this.df[0], length: this.period); + + Assert.Equal(System.Math.Round((double)pta.tail(1), 7), Math.Round(QL.Last().v, 7)); + } + + [Fact] + void BIAS() + { + BIAS_Series QL = new(this.bars.Close, this.period, false); + var pta = this.ta.bias(close: this.df[0], length: this.period); + + Assert.Equal(System.Math.Round((double)pta.tail(1), 7), Math.Round(QL.Last().v, 7)); + } + + [Fact] + void KURT() + { + KURT_Series QL = new(this.bars.Close, this.period, useNaN: false); + var pta = this.ta.kurtosis(close: this.df[0], length: this.period); + + Assert.Equal(System.Math.Round((double)pta.tail(1), 4), Math.Round(QL.Last().v, 4)); + } + + [Fact] + void MAD() + { + MAD_Series QL = new(this.bars.Close, this.period, useNaN: false); + var pta = this.ta.mad(close: this.df[0], length: this.period); + + Assert.Equal(System.Math.Round((double)pta.tail(1), 7), Math.Round(QL.Last().v, 7)); + } +*/ + } \ No newline at end of file From d894d259c3811d2187e2669afb2cbce247e7244a Mon Sep 17 00:00:00 2001 From: Miha Kralj Date: Wed, 27 Apr 2022 18:32:22 -0700 Subject: [PATCH 03/11] MACD and RSI --- Quantower/Indicators/RSI_chart.cs | 53 ++++++++++++++++++++++ Source/Indicators/MACD_Series.cs | 46 +++++++++++++++++++ Source/Indicators/RSI_Series.cs | 73 +++++++++++++++++++++++++++++++ 3 files changed, 172 insertions(+) create mode 100644 Quantower/Indicators/RSI_chart.cs create mode 100644 Source/Indicators/MACD_Series.cs create mode 100644 Source/Indicators/RSI_Series.cs diff --git a/Quantower/Indicators/RSI_chart.cs b/Quantower/Indicators/RSI_chart.cs new file mode 100644 index 00000000..d5b840e7 --- /dev/null +++ b/Quantower/Indicators/RSI_chart.cs @@ -0,0 +1,53 @@ +using System.Drawing; +using TradingPlatform.BusinessLayer; +namespace QuanTAlib; + +public class RSI_chart : Indicator +{ + #region Parameters + + [InputParameter("Smoothing period", 0, 1, 999, 1, 1)] + private int Period = 10; + + [InputParameter("Data source", 1, variants: new object[] + { "Open", 0, "High", 1, "Low", 2, "Close", 3, "HL2", 4, "OC2", 5, + "OHL3", 6, "HLC3", 7, "OHLC4", 8, "Weighted (HLCC4)", 9 })] + private int DataSource = 8; + + #endregion Parameters + + private TBars bars; + + /////// + private RSI_Series indicator; + /////// + + public RSI_chart() + { + this.SeparateWindow = true; + this.Name = "RSI - Relative Strength Index"; + this.Description = "RSI description"; + this.AddLineSeries("RSI", Color.RoyalBlue, 3, LineStyle.Solid); + } + + protected override void OnInit() + { + this.bars = new(); + this.ShortName = + "RSI (" + TBars.SelectStr(this.DataSource) + ", " + this.Period + ")"; + this.indicator = new(source: bars.Select(this.DataSource), + period: this.Period, useNaN: true); + } + protected override void OnUpdate(UpdateArgs args) + { + bool update = !(args.Reason == UpdateReason.NewBar || + args.Reason == UpdateReason.HistoricalBar); + this.bars.Add(this.Time(), this.GetPrice(PriceType.Open), + this.GetPrice(PriceType.High), this.GetPrice(PriceType.Low), + this.GetPrice(PriceType.Close), + this.GetPrice(PriceType.Volume), update); + double result = this.indicator[this.indicator.Count - 1].v; + + this.SetValue(result, 0); + } +} diff --git a/Source/Indicators/MACD_Series.cs b/Source/Indicators/MACD_Series.cs new file mode 100644 index 00000000..0e75a26e --- /dev/null +++ b/Source/Indicators/MACD_Series.cs @@ -0,0 +1,46 @@ +namespace QuanTAlib; +using System; + +/* +MACD: Moving Average Convergence/Divergence + Moving average convergence divergence (MACD) is a trend-following momentum + indicator that shows the relationship between two moving averages of a series. + The MACD is calculated by subtracting the 26-period exponential moving average (EMA) + from the 12-period EMA. MACD Signal is 9-day EMA of MACD. + +Sources: + https://www.investopedia.com/terms/m/macd.asp + https://www.fidelity.com/learning-center/trading-investing/technical-analysis/technical-indicator-guide/macd + + */ + +public class MACD_Series : Single_TSeries_Indicator +{ + private EMA_Series _TSslow; + private EMA_Series _TSfast; + private SUB_Series _TSmacd; + public EMA_Series Signal { get; } + + public MACD_Series(TSeries source, int slow = 26, int fast = 12, int signal = 9, bool useNaN = false) + : base(source, period: 0, useNaN) + { + _TSslow = new(source: source, period: slow, useNaN: false); + _TSfast = new(source: source, period: fast, useNaN: false); + _TSmacd = new(_TSfast, _TSslow); + Signal = new(source: _TSmacd, period: signal, useNaN: useNaN); + + if (source.Count > 0) { base.Add(_TSmacd); } + } + public override void Add((System.DateTime t, double v) TValue, bool update) + { + double _macd; + if (update) + { + _TSslow.Add(TValue, true); + _TSfast.Add(TValue, true); + } + _macd = this._TSmacd[(this.Count < this._TSmacd.Count) ? this.Count : this._TSmacd.Count - 1].v; + var result = (TValue.t, _macd); + base.Add(result, update); + } +} \ No newline at end of file diff --git a/Source/Indicators/RSI_Series.cs b/Source/Indicators/RSI_Series.cs new file mode 100644 index 00000000..e9d57f39 --- /dev/null +++ b/Source/Indicators/RSI_Series.cs @@ -0,0 +1,73 @@ +namespace QuanTAlib; +using System; + +/* +RSI: Relative Strength Index + Created by J. Welles Wilder, the Relative Strength Index measures strength + of the winning/losing streak over N lookback periods on a scale of 0 to 100, + to depict overbought and oversold conditions. + +Sources: + https://www.investopedia.com/terms/r/rsi.asp + + */ + +public class RSI_Series : Single_TSeries_Indicator +{ + private readonly System.Collections.Generic.List _gain = new(); + private readonly System.Collections.Generic.List _loss = new(); + double _avgGain = 0; + double _avgLoss = 0; + double _lastValue = 0; + double _lastlastValue = 0; + + public RSI_Series(TSeries source, int period = 10, bool useNaN = false) : base(source, period: period, useNaN: useNaN) + { if (source.Count > 0) { base.Add(source); } } + + public override void Add((System.DateTime t, double v) TValue, bool update) + { + int i = this.Count; + double _rsi = 0; + if (update) { _lastValue = _lastlastValue; } + if (i == 0) { _lastValue = TValue.v; } + + double _gainval = (TValue.v > _lastValue) ? TValue.v - _lastValue : 0; + if (update) { _gain[_gain.Count - 1] = _gainval; } else { _gain.Add(_gainval); } + if (_gain.Count > this._p) { _gain.RemoveAt(0); } + + double _lossval = (TValue.v < _lastValue) ? _lastValue - TValue.v : 0; + if (update) { _loss[_loss.Count - 1] = _lossval; } else { _loss.Add(_lossval); } + if (_loss.Count > this._p) { _loss.RemoveAt(0); } + + _lastlastValue = _lastValue; + _lastValue = TValue.v; + + // calculate RSI + if (i > _p) + { + _avgGain = ((_avgGain * (_p - 1)) + _gain[_gain.Count - 1]) / _p; + _avgLoss = ((_avgLoss * (_p - 1)) + _loss[_loss.Count - 1]) / _p; + if (_avgLoss > 0) { + double rs = _avgGain / _avgLoss; + _rsi = 100 - (100 / (1 + rs)); + } + else { _rsi = 100; } + } + // initialize average gain + else + { + double _sumGain = 0; + for (int p = 0; p < _gain.Count; p++) { _sumGain += _gain[p]; } + double _sumLoss = 0; + for (int p = 0; p < _loss.Count; p++) { _sumLoss += _loss[p]; } + + _avgGain = _sumGain / _gain.Count; + _avgLoss = _sumLoss / _loss.Count; + + _rsi = (_avgLoss > 0) ? 100 - (100 / (1 + (_avgGain / _avgLoss))) : 100; + } + + var result = (TValue.t, (this.Count < this._p && this._NaN) ? double.NaN : _rsi); + base.Add(result, update); + } +} \ No newline at end of file From e9d7ba8cf12291d50ea434fd237297313893611b Mon Sep 17 00:00:00 2001 From: Miha Kralj Date: Wed, 27 Apr 2022 18:34:59 -0700 Subject: [PATCH 04/11] RSI and MACD --- Docs/coverage.md | 4 ++-- Tests/MovingAvg/MACD_Test.cs | 33 ++++++++++++++++++++++++++++++ Tests/MovingAvg/RSI_Test.cs | 33 ++++++++++++++++++++++++++++++ Tests/Validations/Skender_Stock.cs | 18 ++++++++++++++++ Tests/Validations/TA_LIB.cs | 19 +++++++++++++++++ 5 files changed, 105 insertions(+), 2 deletions(-) create mode 100644 Tests/MovingAvg/MACD_Test.cs create mode 100644 Tests/MovingAvg/RSI_Test.cs diff --git a/Docs/coverage.md b/Docs/coverage.md index 665c170e..e8bb893f 100644 --- a/Docs/coverage.md +++ b/Docs/coverage.md @@ -55,7 +55,7 @@ | JMA - Jurik Moving Average |✔️|||✔️| | KAMA - Kaufman's Adaptive Moving Average |✔️|✔️|✔️|✔️| | LSMA - Least Squares Moving Average |||✔️|| -| MACD - Moving Average Convergence/Divergence ||✔️|✔️|✔️| +| MACD - Moving Average Convergence/Divergence |✔️|✔️|✔️|✔️| | MAMA - MESA Adaptive Moving Average ||✔️|✔️|| | MMA - Modified Moving Average |||✔️|| | NATR - Normalized Average True Range ||✔️|✔️|✔️| @@ -107,7 +107,7 @@ | PO - Price Oscillator ||||✔️| | PPO - Percentage Price Oscillator ||✔️||✔️| | PVI - Positive Volume Index ||||✔️| -| RSI - Relative Strength Index ||✔️|✔️|✔️| +| RSI - Relative Strength Index |✔️|✔️|✔️|✔️| | RVGI - Relative Vigor Index ||||✔️| | SRSI - Stochastic RSI |||✔️|✔️| | TRIX - 1-day ROC of TEMA ||✔️|✔️|✔️| diff --git a/Tests/MovingAvg/MACD_Test.cs b/Tests/MovingAvg/MACD_Test.cs new file mode 100644 index 00000000..61a3b46e --- /dev/null +++ b/Tests/MovingAvg/MACD_Test.cs @@ -0,0 +1,33 @@ +using Xunit; +using System; +using QuanTAlib; + +namespace MovingAvg; +public class MACD_Test +{ + [Fact] + public void Add_Test() + { + TSeries a = new() { 0, 1, 2, 3, 4, 5 }; + MACD_Series c = new(a, 26,12,9); + Assert.Equal(6, c.Count); + a.Add(5); + Assert.Equal(a.Count, c.Count); + a.Add(0, update: true); + Assert.Equal(a.Count, c.Count); + } + + [Fact] + public void Edge_Test() + { + TSeries a = new() { double.NaN, double.Epsilon, double.PositiveInfinity, double.MaxValue }; + MACD_Series c = new(a, 26,12,9); + Assert.Equal(a.Count, c.Count); + a.Add(double.NaN); + Assert.Equal(a.Count, c.Count); + a.Add(double.PositiveInfinity); + Assert.Equal(a.Count, c.Count); + + } + +} diff --git a/Tests/MovingAvg/RSI_Test.cs b/Tests/MovingAvg/RSI_Test.cs new file mode 100644 index 00000000..7506cad7 --- /dev/null +++ b/Tests/MovingAvg/RSI_Test.cs @@ -0,0 +1,33 @@ +using Xunit; +using System; +using QuanTAlib; + +namespace MovingAvg; +public class RSI_Test +{ + [Fact] + public void Add_Test() + { + TSeries a = new() { 0, 1, 2, 3, 4, 5 }; + RSI_Series c = new(a, 3); + Assert.Equal(6, c.Count); + a.Add(5); + Assert.Equal(a.Count, c.Count); + a.Add(0, update: true); + Assert.Equal(a.Count, c.Count); + } + + [Fact] + public void Edge_Test() + { + TSeries a = new() { double.NaN, double.Epsilon, double.PositiveInfinity, double.MaxValue }; + RSI_Series c = new(a, 3); + Assert.Equal(a.Count, c.Count); + a.Add(double.NaN); + Assert.Equal(a.Count, c.Count); + a.Add(double.PositiveInfinity); + Assert.Equal(a.Count, c.Count); + + } + +} diff --git a/Tests/Validations/Skender_Stock.cs b/Tests/Validations/Skender_Stock.cs index d93ee632..553e1209 100644 --- a/Tests/Validations/Skender_Stock.cs +++ b/Tests/Validations/Skender_Stock.cs @@ -126,4 +126,22 @@ public class Skender_Stock Assert.Equal(Math.Round((double)SK.Last().Smma!, 8), Math.Round(QL.Last().v, 8)); } + + [Fact] + public void MACD() + { + MACD_Series QL = new(this.bars.Close, 26,12,9, useNaN: false); + var SK = this.quotes.GetMacd(12,26,9); + + Assert.Equal(Math.Round((double)SK.Last().Macd!, 8), Math.Round(QL.Last().v, 8)); + } + + [Fact] + public void RSI() + { + RSI_Series QL = new(this.bars.Close, this.period, useNaN: false); + var SK = this.quotes.GetRsi(this.period); + + Assert.Equal(Math.Round((double)SK.Last().Rsi!, 8), Math.Round(QL.Last().v, 8)); + } } diff --git a/Tests/Validations/TA_LIB.cs b/Tests/Validations/TA_LIB.cs index 2d5406b4..32a78094 100644 --- a/Tests/Validations/TA_LIB.cs +++ b/Tests/Validations/TA_LIB.cs @@ -101,4 +101,23 @@ public class TA_LIB Assert.Equal(Math.Round(this.TALIB[this.TALIB.Length - outBegIdx - 1], 8), Math.Round(QL.Last().v, 8)); } + + [Fact] + public void RSI() + { + RSI_Series QL = new(this.bars.Close, this.period, false); + Core.Rsi(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 MACD() + { + double[] macdSignal = new double[this.bars.Count]; + double[] macdHist = new double[this.bars.Count]; +MACD_Series QL = new(this.bars.Close, slow: 26, fast: 12, signal: 9, false); +Core.Macd(this.inclose, 0, this.bars.Count - 1, outMacd: this.TALIB, outMacdSignal: macdSignal, outMacdHist: macdHist, out int outBegIdx, out _); +Assert.Equal(Math.Round(this.TALIB[this.TALIB.Length - outBegIdx - 1], 8), Math.Round(QL.Last().v, 8)); + } } From 50ae1d4917d2dcb73af837352577cbc49b754a5f Mon Sep 17 00:00:00 2001 From: Miha Kralj Date: Thu, 28 Apr 2022 12:52:00 -0700 Subject: [PATCH 05/11] CCI --- Docs/coverage.md | 2 +- Quantower/Indicators/CCI_chart.cs | 43 +++++++++++++++++++++++++ Quantower/Indicators/ZLMA_chart.cs | 8 ++--- Quantower/Quantower.csproj | 13 +++----- Source/Basics/Abstracts.cs | 2 +- Source/Indicators/CCI_Series.cs | 50 ++++++++++++++++++++++++++++++ Source/Indicators/MACD_Series.cs | 8 ++--- Source/Indicators/RSI_Series.cs | 8 ++--- Tests/Tests.csproj | 1 + Tests/Validations/Skender_Stock.cs | 8 +++++ Tests/Validations/TA_LIB.cs | 9 ++++++ 11 files changed, 129 insertions(+), 23 deletions(-) create mode 100644 Quantower/Indicators/CCI_chart.cs create mode 100644 Source/Indicators/CCI_Series.cs diff --git a/Docs/coverage.md b/Docs/coverage.md index e8bb893f..7e3a3c51 100644 --- a/Docs/coverage.md +++ b/Docs/coverage.md @@ -89,7 +89,7 @@ | AROON - Aroon oscillator ||✔️|✔️|✔️| | BBANDS - Bollinger Bands ||✔️|✔️|✔️| | BOP - Balance of Power ||✔️|✔️|✔️| -| CCI - Commodity Channel Index ||✔️|✔️|✔️| +| CCI - Commodity Channel Index |✔️|✔️|✔️|✔️| | CFO - Chande Forcast Oscillator ||||✔️| | CMF - Chaikin Money Flow |||✔️|✔️| | CMO - Chande Momentum Oscillator ||✔️||✔️| diff --git a/Quantower/Indicators/CCI_chart.cs b/Quantower/Indicators/CCI_chart.cs new file mode 100644 index 00000000..02a6bb57 --- /dev/null +++ b/Quantower/Indicators/CCI_chart.cs @@ -0,0 +1,43 @@ +using System.Diagnostics; +using System.Drawing; +using TradingPlatform.BusinessLayer; +namespace QuanTAlib; + +public class CCI_chart : Indicator +{ + #region Parameters + + [InputParameter("Smoothing period", 0, 1, 999, 1, 1)] + private readonly int Period = 10; + + #endregion Parameters + + private TBars bars; + + /////// + private CCI_Series indicator; + /////// + + public CCI_chart() + { + this.SeparateWindow = true; + this.Name = "CCI - Commodity Channel Index"; + this.Description = "CCI description"; + this.AddLineSeries("CCI", Color.RoyalBlue, 3, LineStyle.Solid); + } + + protected override void OnInit() + { + this.ShortName = "CCI (" + this.Period + ")"; + this.bars = new(); + this.indicator = new(source: bars, period: this.Period, useNaN: false); + } + protected override void OnUpdate(UpdateArgs args) + { + bool update = (args.Reason != UpdateReason.NewBar && args.Reason != UpdateReason.HistoricalBar); + this.bars.Add(this.Time(), this.GetPrice(PriceType.Open), this.GetPrice(PriceType.High), this.GetPrice(PriceType.Low), + this.GetPrice(PriceType.Close), this.GetPrice(PriceType.Volume), update); + double result = this.indicator[this.indicator.Count - 1].v; + this.SetValue(result); + } +} diff --git a/Quantower/Indicators/ZLMA_chart.cs b/Quantower/Indicators/ZLMA_chart.cs index 61020e38..35cddbdf 100644 --- a/Quantower/Indicators/ZLMA_chart.cs +++ b/Quantower/Indicators/ZLMA_chart.cs @@ -9,12 +9,12 @@ public class ZLMA_chart : Indicator #region Parameters [InputParameter("Smoothing period", 0, 1, 999, 1, 1)] - private int Period = 10; + private readonly int Period = 10; [InputParameter("Data source", 1, variants: new object[] { "Open", 0, "High", 1, "Low", 2, "Close", 3, "HL2", 4, "OC2", 5, "OHL3", 6, "HLC3", 7, "OHLC4", 8, "Weighted (HLCC4)", 9 })] - private int DataSource = 3; + private readonly int DataSource = 3; [InputParameter("MA algorithm", 2, variants: new object[] { "SMA", 0, @@ -27,7 +27,7 @@ public class ZLMA_chart : Indicator "JMA", 7, "SMMA", 8 })] - private int matype = 2; + private readonly int matype = 2; #endregion Parameters @@ -87,7 +87,7 @@ public class ZLMA_chart : Indicator this.GetPrice(PriceType.Close), this.GetPrice(PriceType.Volume), update); - double result = this.indicator[this.indicator.Count - 1].v; + double result = this.indicator[this.indicator.Count-1].v; this.SetValue(result); } } diff --git a/Quantower/Quantower.csproj b/Quantower/Quantower.csproj index cdf21a7c..bf162cf9 100644 --- a/Quantower/Quantower.csproj +++ b/Quantower/Quantower.csproj @@ -2,7 +2,7 @@ net48 - latest + preview true AnyCPU Indicator @@ -14,7 +14,6 @@ disable False - True 3 @@ -22,7 +21,6 @@ anycpu full - embedded True @@ -30,20 +28,17 @@ True anycpu - - + QuanTAlib\%(RecursiveDir)%(Filename)%(Extension) - - - .\dll\TradingPlatform.BusinessLayer.dll + C:\Quantower\TradingPlatform\v1.124.6\bin\TradingPlatform.BusinessLayer.dll - + \ No newline at end of file diff --git a/Source/Basics/Abstracts.cs b/Source/Basics/Abstracts.cs index 1233687c..85701a21 100644 --- a/Source/Basics/Abstracts.cs +++ b/Source/Basics/Abstracts.cs @@ -145,7 +145,7 @@ public abstract class Single_TBars_Indicator : TSeries } // 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.c, update); + public virtual void Add((System.DateTime t, double o, double h, double l, double c, double v) TBar, bool update) => base.Add((TBar.t, TBar.c), update); // potentially overridable Add() method for the whole series (could be replaced with faster bulk algo) public virtual void Add(TBars bars) diff --git a/Source/Indicators/CCI_Series.cs b/Source/Indicators/CCI_Series.cs new file mode 100644 index 00000000..bac14b9e --- /dev/null +++ b/Source/Indicators/CCI_Series.cs @@ -0,0 +1,50 @@ +namespace QuanTAlib; +using System; + +/* +CCI: Commodity Channel Index + Commodity Channel Index is a momentum oscillator used to primarily identify overbought + and oversold levels relative to a mean. CCI measures the current price level relative + to an average price level over a given period of time: + - CCI is relatively high when prices are far above their average. + - CCI is relatively low when prices are far below their average. + Using this method, CCI can be used to identify overbought and oversold levels. + +Sources: + https://www.investopedia.com/terms/c/commoditychannelindex.asp + https://www.fidelity.com/learning-center/trading-investing/technical-analysis/technical-indicator-guide/cci + + */ + +public class CCI_Series : Single_TBars_Indicator +{ + private readonly System.Collections.Generic.List _tp = new(); + + public CCI_Series(TBars source, int period = 10, bool useNaN = false) + : base(source, period: period, useNaN: useNaN) { + + if (_bars.Count > 0) { base.Add(_bars); } + } + + public override void Add((DateTime t, double o, double h, double l, double c, double v) TBar, bool update) { + + double _tpItem = (TBar.h + TBar.l + TBar.c) / 3.0; + if (update) { this._tp[this._tp.Count - 1] = _tpItem; } else { this._tp.Add(_tpItem); } + if (this._tp.Count > this._p) { this._tp.RemoveAt(0); } + + // average TP over _tp buffer + double _avgTp = 0; + for (int i = 0; i < this._tp.Count; i++) { _avgTp+=this._tp[i]; } + _avgTp /= this._tp.Count; + + // average Deviation over _tp buffer + double _avgDv = 0; + for (int i = 0; i < this._tp.Count; i++) { _avgDv += Math.Abs(_avgTp - this._tp[i]); } + _avgDv /= this._tp.Count; + + double _cci = (_avgDv == 0) ? double.NaN : (this._tp[this._tp.Count-1] - _avgTp) / (0.015 * _avgDv); + + var result = (TBar.t, (this.Count < this._p && this._NaN) ? double.NaN : _cci); + base.Add(result, update); + } +} \ No newline at end of file diff --git a/Source/Indicators/MACD_Series.cs b/Source/Indicators/MACD_Series.cs index 0e75a26e..b8172d46 100644 --- a/Source/Indicators/MACD_Series.cs +++ b/Source/Indicators/MACD_Series.cs @@ -16,9 +16,9 @@ Sources: public class MACD_Series : Single_TSeries_Indicator { - private EMA_Series _TSslow; - private EMA_Series _TSfast; - private SUB_Series _TSmacd; + private readonly EMA_Series _TSslow; + private readonly EMA_Series _TSfast; + private readonly SUB_Series _TSmacd; public EMA_Series Signal { get; } public MACD_Series(TSeries source, int slow = 26, int fast = 12, int signal = 9, bool useNaN = false) @@ -27,7 +27,7 @@ public class MACD_Series : Single_TSeries_Indicator _TSslow = new(source: source, period: slow, useNaN: false); _TSfast = new(source: source, period: fast, useNaN: false); _TSmacd = new(_TSfast, _TSslow); - Signal = new(source: _TSmacd, period: signal, useNaN: useNaN); + this.Signal = new(source: _TSmacd, period: signal, useNaN: useNaN); if (source.Count > 0) { base.Add(_TSmacd); } } diff --git a/Source/Indicators/RSI_Series.cs b/Source/Indicators/RSI_Series.cs index e9d57f39..5cec4d88 100644 --- a/Source/Indicators/RSI_Series.cs +++ b/Source/Indicators/RSI_Series.cs @@ -16,10 +16,10 @@ public class RSI_Series : Single_TSeries_Indicator { private readonly System.Collections.Generic.List _gain = new(); private readonly System.Collections.Generic.List _loss = new(); - double _avgGain = 0; - double _avgLoss = 0; - double _lastValue = 0; - double _lastlastValue = 0; + private double _avgGain; + private double _avgLoss; + private double _lastValue; + private double _lastlastValue; public RSI_Series(TSeries source, int period = 10, bool useNaN = false) : base(source, period: period, useNaN: useNaN) { if (source.Count > 0) { base.Add(source); } } diff --git a/Tests/Tests.csproj b/Tests/Tests.csproj index e7f92739..4e5c2357 100644 --- a/Tests/Tests.csproj +++ b/Tests/Tests.csproj @@ -2,6 +2,7 @@ net7.0 + preview enable enable diff --git a/Tests/Validations/Skender_Stock.cs b/Tests/Validations/Skender_Stock.cs index 553e1209..13037492 100644 --- a/Tests/Validations/Skender_Stock.cs +++ b/Tests/Validations/Skender_Stock.cs @@ -99,6 +99,14 @@ public class Skender_Stock Assert.Equal(Math.Round((double)SK.Last().Atr!, 8), Math.Round(QL.Last().v, 8)); } + [Fact] + public void CCI() + { + CCI_Series QL = new(this.bars, this.period, false); + var SK = this.quotes.GetCci(this.period); + + Assert.Equal(Math.Round((double)SK.Last().Cci!, 8), Math.Round(QL.Last().v, 8)); + } [Fact] public void ATRP() diff --git a/Tests/Validations/TA_LIB.cs b/Tests/Validations/TA_LIB.cs index 32a78094..a464cd9b 100644 --- a/Tests/Validations/TA_LIB.cs +++ b/Tests/Validations/TA_LIB.cs @@ -102,6 +102,15 @@ public class TA_LIB Assert.Equal(Math.Round(this.TALIB[this.TALIB.Length - outBegIdx - 1], 8), Math.Round(QL.Last().v, 8)); } + [Fact] + public void CCI() + { + CCI_Series QL = new(this.bars, this.period, false); + Core.Cci(this.inhigh, this.inlow, 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 RSI() { From a6ed7c78eb788cfbbe768002edd528f753ae8bb7 Mon Sep 17 00:00:00 2001 From: Miha Kralj Date: Fri, 29 Apr 2022 12:32:13 -0700 Subject: [PATCH 06/11] ALMA --- Docs/coverage.md | 2 +- Source/Basics/Abstracts.cs | 2 +- Source/Basics/TBars.cs | 17 ++++++++ Source/Indicators/ALMA_Series.cs | 66 ++++++++++++++++++++++++++++++ Tests/MovingAvg/ALMA_Test.cs | 33 +++++++++++++++ Tests/Validations/Skender_Stock.cs | 9 ++++ 6 files changed, 127 insertions(+), 2 deletions(-) create mode 100644 Source/Indicators/ALMA_Series.cs create mode 100644 Tests/MovingAvg/ALMA_Test.cs diff --git a/Docs/coverage.md b/Docs/coverage.md index 7e3a3c51..f8de0937 100644 --- a/Docs/coverage.md +++ b/Docs/coverage.md @@ -41,7 +41,7 @@ |||||| | **Moving Averages** ||||| | AFIRMA - Autoregressive Finite Impulse Response Moving Average ||||| -| ALMA - Arnaud Legoux Moving Average |||✔️|✔️| +| ALMA - Arnaud Legoux Moving Average |✔️||✔️|✔️| | ARIMA - Autoregressive Integrated Moving Average ||||| | ATR - Average True Range |✔️|✔️|✔️|✔️| | ATRP - Average True Range Percent |✔️||✔️|| diff --git a/Source/Basics/Abstracts.cs b/Source/Basics/Abstracts.cs index 85701a21..2deb84e4 100644 --- a/Source/Basics/Abstracts.cs +++ b/Source/Basics/Abstracts.cs @@ -141,7 +141,7 @@ public abstract class Single_TBars_Indicator : TSeries this._p = period; this._bars = source; this._NaN = useNaN; - this._bars.Close.Pub += this.Sub; + this._bars.Pub += this.Sub; } // overridable Add() method to add/update a single item at the end of the list diff --git a/Source/Basics/TBars.cs b/Source/Basics/TBars.cs index 5502253d..81a6db25 100644 --- a/Source/Basics/TBars.cs +++ b/Source/Basics/TBars.cs @@ -108,5 +108,22 @@ public class TBars : System.Collections.Generic.List<(DateTime t, double o, doub _ohlc4.Add((t, (o + h + l + c) * 0.25)); _hlcc4.Add((t, (h + l + c + c) * 0.25)); } + 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 }); + } + } + + } diff --git a/Source/Indicators/ALMA_Series.cs b/Source/Indicators/ALMA_Series.cs new file mode 100644 index 00000000..fde42118 --- /dev/null +++ b/Source/Indicators/ALMA_Series.cs @@ -0,0 +1,66 @@ +namespace QuanTAlib; +using System; + +/* +ALMA: Arnaud Legoux Moving Average + The ALMA moving average uses the curve of the Normal (Gauss) distribution, which + can be shifted from 0 to 1. This allows regulating the smoothness and high + sensitivity of the indicator. Sigma is another parameter that is responsible for + the shape of the curve coefficients. This moving average reduces lag of the data + in conjunction with smoothing to reduce noise. + + +Sources: + https://phemex.com/academy/what-is-arnaud-legoux-moving-averages + https://www.prorealcode.com/prorealtime-indicators/alma-arnaud-legoux-moving-average/ + + */ + +public class ALMA_Series : Single_TSeries_Indicator +{ + private readonly System.Collections.Generic.List _buffer = new(); + private readonly double[] _weight; + private double _norm; + private readonly double _offset, _sigma; + + public ALMA_Series(TSeries source, int period, double offset = 0.85, double sigma = 6.0, bool useNaN = false) + : base(source, period, useNaN) + { + _offset = offset; + _sigma = sigma; + _weight = new double[period]; + + if (this._data.Count > 0) { base.Add(this._data); } + } + + 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._buffer.RemoveAt(0); } + + if (this._buffer.Count <= _p) { calc_weights(); } + + double _weightedSum = 0; + for (int i = 0; i < this._buffer.Count; i++) { _weightedSum += _weight[i] * _buffer[i]; } + double _alma = _weightedSum / _norm; + + var ret = (TValue.t, this.Count < this._p - 1 && this._NaN ? double.NaN : _alma); + base.Add(ret, update); + } + + private void calc_weights() + { + int _len = this._buffer.Count; + _norm = 0; + double _m = _offset * (_len - 1); + double _s = _len / _sigma; + for (int i = 0; i < _len; i++) + { + double _wt = Math.Exp(-((i - _m) * (i - _m)) / (2 * _s * _s)); + _weight[i] = _wt; + _norm += _wt; + } + } + +} \ No newline at end of file diff --git a/Tests/MovingAvg/ALMA_Test.cs b/Tests/MovingAvg/ALMA_Test.cs new file mode 100644 index 00000000..fcf15738 --- /dev/null +++ b/Tests/MovingAvg/ALMA_Test.cs @@ -0,0 +1,33 @@ +using Xunit; +using System; +using QuanTAlib; + +namespace MovingAvg; +public class ALMA_Test +{ + [Fact] + public void Add_Test() + { + TSeries a = new() { 0, 1, 2, 3, 4, 5 }; + ALMA_Series c = new(a, 4); + Assert.Equal(6, c.Count); + a.Add(5); + Assert.Equal(a.Count, c.Count); + a.Add(10, update: true); + Assert.Equal(a.Count, c.Count); + } + + [Fact] + public void Edge_Test() + { + TSeries a = new() { double.NaN, double.Epsilon, double.PositiveInfinity, double.MaxValue }; + ALMA_Series c = new(a, 3); + Assert.Equal(a.Count, c.Count); + a.Add(double.NaN); + Assert.Equal(a.Count, c.Count); + a.Add(double.PositiveInfinity); + Assert.Equal(a.Count, c.Count); + + } + +} diff --git a/Tests/Validations/Skender_Stock.cs b/Tests/Validations/Skender_Stock.cs index 13037492..e2924779 100644 --- a/Tests/Validations/Skender_Stock.cs +++ b/Tests/Validations/Skender_Stock.cs @@ -152,4 +152,13 @@ public class Skender_Stock Assert.Equal(Math.Round((double)SK.Last().Rsi!, 8), Math.Round(QL.Last().v, 8)); } + + [Fact] + public void ALMA() + { + ALMA_Series QL = new(this.bars.Close, this.period, useNaN: false); + var SK = this.quotes.GetAlma(this.period); + + Assert.Equal(Math.Round((double)SK.Last().Alma!, 8), Math.Round(QL.Last().v, 8)); + } } From 6b0408741a91c5f7ac2fa6eca2076a5c4a442bdf Mon Sep 17 00:00:00 2001 From: Miha Kralj Date: Fri, 29 Apr 2022 17:40:46 -0700 Subject: [PATCH 07/11] LinReg --- Docs/coverage.md | 4 +- Source/Statistics/LINREG_Series.cs | 93 ++++++++++++++++++++++++++++++ Tests/Statistics/LINREG_Test.cs | 33 +++++++++++ Tests/Validations/Skender_Stock.cs | 12 ++++ 4 files changed, 139 insertions(+), 3 deletions(-) create mode 100644 Source/Statistics/LINREG_Series.cs create mode 100644 Tests/Statistics/LINREG_Test.cs diff --git a/Docs/coverage.md b/Docs/coverage.md index f8de0937..aff5dc6c 100644 --- a/Docs/coverage.md +++ b/Docs/coverage.md @@ -20,7 +20,7 @@ | BIAS - Bias |✔️|||✔️| | ENTR - Entropy |✔️|||✔️| | KUR - Kurtosis |✔️|||✔️| -| LINREG - Linear Regression ||✔️|✔️|| +| LINREG - Linear Regression |✔️|✔️|✔️|| | MAD - Mean Absolute Deviation |✔️||✔️|✔️| | MAPE - Mean Absolute Percent Error |✔️||✔️|| | MAX - Max value |✔️|✔️||| @@ -30,9 +30,7 @@ | PSDEV - Population Standard Deviation |✔️|||| | PVAR - Population Variance |✔️|||| | QUANTILE ||||✔️| -| RS - R-Squared Coefficient |||✔️|| | SKEW - Skewness ||||✔️| -| SLOPE - Slope |||✔️|| | SMAPE - Symmetric Mean Absolute Percent Error |✔️|||| | SDEV - Sample Standard Deviation |✔️|✔️|✔️|✔️| | VAR - Sample Variance |✔️|||✔️| diff --git a/Source/Statistics/LINREG_Series.cs b/Source/Statistics/LINREG_Series.cs new file mode 100644 index 00000000..9c7aeff0 --- /dev/null +++ b/Source/Statistics/LINREG_Series.cs @@ -0,0 +1,93 @@ +namespace QuanTAlib; +using System; + +/* +LINREG: Linear Regression (using Least Square Method) + Linear Regression provides a slope of a straight line that is the best approximation of the given set of data. + The method of least squares is a standard approach in linear regression analysis to approximate the solution + by minimizing the sum of the squares of the residuals made in the results of each individual equation. + +Additional outputs provided by LINREG: + .Intercept - y-intercept point of the best fit line + .RSquared - R-Squared (R²), Coefficient of Determination + .StdDev - Standard Deviation of data over given periods + + y = Slope * x + Intercept + +Sources: + https://en.wikipedia.org/wiki/Least_squares + + */ + +public class LINREG_Series : Single_TSeries_Indicator +{ + public readonly TSeries Intercept = new(); + public readonly TSeries RSquared = new(); + public readonly TSeries StdDev = new(); + private readonly System.Collections.Generic.List _buffer = new(); + + public LINREG_Series(TSeries source, int period, bool useNaN = false) + : base(source, period, useNaN) + { + if (this._data.Count > 0) { base.Add(this._data); } + } + + 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._buffer.RemoveAt(0); } + + int _len = this._buffer.Count; + + // get averages for period + double sumX = 0; + double sumY = 0; + + for (int p = 0; p < _len; p++) + { + sumX += this.Count - _len + 2 + p; + sumY += _buffer[p]; + } + double avgX = sumX / _len; + double avgY = sumY / _len; + + // least squares method + double sumSqX = 0; + double sumSqY = 0; + double sumSqXY = 0; + + for (int p = 0; p < _len; p++) + { + double devX = this.Count - _len + 2 + p - avgX; + double devY = _buffer[p] - avgY; + + sumSqX += devX * devX; + sumSqY += devY * devY; + sumSqXY += devX * devY; + } + + double _slope = sumSqXY / sumSqX; + double _intercept = avgY - (_slope * avgX); + + // calculate Standard Deviation and R-Squared + double stdDevX = Math.Sqrt((double)sumSqX / _len); + double stdDevY = Math.Sqrt((double)sumSqY / _len); + double _StdDev = stdDevY; + + double arrr = (stdDevX * stdDevY != 0) ? (double)sumSqXY / (stdDevX * stdDevY) / _len : 0; + double _RSquared = arrr * arrr; + + var ret = (TValue.t, this.Count < this._p - 1 && this._NaN ? double.NaN : _slope); + base.Add(ret, update); + + ret = (TValue.t, this.Count < this._p - 1 && this._NaN ? double.NaN : _intercept); + Intercept.Add(ret, update); + + ret = (TValue.t, this.Count < this._p - 1 && this._NaN ? double.NaN : _StdDev); + StdDev.Add(ret, update); + + ret = (TValue.t, this.Count < this._p - 1 && this._NaN ? double.NaN : _RSquared); + RSquared.Add(ret, update); + } +} \ No newline at end of file diff --git a/Tests/Statistics/LINREG_Test.cs b/Tests/Statistics/LINREG_Test.cs new file mode 100644 index 00000000..c0370af1 --- /dev/null +++ b/Tests/Statistics/LINREG_Test.cs @@ -0,0 +1,33 @@ +using Xunit; +using System; +using QuanTAlib; + +namespace Statistics; +public class LINREG_Test +{ + [Fact] + public void Add_Test() + { + TSeries a = new() { 0, 1, 2, 3, 4, 5 }; + LINREG_Series c = new(a, 3); + Assert.Equal(6, c.Count); + a.Add(5); + Assert.Equal(a.Count, c.Count); + a.Add(0, update: true); + Assert.Equal(a.Count, c.Count); + } + + [Fact] + public void Edge_Test() + { + TSeries a = new() { double.NaN, double.Epsilon, double.PositiveInfinity, double.MaxValue }; + LINREG_Series c = new(a, 3); + Assert.Equal(a.Count, c.Count); + a.Add(double.NaN); + Assert.Equal(a.Count, c.Count); + a.Add(double.PositiveInfinity); + Assert.Equal(a.Count, c.Count); + + } + +} diff --git a/Tests/Validations/Skender_Stock.cs b/Tests/Validations/Skender_Stock.cs index e2924779..c45517bf 100644 --- a/Tests/Validations/Skender_Stock.cs +++ b/Tests/Validations/Skender_Stock.cs @@ -161,4 +161,16 @@ public class Skender_Stock Assert.Equal(Math.Round((double)SK.Last().Alma!, 8), Math.Round(QL.Last().v, 8)); } + + [Fact] + public void LINREG() + { + LINREG_Series QL = new(this.bars.Close, this.period, useNaN: false); + var SK = this.quotes.GetSlope(this.period); + + Assert.Equal(Math.Round((double)SK.Last().Slope!, 8), Math.Round(QL.Last().v, 8)); + Assert.Equal(Math.Round((double)SK.Last().Intercept!, 8), Math.Round(QL.Intercept.Last().v, 8)); + Assert.Equal(Math.Round((double)SK.Last().RSquared!, 8), Math.Round(QL.RSquared.Last().v, 8)); + Assert.Equal(Math.Round((double)SK.Last().StdDev!, 8), Math.Round(QL.StdDev.Last().v, 8)); + } } From 28446e56ff49567dcbb93bd067aa33a1a756f2f5 Mon Sep 17 00:00:00 2001 From: Miha Kralj Date: Fri, 29 Apr 2022 17:40:46 -0700 Subject: [PATCH 08/11] LinReg --- Source/QuanTAlib.csproj | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Source/QuanTAlib.csproj b/Source/QuanTAlib.csproj index 45a52c97..6646d49f 100644 --- a/Source/QuanTAlib.csproj +++ b/Source/QuanTAlib.csproj @@ -1,7 +1,7 @@  - 0.1.12 - + 0.1.13 + Added MACD, RSI, CCI, ALMA, LINREG QuanTAlib Library of Technical Indicators for .NET Quantitative Technical Analysis library for both real-time (streaming) and historical data analysis From 819ada509c9a85de7b445136c5549a8aa95a2d3d Mon Sep 17 00:00:00 2001 From: Miha Kralj Date: Wed, 27 Apr 2022 18:32:22 -0700 Subject: [PATCH 09/11] MACD and RSI --- Source/Indicators/MACD_Series.cs | 8 ++++---- Source/Indicators/RSI_Series.cs | 8 ++++---- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/Source/Indicators/MACD_Series.cs b/Source/Indicators/MACD_Series.cs index b8172d46..0e75a26e 100644 --- a/Source/Indicators/MACD_Series.cs +++ b/Source/Indicators/MACD_Series.cs @@ -16,9 +16,9 @@ Sources: public class MACD_Series : Single_TSeries_Indicator { - private readonly EMA_Series _TSslow; - private readonly EMA_Series _TSfast; - private readonly SUB_Series _TSmacd; + private EMA_Series _TSslow; + private EMA_Series _TSfast; + private SUB_Series _TSmacd; public EMA_Series Signal { get; } public MACD_Series(TSeries source, int slow = 26, int fast = 12, int signal = 9, bool useNaN = false) @@ -27,7 +27,7 @@ public class MACD_Series : Single_TSeries_Indicator _TSslow = new(source: source, period: slow, useNaN: false); _TSfast = new(source: source, period: fast, useNaN: false); _TSmacd = new(_TSfast, _TSslow); - this.Signal = new(source: _TSmacd, period: signal, useNaN: useNaN); + Signal = new(source: _TSmacd, period: signal, useNaN: useNaN); if (source.Count > 0) { base.Add(_TSmacd); } } diff --git a/Source/Indicators/RSI_Series.cs b/Source/Indicators/RSI_Series.cs index 5cec4d88..e9d57f39 100644 --- a/Source/Indicators/RSI_Series.cs +++ b/Source/Indicators/RSI_Series.cs @@ -16,10 +16,10 @@ public class RSI_Series : Single_TSeries_Indicator { private readonly System.Collections.Generic.List _gain = new(); private readonly System.Collections.Generic.List _loss = new(); - private double _avgGain; - private double _avgLoss; - private double _lastValue; - private double _lastlastValue; + double _avgGain = 0; + double _avgLoss = 0; + double _lastValue = 0; + double _lastlastValue = 0; public RSI_Series(TSeries source, int period = 10, bool useNaN = false) : base(source, period: period, useNaN: useNaN) { if (source.Count > 0) { base.Add(source); } } From 3ae70ef64b9499458b6c7aa21686d7e25b97f9e7 Mon Sep 17 00:00:00 2001 From: Miha Kralj Date: Wed, 27 Apr 2022 18:34:59 -0700 Subject: [PATCH 10/11] RSI and MACD --- Docs/coverage.md | 310 +++++++++++++++++++++++------------------------ 1 file changed, 155 insertions(+), 155 deletions(-) diff --git a/Docs/coverage.md b/Docs/coverage.md index aff5dc6c..ee5e3cb7 100644 --- a/Docs/coverage.md +++ b/Docs/coverage.md @@ -1,155 +1,155 @@ -# Coverage of indicators - -| Indicator | QuanTAlib | TA-LIB | Skender | Pandas-TA | -|--|:--:|:--:|:--:|:--:| -| **Basics** ||||| -| OC2 - (Open+Close)/2 |✔️|||✔️| -| HL2 - (High+Low)/2 |✔️|||✔️| -| HLC3 - Typical Price |✔️|||✔️| -| OHL3 - (Open+High+Low)/3 |✔️|||✔️| -| OHLC4 - (O+H+L+C)/4 |✔️|||✔️| -| HLCC4 - Weighted Price |✔️||✔️|✔️| -| ZL - Zero Lag - De-lagged price |✔️|||✔️| -| ADD - Addition |✔️|✔️||| -| SUB - Subtraction |✔️|✔️||| -| MUL - Multiplication |✔️|✔️||| -| DIV - Division |✔️|✔️||| -|||||| -| **Statistics** ||||| -| BETA - Beta coefficient |||✔️|| -| BIAS - Bias |✔️|||✔️| -| ENTR - Entropy |✔️|||✔️| -| KUR - Kurtosis |✔️|||✔️| -| LINREG - Linear Regression |✔️|✔️|✔️|| -| MAD - Mean Absolute Deviation |✔️||✔️|✔️| -| MAPE - Mean Absolute Percent Error |✔️||✔️|| -| MAX - Max value |✔️|✔️||| -| MIN - Min value |✔️|✔️||| -| MED - Median value |✔️|✔️||✔️| -| MSE - Mean Squared Error |✔️||✔️|| -| PSDEV - Population Standard Deviation |✔️|||| -| PVAR - Population Variance |✔️|||| -| QUANTILE ||||✔️| -| SKEW - Skewness ||||✔️| -| SMAPE - Symmetric Mean Absolute Percent Error |✔️|||| -| SDEV - Sample Standard Deviation |✔️|✔️|✔️|✔️| -| VAR - Sample Variance |✔️|||✔️| -| WMAPE - Weighted Mean Absolute Percent Error |✔️|||| -| ZSCORE |||✔️|✔️| -|||||| -| **Moving Averages** ||||| -| AFIRMA - Autoregressive Finite Impulse Response Moving Average ||||| -| ALMA - Arnaud Legoux Moving Average |✔️||✔️|✔️| -| ARIMA - Autoregressive Integrated Moving Average ||||| -| ATR - Average True Range |✔️|✔️|✔️|✔️| -| ATRP - Average True Range Percent |✔️||✔️|| -| DEMA - Double EMA |✔️|✔️|✔️|✔️| -| EMA - Exponential Moving Average |✔️|✔️|✔️|✔️| -| EPMA - Endpoint Moving Average |||✔️|| -| FWMA - Fibonacci's Weighted Moving Average ||||✔️| -| HEMA - Hull Exponential Moving Average |✔️|||| -| HMA - Hull Moving Average |✔️||✔️|✔️| -| HWMA - Holt-Winter Moving Average ||||✔️| -| JMA - Jurik Moving Average |✔️|||✔️| -| KAMA - Kaufman's Adaptive Moving Average |✔️|✔️|✔️|✔️| -| LSMA - Least Squares Moving Average |||✔️|| -| MACD - Moving Average Convergence/Divergence |✔️|✔️|✔️|✔️| -| MAMA - MESA Adaptive Moving Average ||✔️|✔️|| -| MMA - Modified Moving Average |||✔️|| -| NATR - Normalized Average True Range ||✔️|✔️|✔️| -| PPMA - Pivot Point Moving Average |||✔️|| -| PWMA - Pascal's Weighted Moving Average ||||✔️| -| RMA - WildeR's Moving Average |✔️|||✔️| -| SINWMA - Sine Weighted Moving Average ||||✔️| -| SMA - Simple Moving Average |✔️|✔️|✔️|✔️| -| SMMA - Smoothed Moving Average |✔️||✔️|| -| STOCH - Stochastic Oscillator ||✔️|✔️|✔️| -| SSF - Ehler's Super Smoother Filter ||||✔️| -| SUP - Supertrend |||✔️|✔️| -| SWMA - Symmetric Weighted Moving Average ||||✔️| -| T3 - Tillson T3 Moving Average ||✔️|✔️|✔️| -| TEMA - Triple EMA |✔️|✔️|✔️|✔️| -| TRIMA - Triangular Moving Average ||✔️||✔️| -| VIDYA - Variable Index Dynamic Average ||||✔️| -| VWAP - Volume Weighted Average Price |||✔️|✔️| -| VWMA - Volume Weighted Moving Average |||✔️|✔️| -| WMA - Weighted Moving Average |✔️|✔️|✔️|✔️| -| ZLEMA - Zero Lag EMA |✔️|||✔️| -|||||| -| **Oscillators and Indices** ||||| -| AC - Acceleration Oscillator ||||✔️| -| AD - Chaikin Accumulation Distribution ||✔️|✔️|✔️| -| ADOSC - Chaikin Accumulation Distribution Oscillator ||✔️|✔️|| -| ADX - Average Directional Movement Index ||✔️|✔️|✔️| -| ADXR - Average Directional Movement Index Rating ||✔️|✔️|| -| AO - Awesome Oscillator |||✔️|✔️| -| APO - Absolute Price Oscillator ||✔️||✔️| -| AROON - Aroon oscillator ||✔️|✔️|✔️| -| BBANDS - Bollinger Bands ||✔️|✔️|✔️| -| BOP - Balance of Power ||✔️|✔️|✔️| -| CCI - Commodity Channel Index |✔️|✔️|✔️|✔️| -| CFO - Chande Forcast Oscillator ||||✔️| -| CMF - Chaikin Money Flow |||✔️|✔️| -| CMO - Chande Momentum Oscillator ||✔️||✔️| -| COG - Center of Gravity ||||✔️| -| CRSI - Connor RSI |||✔️|| -| CTI - Ehler's Correlation Trend Indicator ||||✔️| -| DMI - Directional Movement Index ||✔️|✔️|✔️| -| EFI - Elder Ray's Force Index |||✔️|✔️| -| GAT - Alligator oscillator |||✔️|| -| KRI - Kairi Relative Index ||||| -| KVO - Klinger Volume Oscillator |||✔️|✔️| -| MFI - Money Flow Index ||✔️|✔️|✔️| -| MOM - Momentum |||✔️|✔️| -| NVI - Negative Volume Index ||||✔️| -| PO - Price Oscillator ||||✔️| -| PPO - Percentage Price Oscillator ||✔️||✔️| -| PVI - Positive Volume Index ||||✔️| -| RSI - Relative Strength Index |✔️|✔️|✔️|✔️| -| RVGI - Relative Vigor Index ||||✔️| -| SRSI - Stochastic RSI |||✔️|✔️| -| TRIX - 1-day ROC of TEMA ||✔️|✔️|✔️| -| TSI - True Strength Index |||✔️|✔️| -| UI - Ulcer Index |||✔️|✔️| -| UO - Ultimate Oscillator ||✔️|✔️|✔️| -| WGAT - Williams Alligator |||✔️|| -|||||| -| **Volume** ||||| -| AOBV - Archer On-Balance Volume ||||✔️| -| OBV - On-Balance Volume ||✔️|✔️|✔️| -| PRS - Price Relative Strength |||✔️|| -| PVOL - Price-Volume ||||| -| PVR - Price Volume Rank ||||✔️| -| PVT - Price Volume Trend ||||✔️| -| VP - Volume Profile ||||✔️| -|||||| -|**Unsorted**||||| -| CHN - Price Channel |||✔️|| -| COPPOCK - Coppock Curve ||||✔️| -| CORREL - Pearson's Correlation Coefficient ||✔️|✔️|| -| EOM - Ease of Movement ||||✔️| -| HILO - Gann High-Low Activator ||||✔️| -| HV - Historical Volatility |||✔️|| -| HT - HT Trendline |||✔️|| -| ICH - Ichimoku |||✔️|✔️| -| MCGD - McGinley Dynamic ||||✔️| -| ROC - Rate of Change ||✔️|✔️|✔️| -| SAR - Parabolic Stop and Reverse ||✔️|✔️|✔️| -| STC - Schaff Trend Cycle |||✔️|✔️| -| TR - True Range ||✔️|✔️|✔️| -| WILLR - Larry Williams' %R ||✔️|✔️|✔️| -| HURST - Hurst Exponent |||✔️|| -| VOR - Vortex Indicator |||✔️|✔️| -| DON - Donchian Channels |||✔️|✔️| -| FCB - Fractal Chaos Bands |||✔️|| -| KEL - Keltner Channels |||✔️|✔️| -| PVT - Pivot Points |||✔️|| -| STARC - Starc Bands |||✔️|| -| DPO - De-trended Price Oscillator |||✔️|✔️| -| KDJ - KDJ Index |||✔️|✔️| -| SMI - Stochastic Momentum Index |||✔️|✔️| -| CHAND - Chandelier Exit |||✔️|| -| VSTOP - Volatility Stop |||✔️|| -| PVO - Percentage Volume Oscillator |||✔️|✔️| -| Hilbert Transform Instantaneous Trendline ||||| -| PMO - Price Momentum Oscillator |||✔️|| +# Coverage of indicators + +| Indicator | QuanTAlib | TA-LIB | Skender | Pandas-TA | +|--|:--:|:--:|:--:|:--:| +| **Basics** ||||| +| OC2 - (Open+Close)/2 |✔️|||✔️| +| HL2 - (High+Low)/2 |✔️|||✔️| +| HLC3 - Typical Price |✔️|||✔️| +| OHL3 - (Open+High+Low)/3 |✔️|||✔️| +| OHLC4 - (O+H+L+C)/4 |✔️|||✔️| +| HLCC4 - Weighted Price |✔️||✔️|✔️| +| ZL - Zero Lag - De-lagged price |✔️|||✔️| +| ADD - Addition |✔️|✔️||| +| SUB - Subtraction |✔️|✔️||| +| MUL - Multiplication |✔️|✔️||| +| DIV - Division |✔️|✔️||| +|||||| +| **Statistics** ||||| +| BETA - Beta coefficient |||✔️|| +| BIAS - Bias |✔️|||✔️| +| ENTR - Entropy |✔️|||✔️| +| KUR - Kurtosis |✔️|||✔️| +| LINREG - Linear Regression |✔️|✔️|✔️|| +| MAD - Mean Absolute Deviation |✔️||✔️|✔️| +| MAPE - Mean Absolute Percent Error |✔️||✔️|| +| MAX - Max value |✔️|✔️||| +| MIN - Min value |✔️|✔️||| +| MED - Median value |✔️|✔️||✔️| +| MSE - Mean Squared Error |✔️||✔️|| +| PSDEV - Population Standard Deviation |✔️|||| +| PVAR - Population Variance |✔️|||| +| QUANTILE ||||✔️| +| SKEW - Skewness ||||✔️| +| SMAPE - Symmetric Mean Absolute Percent Error |✔️|||| +| SDEV - Sample Standard Deviation |✔️|✔️|✔️|✔️| +| VAR - Sample Variance |✔️|||✔️| +| WMAPE - Weighted Mean Absolute Percent Error |✔️|||| +| ZSCORE |||✔️|✔️| +|||||| +| **Moving Averages** ||||| +| AFIRMA - Autoregressive Finite Impulse Response Moving Average ||||| +| ALMA - Arnaud Legoux Moving Average |✔️||✔️|✔️| +| ARIMA - Autoregressive Integrated Moving Average ||||| +| ATR - Average True Range |✔️|✔️|✔️|✔️| +| ATRP - Average True Range Percent |✔️||✔️|| +| DEMA - Double EMA |✔️|✔️|✔️|✔️| +| EMA - Exponential Moving Average |✔️|✔️|✔️|✔️| +| EPMA - Endpoint Moving Average |||✔️|| +| FWMA - Fibonacci's Weighted Moving Average ||||✔️| +| HEMA - Hull Exponential Moving Average |✔️|||| +| HMA - Hull Moving Average |✔️||✔️|✔️| +| HWMA - Holt-Winter Moving Average ||||✔️| +| JMA - Jurik Moving Average |✔️|||✔️| +| KAMA - Kaufman's Adaptive Moving Average |✔️|✔️|✔️|✔️| +| LSMA - Least Squares Moving Average |||✔️|| +| MACD - Moving Average Convergence/Divergence |✔️|✔️|✔️|✔️| +| MAMA - MESA Adaptive Moving Average ||✔️|✔️|| +| MMA - Modified Moving Average |||✔️|| +| NATR - Normalized Average True Range ||✔️|✔️|✔️| +| PPMA - Pivot Point Moving Average |||✔️|| +| PWMA - Pascal's Weighted Moving Average ||||✔️| +| RMA - WildeR's Moving Average |✔️|||✔️| +| SINWMA - Sine Weighted Moving Average ||||✔️| +| SMA - Simple Moving Average |✔️|✔️|✔️|✔️| +| SMMA - Smoothed Moving Average |✔️||✔️|| +| STOCH - Stochastic Oscillator ||✔️|✔️|✔️| +| SSF - Ehler's Super Smoother Filter ||||✔️| +| SUP - Supertrend |||✔️|✔️| +| SWMA - Symmetric Weighted Moving Average ||||✔️| +| T3 - Tillson T3 Moving Average ||✔️|✔️|✔️| +| TEMA - Triple EMA |✔️|✔️|✔️|✔️| +| TRIMA - Triangular Moving Average ||✔️||✔️| +| VIDYA - Variable Index Dynamic Average ||||✔️| +| VWAP - Volume Weighted Average Price |||✔️|✔️| +| VWMA - Volume Weighted Moving Average |||✔️|✔️| +| WMA - Weighted Moving Average |✔️|✔️|✔️|✔️| +| ZLEMA - Zero Lag EMA |✔️|||✔️| +|||||| +| **Oscillators and Indices** ||||| +| AC - Acceleration Oscillator ||||✔️| +| AD - Chaikin Accumulation Distribution ||✔️|✔️|✔️| +| ADOSC - Chaikin Accumulation Distribution Oscillator ||✔️|✔️|| +| ADX - Average Directional Movement Index ||✔️|✔️|✔️| +| ADXR - Average Directional Movement Index Rating ||✔️|✔️|| +| AO - Awesome Oscillator |||✔️|✔️| +| APO - Absolute Price Oscillator ||✔️||✔️| +| AROON - Aroon oscillator ||✔️|✔️|✔️| +| BBANDS - Bollinger Bands ||✔️|✔️|✔️| +| BOP - Balance of Power ||✔️|✔️|✔️| +| CCI - Commodity Channel Index |✔️|✔️|✔️|✔️| +| CFO - Chande Forcast Oscillator ||||✔️| +| CMF - Chaikin Money Flow |||✔️|✔️| +| CMO - Chande Momentum Oscillator ||✔️||✔️| +| COG - Center of Gravity ||||✔️| +| CRSI - Connor RSI |||✔️|| +| CTI - Ehler's Correlation Trend Indicator ||||✔️| +| DMI - Directional Movement Index ||✔️|✔️|✔️| +| EFI - Elder Ray's Force Index |||✔️|✔️| +| GAT - Alligator oscillator |||✔️|| +| KRI - Kairi Relative Index ||||| +| KVO - Klinger Volume Oscillator |||✔️|✔️| +| MFI - Money Flow Index ||✔️|✔️|✔️| +| MOM - Momentum |||✔️|✔️| +| NVI - Negative Volume Index ||||✔️| +| PO - Price Oscillator ||||✔️| +| PPO - Percentage Price Oscillator ||✔️||✔️| +| PVI - Positive Volume Index ||||✔️| +| RSI - Relative Strength Index |✔️|✔️|✔️|✔️| +| RVGI - Relative Vigor Index ||||✔️| +| SRSI - Stochastic RSI |||✔️|✔️| +| TRIX - 1-day ROC of TEMA ||✔️|✔️|✔️| +| TSI - True Strength Index |||✔️|✔️| +| UI - Ulcer Index |||✔️|✔️| +| UO - Ultimate Oscillator ||✔️|✔️|✔️| +| WGAT - Williams Alligator |||✔️|| +|||||| +| **Volume** ||||| +| AOBV - Archer On-Balance Volume ||||✔️| +| OBV - On-Balance Volume ||✔️|✔️|✔️| +| PRS - Price Relative Strength |||✔️|| +| PVOL - Price-Volume ||||| +| PVR - Price Volume Rank ||||✔️| +| PVT - Price Volume Trend ||||✔️| +| VP - Volume Profile ||||✔️| +|||||| +|**Unsorted**||||| +| CHN - Price Channel |||✔️|| +| COPPOCK - Coppock Curve ||||✔️| +| CORREL - Pearson's Correlation Coefficient ||✔️|✔️|| +| EOM - Ease of Movement ||||✔️| +| HILO - Gann High-Low Activator ||||✔️| +| HV - Historical Volatility |||✔️|| +| HT - HT Trendline |||✔️|| +| ICH - Ichimoku |||✔️|✔️| +| MCGD - McGinley Dynamic ||||✔️| +| ROC - Rate of Change ||✔️|✔️|✔️| +| SAR - Parabolic Stop and Reverse ||✔️|✔️|✔️| +| STC - Schaff Trend Cycle |||✔️|✔️| +| TR - True Range ||✔️|✔️|✔️| +| WILLR - Larry Williams' %R ||✔️|✔️|✔️| +| HURST - Hurst Exponent |||✔️|| +| VOR - Vortex Indicator |||✔️|✔️| +| DON - Donchian Channels |||✔️|✔️| +| FCB - Fractal Chaos Bands |||✔️|| +| KEL - Keltner Channels |||✔️|✔️| +| PVT - Pivot Points |||✔️|| +| STARC - Starc Bands |||✔️|| +| DPO - De-trended Price Oscillator |||✔️|✔️| +| KDJ - KDJ Index |||✔️|✔️| +| SMI - Stochastic Momentum Index |||✔️|✔️| +| CHAND - Chandelier Exit |||✔️|| +| VSTOP - Volatility Stop |||✔️|| +| PVO - Percentage Volume Oscillator |||✔️|✔️| +| Hilbert Transform Instantaneous Trendline ||||| +| PMO - Price Momentum Oscillator |||✔️|| From 9a0dd12ab2ab82d618f86fcbd124ada2a42c666b Mon Sep 17 00:00:00 2001 From: Miha Kralj Date: Thu, 28 Apr 2022 12:52:00 -0700 Subject: [PATCH 11/11] CCI --- Source/Indicators/MACD_Series.cs | 8 ++++---- Source/Indicators/RSI_Series.cs | 8 ++++---- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/Source/Indicators/MACD_Series.cs b/Source/Indicators/MACD_Series.cs index 0e75a26e..b8172d46 100644 --- a/Source/Indicators/MACD_Series.cs +++ b/Source/Indicators/MACD_Series.cs @@ -16,9 +16,9 @@ Sources: public class MACD_Series : Single_TSeries_Indicator { - private EMA_Series _TSslow; - private EMA_Series _TSfast; - private SUB_Series _TSmacd; + private readonly EMA_Series _TSslow; + private readonly EMA_Series _TSfast; + private readonly SUB_Series _TSmacd; public EMA_Series Signal { get; } public MACD_Series(TSeries source, int slow = 26, int fast = 12, int signal = 9, bool useNaN = false) @@ -27,7 +27,7 @@ public class MACD_Series : Single_TSeries_Indicator _TSslow = new(source: source, period: slow, useNaN: false); _TSfast = new(source: source, period: fast, useNaN: false); _TSmacd = new(_TSfast, _TSslow); - Signal = new(source: _TSmacd, period: signal, useNaN: useNaN); + this.Signal = new(source: _TSmacd, period: signal, useNaN: useNaN); if (source.Count > 0) { base.Add(_TSmacd); } } diff --git a/Source/Indicators/RSI_Series.cs b/Source/Indicators/RSI_Series.cs index e9d57f39..5cec4d88 100644 --- a/Source/Indicators/RSI_Series.cs +++ b/Source/Indicators/RSI_Series.cs @@ -16,10 +16,10 @@ public class RSI_Series : Single_TSeries_Indicator { private readonly System.Collections.Generic.List _gain = new(); private readonly System.Collections.Generic.List _loss = new(); - double _avgGain = 0; - double _avgLoss = 0; - double _lastValue = 0; - double _lastlastValue = 0; + private double _avgGain; + private double _avgLoss; + private double _lastValue; + private double _lastlastValue; public RSI_Series(TSeries source, int period = 10, bool useNaN = false) : base(source, period: period, useNaN: useNaN) { if (source.Count > 0) { base.Add(source); } }