mirror of
https://github.com/mihakralj/QuanTAlib.git
synced 2026-08-17 10:08:05 +00:00
Bollinger Bands® with validation tests
This commit is contained in:
@@ -0,0 +1,58 @@
|
||||
using Xunit;
|
||||
using System;
|
||||
using QuanTAlib;
|
||||
|
||||
namespace MovingAvg;
|
||||
public class BBANDS_Test
|
||||
{
|
||||
[Fact]
|
||||
public void Add_Test()
|
||||
{
|
||||
TSeries a = new() { 0, 1, 2, 3, 4, 5 };
|
||||
BBANDS_Series c = new(a, 4,2.5);
|
||||
Assert.Equal(6, c.Count);
|
||||
a.Add(5);
|
||||
Assert.Equal(a.Count, c.Count);
|
||||
Assert.Equal(a.Count, c.Mid.Count);
|
||||
Assert.Equal(a.Count, c.Upper.Count);
|
||||
Assert.Equal(a.Count, c.Lower.Count);
|
||||
Assert.Equal(a.Count, c.PercentB.Count);
|
||||
Assert.Equal(a.Count, c.Zscore.Count);
|
||||
Assert.Equal(a.Count, c.Bandwidth.Count);
|
||||
|
||||
a.Add(0, update: true);
|
||||
Assert.Equal(a.Count, c.Count);
|
||||
Assert.Equal(a.Count, c.Mid.Count);
|
||||
Assert.Equal(a.Count, c.Upper.Count);
|
||||
Assert.Equal(a.Count, c.Lower.Count);
|
||||
Assert.Equal(a.Count, c.PercentB.Count);
|
||||
Assert.Equal(a.Count, c.Zscore.Count);
|
||||
Assert.Equal(a.Count, c.Bandwidth.Count);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Edge_Test()
|
||||
{
|
||||
TSeries a = new() { double.NaN, double.Epsilon, double.PositiveInfinity, double.MaxValue };
|
||||
BBANDS_Series c = new(a, 4, 2.5);
|
||||
Assert.Equal(a.Count, c.Count);
|
||||
a.Add(double.NaN);
|
||||
Assert.Equal(a.Count, c.Count);
|
||||
Assert.Equal(a.Count, c.Mid.Count);
|
||||
Assert.Equal(a.Count, c.Upper.Count);
|
||||
Assert.Equal(a.Count, c.Lower.Count);
|
||||
Assert.Equal(a.Count, c.PercentB.Count);
|
||||
Assert.Equal(a.Count, c.Zscore.Count);
|
||||
Assert.Equal(a.Count, c.Bandwidth.Count);
|
||||
a.Add(double.PositiveInfinity);
|
||||
Assert.Equal(a.Count, c.Count);
|
||||
Assert.Equal(a.Count, c.Mid.Count);
|
||||
Assert.Equal(a.Count, c.Upper.Count);
|
||||
Assert.Equal(a.Count, c.Lower.Count);
|
||||
Assert.Equal(a.Count, c.PercentB.Count);
|
||||
Assert.Equal(a.Count, c.Zscore.Count);
|
||||
Assert.Equal(a.Count, c.Bandwidth.Count);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -160,9 +160,25 @@ public class Skender_Stock
|
||||
var SK = this.quotes.GetMacd(12,26,9);
|
||||
|
||||
Assert.Equal(Math.Round((double)SK.Last().Macd!, 8), Math.Round(QL.Last().v, 8));
|
||||
Assert.Equal(Math.Round((double)SK.Last().Signal!, 8), Math.Round(QL.Signal.Last().v, 8));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void BBANDS()
|
||||
{
|
||||
BBANDS_Series QL = new(this.bars.Close, this.period, 2.0, useNaN: false);
|
||||
var SK = this.quotes.GetBollingerBands(this.period, 2.0);
|
||||
|
||||
Assert.Equal(Math.Round((double)SK.Last().Sma!, 8), Math.Round(QL.Mid.Last().v, 8));
|
||||
Assert.Equal(Math.Round((double)SK.Last().UpperBand!, 8), Math.Round(QL.Upper.Last().v, 8));
|
||||
Assert.Equal(Math.Round((double)SK.Last().LowerBand!, 8), Math.Round(QL.Lower.Last().v, 8));
|
||||
Assert.Equal(Math.Round((double)SK.Last().Width!, 8), Math.Round(QL.Bandwidth.Last().v, 8));
|
||||
Assert.Equal(Math.Round((double)SK.Last().PercentB!, 8), Math.Round(QL.PercentB.Last().v, 8));
|
||||
Assert.Equal(Math.Round((double)SK.Last().ZScore!, 8), Math.Round(QL.Zscore.Last().v, 8));
|
||||
}
|
||||
|
||||
|
||||
[Fact]
|
||||
public void RSI()
|
||||
{
|
||||
RSI_Series QL = new(this.bars.Close, this.period, useNaN: false);
|
||||
|
||||
@@ -164,8 +164,25 @@ public class TA_LIB
|
||||
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));
|
||||
Assert.Equal(Math.Round(macdSignal[macdSignal.Length - outBegIdx - 1], 8), Math.Round(QL.Signal.Last().v, 8));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void BBANDS()
|
||||
{
|
||||
double[] outMiddle = new double[this.bars.Count];
|
||||
double[] outUpper = new double[this.bars.Count];
|
||||
double[] outLower = new double[this.bars.Count];
|
||||
BBANDS_Series QL = new(this.bars.Close, period:26, multiplier:2.0, false);
|
||||
Core.Bbands(this.inclose, 0, this.bars.Count - 1, outRealUpperBand: outUpper, outRealMiddleBand: outMiddle, outRealLowerBand: outLower, out int outBegIdx, out _, optInTimePeriod:26, optInNbDevUp:2.0, optInNbDevDn:2.0);
|
||||
Assert.Equal(Math.Round(outUpper[outUpper.Length - outBegIdx - 1], 8), Math.Round(QL.Upper.Last().v, 8));
|
||||
Assert.Equal(Math.Round(outMiddle[outMiddle.Length - outBegIdx - 1], 8), Math.Round(QL.Mid.Last().v, 8));
|
||||
Assert.Equal(Math.Round(outLower[outLower.Length - outBegIdx - 1], 8), Math.Round(QL.Lower.Last().v, 8));
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
[Fact]
|
||||
public void HL2()
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user