Files
QuanTAlib/Tests/test_talib.cs
T

130 lines
4.2 KiB
C#
Raw Normal View History

2024-09-22 17:31:24 -07:00
using Xunit;
using TALib;
2024-10-05 19:36:01 -07:00
using System.Diagnostics.CodeAnalysis;
2024-10-08 17:34:58 -07:00
using System.Security.Cryptography;
2024-10-05 19:36:01 -07:00
namespace QuanTAlib;
2024-09-22 17:31:24 -07:00
public class TAlibTests
{
private readonly GbmFeed feed;
2024-10-08 17:34:58 -07:00
private readonly RandomNumberGenerator rng;
2024-09-22 17:31:24 -07:00
private readonly double range;
2024-10-06 06:59:26 +00:00
private readonly int iterations;
2024-09-22 17:31:24 -07:00
private readonly double[] data;
private readonly double[] TALIB;
public TAlibTests()
{
2024-10-08 17:34:58 -07:00
rng = RandomNumberGenerator.Create();
2024-09-22 17:31:24 -07:00
feed = new(sigma: 0.5, mu: 0.0);
range = 1e-9;
feed.Add(10000);
iterations = 3;
data = feed.Close.v.ToArray();
2024-09-24 16:41:26 -07:00
TALIB = new double[data.Count()];
2024-10-08 17:34:58 -07:00
}
2024-09-22 17:31:24 -07:00
2024-10-08 17:34:58 -07:00
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;
2024-09-22 17:31:24 -07:00
}
[Fact]
public void SMA()
{
for (int run = 0; run < iterations; run++)
{
2024-10-08 17:34:58 -07:00
int period = GetRandomNumber(5, 55);
2024-09-22 17:31:24 -07:00
Sma ma = new(period);
TSeries QL = new();
foreach (TBar item in feed)
{ QL.Add(ma.Calc(new TValue(item.Time, item.Close))); }
Core.Sma(data, 0, QL.Length - 1, TALIB, out int outBegIdx, out _, period);
2024-09-24 16:41:26 -07:00
Assert.Equal(QL.Length, TALIB.Count());
2024-09-22 17:31:24 -07:00
for (int i = QL.Length - 1; i > period; i--)
{
2024-09-24 16:41:26 -07:00
Assert.InRange(TALIB[i - outBegIdx] - QL[i].Value, -range, range);
2024-09-22 17:31:24 -07:00
}
}
}
[Fact]
public void EMA()
{
for (int run = 0; run < iterations; run++)
{
2024-10-08 17:34:58 -07:00
int period = GetRandomNumber(5, 55);
2024-09-22 17:31:24 -07:00
Ema ma = new(period, useSma: true);
TSeries QL = new();
foreach (TBar item in feed)
{ QL.Add(ma.Calc(new TValue(item.Time, item.Close))); }
Core.Ema(data, 0, QL.Length - 1, TALIB, out int outBegIdx, out _, period);
2024-09-24 16:41:26 -07:00
Assert.Equal(QL.Length, TALIB.Count());
2024-09-22 17:31:24 -07:00
for (int i = QL.Length - 1; i > period; i--)
{
Assert.InRange(TALIB[i - outBegIdx] - QL[i].Value, -range, range);
}
}
}
[Fact]
public void DEMA()
{
for (int run = 0; run < iterations; run++)
{
2024-10-08 17:34:58 -07:00
int period = GetRandomNumber(5, 55);
2024-09-22 17:31:24 -07:00
Dema ma = new(period);
TSeries QL = new();
foreach (TBar item in feed)
{ QL.Add(ma.Calc(new TValue(item.Time, item.Close))); }
Core.Dema(data, 0, QL.Length - 1, TALIB, out int outBegIdx, out _, period);
2024-09-23 22:08:40 -07:00
Assert.Equal(QL.Length, TALIB.Length);
2024-09-30 15:53:48 -07:00
for (int i = QL.Length - 1; i > period * 20; i--)
2024-09-22 17:31:24 -07:00
{
Assert.InRange(TALIB[i - outBegIdx] - QL[i].Value, -range, range);
}
}
}
[Fact]
public void TEMA()
{
for (int run = 0; run < iterations; run++)
{
2024-10-08 17:34:58 -07:00
int period = GetRandomNumber(5, 55);
2024-09-22 17:31:24 -07:00
Tema ma = new(period);
TSeries QL = new();
foreach (TBar item in feed)
{ QL.Add(ma.Calc(new TValue(item.Time, item.Close))); }
Core.Tema(data, 0, QL.Length - 1, TALIB, out int outBegIdx, out _, period);
2024-09-23 22:08:40 -07:00
Assert.Equal(QL.Length, TALIB.Length);
2024-09-30 15:53:48 -07:00
for (int i = QL.Length - 1; i > period * 20; i--)
2024-09-22 17:31:24 -07:00
{
Assert.InRange(TALIB[i - outBegIdx] - QL[i].Value, -range, range);
}
}
}
[Fact]
public void T3()
{
for (int run = 0; run < iterations; run++)
{
2024-10-08 17:34:58 -07:00
int period = GetRandomNumber(5, 55);
2024-09-22 17:31:24 -07:00
T3 ma = new(period, vfactor: 0.7, useSma: false);
TSeries QL = new();
foreach (TBar item in feed)
{ QL.Add(ma.Calc(new TValue(item.Time, item.Close))); }
2024-09-30 15:53:48 -07:00
Core.T3(data, 0, QL.Length - 1, TALIB, out int outBegIdx, out _, optInTimePeriod: period, optInVFactor: 0.7);
2024-09-23 22:08:40 -07:00
Assert.Equal(QL.Length, TALIB.Length);
2024-09-30 15:53:48 -07:00
for (int i = QL.Length - 1; i > period * 20; i--)
2024-09-22 17:31:24 -07:00
{
Assert.InRange(TALIB[i - outBegIdx] - QL[i].Value, -range, range);
}
}
}
}