This commit is contained in:
Miha Kralj
2024-10-08 17:58:02 -07:00
parent 00b5bf3242
commit 14ec3cd2b0
11 changed files with 204 additions and 156 deletions
+30 -36
View File
@@ -1,33 +1,36 @@
using Xunit;
using TALib;
using System.Diagnostics.CodeAnalysis;
using System.Security.Cryptography;
namespace QuanTAlib;
[SuppressMessage("Security", "SCS0005:Weak random number generator.", Justification = "Acceptable for tests")]
public class TAlibTests
{
private readonly TBarSeries bars;
private readonly GbmFeed feed;
private readonly Random rnd;
private readonly RandomNumberGenerator rng;
private readonly double range;
private readonly int iterations;
private readonly double[] data;
private readonly double[] TALIB;
public TAlibTests()
{
rnd = new((int)DateTime.Now.Ticks);
rng = RandomNumberGenerator.Create();
feed = new(sigma: 0.5, mu: 0.0);
bars = new(feed);
range = 1e-9;
feed.Add(10000);
iterations = 3;
data = feed.Close.v.ToArray();
TALIB = new double[data.Count()];
}
private int GetRandomNumber(int minValue, int maxValue)
{
byte[] randomBytes = new byte[4];
rng.GetBytes(randomBytes);
int randomInt = BitConverter.ToInt32(randomBytes, 0);
return Math.Abs(randomInt % (maxValue - minValue)) + minValue;
}
[Fact]
@@ -35,7 +38,7 @@ public class TAlibTests
{
for (int run = 0; run < iterations; run++)
{
int period = rnd.Next(50) + 5;
int period = GetRandomNumber(5, 55);
Sma ma = new(period);
TSeries QL = new();
foreach (TBar item in feed)
@@ -44,7 +47,6 @@ public class TAlibTests
Assert.Equal(QL.Length, TALIB.Count());
for (int i = QL.Length - 1; i > period; i--)
{
double TL = i < outBegIdx ? double.NaN : TALIB[i - outBegIdx];
Assert.InRange(TALIB[i - outBegIdx] - QL[i].Value, -range, range);
}
}
@@ -55,7 +57,7 @@ public class TAlibTests
{
for (int run = 0; run < iterations; run++)
{
int period = rnd.Next(50) + 5;
int period = GetRandomNumber(5, 55);
Ema ma = new(period, useSma: true);
TSeries QL = new();
foreach (TBar item in feed)
@@ -64,7 +66,6 @@ public class TAlibTests
Assert.Equal(QL.Length, TALIB.Count());
for (int i = QL.Length - 1; i > period; i--)
{
double TL = i < outBegIdx ? double.NaN : TALIB[i - outBegIdx];
Assert.InRange(TALIB[i - outBegIdx] - QL[i].Value, -range, range);
}
}
@@ -75,7 +76,7 @@ public class TAlibTests
{
for (int run = 0; run < iterations; run++)
{
int period = rnd.Next(50) + 5;
int period = GetRandomNumber(5, 55);
Dema ma = new(period);
TSeries QL = new();
foreach (TBar item in feed)
@@ -84,7 +85,6 @@ public class TAlibTests
Assert.Equal(QL.Length, TALIB.Length);
for (int i = QL.Length - 1; i > period * 20; i--)
{
double TL = i < outBegIdx ? double.NaN : TALIB[i - outBegIdx];
Assert.InRange(TALIB[i - outBegIdx] - QL[i].Value, -range, range);
}
}
@@ -95,7 +95,7 @@ public class TAlibTests
{
for (int run = 0; run < iterations; run++)
{
int period = rnd.Next(50) + 5;
int period = GetRandomNumber(5, 55);
Tema ma = new(period);
TSeries QL = new();
foreach (TBar item in feed)
@@ -104,41 +104,37 @@ public class TAlibTests
Assert.Equal(QL.Length, TALIB.Length);
for (int i = QL.Length - 1; i > period * 20; i--)
{
double TL = i < outBegIdx ? double.NaN : TALIB[i - outBegIdx];
Assert.InRange(TALIB[i - outBegIdx] - QL[i].Value, -range, range);
}
}
}
//TODO fix WMA
/*
[Fact]
public void WMA()
[Fact]
public void WMA()
{
for (int run = 0; run < iterations; run++)
{
for (int run = 0; run < iterations; run++)
int period = GetRandomNumber(5, 55);
Wma ma = new(period);
TSeries QL = new();
foreach (TBar item in feed)
{ QL.Add(ma.Calc(new TValue(item.Time, item.Close))); }
Core.Wma(data, 0, QL.Length - 1, TALIB, out int outBegIdx, out _, period);
Assert.Equal(QL.Length, TALIB.Count());
for (int i = QL.Length - 1; i > period * 10; i--)
{
period = rnd.Next(50) + 5;
Wma ma = new(period);
TSeries QL = new();
foreach (TBar item in feed)
{ QL.Add(ma.Calc(new TValue(item.Time, item.Close))); }
Core.Wma(data, 0, QL.Length - 1, TALIB, out int outBegIdx, out _, period);
Assert.Equal(QL.Length, TALIB.Count());
for (int i = QL.Length - 1; i > period*3; i--)
{
double TL = i < outBegIdx ? double.NaN : TALIB[i - outBegIdx];
Assert.InRange(TALIB[i - outBegIdx] - QL[i].Value, -range, range);
}
Assert.InRange(TALIB[i - outBegIdx] - QL[i].Value, -range, range);
}
}
*/
}
[Fact]
public void T3()
{
for (int run = 0; run < iterations; run++)
{
int period = rnd.Next(50) + 5;
int period = GetRandomNumber(5, 55);
T3 ma = new(period, vfactor: 0.7, useSma: false);
TSeries QL = new();
foreach (TBar item in feed)
@@ -147,10 +143,8 @@ public class TAlibTests
Assert.Equal(QL.Length, TALIB.Length);
for (int i = QL.Length - 1; i > period * 20; i--)
{
double TL = i < outBegIdx ? double.NaN : TALIB[i - outBegIdx];
Assert.InRange(TALIB[i - outBegIdx] - QL[i].Value, -range, range);
}
}
}
}