mirror of
https://github.com/mihakralj/QuanTAlib.git
synced 2026-08-20 11:38:05 +00:00
Dpo chart + refactored tests
This commit is contained in:
@@ -1,272 +1,132 @@
|
||||
using Xunit;
|
||||
using System.Security.Cryptography;
|
||||
|
||||
namespace QuanTAlib.Tests;
|
||||
|
||||
public class StatisticsUpdateTests
|
||||
public class StatisticsUpdateTests : UpdateTestBase
|
||||
{
|
||||
private readonly RandomNumberGenerator rng = RandomNumberGenerator.Create();
|
||||
private const int RandomUpdates = 100;
|
||||
private const double ReferenceValue = 100.0;
|
||||
private const int precision = 8;
|
||||
|
||||
private double GetRandomDouble()
|
||||
{
|
||||
byte[] bytes = new byte[8];
|
||||
rng.GetBytes(bytes);
|
||||
return ((double)BitConverter.ToUInt64(bytes, 0) / ulong.MaxValue * 200) - 100; // Range: -100 to 100
|
||||
}
|
||||
|
||||
private TBar GetRandomBar(bool IsNew)
|
||||
{
|
||||
double open = GetRandomDouble();
|
||||
double high = open + Math.Abs(GetRandomDouble());
|
||||
double low = open - Math.Abs(GetRandomDouble());
|
||||
double close = low + ((high - low) * GetRandomDouble());
|
||||
return new TBar(DateTime.Now, open, high, low, close, 1000, IsNew);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Beta_Update()
|
||||
{
|
||||
var indicator = new Beta(period: 14);
|
||||
TBar marketBar = GetRandomBar(true);
|
||||
TBar assetBar = GetRandomBar(true);
|
||||
double initialValue = indicator.Calc(marketBar, assetBar);
|
||||
|
||||
for (int i = 0; i < RandomUpdates; i++)
|
||||
{
|
||||
indicator.Calc(GetRandomBar(false), GetRandomBar(false));
|
||||
}
|
||||
double finalValue = indicator.Calc(new TBar(marketBar.Time, marketBar.Open, marketBar.High, marketBar.Low, marketBar.Close, marketBar.Volume, false),
|
||||
new TBar(assetBar.Time, assetBar.Open, assetBar.High, assetBar.Low, assetBar.Close, assetBar.Volume, false));
|
||||
|
||||
Assert.Equal(initialValue, finalValue, precision);
|
||||
TestDualTBarUpdate(indicator, indicator.Calc);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Corr_Update()
|
||||
{
|
||||
var indicator = new Corr(period: 14);
|
||||
double initialValue = indicator.Calc(new TValue(DateTime.Now, ReferenceValue, IsNew: true), new TValue(DateTime.Now, ReferenceValue, IsNew: true));
|
||||
|
||||
for (int i = 0; i < RandomUpdates; i++)
|
||||
{
|
||||
indicator.Calc(new TValue(DateTime.Now, GetRandomDouble(), IsNew: false), new TValue(DateTime.Now, GetRandomDouble(), IsNew: false));
|
||||
}
|
||||
double finalValue = indicator.Calc(new TValue(DateTime.Now, ReferenceValue, IsNew: false), new TValue(DateTime.Now, ReferenceValue, IsNew: false));
|
||||
|
||||
Assert.Equal(initialValue, finalValue, precision);
|
||||
TestDualTValueUpdate(indicator, indicator.Calc);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Curvature_Update()
|
||||
{
|
||||
var indicator = new Curvature(period: 14);
|
||||
double initialValue = indicator.Calc(new TValue(DateTime.Now, ReferenceValue, IsNew: true));
|
||||
|
||||
for (int i = 0; i < RandomUpdates; i++)
|
||||
{
|
||||
indicator.Calc(new TValue(DateTime.Now, GetRandomDouble(), IsNew: false));
|
||||
}
|
||||
double finalValue = indicator.Calc(new TValue(DateTime.Now, ReferenceValue, IsNew: false));
|
||||
|
||||
Assert.Equal(initialValue, finalValue, precision);
|
||||
TestTValueUpdate(indicator, indicator.Calc);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Entropy_Update()
|
||||
{
|
||||
var indicator = new Entropy(period: 14);
|
||||
double initialValue = indicator.Calc(new TValue(DateTime.Now, ReferenceValue, IsNew: true));
|
||||
|
||||
for (int i = 0; i < RandomUpdates; i++)
|
||||
{
|
||||
indicator.Calc(new TValue(DateTime.Now, GetRandomDouble(), IsNew: false));
|
||||
}
|
||||
double finalValue = indicator.Calc(new TValue(DateTime.Now, ReferenceValue, IsNew: false));
|
||||
|
||||
Assert.Equal(initialValue, finalValue, precision);
|
||||
TestTValueUpdate(indicator, indicator.Calc);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Hurst_Update()
|
||||
{
|
||||
var indicator = new Hurst(period: 100, minLength: 10);
|
||||
TBar r = GetRandomBar(true);
|
||||
double initialValue = indicator.Calc(r);
|
||||
|
||||
for (int i = 0; i < RandomUpdates; i++)
|
||||
{
|
||||
indicator.Calc(GetRandomBar(IsNew: false));
|
||||
}
|
||||
double finalValue = indicator.Calc(new TBar(r.Time, r.Open, r.High, r.Low, r.Close, r.Volume, IsNew: false));
|
||||
|
||||
Assert.Equal(initialValue, finalValue, precision);
|
||||
TestTBarUpdate(indicator, indicator.Calc);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Kurtosis_Update()
|
||||
{
|
||||
var indicator = new Kurtosis(period: 14);
|
||||
double initialValue = indicator.Calc(new TValue(DateTime.Now, ReferenceValue, IsNew: true));
|
||||
|
||||
for (int i = 0; i < RandomUpdates; i++)
|
||||
{
|
||||
indicator.Calc(new TValue(DateTime.Now, GetRandomDouble(), IsNew: false));
|
||||
}
|
||||
double finalValue = indicator.Calc(new TValue(DateTime.Now, ReferenceValue, IsNew: false));
|
||||
|
||||
Assert.Equal(initialValue, finalValue, precision);
|
||||
TestTValueUpdate(indicator, indicator.Calc);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Max_Update()
|
||||
{
|
||||
var indicator = new Max(period: 14);
|
||||
double initialValue = indicator.Calc(new TValue(DateTime.Now, ReferenceValue, IsNew: true));
|
||||
|
||||
for (int i = 0; i < RandomUpdates; i++)
|
||||
{
|
||||
indicator.Calc(new TValue(DateTime.Now, GetRandomDouble(), IsNew: false));
|
||||
}
|
||||
double finalValue = indicator.Calc(new TValue(DateTime.Now, ReferenceValue, IsNew: false));
|
||||
|
||||
Assert.Equal(initialValue, finalValue, precision);
|
||||
TestTValueUpdate(indicator, indicator.Calc);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Median_Update()
|
||||
{
|
||||
var indicator = new Median(period: 14);
|
||||
double initialValue = indicator.Calc(new TValue(DateTime.Now, ReferenceValue, IsNew: true));
|
||||
|
||||
for (int i = 0; i < RandomUpdates; i++)
|
||||
{
|
||||
indicator.Calc(new TValue(DateTime.Now, GetRandomDouble(), IsNew: false));
|
||||
}
|
||||
double finalValue = indicator.Calc(new TValue(DateTime.Now, ReferenceValue, IsNew: false));
|
||||
|
||||
Assert.Equal(initialValue, finalValue, precision);
|
||||
TestTValueUpdate(indicator, indicator.Calc);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Min_Update()
|
||||
{
|
||||
var indicator = new Min(period: 14);
|
||||
double initialValue = indicator.Calc(new TValue(DateTime.Now, ReferenceValue, IsNew: true));
|
||||
|
||||
for (int i = 0; i < RandomUpdates; i++)
|
||||
{
|
||||
indicator.Calc(new TValue(DateTime.Now, GetRandomDouble(), IsNew: false));
|
||||
}
|
||||
double finalValue = indicator.Calc(new TValue(DateTime.Now, ReferenceValue, IsNew: false));
|
||||
|
||||
Assert.Equal(initialValue, finalValue, precision);
|
||||
TestTValueUpdate(indicator, indicator.Calc);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Mode_Update()
|
||||
{
|
||||
var indicator = new Mode(period: 14);
|
||||
double initialValue = indicator.Calc(new TValue(DateTime.Now, ReferenceValue, IsNew: true));
|
||||
|
||||
for (int i = 0; i < RandomUpdates; i++)
|
||||
{
|
||||
indicator.Calc(new TValue(DateTime.Now, GetRandomDouble(), IsNew: false));
|
||||
}
|
||||
double finalValue = indicator.Calc(new TValue(DateTime.Now, ReferenceValue, IsNew: false));
|
||||
|
||||
Assert.Equal(initialValue, finalValue, precision);
|
||||
TestTValueUpdate(indicator, indicator.Calc);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Percentile_Update()
|
||||
{
|
||||
var indicator = new Percentile(period: 14, percent: 50);
|
||||
double initialValue = indicator.Calc(new TValue(DateTime.Now, ReferenceValue, IsNew: true));
|
||||
|
||||
for (int i = 0; i < RandomUpdates; i++)
|
||||
{
|
||||
indicator.Calc(new TValue(DateTime.Now, GetRandomDouble(), IsNew: false));
|
||||
}
|
||||
double finalValue = indicator.Calc(new TValue(DateTime.Now, ReferenceValue, IsNew: false));
|
||||
|
||||
Assert.Equal(initialValue, finalValue, precision);
|
||||
TestTValueUpdate(indicator, indicator.Calc);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Skew_Update()
|
||||
{
|
||||
var indicator = new Skew(period: 14);
|
||||
double initialValue = indicator.Calc(new TValue(DateTime.Now, ReferenceValue, IsNew: true));
|
||||
|
||||
for (int i = 0; i < RandomUpdates; i++)
|
||||
{
|
||||
indicator.Calc(new TValue(DateTime.Now, GetRandomDouble(), IsNew: false));
|
||||
}
|
||||
double finalValue = indicator.Calc(new TValue(DateTime.Now, ReferenceValue, IsNew: false));
|
||||
|
||||
Assert.Equal(initialValue, finalValue, precision);
|
||||
TestTValueUpdate(indicator, indicator.Calc);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Slope_Update()
|
||||
{
|
||||
var indicator = new Slope(period: 14);
|
||||
double initialValue = indicator.Calc(new TValue(DateTime.Now, ReferenceValue, IsNew: true));
|
||||
|
||||
for (int i = 0; i < RandomUpdates; i++)
|
||||
{
|
||||
indicator.Calc(new TValue(DateTime.Now, GetRandomDouble(), IsNew: false));
|
||||
}
|
||||
double finalValue = indicator.Calc(new TValue(DateTime.Now, ReferenceValue, IsNew: false));
|
||||
|
||||
Assert.Equal(initialValue, finalValue, precision);
|
||||
TestTValueUpdate(indicator, indicator.Calc);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Stddev_Update()
|
||||
{
|
||||
var indicator = new Stddev(period: 14);
|
||||
double initialValue = indicator.Calc(new TValue(DateTime.Now, ReferenceValue, IsNew: true));
|
||||
TestTValueUpdate(indicator, indicator.Calc);
|
||||
}
|
||||
|
||||
for (int i = 0; i < RandomUpdates; i++)
|
||||
{
|
||||
indicator.Calc(new TValue(DateTime.Now, GetRandomDouble(), IsNew: false));
|
||||
}
|
||||
double finalValue = indicator.Calc(new TValue(DateTime.Now, ReferenceValue, IsNew: false));
|
||||
[Fact]
|
||||
public void Theil_Update()
|
||||
{
|
||||
var indicator = new Theil(period: 14);
|
||||
TestTValueUpdate(indicator, indicator.Calc);
|
||||
}
|
||||
|
||||
Assert.Equal(initialValue, finalValue, precision);
|
||||
[Fact]
|
||||
public void Tsf_Update()
|
||||
{
|
||||
var indicator = new Tsf(period: 14);
|
||||
TestTValueUpdate(indicator, indicator.Calc);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Variance_Update()
|
||||
{
|
||||
var indicator = new Variance(period: 14);
|
||||
double initialValue = indicator.Calc(new TValue(DateTime.Now, ReferenceValue, IsNew: true));
|
||||
|
||||
for (int i = 0; i < RandomUpdates; i++)
|
||||
{
|
||||
indicator.Calc(new TValue(DateTime.Now, GetRandomDouble(), IsNew: false));
|
||||
}
|
||||
double finalValue = indicator.Calc(new TValue(DateTime.Now, ReferenceValue, IsNew: false));
|
||||
|
||||
Assert.Equal(initialValue, finalValue, precision);
|
||||
TestTValueUpdate(indicator, indicator.Calc);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Zscore_Update()
|
||||
{
|
||||
var indicator = new Zscore(period: 14);
|
||||
double initialValue = indicator.Calc(new TValue(DateTime.Now, ReferenceValue, IsNew: true));
|
||||
|
||||
for (int i = 0; i < RandomUpdates; i++)
|
||||
{
|
||||
indicator.Calc(new TValue(DateTime.Now, GetRandomDouble(), IsNew: false));
|
||||
}
|
||||
double finalValue = indicator.Calc(new TValue(DateTime.Now, ReferenceValue, IsNew: false));
|
||||
|
||||
Assert.Equal(initialValue, finalValue, precision);
|
||||
TestTValueUpdate(indicator, indicator.Calc);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user