From e9d7ba8cf12291d50ea434fd237297313893611b Mon Sep 17 00:00:00 2001 From: Miha Kralj Date: Wed, 27 Apr 2022 18:34:59 -0700 Subject: [PATCH] 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)); + } }