Pandas-TA tests

This commit is contained in:
Miha Kralj
2022-11-13 19:58:53 -08:00
parent 59ca5c2395
commit ddfcb6bd99
5 changed files with 497 additions and 485 deletions
+111 -105
View File
@@ -1,122 +1,128 @@
/*
using Xunit;
using System;
using QuanTAlib;
using Python.Runtime;
using Python.Included;
using Xunit;
using System;
using QuanTAlib;
using Python.Runtime;
using Python.Included;
namespace Validation;
public class PandasTA
namespace Validations;
public class PandasTA : IDisposable
{
private readonly RND_Feed bars;
private readonly Random rnd = new();
private readonly int period;
private readonly dynamic ta;
private readonly dynamic df;
private GBM_Feed bars;
private Random rnd = new();
private int period;
private string OStype;
private dynamic np;
private dynamic ta;
private dynamic df;
public PandasTA()
{
this.bars = new(1000);
this.period = this.rnd.Next(28) + 3;
public PandasTA()
{
bars = new(5000);
period = rnd.Next(28) + 3;
Runtime.PythonDLL = @"python310.dll";
Installer.InstallPath = Path.GetFullPath(".");
Installer.SetupPython().Wait();
Installer.TryInstallPip();
Installer.PipInstallModule("numpy");
Installer.PipInstallModule("pandas");
Installer.PipInstallModule("pandas-ta");
PythonEngine.Initialize();
this.ta = Py.Import("pandas_ta");
this.df = this.ta.DataFrame(this.bars.Close.v);
}
// Checking the host OS and setting PythonDLL accordingly
OStype = Environment.OSVersion.ToString();
if (OStype == "Unix 13.1.0")
OStype = @"/usr/local/Cellar/python@3.10/3.10.8/Frameworks/Python.framework/Versions/3.10/lib/libpython3.10.dylib";
else OStype = Path.GetFullPath(".") + @"\python-3.10.0-embed-amd64\python310.dll";
~PandasTA()
{
PythonEngine.Shutdown();
}
[Fact]
void SMA()
{
SMA_Series QL = new(this.bars.Close, this.period, false);
var pta = this.ta.sma(close: this.df[0], length: this.period);
Installer.InstallPath = Path.GetFullPath(".");
Installer.SetupPython().Wait();
Installer.TryInstallPip();
Installer.PipInstallModule("pandas-ta");
Assert.Equal(System.Math.Round((double)pta.tail(1), 7), Math.Round(QL.Last().v, 7));
}
Runtime.PythonDLL = OStype;
PythonEngine.Initialize();
np = Py.Import("numpy");
ta = Py.Import("pandas_ta");
[Fact]
void EMA()
{
EMA_Series QL = new(this.bars.Close, this.period, false);
var pta = this.ta.ema(close: this.df[0], length: this.period);
string[] cols = { "open", "high", "low", "close", "volume" };
double[,] ary = new double[bars.Count, 5];
for (int i = 0; i < bars.Count; i++)
{
ary[i, 0] = bars.Open[i].v;
ary[i, 1] = bars.High[i].v;
ary[i, 2] = bars.Low[i].v;
ary[i, 3] = bars.Close[i].v;
ary[i, 4] = bars.Volume[i].v;
}
df = ta.DataFrame(data: np.array(ary), index: np.array(bars.Close.t), columns: np.array(cols));
}
Assert.Equal(System.Math.Round((double)pta.tail(1), 7), Math.Round(QL.Last().v, 7));
}
public void Dispose()
{
PythonEngine.Shutdown();
}
[Fact]
void SMA()
{
SMA_Series QL = new(bars.Close, period, false);
var pta = df.ta.sma(close: df.close, length: period);
Assert.Equal(Math.Round((double)pta.tail(1), 7), Math.Round(QL.Last().v, 7));
}
[Fact]
void TEMA()
{
TEMA_Series QL = new(this.bars.Close, this.period, false);
var pta = this.ta.tema(close: this.df[0], length: this.period);
[Fact]
void EMA()
{
EMA_Series QL = new(bars.Close, period, false);
var pta = df.ta.ema(close: df.close, length: period);
Assert.Equal(Math.Round((double)pta.tail(1), 7), Math.Round(QL.Last().v, 7));
}
Assert.Equal(System.Math.Round((double)pta.tail(1), 7), Math.Round(QL.Last().v, 7));
}
[Fact]
void TEMA()
{
TEMA_Series QL = new(bars.Close, period, false);
var pta = df.ta.tema(close: df.close, length: period);
Assert.Equal(Math.Round((double)pta.tail(1), 7), Math.Round(QL.Last().v, 7));
}
[Fact]
void ENTP()
{
ENTP_Series QL = new(this.bars.Close, this.period, useNaN:false);
var pta = this.ta.entropy(close: this.df[0], length: this.period);
[Fact]
void ENTP()
{
ENTP_Series QL = new(bars.Close, period, useNaN: false);
var pta = df.ta.entropy(close: df.close, length: period);
Assert.Equal(Math.Round((double)pta.tail(1), 7), Math.Round(QL.Last().v, 7));
}
Assert.Equal(System.Math.Round((double)pta.tail(1), 7), Math.Round(QL.Last().v, 7));
}
[Fact]
void WMA()
{
WMA_Series QL = new(bars.Close, period, false);
var pta = df.ta.wma(close: df.close, length: period);
Assert.Equal(Math.Round((double)pta.tail(1), 7), Math.Round(QL.Last().v, 7));
}
[Fact]
void DEMA()
{
DEMA_Series QL = new(bars.Close, period, false);
var pta = df.ta.dema(close: df.close, length: period);
Assert.Equal(Math.Round((double)pta.tail(1), 7), Math.Round(QL.Last().v, 7));
}
[Fact]
void WMA()
{
WMA_Series QL = new(this.bars.Close, this.period, false);
var pta = this.ta.wma(close: this.df[0], length: this.period);
[Fact]
void BIAS()
{
BIAS_Series QL = new(bars.Close, period, false);
var pta = df.ta.bias(close: df.close, length: period);
Assert.Equal(Math.Round((double)pta.tail(1), 7), Math.Round(QL.Last().v, 7));
}
Assert.Equal(System.Math.Round((double)pta.tail(1), 7), Math.Round(QL.Last().v, 7));
}
[Fact]
void KURT()
{
KURT_Series QL = new(bars.Close, period, useNaN: false);
var pta = df.ta.kurtosis(close: df.close, length: period);
Assert.Equal(Math.Round((double)pta.tail(1), 4), Math.Round(QL.Last().v, 4));
}
[Fact]
void DEMA()
{
DEMA_Series QL = new(this.bars.Close, this.period, false);
var pta = this.ta.dema(close: this.df[0], length: this.period);
Assert.Equal(System.Math.Round((double)pta.tail(1), 7), Math.Round(QL.Last().v, 7));
}
[Fact]
void BIAS()
{
BIAS_Series QL = new(this.bars.Close, this.period, false);
var pta = this.ta.bias(close: this.df[0], length: this.period);
Assert.Equal(System.Math.Round((double)pta.tail(1), 7), Math.Round(QL.Last().v, 7));
}
[Fact]
void KURT()
{
KURT_Series QL = new(this.bars.Close, this.period, useNaN: false);
var pta = this.ta.kurtosis(close: this.df[0], length: this.period);
Assert.Equal(System.Math.Round((double)pta.tail(1), 4), Math.Round(QL.Last().v, 4));
}
[Fact]
void MAD()
{
MAD_Series QL = new(this.bars.Close, this.period, useNaN: false);
var pta = this.ta.mad(close: this.df[0], length: this.period);
Assert.Equal(System.Math.Round((double)pta.tail(1), 7), Math.Round(QL.Last().v, 7));
}
}
*/
[Fact]
void MAD()
{
MAD_Series QL = new(bars.Close, period, useNaN: false);
var pta = df.ta.mad(close: df.close, length: period);
Assert.Equal(Math.Round((double)pta.tail(1), 7), Math.Round(QL.Last().v, 7));
}
}