mirror of
https://github.com/mihakralj/QuanTAlib.git
synced 2026-08-20 11:38:05 +00:00
refactoring
This commit is contained in:
@@ -0,0 +1,166 @@
|
||||
using System;
|
||||
using QuanTAlib;
|
||||
using Skender.Stock.Indicators;
|
||||
using Tulip;
|
||||
using Python.Runtime;
|
||||
using Python.Included;
|
||||
using TALib;
|
||||
using Validations;
|
||||
using Xunit;
|
||||
|
||||
namespace One.by.one;
|
||||
public class Dema : IDisposable
|
||||
{
|
||||
private readonly GBM_Feed bars;
|
||||
private readonly Random rnd = new();
|
||||
private readonly int period, skip;
|
||||
private readonly double precision;
|
||||
|
||||
private readonly IEnumerable<Quote> quotes;
|
||||
private readonly double[] outdata;
|
||||
private readonly double[] inopen;
|
||||
private readonly double[] inhigh;
|
||||
private readonly double[] inlow;
|
||||
private readonly double[] inclose;
|
||||
private readonly double[] involume;
|
||||
|
||||
private readonly string OStype;
|
||||
private readonly dynamic np;
|
||||
private readonly dynamic ta;
|
||||
private readonly dynamic df;
|
||||
|
||||
public void Dispose() {
|
||||
PythonEngine.Shutdown();
|
||||
GC.SuppressFinalize(this);
|
||||
}
|
||||
public Dema()
|
||||
{
|
||||
bars = new(Bars: 1000, Volatility: 0.5, Drift: 0.0);
|
||||
period = rnd.Next(30) + 5;
|
||||
skip = period*8;
|
||||
precision = 1e-6;
|
||||
|
||||
quotes = bars.Select(q => new Quote
|
||||
{
|
||||
Date = q.t,
|
||||
Open = (decimal)q.o,
|
||||
High = (decimal)q.h,
|
||||
Low = (decimal)q.l,
|
||||
Close = (decimal)q.c,
|
||||
Volume = (decimal)q.v
|
||||
});
|
||||
|
||||
outdata = new double[bars.Count];
|
||||
inopen = bars.Open.v.ToArray();
|
||||
inhigh = bars.High.v.ToArray();
|
||||
inlow = bars.Low.v.ToArray();
|
||||
inclose = bars.Close.v.ToArray();
|
||||
involume = bars.Volume.v.ToArray();
|
||||
|
||||
// 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";
|
||||
|
||||
Installer.InstallPath = Path.GetFullPath(path: ".");
|
||||
Installer.SetupPython().Wait();
|
||||
Installer.TryInstallPip();
|
||||
Installer.PipInstallModule(module_name: "pandas-ta");
|
||||
Runtime.PythonDLL = OStype;
|
||||
PythonEngine.Initialize();
|
||||
np = Py.Import(name: "numpy");
|
||||
ta = Py.Import(name: "pandas_ta");
|
||||
|
||||
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));
|
||||
}
|
||||
[Fact]
|
||||
public void WeirdData() {
|
||||
DEMA_Series QL = new(source: bars.Close, period);
|
||||
var lastData = bars.Close.Last();
|
||||
var lastCalc = QL.Last();
|
||||
QL.Add((DateTime.Today.AddDays(1), double.NaN), update: true);
|
||||
Assert.NotEqual(lastCalc, QL.Last()); //value changed
|
||||
QL.Add(lastData, update: true);
|
||||
Assert.Equal(lastCalc, QL.Last()); // back to the same data
|
||||
|
||||
QL.Add((DateTime.Today.AddDays(-1), double.NegativeInfinity), update: true);
|
||||
Assert.NotEqual(lastCalc, QL.Last()); //value changed
|
||||
QL.Add(lastData, update: true);
|
||||
Assert.Equal(lastCalc, QL.Last()); // back to the same data
|
||||
|
||||
QL.Add((new DateTime(), double.Epsilon), update: true);
|
||||
Assert.NotEqual(lastCalc, QL.Last()); //value changed
|
||||
QL.Add(lastData, update: true);
|
||||
Assert.Equal(lastCalc, QL.Last()); // back to the same data
|
||||
}
|
||||
[Fact]
|
||||
public void Updating() {
|
||||
DEMA_Series QL = new(source: bars.Close, period);
|
||||
var lastData = bars.Close.Last();
|
||||
var lastCalc = QL.Last();
|
||||
int lastLen = QL.Count;
|
||||
QL.Add((DateTime.Today, 0), update: true);
|
||||
Assert.NotEqual(lastCalc, QL.Last()); //value changed
|
||||
QL.Add(lastData, update: true);
|
||||
Assert.Equal(lastLen, QL.Count); // same size
|
||||
Assert.Equal(lastCalc, QL.Last()); // same data
|
||||
}
|
||||
[Fact]
|
||||
public void Skender_Test()
|
||||
{
|
||||
DEMA_Series QL = new(bars.Close, period, false);
|
||||
var SK = quotes.GetDema(period).Select(i => i.Dema.Null2NaN()!);
|
||||
for (int i = QL.Length; i > skip; i--)
|
||||
{
|
||||
double QL_item = QL[i - 1].v;
|
||||
double SK_item = SK.ElementAt(i - 1);
|
||||
Assert.InRange(SK_item! - QL_item, -precision, precision);
|
||||
|
||||
}
|
||||
}
|
||||
[Fact]
|
||||
public void TALIB_Test()
|
||||
{
|
||||
DEMA_Series QL = new(bars.Close, period, false);
|
||||
Core.Dema(inclose, 0, bars.Count - 1, outdata, out int outBegIdx, out _, period);
|
||||
for (int i = QL.Length - 1; i > skip; i--)
|
||||
{
|
||||
double QL_item = QL[i].v;
|
||||
double TA_item = outdata[i - outBegIdx];
|
||||
Assert.InRange(TA_item! - QL_item, -precision, precision);
|
||||
}
|
||||
}
|
||||
[Fact]
|
||||
public void Tulip_Test() {
|
||||
double[][] arrin = { inclose };
|
||||
double[][] arrout = { outdata };
|
||||
DEMA_Series QL = new(bars.Close, period: period, useNaN: false);
|
||||
Tulip.Indicators.dema.Run(inputs: arrin, options: new double[] { period }, outputs: arrout);
|
||||
for (int i = QL.Length - 1; i > skip*3; i--) {
|
||||
double QL_item = QL[i].v;
|
||||
double TU_item = arrout[0][i];
|
||||
Assert.InRange(TU_item! - QL_item, -precision, precision);
|
||||
}
|
||||
}
|
||||
[Fact]
|
||||
void PandasTA_Test() {
|
||||
DEMA_Series QL = new(bars.Close, period, false);
|
||||
var pta = df.ta.dema(close: df.close, length: period);
|
||||
for (int i = QL.Length; i > skip; i--) {
|
||||
double QL_item = QL[i - 1].v;
|
||||
double PanTA_item = (double)pta[i - 1];
|
||||
Assert.InRange(PanTA_item! - QL_item, -1e-5, 1e-5);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,166 @@
|
||||
using System;
|
||||
using QuanTAlib;
|
||||
using Skender.Stock.Indicators;
|
||||
using Tulip;
|
||||
using Python.Runtime;
|
||||
using Python.Included;
|
||||
using TALib;
|
||||
using Validations;
|
||||
using Xunit;
|
||||
|
||||
namespace One.by.one;
|
||||
public class Ema : IDisposable
|
||||
{
|
||||
private readonly GBM_Feed bars;
|
||||
private readonly Random rnd = new();
|
||||
private readonly int period, skip;
|
||||
private readonly double precision;
|
||||
|
||||
private readonly IEnumerable<Quote> quotes;
|
||||
private readonly double[] outdata;
|
||||
private readonly double[] inopen;
|
||||
private readonly double[] inhigh;
|
||||
private readonly double[] inlow;
|
||||
private readonly double[] inclose;
|
||||
private readonly double[] involume;
|
||||
|
||||
private readonly string OStype;
|
||||
private readonly dynamic np;
|
||||
private readonly dynamic ta;
|
||||
private readonly dynamic df;
|
||||
|
||||
public void Dispose() {
|
||||
PythonEngine.Shutdown();
|
||||
GC.SuppressFinalize(this);
|
||||
}
|
||||
public Ema()
|
||||
{
|
||||
bars = new(Bars: 1000, Volatility: 0.5, Drift: 0.0);
|
||||
period = rnd.Next(30) + 5;
|
||||
skip = period-1;
|
||||
precision = 1e-8;
|
||||
|
||||
quotes = bars.Select(q => new Quote
|
||||
{
|
||||
Date = q.t,
|
||||
Open = (decimal)q.o,
|
||||
High = (decimal)q.h,
|
||||
Low = (decimal)q.l,
|
||||
Close = (decimal)q.c,
|
||||
Volume = (decimal)q.v
|
||||
});
|
||||
|
||||
outdata = new double[bars.Count];
|
||||
inopen = bars.Open.v.ToArray();
|
||||
inhigh = bars.High.v.ToArray();
|
||||
inlow = bars.Low.v.ToArray();
|
||||
inclose = bars.Close.v.ToArray();
|
||||
involume = bars.Volume.v.ToArray();
|
||||
|
||||
// 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";
|
||||
|
||||
Installer.InstallPath = Path.GetFullPath(path: ".");
|
||||
Installer.SetupPython().Wait();
|
||||
Installer.TryInstallPip();
|
||||
Installer.PipInstallModule(module_name: "pandas-ta");
|
||||
Runtime.PythonDLL = OStype;
|
||||
PythonEngine.Initialize();
|
||||
np = Py.Import(name: "numpy");
|
||||
ta = Py.Import(name: "pandas_ta");
|
||||
|
||||
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));
|
||||
}
|
||||
[Fact]
|
||||
public void WeirdData() {
|
||||
EMA_Series QL = new(source: bars.Close, period);
|
||||
var lastData = bars.Close.Last();
|
||||
var lastCalc = QL.Last();
|
||||
QL.Add((DateTime.Today.AddDays(1), double.NaN), update: true);
|
||||
Assert.NotEqual(lastCalc, QL.Last()); //value changed
|
||||
QL.Add(lastData, update: true);
|
||||
Assert.Equal(lastCalc, QL.Last()); // back to the same data
|
||||
|
||||
QL.Add((DateTime.Today.AddDays(-1), double.NegativeInfinity), update: true);
|
||||
Assert.NotEqual(lastCalc, QL.Last()); //value changed
|
||||
QL.Add(lastData, update: true);
|
||||
Assert.Equal(lastCalc, QL.Last()); // back to the same data
|
||||
|
||||
QL.Add((new DateTime(), double.Epsilon), update: true);
|
||||
Assert.NotEqual(lastCalc, QL.Last()); //value changed
|
||||
QL.Add(lastData, update: true);
|
||||
Assert.Equal(lastCalc, QL.Last()); // back to the same data
|
||||
}
|
||||
[Fact]
|
||||
public void Updating() {
|
||||
EMA_Series QL = new(source: bars.Close, period);
|
||||
var lastData = bars.Close.Last();
|
||||
var lastCalc = QL.Last();
|
||||
int lastLen = QL.Count;
|
||||
QL.Add((DateTime.Today, 0), update: true);
|
||||
Assert.NotEqual(lastCalc, QL.Last()); //value changed
|
||||
QL.Add(lastData, update: true);
|
||||
Assert.Equal(lastLen, QL.Count); // same size
|
||||
Assert.Equal(lastCalc, QL.Last()); // same data
|
||||
}
|
||||
[Fact]
|
||||
public void Skender_Test()
|
||||
{
|
||||
EMA_Series QL = new(bars.Close, period, false);
|
||||
var SK = quotes.GetEma(period).Select(i => i.Ema.Null2NaN()!);
|
||||
for (int i = QL.Length; i > skip; i--)
|
||||
{
|
||||
double QL_item = QL[i - 1].v;
|
||||
double SK_item = SK.ElementAt(i - 1);
|
||||
Assert.InRange(SK_item! - QL_item, -precision, precision);
|
||||
|
||||
}
|
||||
}
|
||||
[Fact]
|
||||
public void TALIB_Test()
|
||||
{
|
||||
EMA_Series QL = new(bars.Close, period, false);
|
||||
Core.Ema(inclose, 0, bars.Count - 1, outdata, out int outBegIdx, out _, period);
|
||||
for (int i = QL.Length - 1; i > skip; i--)
|
||||
{
|
||||
double QL_item = QL[i].v;
|
||||
double TA_item = outdata[i - outBegIdx];
|
||||
Assert.InRange(TA_item! - QL_item, -precision, precision);
|
||||
}
|
||||
}
|
||||
[Fact]
|
||||
public void Tulip_Test() {
|
||||
double[][] arrin = { inclose };
|
||||
double[][] arrout = { outdata };
|
||||
EMA_Series QL = new(bars.Close, period: period, useNaN: false, useSMA: false);
|
||||
Tulip.Indicators.ema.Run(inputs: arrin, options: new double[] { period }, outputs: arrout);
|
||||
for (int i = QL.Length - 1; i > skip; i--) {
|
||||
double QL_item = QL[i].v;
|
||||
double TU_item = arrout[0][i];
|
||||
Assert.InRange(TU_item! - QL_item, -precision, precision);
|
||||
}
|
||||
}
|
||||
[Fact]
|
||||
void PandasTA_Test() {
|
||||
EMA_Series QL = new(bars.Close, period, false);
|
||||
var pta = df.ta.ema(close: df.close, length: period);
|
||||
for (int i = QL.Length; i > skip; i--) {
|
||||
double QL_item = QL[i - 1].v;
|
||||
double PanTA_item = (double)pta[i - 1];
|
||||
Assert.InRange(PanTA_item! - QL_item, -1e-5, 1e-5);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,160 @@
|
||||
using System;
|
||||
using QuanTAlib;
|
||||
using Skender.Stock.Indicators;
|
||||
using Tulip;
|
||||
using Python.Runtime;
|
||||
using Python.Included;
|
||||
using TALib;
|
||||
using Validations;
|
||||
using Xunit;
|
||||
|
||||
namespace One.by.one;
|
||||
public class SMA : IDisposable {
|
||||
private readonly GBM_Feed bars;
|
||||
private readonly Random rnd = new();
|
||||
private readonly int period, skip;
|
||||
private readonly double precision;
|
||||
|
||||
private readonly IEnumerable<Quote> quotes;
|
||||
private readonly double[] outdata;
|
||||
private readonly double[] inopen;
|
||||
private readonly double[] inhigh;
|
||||
private readonly double[] inlow;
|
||||
private readonly double[] inclose;
|
||||
private readonly double[] involume;
|
||||
|
||||
private readonly string OStype;
|
||||
private readonly dynamic np;
|
||||
private readonly dynamic ta;
|
||||
private readonly dynamic df;
|
||||
|
||||
public void Dispose() {
|
||||
PythonEngine.Shutdown();
|
||||
GC.SuppressFinalize(this);
|
||||
}
|
||||
public SMA() {
|
||||
bars = new(Bars: 1000, Volatility: 0.5, Drift: 0.0);
|
||||
period = rnd.Next(30) + 5;
|
||||
precision = 1e-8;
|
||||
skip = period-1;
|
||||
|
||||
quotes = bars.Select(q => new Quote {
|
||||
Date = q.t,
|
||||
Open = (decimal)q.o,
|
||||
High = (decimal)q.h,
|
||||
Low = (decimal)q.l,
|
||||
Close = (decimal)q.c,
|
||||
Volume = (decimal)q.v
|
||||
});
|
||||
|
||||
outdata = new double[bars.Count];
|
||||
inopen = bars.Open.v.ToArray();
|
||||
inhigh = bars.High.v.ToArray();
|
||||
inlow = bars.Low.v.ToArray();
|
||||
inclose = bars.Close.v.ToArray();
|
||||
involume = bars.Volume.v.ToArray();
|
||||
|
||||
// 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";
|
||||
|
||||
Installer.InstallPath = Path.GetFullPath(path: ".");
|
||||
Installer.SetupPython().Wait();
|
||||
Installer.TryInstallPip();
|
||||
Installer.PipInstallModule(module_name: "pandas-ta");
|
||||
Runtime.PythonDLL = OStype;
|
||||
PythonEngine.Initialize();
|
||||
np = Py.Import(name: "numpy");
|
||||
ta = Py.Import(name: "pandas_ta");
|
||||
|
||||
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));
|
||||
}
|
||||
[Fact]
|
||||
public void WeirdData() {
|
||||
SMA_Series QL = new(source: bars.Close, period);
|
||||
var lastData = bars.Close.Last();
|
||||
var lastCalc = QL.Last();
|
||||
QL.Add((DateTime.Today.AddDays(1), double.NaN), update: true);
|
||||
Assert.NotEqual(lastCalc, QL.Last()); //value changed
|
||||
QL.Add(lastData, update: true);
|
||||
Assert.Equal(lastCalc, QL.Last()); // back to the same data
|
||||
|
||||
QL.Add((DateTime.Today.AddDays(-1), double.NegativeInfinity), update: true);
|
||||
Assert.NotEqual(lastCalc, QL.Last()); //value changed
|
||||
QL.Add(lastData, update: true);
|
||||
Assert.Equal(lastCalc, QL.Last()); // back to the same data
|
||||
|
||||
QL.Add((new DateTime(), double.Epsilon), update: true);
|
||||
Assert.NotEqual(lastCalc, QL.Last()); //value changed
|
||||
QL.Add(lastData, update: true);
|
||||
Assert.Equal(lastCalc, QL.Last()); // back to the same data
|
||||
}
|
||||
[Fact]
|
||||
public void Updating() {
|
||||
SMA_Series QL = new(source: bars.Close, period);
|
||||
var lastData = bars.Close.Last();
|
||||
var lastCalc = QL.Last();
|
||||
int lastLen = QL.Count;
|
||||
QL.Add((DateTime.Today, 0), update: true);
|
||||
Assert.NotEqual(lastCalc, QL.Last()); //value changed
|
||||
QL.Add(lastData, update: true);
|
||||
Assert.Equal(lastLen, QL.Count); // same size
|
||||
Assert.Equal(lastCalc, QL.Last()); // same data
|
||||
}
|
||||
[Fact]
|
||||
public void Skender_Test() {
|
||||
SMA_Series QL = new(bars.Close, period, false);
|
||||
var SK = quotes.GetSma(period).Select(i => i.Sma.Null2NaN()!);
|
||||
for (int i = QL.Length; i > skip; i--) {
|
||||
double QL_item = QL[i - 1].v;
|
||||
double SK_item = SK.ElementAt(i - 1);
|
||||
Assert.InRange(SK_item! - QL_item, -precision, precision);
|
||||
}
|
||||
}
|
||||
[Fact]
|
||||
public void TALIB_Test() {
|
||||
SMA_Series QL = new(bars.Close, period, false);
|
||||
Core.Sma(inclose, 0, bars.Count - 1, outdata, out int outBegIdx, out _, period);
|
||||
for (int i = QL.Length - 1; i > skip; i--) {
|
||||
double QL_item = QL[i].v;
|
||||
double TA_item = outdata[i - outBegIdx];
|
||||
Assert.InRange(TA_item! - QL_item, -precision, precision);
|
||||
|
||||
}
|
||||
}
|
||||
[Fact]
|
||||
public void Tulip_Test() {
|
||||
double[][] arrin = { inclose };
|
||||
double[][] arrout = { outdata };
|
||||
SMA_Series QL = new(bars.Close, period, false);
|
||||
Tulip.Indicators.sma.Run(inputs: arrin, options: new double[] { period }, outputs: arrout);
|
||||
for (int i = QL.Length - 1; i > skip; i--) {
|
||||
double QL_item = QL[i].v;
|
||||
double TU_item = arrout[0][i - period + 1];
|
||||
Assert.InRange(TU_item! - QL_item, -precision, precision);
|
||||
|
||||
}
|
||||
}
|
||||
[Fact]
|
||||
void PandasTA_Test() {
|
||||
SMA_Series QL = new(bars.Close, period, false);
|
||||
var pta = df.ta.sma(close: df.close, length: period);
|
||||
for (int i = QL.Length; i > skip; i--) {
|
||||
double QL_item = QL[i - 1].v;
|
||||
double PanTA_item = (double)pta[i - 1];
|
||||
Assert.InRange(PanTA_item! - QL_item, -precision, precision);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,354 +1,360 @@
|
||||
using Xunit;
|
||||
using System;
|
||||
using QuanTAlib;
|
||||
using Python.Runtime;
|
||||
using Python.Included;
|
||||
|
||||
namespace Validations;
|
||||
public class PandasTA : IDisposable
|
||||
{
|
||||
private readonly GBM_Feed bars;
|
||||
private readonly Random rnd = new();
|
||||
private readonly int period;
|
||||
private int digits;
|
||||
private readonly string OStype;
|
||||
private readonly dynamic np;
|
||||
private readonly dynamic ta;
|
||||
private readonly dynamic df;
|
||||
|
||||
public PandasTA() {
|
||||
bars = new(Bars: 5000, Volatility: 0.8, Drift: 0.0);
|
||||
period = rnd.Next(maxValue: 28) + 3;
|
||||
digits = 4; //minimizing rounding errors in type conversions
|
||||
|
||||
// Checking the host OS and setting PythonDLL accordingly
|
||||
OStype = Path.GetFullPath(path: ".") + @"\python-3.10.0-embed-amd64\python310.dll";
|
||||
|
||||
Installer.InstallPath = Path.GetFullPath(path: ".");
|
||||
Installer.SetupPython().Wait();
|
||||
Installer.TryInstallPip();
|
||||
Installer.PipInstallModule(module_name: "pandas-ta");
|
||||
Runtime.PythonDLL = OStype;
|
||||
PythonEngine.Initialize();
|
||||
np = Py.Import(name: "numpy");
|
||||
ta = Py.Import(name: "pandas_ta");
|
||||
|
||||
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));
|
||||
}
|
||||
public void Dispose()
|
||||
{
|
||||
PythonEngine.Shutdown();
|
||||
GC.SuppressFinalize(this);
|
||||
}
|
||||
|
||||
[Fact] void ADL() {
|
||||
ADL_Series QL = new(bars);
|
||||
var pta = df.ta.ad(high: df.high, low: df.low, close:df.close, volume:df.volume);
|
||||
for (int i = QL.Length; i > 0; i--)
|
||||
{
|
||||
double QL_item = Math.Round(QL[i-1].v, digits: digits);
|
||||
double PanTA_item = Math.Round((double)pta[i-1], digits: digits);
|
||||
Assert.Equal(PanTA_item, QL_item);
|
||||
}
|
||||
}
|
||||
[Fact] void ADOSC() {
|
||||
ADOSC_Series QL = new(bars);
|
||||
var pta = df.ta.adosc(high: df.high, low: df.low, close: df.close, volume: df.volume);
|
||||
for (int i = QL.Length; i > 0; i--)
|
||||
{
|
||||
double QL_item = Math.Round(QL[i - 1].v, digits: digits);
|
||||
double PanTA_item = Math.Round((double)pta[i - 1], digits: digits);
|
||||
Assert.Equal(PanTA_item, QL_item);
|
||||
}
|
||||
}
|
||||
[Fact] void ATR() {
|
||||
ATR_Series QL = new(bars, period);
|
||||
var pta = df.ta.atr(high: df.high, low: df.low, close: df.close, length: period);
|
||||
for (int i = QL.Length; i > 0; i--)
|
||||
{
|
||||
double QL_item = Math.Round(QL[i - 1].v, digits: digits);
|
||||
double PanTA_item = Math.Round((double)pta[i - 1], digits: digits);
|
||||
Assert.Equal(PanTA_item, QL_item);
|
||||
}
|
||||
}
|
||||
[Fact] void BIAS() {
|
||||
BIAS_Series QL = new(bars.Close, period, false);
|
||||
var pta = df.ta.bias(close: df.close, length: period);
|
||||
for (int i = QL.Length; i > period-1; i--)
|
||||
{
|
||||
double QL_item = Math.Round(QL[i - 1].v, digits: digits);
|
||||
double PanTA_item = Math.Round((double)pta[i - 1], digits: digits);
|
||||
Assert.Equal(PanTA_item, QL_item);
|
||||
}
|
||||
}
|
||||
[Fact] void DEMA() {
|
||||
DEMA_Series QL = new(bars.Close, period, false);
|
||||
var pta = df.ta.dema(close: df.close, length: period);
|
||||
for (int i = QL.Length; i > period-1; i--)
|
||||
{
|
||||
double QL_item = Math.Round(QL[i - 1].v, digits: digits);
|
||||
double PanTA_item = Math.Round((double)pta[i - 1], digits: digits);
|
||||
Assert.Equal(PanTA_item, QL_item);
|
||||
}
|
||||
}
|
||||
[Fact] void EMA() {
|
||||
EMA_Series QL = new(bars.Close, period, false);
|
||||
var pta = df.ta.ema(close: df.close, length: period);
|
||||
for (int i = QL.Length; i > period-1; i--)
|
||||
{
|
||||
double QL_item = Math.Round(QL[i - 1].v, digits: digits);
|
||||
double PanTA_item = Math.Round((double)pta[i - 1], digits: digits);
|
||||
Assert.Equal(PanTA_item, QL_item);
|
||||
}
|
||||
}
|
||||
[Fact] void ENTROPY() {
|
||||
ENTROPY_Series QL = new(bars.Close, period, useNaN: false);
|
||||
var pta = df.ta.entropy(close: df.close, length: period);
|
||||
for (int i = QL.Length; i > period+1; i--)
|
||||
{
|
||||
double QL_item = Math.Round(QL[i - 1].v, digits: digits);
|
||||
double PanTA_item = Math.Round((double)pta[i - 1], digits: digits);
|
||||
Assert.Equal(PanTA_item, QL_item);
|
||||
}
|
||||
}
|
||||
[Fact] void HL2() {
|
||||
var pta = df.ta.hl2(high: df.high, low: df.low);
|
||||
for (int i = bars.HL2.Length; i > 0; i--)
|
||||
{
|
||||
double QL_item = Math.Round(bars.HL2[i - 1].v, digits: digits);
|
||||
double PanTA_item = Math.Round((double)pta[i - 1], digits: digits);
|
||||
Assert.Equal(PanTA_item, QL_item);
|
||||
}
|
||||
}
|
||||
[Fact] void HLC3() {
|
||||
var pta = df.ta.hlc3(high: df.high, low: df.low, close: df.close);
|
||||
for (int i = bars.HLC3.Length; i > 0; i--)
|
||||
{
|
||||
double QL_item = Math.Round(bars.HLC3[i - 1].v, digits: digits);
|
||||
double PanTA_item = Math.Round((double)pta[i - 1], digits: digits);
|
||||
Assert.Equal(PanTA_item, QL_item);
|
||||
}
|
||||
}
|
||||
[Fact] void HMA() {
|
||||
HMA_Series QL = new(bars.Close, period, false);
|
||||
var pta = df.ta.hma(close: df.close, length: period);
|
||||
for (int i = QL.Length; i > period+1; i--)
|
||||
{
|
||||
double QL_item = Math.Round(QL[i - 1].v, digits: digits);
|
||||
double PanTA_item = Math.Round((double)pta[i - 1], digits: digits);
|
||||
Assert.Equal(PanTA_item, QL_item);
|
||||
}
|
||||
|
||||
}
|
||||
[Fact] void KAMA() {
|
||||
KAMA_Series QL = new(bars.Close, period);
|
||||
var pta = df.ta.kama(close: df.close, length: period);
|
||||
for (int i = QL.Length; i > 0; i--)
|
||||
{
|
||||
double QL_item = Math.Round(QL[i - 1].v, digits: digits);
|
||||
double PanTA_item = Math.Round((double)pta[i - 1], digits: digits);
|
||||
Assert.Equal(PanTA_item, QL_item);
|
||||
}
|
||||
}
|
||||
[Fact] void KURTOSIS() {
|
||||
KURTOSIS_Series QL = new(bars.Close, period, useNaN: false);
|
||||
var pta = df.ta.kurtosis(close: df.close, length: period);
|
||||
for (int i = QL.Length; i > period+1; i--)
|
||||
{
|
||||
double QL_item = Math.Round(QL[i - 1].v, digits: digits);
|
||||
double PanTA_item = Math.Round((double)pta[i - 1], digits: digits);
|
||||
Assert.Equal(PanTA_item, QL_item);
|
||||
}
|
||||
}
|
||||
[Fact] void MAD()
|
||||
{
|
||||
MAD_Series QL = new(bars.Close, period, useNaN: false);
|
||||
var pta = df.ta.mad(close: df.close, length: period);
|
||||
for (int i = QL.Length; i > period-1; i--)
|
||||
{
|
||||
double QL_item = Math.Round(QL[i - 1].v, digits: digits);
|
||||
double PanTA_item = Math.Round((double)pta[i - 1], digits: digits);
|
||||
Assert.Equal(PanTA_item, QL_item);
|
||||
}
|
||||
}
|
||||
[Fact] void MEDIAN() {
|
||||
MEDIAN_Series QL = new(bars.Close, period);
|
||||
var pta = df.ta.median(close: df.close, length: period);
|
||||
for (int i = QL.Length; i > period-1; i--)
|
||||
{
|
||||
double QL_item = Math.Round(QL[i - 1].v, digits: digits);
|
||||
double PanTA_item = Math.Round((double)pta[i - 1], digits: digits);
|
||||
Assert.Equal(PanTA_item, QL_item);
|
||||
}
|
||||
}
|
||||
[Fact] void OBV() {
|
||||
OBV_Series QL = new(bars);
|
||||
var pta = df.ta.obv(close: df.close, volume: df.volume);
|
||||
for (int i = QL.Length; i > 0; i--)
|
||||
{
|
||||
double QL_item = Math.Round(QL[i - 1].v, digits: digits);
|
||||
double PanTA_item = Math.Round((double)pta[i - 1], digits: digits);
|
||||
Assert.Equal(PanTA_item, QL_item);
|
||||
}
|
||||
}
|
||||
[Fact] void OHLC4() {
|
||||
var pta = df.ta.ohlc4(open: df.open, high: df.high, low: df.low, close: df.close);
|
||||
for (int i = bars.OHLC4.Length; i > 0; i--)
|
||||
{
|
||||
double QL_item = Math.Round(bars.OHLC4[i - 1].v, digits: digits);
|
||||
double PanTA_item = Math.Round((double)pta[i - 1], digits: digits);
|
||||
Assert.Equal(PanTA_item, QL_item);
|
||||
}
|
||||
}
|
||||
[Fact] void RMA() {
|
||||
RMA_Series QL = new(bars.Close, period, false);
|
||||
var pta = df.ta.rma(close: df.close, length: period);
|
||||
for (int i = QL.Length; i > 0; i--)
|
||||
{
|
||||
double QL_item = Math.Round(QL[i - 1].v, digits: digits);
|
||||
double PanTA_item = Math.Round((double)pta[i - 1], digits: digits);
|
||||
Assert.Equal(PanTA_item, QL_item);
|
||||
}
|
||||
}
|
||||
[Fact] void RSI() {
|
||||
RSI_Series QL = new(bars.Close, period);
|
||||
var pta = df.ta.rsi(close: df.close, length: period);
|
||||
for (int i = QL.Length; i > 0; i--)
|
||||
{
|
||||
double QL_item = Math.Round(QL[i - 1].v, digits: digits);
|
||||
double PanTA_item = Math.Round((double)pta[i - 1], digits: digits);
|
||||
Assert.Equal(PanTA_item, QL_item);
|
||||
}
|
||||
}
|
||||
[Fact] void SDEV() {
|
||||
SDEV_Series QL = new(bars.Close, period, useNaN: false);
|
||||
var pta = df.ta.stdev(close: df.close, length: period, ddof: 0);
|
||||
for (int i = QL.Length; i > period-1; i--)
|
||||
{
|
||||
double QL_item = Math.Round(QL[i - 1].v, digits: digits);
|
||||
double PanTA_item = Math.Round((double)pta[i - 1], digits: digits);
|
||||
Assert.Equal(PanTA_item, QL_item);
|
||||
}
|
||||
}
|
||||
[Fact] void SMA() {
|
||||
SMA_Series QL = new(bars.Close, period, false);
|
||||
var pta = df.ta.sma(close: df.close, length: period);
|
||||
for (int i = QL.Length; i > period-1; i--)
|
||||
{
|
||||
double QL_item = Math.Round(QL[i - 1].v, digits: digits);
|
||||
double PanTA_item = Math.Round((double)pta[i - 1], digits: digits);
|
||||
Assert.Equal(PanTA_item, QL_item);
|
||||
}
|
||||
}
|
||||
[Fact] void SSDEV() {
|
||||
SSDEV_Series QL = new(bars.Close, period, useNaN: false);
|
||||
var pta = df.ta.stdev(close: df.close, length: period, ddof: 1);
|
||||
for (int i = QL.Length; i > period-1; i--)
|
||||
{
|
||||
double QL_item = Math.Round(QL[i - 1].v, digits: digits);
|
||||
double PanTA_item = Math.Round((double)pta[i - 1], digits: digits);
|
||||
Assert.Equal(PanTA_item, QL_item);
|
||||
}
|
||||
}
|
||||
[Fact] void SVARIANCE() {
|
||||
SVAR_Series QL = new(bars.Close, period);
|
||||
var pta = df.ta.variance(close: df.close, length: period, ddof: 1);
|
||||
for (int i = QL.Length; i > 0; i--)
|
||||
{
|
||||
double QL_item = Math.Round(QL[i - 1].v, digits: digits);
|
||||
double PanTA_item = Math.Round((double)pta[i - 1], digits: digits);
|
||||
Assert.Equal(PanTA_item, QL_item);
|
||||
}
|
||||
}
|
||||
[Fact] void T3() {
|
||||
T3_Series QL = new(source: bars.Close, period: period, vfactor: 0.7, useNaN: false);
|
||||
var pta = df.ta.t3(close: df.close, length: period, a: 0.7);
|
||||
for (int i = QL.Length; i > 0; i--)
|
||||
{
|
||||
double QL_item = Math.Round(QL[i - 1].v, digits: digits);
|
||||
double PanTA_item = Math.Round((double)pta[i - 1], digits: digits);
|
||||
Assert.Equal(PanTA_item, QL_item);
|
||||
}
|
||||
}
|
||||
[Fact] void TEMA() {
|
||||
TEMA_Series QL = new(bars.Close, period, false);
|
||||
var pta = df.ta.tema(close: df.close, length: period);
|
||||
for (int i = QL.Length; i > period; i--)
|
||||
{
|
||||
double QL_item = Math.Round(QL[i - 1].v, digits: digits);
|
||||
double PanTA_item = Math.Round((double)pta[i - 1], digits: digits);
|
||||
Assert.Equal(PanTA_item, QL_item);
|
||||
}
|
||||
}
|
||||
[Fact] void TR() {
|
||||
TR_Series QL = new(bars);
|
||||
var pta = df.ta.true_range(high: df.high, low: df.low, close: df.close);
|
||||
for (int i = QL.Length; i > 1; i--)
|
||||
{
|
||||
double QL_item = Math.Round(QL[i - 1].v, digits: digits);
|
||||
double PanTA_item = Math.Round((double)pta[i - 1], digits: digits);
|
||||
Assert.Equal(PanTA_item, QL_item);
|
||||
}
|
||||
}
|
||||
[Fact] void TRIMA() {
|
||||
// TODO: return length to variable length (period) when Pandas-TA fixes trima to calculate even periods right
|
||||
TRIMA_Series QL = new(bars.Close, 11);
|
||||
var pta = df.ta.trima(close: df.close, length: 11);
|
||||
for (int i = QL.Length; i > period-1; i--)
|
||||
{
|
||||
double QL_item = Math.Round(QL[i - 1].v, digits: digits);
|
||||
double PanTA_item = Math.Round((double)pta[i - 1], digits: digits);
|
||||
Assert.Equal(PanTA_item, QL_item);
|
||||
}
|
||||
}
|
||||
[Fact] void VARIANCE() {
|
||||
VAR_Series QL = new(bars.Close, period);
|
||||
var pta = df.ta.variance(close: df.close, length: period, ddof:0);
|
||||
for (int i = QL.Length; i > 0; i--)
|
||||
{
|
||||
double QL_item = Math.Round(QL[i - 1].v, digits: digits);
|
||||
double PanTA_item = Math.Round((double)pta[i - 1], digits: digits);
|
||||
Assert.Equal(PanTA_item, QL_item);
|
||||
}
|
||||
}
|
||||
[Fact] void WMA() {
|
||||
WMA_Series QL = new(bars.Close, period, false);
|
||||
var pta = df.ta.wma(close: df.close, length: period);
|
||||
for (int i = QL.Length; i > period-1; i--)
|
||||
{
|
||||
double QL_item = Math.Round(QL[i - 1].v, digits: digits);
|
||||
double PanTA_item = Math.Round((double)pta[i - 1], digits: digits);
|
||||
Assert.Equal(PanTA_item, QL_item);
|
||||
}
|
||||
}
|
||||
[Fact] void ZLEMA() {
|
||||
ZLEMA_Series QL = new(bars.Close, period, false);
|
||||
var pta = df.ta.zlma(close: df.close, length: period);
|
||||
for (int i = QL.Length; i > 0; i--)
|
||||
{
|
||||
double QL_item = Math.Round(QL[i - 1].v, digits: digits);
|
||||
double PanTA_item = Math.Round((double)pta[i - 1], digits: digits);
|
||||
Assert.Equal(PanTA_item, QL_item);
|
||||
}
|
||||
}
|
||||
[Fact] void ZSCORE() {
|
||||
ZSCORE_Series QL = new(bars.Close, period, useNaN: false);
|
||||
var pta = df.ta.zscore(close: df.close, length: period, ddof: 0);
|
||||
for (int i = QL.Length; i > period-1; i--)
|
||||
{
|
||||
double QL_item = Math.Round(QL[i - 1].v, digits: digits);
|
||||
double PanTA_item = Math.Round((double)pta[i - 1], digits: digits);
|
||||
Assert.Equal(PanTA_item, QL_item);
|
||||
}
|
||||
}
|
||||
|
||||
using Xunit;
|
||||
using System;
|
||||
using QuanTAlib;
|
||||
using Python.Runtime;
|
||||
using Python.Included;
|
||||
|
||||
namespace Validations;
|
||||
public class PandasTA : IDisposable
|
||||
{
|
||||
private readonly GBM_Feed bars;
|
||||
private readonly Random rnd = new();
|
||||
private readonly int period, sample;
|
||||
private int digits;
|
||||
private readonly string OStype;
|
||||
private readonly dynamic np;
|
||||
private readonly dynamic ta;
|
||||
private readonly dynamic df;
|
||||
|
||||
public PandasTA() {
|
||||
bars = new(Bars: 5000, Volatility: 0.8, Drift: 0.0);
|
||||
period = rnd.Next(maxValue: 28) + 3;
|
||||
sample = 200;
|
||||
digits = 3; //minimizing rounding errors in type conversions
|
||||
|
||||
// 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";
|
||||
|
||||
Installer.InstallPath = Path.GetFullPath(path: ".");
|
||||
Installer.SetupPython().Wait();
|
||||
Installer.TryInstallPip();
|
||||
Installer.PipInstallModule(module_name: "pandas-ta");
|
||||
Runtime.PythonDLL = OStype;
|
||||
PythonEngine.Initialize();
|
||||
np = Py.Import(name: "numpy");
|
||||
ta = Py.Import(name: "pandas_ta");
|
||||
|
||||
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));
|
||||
}
|
||||
public void Dispose()
|
||||
{
|
||||
PythonEngine.Shutdown();
|
||||
GC.SuppressFinalize(this);
|
||||
}
|
||||
|
||||
[Fact] void ADL() {
|
||||
ADL_Series QL = new(bars);
|
||||
var pta = df.ta.ad(high: df.high, low: df.low, close:df.close, volume:df.volume);
|
||||
for (int i = QL.Length; i > QL.Length-sample; i--)
|
||||
{
|
||||
double QL_item = Math.Round(QL[i-1].v, digits: digits);
|
||||
double PanTA_item = Math.Round((double)pta[i-1], digits: digits);
|
||||
Assert.Equal(PanTA_item, QL_item);
|
||||
}
|
||||
}
|
||||
[Fact] void ADOSC() {
|
||||
ADOSC_Series QL = new(bars);
|
||||
var pta = df.ta.adosc(high: df.high, low: df.low, close: df.close, volume: df.volume);
|
||||
for (int i = QL.Length; i > QL.Length-sample; i--)
|
||||
{
|
||||
double QL_item = Math.Round(QL[i - 1].v, digits: digits);
|
||||
double PanTA_item = Math.Round((double)pta[i - 1], digits: digits);
|
||||
Assert.Equal(PanTA_item, QL_item);
|
||||
}
|
||||
}
|
||||
[Fact] void ATR() {
|
||||
ATR_Series QL = new(bars, period);
|
||||
var pta = df.ta.atr(high: df.high, low: df.low, close: df.close, length: period);
|
||||
for (int i = QL.Length; i > QL.Length-sample; i--)
|
||||
{
|
||||
double QL_item = Math.Round(QL[i - 1].v, digits: digits);
|
||||
double PanTA_item = Math.Round((double)pta[i - 1], digits: digits);
|
||||
Assert.Equal(PanTA_item, QL_item);
|
||||
}
|
||||
}
|
||||
[Fact] void BIAS() {
|
||||
BIAS_Series QL = new(bars.Close, period, false);
|
||||
var pta = df.ta.bias(close: df.close, length: period);
|
||||
for (int i = QL.Length; i > QL.Length-sample; i--)
|
||||
{
|
||||
double QL_item = Math.Round(QL[i - 1].v, digits: digits);
|
||||
double PanTA_item = Math.Round((double)pta[i - 1], digits: digits);
|
||||
Assert.Equal(PanTA_item, QL_item);
|
||||
}
|
||||
}
|
||||
[Fact] void DEMA() {
|
||||
DEMA_Series QL = new(bars.Close, period, false);
|
||||
var pta = df.ta.dema(close: df.close, length: period);
|
||||
for (int i = QL.Length; i > QL.Length-sample; i--)
|
||||
{
|
||||
double QL_item = Math.Round(QL[i - 1].v, digits: digits);
|
||||
double PanTA_item = Math.Round((double)pta[i - 1], digits: digits);
|
||||
Assert.Equal(PanTA_item, QL_item);
|
||||
}
|
||||
}
|
||||
[Fact] void EMA() {
|
||||
EMA_Series QL = new(bars.Close, period, false);
|
||||
var pta = df.ta.ema(close: df.close, length: period);
|
||||
for (int i = QL.Length; i > QL.Length-sample; i--)
|
||||
{
|
||||
double QL_item = Math.Round(QL[i - 1].v, digits: digits);
|
||||
double PanTA_item = Math.Round((double)pta[i - 1], digits: digits);
|
||||
Assert.Equal(PanTA_item, QL_item);
|
||||
}
|
||||
}
|
||||
[Fact] void ENTROPY() {
|
||||
ENTROPY_Series QL = new(bars.Close, period, useNaN: false);
|
||||
var pta = df.ta.entropy(close: df.close, length: period);
|
||||
for (int i = QL.Length; i > QL.Length-sample; i--)
|
||||
{
|
||||
double QL_item = Math.Round(QL[i - 1].v, digits: digits);
|
||||
double PanTA_item = Math.Round((double)pta[i - 1], digits: digits);
|
||||
Assert.Equal(PanTA_item, QL_item);
|
||||
}
|
||||
}
|
||||
[Fact] void HL2() {
|
||||
var pta = df.ta.hl2(high: df.high, low: df.low);
|
||||
for (int i = bars.HL2.Length; i > bars.HL2.Length-sample; i--)
|
||||
{
|
||||
double QL_item = Math.Round(bars.HL2[i - 1].v, digits: digits);
|
||||
double PanTA_item = Math.Round((double)pta[i - 1], digits: digits);
|
||||
Assert.Equal(PanTA_item, QL_item);
|
||||
}
|
||||
}
|
||||
[Fact] void HLC3() {
|
||||
var pta = df.ta.hlc3(high: df.high, low: df.low, close: df.close);
|
||||
for (int i = bars.HLC3.Length; i > bars.HLC3.Length-sample; i--)
|
||||
{
|
||||
double QL_item = Math.Round(bars.HLC3[i - 1].v, digits: digits);
|
||||
double PanTA_item = Math.Round((double)pta[i - 1], digits: digits);
|
||||
Assert.Equal(PanTA_item, QL_item);
|
||||
}
|
||||
}
|
||||
[Fact] void HMA() {
|
||||
HMA_Series QL = new(bars.Close, period, false);
|
||||
var pta = df.ta.hma(close: df.close, length: period);
|
||||
for (int i = QL.Length; i > QL.Length-sample; i--)
|
||||
{
|
||||
double QL_item = Math.Round(QL[i - 1].v, digits: digits);
|
||||
double PanTA_item = Math.Round((double)pta[i - 1], digits: digits);
|
||||
Assert.Equal(PanTA_item, QL_item);
|
||||
}
|
||||
|
||||
}
|
||||
[Fact] void KAMA() {
|
||||
KAMA_Series QL = new(bars.Close, period);
|
||||
var pta = df.ta.kama(close: df.close, length: period);
|
||||
for (int i = QL.Length; i > QL.Length-sample; i--)
|
||||
{
|
||||
double QL_item = Math.Round(QL[i - 1].v, digits: digits);
|
||||
double PanTA_item = Math.Round((double)pta[i - 1], digits: digits);
|
||||
Assert.Equal(PanTA_item, QL_item);
|
||||
}
|
||||
}
|
||||
[Fact] void KURTOSIS() {
|
||||
KURTOSIS_Series QL = new(bars.Close, period, useNaN: false);
|
||||
var pta = df.ta.kurtosis(close: df.close, length: period);
|
||||
for (int i = QL.Length; i > QL.Length-sample; i--)
|
||||
{
|
||||
double QL_item = Math.Round(QL[i - 1].v, digits: digits);
|
||||
double PanTA_item = Math.Round((double)pta[i - 1], digits: digits);
|
||||
Assert.Equal(PanTA_item, QL_item);
|
||||
}
|
||||
}
|
||||
[Fact] void MAD()
|
||||
{
|
||||
MAD_Series QL = new(bars.Close, period, useNaN: false);
|
||||
var pta = df.ta.mad(close: df.close, length: period);
|
||||
for (int i = QL.Length; i > QL.Length-sample; i--)
|
||||
{
|
||||
double QL_item = Math.Round(QL[i - 1].v, digits: digits);
|
||||
double PanTA_item = Math.Round((double)pta[i - 1], digits: digits);
|
||||
Assert.Equal(PanTA_item, QL_item);
|
||||
}
|
||||
}
|
||||
[Fact] void MEDIAN() {
|
||||
MEDIAN_Series QL = new(bars.Close, period);
|
||||
var pta = df.ta.median(close: df.close, length: period);
|
||||
for (int i = QL.Length; i > QL.Length-sample; i--)
|
||||
{
|
||||
double QL_item = Math.Round(QL[i - 1].v, digits: digits);
|
||||
double PanTA_item = Math.Round((double)pta[i - 1], digits: digits);
|
||||
Assert.Equal(PanTA_item, QL_item);
|
||||
}
|
||||
}
|
||||
[Fact] void OBV() {
|
||||
OBV_Series QL = new(bars);
|
||||
var pta = df.ta.obv(close: df.close, volume: df.volume);
|
||||
for (int i = QL.Length; i > QL.Length-sample; i--)
|
||||
{
|
||||
double QL_item = Math.Round(QL[i - 1].v, digits: digits);
|
||||
double PanTA_item = Math.Round((double)pta[i - 1], digits: digits);
|
||||
Assert.Equal(PanTA_item, QL_item);
|
||||
}
|
||||
}
|
||||
[Fact] void OHLC4() {
|
||||
var pta = df.ta.ohlc4(open: df.open, high: df.high, low: df.low, close: df.close);
|
||||
for (int i = bars.OHLC4.Length; i > bars.OHLC4.Length-sample; i--)
|
||||
{
|
||||
double QL_item = Math.Round(bars.OHLC4[i - 1].v, digits: digits);
|
||||
double PanTA_item = Math.Round((double)pta[i - 1], digits: digits);
|
||||
Assert.Equal(PanTA_item, QL_item);
|
||||
}
|
||||
}
|
||||
[Fact] void RMA() {
|
||||
RMA_Series QL = new(bars.Close, period, false);
|
||||
var pta = df.ta.rma(close: df.close, length: period);
|
||||
for (int i = QL.Length; i > QL.Length-sample; i--)
|
||||
{
|
||||
double QL_item = Math.Round(QL[i - 1].v, digits: digits);
|
||||
double PanTA_item = Math.Round((double)pta[i - 1], digits: digits);
|
||||
Assert.Equal(PanTA_item, QL_item);
|
||||
}
|
||||
}
|
||||
[Fact] void RSI() {
|
||||
RSI_Series QL = new(bars.Close, period);
|
||||
var pta = df.ta.rsi(close: df.close, length: period);
|
||||
for (int i = QL.Length; i > QL.Length-sample; i--)
|
||||
{
|
||||
double QL_item = Math.Round(QL[i - 1].v, digits: digits);
|
||||
double PanTA_item = Math.Round((double)pta[i - 1], digits: digits);
|
||||
Assert.Equal(PanTA_item, QL_item);
|
||||
}
|
||||
}
|
||||
[Fact] void SDEV() {
|
||||
SDEV_Series QL = new(bars.Close, period, useNaN: false);
|
||||
var pta = df.ta.stdev(close: df.close, length: period, ddof: 0);
|
||||
for (int i = QL.Length; i > QL.Length-sample; i--)
|
||||
{
|
||||
double QL_item = Math.Round(QL[i - 1].v, digits: digits);
|
||||
double PanTA_item = Math.Round((double)pta[i - 1], digits: digits);
|
||||
Assert.Equal(PanTA_item, QL_item);
|
||||
}
|
||||
}
|
||||
[Fact] void SMA() {
|
||||
SMA_Series QL = new(bars.Close, period, false);
|
||||
var pta = df.ta.sma(close: df.close, length: period);
|
||||
for (int i = QL.Length; i > QL.Length-sample; i--)
|
||||
{
|
||||
double QL_item = Math.Round(QL[i - 1].v, digits: digits);
|
||||
double PanTA_item = Math.Round((double)pta[i - 1], digits: digits);
|
||||
Assert.Equal(PanTA_item, QL_item);
|
||||
}
|
||||
}
|
||||
[Fact] void SSDEV() {
|
||||
SSDEV_Series QL = new(bars.Close, period, useNaN: false);
|
||||
var pta = df.ta.stdev(close: df.close, length: period, ddof: 1);
|
||||
for (int i = QL.Length; i > QL.Length-sample; i--)
|
||||
{
|
||||
double QL_item = Math.Round(QL[i - 1].v, digits: digits);
|
||||
double PanTA_item = Math.Round((double)pta[i - 1], digits: digits);
|
||||
Assert.Equal(PanTA_item, QL_item);
|
||||
}
|
||||
}
|
||||
/*
|
||||
[Fact] void SVARIANCE() {
|
||||
SVAR_Series QL = new(bars.Close, period);
|
||||
var pta = df.ta.variance(close: df.close, length: period, ddof: 1);
|
||||
for (int i = QL.Length; i > QL.Length-sample; i--)
|
||||
{
|
||||
double QL_item = Math.Round(QL[i - 1].v, digits: digits);
|
||||
double PanTA_item = Math.Round((double)pta[i - 1], digits: digits);
|
||||
Assert.Equal(PanTA_item, QL_item);
|
||||
}
|
||||
}
|
||||
*/
|
||||
[Fact] void T3() {
|
||||
T3_Series QL = new(source: bars.Close, period: period, vfactor: 0.7, useNaN: false);
|
||||
var pta = df.ta.t3(close: df.close, length: period, a: 0.7);
|
||||
for (int i = QL.Length; i > QL.Length-sample; i--)
|
||||
{
|
||||
double QL_item = Math.Round(QL[i - 1].v, digits: digits);
|
||||
double PanTA_item = Math.Round((double)pta[i - 1], digits: digits);
|
||||
Assert.Equal(PanTA_item, QL_item);
|
||||
}
|
||||
}
|
||||
[Fact] void TEMA() {
|
||||
TEMA_Series QL = new(bars.Close, period, false);
|
||||
var pta = df.ta.tema(close: df.close, length: period);
|
||||
for (int i = QL.Length; i > QL.Length-sample; i--)
|
||||
{
|
||||
double QL_item = Math.Round(QL[i - 1].v, digits: digits);
|
||||
double PanTA_item = Math.Round((double)pta[i - 1], digits: digits);
|
||||
Assert.Equal(PanTA_item, QL_item);
|
||||
}
|
||||
}
|
||||
[Fact] void TR() {
|
||||
TR_Series QL = new(bars);
|
||||
var pta = df.ta.true_range(high: df.high, low: df.low, close: df.close);
|
||||
for (int i = QL.Length; i > QL.Length-sample; i--)
|
||||
{
|
||||
double QL_item = Math.Round(QL[i - 1].v, digits: digits);
|
||||
double PanTA_item = Math.Round((double)pta[i - 1], digits: digits);
|
||||
Assert.Equal(PanTA_item, QL_item);
|
||||
}
|
||||
}
|
||||
[Fact] void TRIMA() {
|
||||
// TODO: return length to variable length (period) when Pandas-TA fixes trima to calculate even periods right
|
||||
TRIMA_Series QL = new(bars.Close, 11);
|
||||
var pta = df.ta.trima(close: df.close, length: 11);
|
||||
for (int i = QL.Length; i > QL.Length-sample; i--)
|
||||
{
|
||||
double QL_item = Math.Round(QL[i - 1].v, digits: digits);
|
||||
double PanTA_item = Math.Round((double)pta[i - 1], digits: digits);
|
||||
Assert.Equal(PanTA_item, QL_item);
|
||||
}
|
||||
}
|
||||
[Fact] void VARIANCE() {
|
||||
VAR_Series QL = new(bars.Close, period);
|
||||
var pta = df.ta.variance(close: df.close, length: period, ddof:0);
|
||||
for (int i = QL.Length; i > QL.Length-sample; i--)
|
||||
{
|
||||
double QL_item = Math.Round(QL[i - 1].v, digits: digits);
|
||||
double PanTA_item = Math.Round((double)pta[i - 1], digits: digits);
|
||||
Assert.Equal(PanTA_item, QL_item);
|
||||
}
|
||||
}
|
||||
[Fact] void WMA() {
|
||||
WMA_Series QL = new(bars.Close, period, false);
|
||||
var pta = df.ta.wma(close: df.close, length: period);
|
||||
for (int i = QL.Length; i > QL.Length-sample; i--)
|
||||
{
|
||||
double QL_item = Math.Round(QL[i - 1].v, digits: digits);
|
||||
double PanTA_item = Math.Round((double)pta[i - 1], digits: digits);
|
||||
Assert.Equal(PanTA_item, QL_item);
|
||||
}
|
||||
}
|
||||
[Fact] void ZLEMA() {
|
||||
ZLEMA_Series QL = new(bars.Close, period, false);
|
||||
var pta = df.ta.zlma(close: df.close, length: period);
|
||||
for (int i = QL.Length; i > QL.Length-sample; i--)
|
||||
{
|
||||
double QL_item = Math.Round(QL[i - 1].v, digits: digits);
|
||||
double PanTA_item = Math.Round((double)pta[i - 1], digits: digits);
|
||||
Assert.Equal(PanTA_item, QL_item);
|
||||
}
|
||||
}
|
||||
[Fact] void ZSCORE() {
|
||||
ZSCORE_Series QL = new(bars.Close, period, useNaN: false);
|
||||
var pta = df.ta.zscore(close: df.close, length: period, ddof: 0);
|
||||
for (int i = QL.Length; i > QL.Length-sample; i--)
|
||||
{
|
||||
double QL_item = Math.Round(QL[i - 1].v, digits: digits);
|
||||
double PanTA_item = Math.Round((double)pta[i - 1], digits: digits);
|
||||
Assert.Equal(PanTA_item, QL_item);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,21 +1,23 @@
|
||||
using System;
|
||||
using QuanTAlib;
|
||||
using Skender.Stock.Indicators;
|
||||
using Xunit;
|
||||
|
||||
namespace Validations;
|
||||
public class Skender_Stock
|
||||
using System;
|
||||
using QuanTAlib;
|
||||
using Skender.Stock.Indicators;
|
||||
using Xunit;
|
||||
|
||||
namespace Validations;
|
||||
public class Skender
|
||||
{
|
||||
private readonly GBM_Feed bars;
|
||||
private readonly Random rnd = new();
|
||||
private readonly int period, digits;
|
||||
private readonly int period, digits, skip;
|
||||
private readonly IEnumerable<Quote> quotes;
|
||||
|
||||
|
||||
public Skender_Stock()
|
||||
public Skender()
|
||||
{
|
||||
bars = new(Bars: 10000, Volatility: 0.5, Drift: 0.0, Precision: 2);
|
||||
period = rnd.Next(30) + 5;
|
||||
digits = 4; //minimizing rounding errors in type conversions
|
||||
digits = 2; //minimizing rounding errors in type conversions
|
||||
skip = 300;
|
||||
|
||||
quotes = bars.Select(q => new Quote
|
||||
{
|
||||
@@ -27,27 +29,26 @@ public class Skender_Stock
|
||||
Volume = (decimal)q.v
|
||||
});
|
||||
}
|
||||
/*
|
||||
|
||||
[Fact]
|
||||
public void ADL()
|
||||
{
|
||||
// TODO: check precision of ADL()
|
||||
ADL_Series QL = new(bars, false);
|
||||
var SK = quotes.GetAdl().Select(i => i.Adl);
|
||||
for (int i = QL.Length; i > period; i--)
|
||||
for (int i = QL.Length; i > skip; i--)
|
||||
{
|
||||
double QL_item = Math.Round(QL[i - 1].v, digits: digits);
|
||||
double SK_item = Math.Round(SK.ElementAt(i - 1)!, digits: digits);
|
||||
Assert.Equal(SK_item!, QL_item);
|
||||
}
|
||||
}
|
||||
*/
|
||||
[Fact]
|
||||
public void ALMA()
|
||||
{
|
||||
ALMA_Series QL = new(bars.Close, period, useNaN: false);
|
||||
var SK = quotes.GetAlma(period).Select(i => i.Alma.Null2NaN()!);
|
||||
for (int i = QL.Length; i > period; i--)
|
||||
for (int i = QL.Length; i > skip; i--)
|
||||
{
|
||||
double QL_item = Math.Round(QL[i - 1].v, digits: digits);
|
||||
double SK_item = Math.Round((double)SK.ElementAt(i - 1), digits: digits);
|
||||
@@ -59,7 +60,7 @@ public class Skender_Stock
|
||||
{
|
||||
ATR_Series QL = new(bars, period, false);
|
||||
var SK = quotes.GetAtr(period).Select(i => i.Atr.Null2NaN()!);
|
||||
for (int i = QL.Length; i > period; i--)
|
||||
for (int i = QL.Length; i > skip; i--)
|
||||
{
|
||||
double QL_item = Math.Round(QL[i - 1].v, digits: digits);
|
||||
double SK_item = Math.Round((double)SK.ElementAt(i - 1), digits: digits);
|
||||
@@ -71,7 +72,7 @@ public class Skender_Stock
|
||||
{
|
||||
ATRP_Series QL = new(bars, period, false);
|
||||
var SK = quotes.GetAtr(period).Select(i => i.Atrp.Null2NaN()!);
|
||||
for (int i = QL.Length; i > period; i--)
|
||||
for (int i = QL.Length; i > skip; i--)
|
||||
{
|
||||
double QL_item = Math.Round(QL[i - 1].v, digits: digits);
|
||||
double SK_item = Math.Round((double)SK.ElementAt(i - 1), digits: digits);
|
||||
@@ -83,7 +84,7 @@ public class Skender_Stock
|
||||
{
|
||||
BBANDS_Series QL = new(bars.Close, period, 2.0, useNaN: false);
|
||||
var SK = quotes.GetBollingerBands(period, 2.0);
|
||||
for (int i = QL.Length; i > period; i--)
|
||||
for (int i = QL.Length; i > skip; i--)
|
||||
{
|
||||
double QL_item = Math.Round(QL.Mid[i - 1].v, digits: digits);
|
||||
double SK_item = Math.Round((double)SK.ElementAt(i - 1).Sma!.Value, digits: digits);
|
||||
@@ -110,7 +111,7 @@ public class Skender_Stock
|
||||
{
|
||||
CCI_Series QL = new(bars, period, false);
|
||||
var SK = quotes.GetCci(period).Select(i => i.Cci.Null2NaN()!);
|
||||
for (int i = QL.Length; i > period; i--)
|
||||
for (int i = QL.Length; i > skip; i--)
|
||||
{
|
||||
double QL_item = Math.Round(QL[i - 1].v, digits: digits);
|
||||
double SK_item = Math.Round((double)SK.ElementAt(i - 1), digits: digits);
|
||||
@@ -122,20 +123,19 @@ public class Skender_Stock
|
||||
{
|
||||
CORR_Series QL = new(bars.High, bars.Low, period, false);
|
||||
var SK = quotes.Use(CandlePart.High).GetCorrelation(quotes.Use(CandlePart.Low), period).Select(i => i.Correlation.Null2NaN()!);
|
||||
for (int i = QL.Length; i > period; i--)
|
||||
for (int i = QL.Length; i > skip; i--)
|
||||
{
|
||||
double QL_item = Math.Round(QL[i - 1].v, digits: digits);
|
||||
double SK_item = Math.Round((double)SK.ElementAt(i - 1), digits: digits);
|
||||
Assert.Equal(SK_item!, QL_item);
|
||||
}
|
||||
}
|
||||
/*
|
||||
[Fact]
|
||||
public void COVAR()
|
||||
{
|
||||
COVAR_Series QL = new(bars.High, bars.Low, period, false);
|
||||
var SK = quotes.Use(CandlePart.High).GetCorrelation(quotes.Use(CandlePart.Low), period).Select(i => i.Covariance.Null2NaN()!);
|
||||
for (int i = QL.Length; i > period; i--)
|
||||
for (int i = QL.Length; i > skip; i--)
|
||||
{
|
||||
double QL_item = Math.Round(QL[i - 1].v, digits: digits);
|
||||
double SK_item = Math.Round((double)SK.ElementAt(i - 1), digits: digits);
|
||||
@@ -147,33 +147,32 @@ public class Skender_Stock
|
||||
{
|
||||
DEMA_Series QL = new(bars.Close, period, false);
|
||||
var SK = quotes.GetDema(period).Select(i => i.Dema.Null2NaN()!);
|
||||
for (int i = QL.Length; i > period; i--)
|
||||
for (int i = QL.Length; i > skip; i--)
|
||||
{
|
||||
double QL_item = Math.Round(QL[i - 1].v, digits: digits);
|
||||
double SK_item = Math.Round((double)SK.ElementAt(i - 1), digits: digits);
|
||||
Assert.Equal(SK_item!, QL_item);
|
||||
}
|
||||
}
|
||||
*/
|
||||
[Fact]
|
||||
public void EMA()
|
||||
{
|
||||
EMA_Series QL = new(bars.Close, period, false);
|
||||
var SK = quotes.GetEma(period).Select(i => i.Ema.Null2NaN()!);
|
||||
for (int i = QL.Length; i > period; i--)
|
||||
for (int i = QL.Length; i > skip; i--)
|
||||
{
|
||||
double QL_item = Math.Round(QL[i - 1].v, digits: digits);
|
||||
double SK_item = Math.Round((double)SK.ElementAt(i - 1), digits: digits);
|
||||
Assert.Equal(SK_item!, QL_item);
|
||||
}
|
||||
}
|
||||
/*
|
||||
/*
|
||||
[Fact]
|
||||
public void HL2()
|
||||
{
|
||||
TSeries QL = bars.HL2;
|
||||
var SK = quotes.GetBaseQuote(CandlePart.HL2).Select(i => i.Value);
|
||||
for (int i = QL.Length; i > period; i--)
|
||||
for (int i = QL.Length; i > skip; i--)
|
||||
{
|
||||
double QL_item = Math.Round(QL[i - 1].v, digits: digits);
|
||||
double SK_item = Math.Round((double)SK.ElementAt(i - 1)!, digits: digits);
|
||||
@@ -185,33 +184,33 @@ public class Skender_Stock
|
||||
{
|
||||
TSeries QL = bars.HLC3;
|
||||
var SK = quotes.GetBaseQuote(CandlePart.HLC3).Select(i => i.Value);
|
||||
for (int i = QL.Length; i > period; i--)
|
||||
for (int i = QL.Length; i > skip; i--)
|
||||
{
|
||||
double QL_item = Math.Round(QL[i - 1].v, digits: digits);
|
||||
double SK_item = Math.Round((double)SK.ElementAt(i - 1)!, digits: digits);
|
||||
Assert.Equal(SK_item!, QL_item);
|
||||
}
|
||||
}
|
||||
*/
|
||||
[Fact]
|
||||
public void HMA()
|
||||
{
|
||||
HMA_Series QL = new(bars.Close, period, useNaN: false);
|
||||
var SK = quotes.GetHma(period).Select(i => i.Hma.Null2NaN()!);
|
||||
for (int i = QL.Length; i > period; i--)
|
||||
for (int i = QL.Length; i > skip; i--)
|
||||
{
|
||||
double QL_item = Math.Round(QL[i - 1].v, digits: digits);
|
||||
double SK_item = Math.Round(SK.ElementAt(i - 1), digits: digits);
|
||||
Assert.Equal(SK_item!, QL_item);
|
||||
}
|
||||
}
|
||||
*/
|
||||
[Fact]
|
||||
public void KAMA()
|
||||
{
|
||||
// TODO: check precision of KAMA()
|
||||
KAMA_Series QL = new(bars.Close, period, useNaN: false);
|
||||
var SK = quotes.GetKama(period).Select(i => i.Kama.Null2NaN()!);
|
||||
for (int i = QL.Length; i > 600; i--)
|
||||
for (int i = QL.Length; i > skip; i--)
|
||||
{
|
||||
double QL_item = Math.Round(QL[i - 1].v, digits: digits);
|
||||
double SK_item = Math.Round(SK.ElementAt(i - 1), digits: digits);
|
||||
@@ -223,7 +222,7 @@ public class Skender_Stock
|
||||
{
|
||||
LINREG_Series QL = new(bars.Close, period, useNaN: false);
|
||||
var SK = quotes.GetSlope(period);
|
||||
for (int i = QL.Length; i > period; i--)
|
||||
for (int i = QL.Length; i > skip; i--)
|
||||
{
|
||||
double QL_item = Math.Round(QL[i - 1].v, digits: digits);
|
||||
double SK_item = Math.Round((double)SK.ElementAt(i - 1).Slope!, digits: digits);
|
||||
@@ -244,7 +243,7 @@ public class Skender_Stock
|
||||
{
|
||||
MACD_Series QL = new(bars.Close, 26, 12, 9, useNaN: false);
|
||||
var SK = quotes.GetMacd(12, 26, 9);
|
||||
for (int i = QL.Length; i > 500; i--)
|
||||
for (int i = QL.Length; i > skip; i--)
|
||||
{
|
||||
double QL_item = Math.Round(QL[i - 1].v, digits: digits);
|
||||
double SK_item = Math.Round(SK.ElementAt(i - 1).Macd.Null2NaN()!, digits: digits);
|
||||
@@ -259,7 +258,7 @@ public class Skender_Stock
|
||||
{
|
||||
MAD_Series QL = new(bars.Close, period, false);
|
||||
var SK = quotes.GetSmaAnalysis(period).Select(i => i.Mad.Null2NaN()!);
|
||||
for (int i = QL.Length; i > period; i--)
|
||||
for (int i = QL.Length; i > skip; i--)
|
||||
{
|
||||
double QL_item = Math.Round(QL[i - 1].v, digits: digits);
|
||||
double SK_item = Math.Round(SK.ElementAt(i - 1), digits: digits);
|
||||
@@ -271,7 +270,7 @@ public class Skender_Stock
|
||||
{
|
||||
MAMA_Series QL = new(bars.HL2, fastlimit: 0.5, slowlimit: 0.05);
|
||||
var SK = quotes.GetMama(fastLimit: 0.5, slowLimit: 0.05);
|
||||
for (int i = QL.Length; i > period; i--)
|
||||
for (int i = QL.Length; i > skip; i--)
|
||||
{
|
||||
double QL_item = Math.Round(QL[i - 1].v, digits: digits);
|
||||
double SK_item = Math.Round(SK.ElementAt(i - 1).Mama.Null2NaN()!, digits: digits);
|
||||
@@ -286,7 +285,7 @@ public class Skender_Stock
|
||||
{
|
||||
MAPE_Series QL = new(bars.Close, period, false);
|
||||
var SK = quotes.GetSmaAnalysis(period).Select(i => i.Mape.Null2NaN()!);
|
||||
for (int i = QL.Length; i > period; i--)
|
||||
for (int i = QL.Length; i > skip; i--)
|
||||
{
|
||||
double QL_item = Math.Round(QL[i - 1].v, digits: digits);
|
||||
double SK_item = Math.Round(SK.ElementAt(i - 1), digits: digits);
|
||||
@@ -298,7 +297,7 @@ public class Skender_Stock
|
||||
{
|
||||
MSE_Series QL = new(bars.Close, period, false);
|
||||
var SK = quotes.GetSmaAnalysis(period).Select(i => i.Mse.Null2NaN()!);
|
||||
for (int i = QL.Length; i > period; i--)
|
||||
for (int i = QL.Length; i > skip; i--)
|
||||
{
|
||||
double QL_item = Math.Round(QL[i - 1].v, digits: digits);
|
||||
double SK_item = Math.Round(SK.ElementAt(i - 1), digits: digits);
|
||||
@@ -311,27 +310,32 @@ public class Skender_Stock
|
||||
OBV_Series QL = new(bars, period, false);
|
||||
var SK = quotes.GetObv(period).Select(i => i.Obv!);
|
||||
// adding volume[0] to OBV to pass the test and keep compatibility with TA-LIB
|
||||
Assert.Equal(Math.Round(SK.Last()! + (double)quotes.First().Volume!, digits: digits), Math.Round(QL.Last().v, digits: digits));
|
||||
for (int i = QL.Length; i > skip; i--)
|
||||
{
|
||||
double QL_item = Math.Round(QL.Last().v, digits: digits);
|
||||
double SK_item = Math.Round(SK.Last()! + (double)quotes.First().Volume!, digits: digits);
|
||||
Assert.Equal(SK_item!, QL_item);
|
||||
}
|
||||
}
|
||||
/*
|
||||
/*
|
||||
[Fact]
|
||||
public void OC2()
|
||||
{
|
||||
TSeries QL = bars.OC2;
|
||||
var SK = quotes.GetBaseQuote(CandlePart.OC2).Select(i => i.Value);
|
||||
for (int i = QL.Length; i > period; i--)
|
||||
for (int i = QL.Length; i > skip; i--)
|
||||
{
|
||||
double QL_item = Math.Round(QL[i - 1].v, digits: digits);
|
||||
double SK_item = Math.Round((double)SK.ElementAt(i - 1)!, digits: digits);
|
||||
Assert.Equal(SK_item!, QL_item);
|
||||
}
|
||||
}
|
||||
[Fact]
|
||||
[Fact]
|
||||
public void OHL3()
|
||||
{
|
||||
TSeries QL = bars.OHL3;
|
||||
var SK = quotes.GetBaseQuote(CandlePart.OHL3).Select(i => i.Value);
|
||||
for (int i = QL.Length; i > period; i--)
|
||||
for (int i = QL.Length; i > skip; i--)
|
||||
{
|
||||
double QL_item = Math.Round(QL[i - 1].v, digits: digits);
|
||||
double SK_item = Math.Round((double)SK.ElementAt(i - 1)!, digits: digits);
|
||||
@@ -343,20 +347,20 @@ public class Skender_Stock
|
||||
{
|
||||
TSeries QL = bars.OHLC4;
|
||||
var SK = quotes.GetBaseQuote(CandlePart.OHLC4).Select(i => i.Value);
|
||||
for (int i = QL.Length; i > period; i--)
|
||||
for (int i = QL.Length; i > skip; i--)
|
||||
{
|
||||
double QL_item = Math.Round(QL[i - 1].v, digits: digits);
|
||||
double SK_item = Math.Round((double)SK.ElementAt(i - 1)!, digits: digits);
|
||||
Assert.Equal(SK_item!, QL_item);
|
||||
}
|
||||
}
|
||||
*/
|
||||
[Fact]
|
||||
*/
|
||||
[Fact]
|
||||
public void RSI()
|
||||
{
|
||||
RSI_Series QL = new(bars.Close, period, useNaN: false);
|
||||
var SK = quotes.GetRsi(period).Select(i => i.Rsi.Null2NaN()!);
|
||||
for (int i = QL.Length; i > period; i--)
|
||||
for (int i = QL.Length; i > skip; i--)
|
||||
{
|
||||
double QL_item = Math.Round(QL[i - 1].v, digits: digits);
|
||||
double SK_item = Math.Round(SK.ElementAt(i - 1), digits: digits);
|
||||
@@ -368,7 +372,7 @@ public class Skender_Stock
|
||||
{
|
||||
SDEV_Series QL = new(bars.Close, period, useNaN: false);
|
||||
var SK = quotes.GetStdDev(period).Select(i => i.StdDev.Null2NaN()!);
|
||||
for (int i = QL.Length; i > period; i--)
|
||||
for (int i = QL.Length; i > skip; i--)
|
||||
{
|
||||
double QL_item = Math.Round(QL[i - 1].v, digits: digits);
|
||||
double SK_item = Math.Round(SK.ElementAt(i - 1), digits: digits);
|
||||
@@ -380,7 +384,7 @@ public class Skender_Stock
|
||||
{
|
||||
SMA_Series QL = new(bars.Close, period, false);
|
||||
var SK = quotes.GetSma(period).Select(i => i.Sma.Null2NaN()!);
|
||||
for (int i = QL.Length; i > period; i--)
|
||||
for (int i = QL.Length; i > skip; i--)
|
||||
{
|
||||
double QL_item = Math.Round(QL[i - 1].v, digits: digits);
|
||||
double SK_item = Math.Round(SK.ElementAt(i - 1), digits: digits);
|
||||
@@ -392,33 +396,31 @@ public class Skender_Stock
|
||||
{
|
||||
SMMA_Series QL = new(bars.Close, period, useNaN: false);
|
||||
var SK = quotes.GetSmma(period).Select(i => i.Smma.Null2NaN()!);
|
||||
for (int i = QL.Length; i > period; i--)
|
||||
for (int i = QL.Length; i > skip; i--)
|
||||
{
|
||||
double QL_item = Math.Round(QL[i - 1].v, digits: digits);
|
||||
double SK_item = Math.Round(SK.ElementAt(i - 1), digits: digits);
|
||||
Assert.Equal(SK_item!, QL_item);
|
||||
}
|
||||
}
|
||||
/*
|
||||
[Fact]
|
||||
public void T3()
|
||||
{
|
||||
T3_Series QL = new(source: bars.Close, period: period, vfactor: 0.7, false);
|
||||
var SK = quotes.GetT3(lookbackPeriods: period, volumeFactor: 0.7).Select(i => i.T3.Null2NaN()!);
|
||||
for (int i = QL.Length; i > period; i--)
|
||||
for (int i = QL.Length; i > skip; i--)
|
||||
{
|
||||
double QL_item = Math.Round(QL[i - 1].v, digits: digits);
|
||||
double SK_item = Math.Round(SK.ElementAt(i - 1), digits: digits);
|
||||
Assert.Equal(SK_item!, QL_item);
|
||||
}
|
||||
}
|
||||
*/
|
||||
[Fact]
|
||||
public void TEMA()
|
||||
{
|
||||
TEMA_Series QL = new(bars.Close, period, false);
|
||||
var SK = quotes.GetTema(period).Select(i => i.Tema.Null2NaN()!);
|
||||
for (int i = QL.Length; i > period; i--)
|
||||
for (int i = QL.Length; i > skip; i--)
|
||||
{
|
||||
double QL_item = Math.Round(QL[i - 1].v, digits: digits);
|
||||
double SK_item = Math.Round(SK.ElementAt(i - 1), digits: digits);
|
||||
@@ -430,38 +432,36 @@ public class Skender_Stock
|
||||
{
|
||||
TR_Series QL = new(bars, useNaN: false);
|
||||
var SK = quotes.GetTr().Select(i => i.Tr.Null2NaN()!);
|
||||
for (int i = QL.Length; i > 1; i--)
|
||||
for (int i = QL.Length; i > skip; i--)
|
||||
{
|
||||
double QL_item = Math.Round(QL[i - 1].v, digits: digits);
|
||||
double SK_item = Math.Round(SK.ElementAt(i - 1), digits: digits);
|
||||
Assert.Equal(SK_item!, QL_item);
|
||||
}
|
||||
}
|
||||
/*
|
||||
[Fact]
|
||||
public void WMA()
|
||||
{
|
||||
WMA_Series QL = new(bars.Close, period, false);
|
||||
var SK = quotes.GetWma(period).Select(i => i.Wma.Null2NaN()!);
|
||||
for (int i = QL.Length; i > period; i--)
|
||||
for (int i = QL.Length; i > skip; i--)
|
||||
{
|
||||
double QL_item = Math.Round(QL[i - 1].v, digits: digits);
|
||||
double SK_item = Math.Round(SK.ElementAt(i - 1), digits: digits);
|
||||
Assert.Equal(SK_item!, QL_item);
|
||||
}
|
||||
}
|
||||
*/
|
||||
[Fact]
|
||||
public void ZSCORE()
|
||||
{
|
||||
ZSCORE_Series QL = new(bars.Close, period, useNaN: false);
|
||||
var SK = quotes.GetStdDev(period).Select(i => i.ZScore.Null2NaN()!);
|
||||
for (int i = QL.Length; i > period; i--)
|
||||
for (int i = QL.Length; i > skip; i--)
|
||||
{
|
||||
double QL_item = Math.Round(QL[i - 1].v, digits: digits);
|
||||
double SK_item = Math.Round(SK.ElementAt(i - 1), digits: digits);
|
||||
Assert.Equal(SK_item!, QL_item);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -8,7 +8,7 @@ public class Ta_Lib
|
||||
{
|
||||
private readonly GBM_Feed bars;
|
||||
private readonly Random rnd = new();
|
||||
private readonly int period, digits;
|
||||
private readonly int period, digits, skip;
|
||||
private readonly double[] TALIB;
|
||||
private readonly double[] TALIB2;
|
||||
private readonly double[] inopen;
|
||||
@@ -21,6 +21,7 @@ public class Ta_Lib
|
||||
{
|
||||
bars = new(Bars: 5000, Volatility: 0.8, Drift: 0.0, Precision: 3);
|
||||
period = rnd.Next(28) + 3;
|
||||
skip = 200;
|
||||
digits = 6;
|
||||
|
||||
TALIB = new double[bars.Count];
|
||||
@@ -37,7 +38,7 @@ public class Ta_Lib
|
||||
{
|
||||
ADD_Series QL = new(bars.Open, bars.Close);
|
||||
Core.Add(inopen, inclose, 0, bars.Count - 1, TALIB, out int outBegIdx, out _);
|
||||
for (int i = QL.Length - 1; i > outBegIdx; i--)
|
||||
for (int i = QL.Length - 1; i > skip; i--)
|
||||
{
|
||||
double QL_item = Math.Round(QL[i].v, digits: digits);
|
||||
double TA_item = Math.Round(TALIB[i - outBegIdx], digits: digits);
|
||||
@@ -59,9 +60,9 @@ public class Ta_Lib
|
||||
[Fact]
|
||||
public void ADOSC()
|
||||
{
|
||||
ADOSC_Series QL = new(bars, false);
|
||||
ADOSC_Series QL = new(bars, 3, 10, false);
|
||||
Core.AdOsc(inhigh, inlow, inclose, involume, 0, bars.Count - 1, TALIB, out int outBegIdx, out _);
|
||||
for (int i = QL.Length - 1; i > outBegIdx; i--)
|
||||
for (int i = QL.Length - 1; i > skip; i--)
|
||||
{
|
||||
double QL_item = Math.Round(QL[i].v, digits: digits);
|
||||
double TA_item = Math.Round(TALIB[i - outBegIdx], digits: digits);
|
||||
@@ -73,7 +74,7 @@ public class Ta_Lib
|
||||
{
|
||||
ATR_Series QL = new(bars, period, false);
|
||||
Core.Atr(inhigh, inlow, inclose, 0, bars.Count - 1, TALIB, out int outBegIdx, out _, period);
|
||||
for (int i = QL.Length - 1; i > outBegIdx * 15; i--)
|
||||
for (int i = QL.Length - 1; i > skip * 15; i--)
|
||||
{
|
||||
double QL_item = Math.Round(QL[i].v, digits: digits);
|
||||
double TA_item = Math.Round(TALIB[i - outBegIdx], digits: digits);
|
||||
@@ -88,7 +89,7 @@ public class Ta_Lib
|
||||
double[] outLower = new double[bars.Count];
|
||||
BBANDS_Series QL = new(bars.Close, period: 26, multiplier: 2.0, false);
|
||||
Core.Bbands(inclose, 0, bars.Count - 1, outRealUpperBand: outUpper, outRealMiddleBand: outMiddle, outRealLowerBand: outLower, out int outBegIdx, out _, optInTimePeriod: 26, optInNbDevUp: 2.0, optInNbDevDn: 2.0);
|
||||
for (int i = QL.Length - 1; i > outBegIdx; i--)
|
||||
for (int i = QL.Length - 1; i > skip; i--)
|
||||
{
|
||||
double QL_item = Math.Round(QL.Upper[i].v, digits: digits);
|
||||
double TA_item = Math.Round(outUpper[i - outBegIdx], digits: digits);
|
||||
@@ -109,7 +110,7 @@ public class Ta_Lib
|
||||
{
|
||||
CCI_Series QL = new(bars, period, false);
|
||||
Core.Cci(inhigh, inlow, inclose, 0, bars.Count - 1, TALIB, out int outBegIdx, out _, period);
|
||||
for (int i = QL.Length - 1; i > outBegIdx; i--)
|
||||
for (int i = QL.Length - 1; i > skip; i--)
|
||||
{
|
||||
double QL_item = Math.Round(QL[i].v, digits: digits);
|
||||
double TA_item = Math.Round(TALIB[i - outBegIdx], digits: digits);
|
||||
@@ -121,7 +122,7 @@ public class Ta_Lib
|
||||
{
|
||||
CORR_Series QL = new(bars.Open, bars.Close, period);
|
||||
Core.Correl(inopen, inclose, 0, bars.Count - 1, TALIB, out int outBegIdx, out _, optInTimePeriod: period);
|
||||
for (int i = QL.Length - 1; i > outBegIdx; i--)
|
||||
for (int i = QL.Length - 1; i > skip; i--)
|
||||
{
|
||||
double QL_item = Math.Round(QL[i].v, digits: digits);
|
||||
double TA_item = Math.Round(TALIB[i - outBegIdx], digits: digits);
|
||||
@@ -133,7 +134,7 @@ public class Ta_Lib
|
||||
{
|
||||
DEMA_Series QL = new(bars.Close, period, false);
|
||||
Core.Dema(inclose, 0, bars.Count - 1, TALIB, out int outBegIdx, out _, period);
|
||||
for (int i = QL.Length - 1; i > outBegIdx; i--)
|
||||
for (int i = QL.Length - 1; i > skip; i--)
|
||||
{
|
||||
double QL_item = Math.Round(QL[i].v, digits: digits);
|
||||
double TA_item = Math.Round(TALIB[i - outBegIdx], digits: digits);
|
||||
@@ -145,7 +146,7 @@ public class Ta_Lib
|
||||
{
|
||||
DIV_Series QL = new(bars.Open, bars.Close);
|
||||
Core.Div(inopen, inclose, 0, bars.Count - 1, TALIB, out int outBegIdx, out _);
|
||||
for (int i = QL.Length - 1; i > outBegIdx; i--)
|
||||
for (int i = QL.Length - 1; i > skip; i--)
|
||||
{
|
||||
double QL_item = Math.Round(QL[i].v, digits: digits);
|
||||
double TA_item = Math.Round(TALIB[i - outBegIdx], digits: digits);
|
||||
@@ -157,7 +158,7 @@ public class Ta_Lib
|
||||
{
|
||||
EMA_Series QL = new(bars.Close, period, false);
|
||||
Core.Ema(inclose, 0, bars.Count - 1, TALIB, out int outBegIdx, out _, period);
|
||||
for (int i = QL.Length - 1; i > outBegIdx; i--)
|
||||
for (int i = QL.Length - 1; i > skip; i--)
|
||||
{
|
||||
double QL_item = Math.Round(QL[i].v, digits: digits);
|
||||
double TA_item = Math.Round(TALIB[i - outBegIdx], digits: digits);
|
||||
@@ -169,7 +170,7 @@ public class Ta_Lib
|
||||
{
|
||||
TSeries QL = bars.HL2;
|
||||
Core.MedPrice(inhigh, inlow, 0, bars.Count - 1, TALIB, out int outBegIdx, out _);
|
||||
for (int i = QL.Length - 1; i > outBegIdx; i--)
|
||||
for (int i = QL.Length - 1; i > skip; i--)
|
||||
{
|
||||
double QL_item = Math.Round(QL[i].v, digits: digits);
|
||||
double TA_item = Math.Round(TALIB[i - outBegIdx], digits: digits);
|
||||
@@ -181,7 +182,7 @@ public class Ta_Lib
|
||||
{
|
||||
TSeries QL = bars.HLC3;
|
||||
Core.TypPrice(inhigh, inlow, inclose, 0, bars.Count - 1, TALIB, out int outBegIdx, out _);
|
||||
for (int i = QL.Length - 1; i > outBegIdx; i--)
|
||||
for (int i = QL.Length - 1; i > skip; i--)
|
||||
{
|
||||
double QL_item = Math.Round(QL[i].v, digits: digits);
|
||||
double TA_item = Math.Round(TALIB[i - outBegIdx], digits: digits);
|
||||
@@ -193,7 +194,7 @@ public class Ta_Lib
|
||||
{
|
||||
TSeries QL = bars.HLCC4;
|
||||
Core.WclPrice(inhigh, inlow, inclose, 0, bars.Count - 1, TALIB, out int outBegIdx, out _);
|
||||
for (int i = QL.Length - 1; i > outBegIdx; i--)
|
||||
for (int i = QL.Length - 1; i > skip; i--)
|
||||
{
|
||||
double QL_item = Math.Round(QL[i].v, digits: digits);
|
||||
double TA_item = Math.Round(TALIB[i - outBegIdx], digits: digits);
|
||||
@@ -207,7 +208,7 @@ public class Ta_Lib
|
||||
double[] macdHist = new double[bars.Count];
|
||||
MACD_Series QL = new(bars.Close, slow: 26, fast: 12, signal: 9, false);
|
||||
Core.Macd(inclose, 0, bars.Count - 1, outMacd: TALIB, outMacdSignal: macdSignal, outMacdHist: macdHist, out int outBegIdx, out _);
|
||||
for (int i = QL.Length - 1; i > outBegIdx * 10; i--)
|
||||
for (int i = QL.Length - 1; i > skip * 10; i--)
|
||||
{
|
||||
double QL_item = Math.Round(QL[i].v, digits: digits);
|
||||
double TA_item = Math.Round(TALIB[i - outBegIdx], digits: digits);
|
||||
@@ -222,7 +223,7 @@ public class Ta_Lib
|
||||
{
|
||||
MAMA_Series QL = new(bars.Close, fastlimit: 0.5, slowlimit: 0.05);
|
||||
Core.Mama(inReal: inclose, startIdx: 0, endIdx: bars.Count - 1, outMama: TALIB, outFama: TALIB2, outBegIdx: out int outBegIdx, outNbElement: out _, optInFastLimit: 0.5, optInSlowLimit: 0.05);
|
||||
for (int i = QL.Length - 1; i > outBegIdx * 15; i--)
|
||||
for (int i = QL.Length - 1; i > skip * 15; i--)
|
||||
{
|
||||
double QL_item = Math.Round(QL[i].v, digits: digits);
|
||||
double TA_item = Math.Round(TALIB[i - outBegIdx], digits: digits);
|
||||
@@ -234,7 +235,7 @@ public class Ta_Lib
|
||||
{
|
||||
MAX_Series QL = new(bars.Close, period, false);
|
||||
Core.Max(inclose, 0, bars.Count - 1, TALIB, out int outBegIdx, out _, period);
|
||||
for (int i = QL.Length - 1; i > outBegIdx; i--)
|
||||
for (int i = QL.Length - 1; i > skip; i--)
|
||||
{
|
||||
double QL_item = Math.Round(QL[i].v, digits: digits);
|
||||
double TA_item = Math.Round(TALIB[i - outBegIdx], digits: digits);
|
||||
@@ -246,7 +247,7 @@ public class Ta_Lib
|
||||
{
|
||||
MIDPOINT_Series QL = new(bars.Close, period, false);
|
||||
Core.MidPoint(inclose, 0, bars.Count - 1, TALIB, out int outBegIdx, out _, period);
|
||||
for (int i = QL.Length - 1; i > outBegIdx; i--)
|
||||
for (int i = QL.Length - 1; i > skip; i--)
|
||||
{
|
||||
double QL_item = Math.Round(QL[i].v, digits: digits);
|
||||
double TA_item = Math.Round(TALIB[i - outBegIdx], digits: digits);
|
||||
@@ -258,7 +259,7 @@ public class Ta_Lib
|
||||
{
|
||||
MIDPRICE_Series QL = new(bars, period, false);
|
||||
Core.MidPrice(inhigh, inlow, 0, bars.Count - 1, TALIB, out int outBegIdx, out _, period);
|
||||
for (int i = QL.Length - 1; i > outBegIdx; i--)
|
||||
for (int i = QL.Length - 1; i > skip; i--)
|
||||
{
|
||||
double QL_item = Math.Round(QL[i].v, digits: digits);
|
||||
double TA_item = Math.Round(TALIB[i - outBegIdx], digits: digits);
|
||||
@@ -270,7 +271,7 @@ public class Ta_Lib
|
||||
{
|
||||
MIN_Series QL = new(bars.Close, period, false);
|
||||
Core.Min(inclose, 0, bars.Count - 1, TALIB, out int outBegIdx, out _, period);
|
||||
for (int i = QL.Length - 1; i > outBegIdx; i--)
|
||||
for (int i = QL.Length - 1; i > skip; i--)
|
||||
{
|
||||
double QL_item = Math.Round(QL[i].v, digits: digits);
|
||||
double TA_item = Math.Round(TALIB[i - outBegIdx], digits: digits);
|
||||
@@ -282,7 +283,7 @@ public class Ta_Lib
|
||||
{
|
||||
MUL_Series QL = new(bars.Open, bars.Close);
|
||||
Core.Mult(inopen, inclose, 0, bars.Count - 1, TALIB, out int outBegIdx, out _);
|
||||
for (int i = QL.Length - 1; i > outBegIdx; i--)
|
||||
for (int i = QL.Length - 1; i > skip; i--)
|
||||
{
|
||||
double QL_item = Math.Round(QL[i].v, digits: digits);
|
||||
double TA_item = Math.Round(TALIB[i - outBegIdx], digits: digits);
|
||||
@@ -294,7 +295,7 @@ public class Ta_Lib
|
||||
{
|
||||
OBV_Series QL = new(bars, period, false);
|
||||
Core.Obv(inclose, involume, 0, bars.Count - 1, TALIB, out int outBegIdx, out _);
|
||||
for (int i = QL.Length - 1; i > outBegIdx; i--)
|
||||
for (int i = QL.Length - 1; i > skip; i--)
|
||||
{
|
||||
double QL_item = Math.Round(QL[i].v, digits: digits);
|
||||
double TA_item = Math.Round(TALIB[i - outBegIdx], digits: digits);
|
||||
@@ -306,7 +307,7 @@ public class Ta_Lib
|
||||
{
|
||||
TSeries QL = bars.OHLC4;
|
||||
Core.AvgPrice(inopen, inhigh, inlow, inclose, 0, bars.Count - 1, TALIB, out int outBegIdx, out _);
|
||||
for (int i = QL.Length - 1; i > outBegIdx; i--)
|
||||
for (int i = QL.Length - 1; i > skip; i--)
|
||||
{
|
||||
double QL_item = Math.Round(QL[i].v, digits: digits);
|
||||
double TA_item = Math.Round(TALIB[i - outBegIdx], digits: digits);
|
||||
@@ -318,7 +319,7 @@ public class Ta_Lib
|
||||
{
|
||||
RSI_Series QL = new(bars.Close, period, false);
|
||||
Core.Rsi(inclose, 0, bars.Count - 1, TALIB, out int outBegIdx, out _, period);
|
||||
for (int i = QL.Length - 1; i > outBegIdx; i--)
|
||||
for (int i = QL.Length - 1; i > skip; i--)
|
||||
{
|
||||
double QL_item = Math.Round(QL[i].v, digits: digits);
|
||||
double TA_item = Math.Round(TALIB[i - outBegIdx], digits: digits);
|
||||
@@ -330,7 +331,7 @@ public class Ta_Lib
|
||||
{
|
||||
SDEV_Series QL = new(bars.Close, period, false);
|
||||
Core.StdDev(inclose, 0, bars.Count - 1, TALIB, out int outBegIdx, out _, period);
|
||||
for (int i = QL.Length - 1; i > outBegIdx; i--)
|
||||
for (int i = QL.Length - 1; i > skip; i--)
|
||||
{
|
||||
double QL_item = Math.Round(QL[i].v, digits: digits);
|
||||
double TA_item = Math.Round(TALIB[i - outBegIdx], digits: digits);
|
||||
@@ -342,7 +343,7 @@ public class Ta_Lib
|
||||
{
|
||||
SMA_Series QL = new(bars.Close, period, false);
|
||||
Core.Sma(inclose, 0, bars.Count - 1, TALIB, out int outBegIdx, out _, period);
|
||||
for (int i = QL.Length - 1; i > outBegIdx; i--)
|
||||
for (int i = QL.Length - 1; i > skip; i--)
|
||||
{
|
||||
double QL_item = Math.Round(QL[i].v, digits: digits);
|
||||
double TA_item = Math.Round(TALIB[i - outBegIdx], digits: digits);
|
||||
@@ -354,7 +355,7 @@ public class Ta_Lib
|
||||
{
|
||||
SUB_Series QL = new(bars.Open, bars.Close);
|
||||
Core.Sub(inopen, inclose, 0, bars.Count - 1, TALIB, out int outBegIdx, out _);
|
||||
for (int i = QL.Length - 1; i > outBegIdx; i--)
|
||||
for (int i = QL.Length - 1; i > skip; i--)
|
||||
{
|
||||
double QL_item = Math.Round(QL[i].v, digits: digits);
|
||||
double TA_item = Math.Round(TALIB[i - outBegIdx], digits: digits);
|
||||
@@ -366,7 +367,7 @@ public class Ta_Lib
|
||||
{
|
||||
SUM_Series QL = new(bars.Close, period, false);
|
||||
Core.Sum(inclose, 0, bars.Count - 1, TALIB, out int outBegIdx, out _, period);
|
||||
for (int i = QL.Length - 1; i > outBegIdx; i--)
|
||||
for (int i = QL.Length - 1; i > skip; i--)
|
||||
{
|
||||
double QL_item = Math.Round(QL[i].v, digits: digits);
|
||||
double TA_item = Math.Round(TALIB[i - outBegIdx], digits: digits);
|
||||
@@ -378,7 +379,7 @@ public class Ta_Lib
|
||||
{
|
||||
T3_Series QL = new(source: bars.Close, period: period, vfactor: 0.7, useNaN: false);
|
||||
Core.T3(inReal: inclose, startIdx: 0, endIdx: bars.Count - 1, outReal: TALIB, outBegIdx: out int outBegIdx, outNbElement: out _, optInTimePeriod: period, optInVFactor: 0.7);
|
||||
for (int i = QL.Length - 1; i > outBegIdx * 15; i--)
|
||||
for (int i = QL.Length - 1; i > skip * 15; i--)
|
||||
{
|
||||
double QL_item = Math.Round(QL[i].v, digits: digits);
|
||||
double TA_item = Math.Round(TALIB[i - outBegIdx], digits: digits);
|
||||
@@ -390,7 +391,7 @@ public class Ta_Lib
|
||||
{
|
||||
TEMA_Series QL = new(bars.Close, period, false);
|
||||
Core.Tema(inclose, 0, bars.Count - 1, TALIB, out int outBegIdx, out _, period);
|
||||
for (int i = QL.Length - 1; i > outBegIdx * 15; i--)
|
||||
for (int i = QL.Length - 1; i > skip * 15; i--)
|
||||
{
|
||||
double QL_item = Math.Round(QL[i].v, digits: digits);
|
||||
double TA_item = Math.Round(TALIB[i - outBegIdx], digits: digits);
|
||||
@@ -402,7 +403,7 @@ public class Ta_Lib
|
||||
{
|
||||
TR_Series QL = new(bars, false);
|
||||
Core.TRange(inhigh, inlow, inclose, 0, bars.Count - 1, TALIB, out int outBegIdx, out _);
|
||||
for (int i = QL.Length - 1; i > outBegIdx; i--)
|
||||
for (int i = QL.Length - 1; i > skip; i--)
|
||||
{
|
||||
double QL_item = Math.Round(QL[i].v, digits: digits);
|
||||
double TA_item = Math.Round(TALIB[i - outBegIdx], digits: digits);
|
||||
@@ -414,7 +415,7 @@ public class Ta_Lib
|
||||
{
|
||||
TRIMA_Series QL = new(bars.Close, period, false);
|
||||
Core.Trima(inclose, 0, bars.Count - 1, TALIB, out int outBegIdx, out _, period);
|
||||
for (int i = QL.Length - 1; i > outBegIdx; i--)
|
||||
for (int i = QL.Length - 1; i > skip; i--)
|
||||
{
|
||||
double QL_item = Math.Round(QL[i].v, digits: digits);
|
||||
double TA_item = Math.Round(TALIB[i - outBegIdx], digits: digits);
|
||||
@@ -426,7 +427,7 @@ public class Ta_Lib
|
||||
{
|
||||
VAR_Series QL = new(bars.Close, period, false);
|
||||
Core.Var(inclose, 0, bars.Count - 1, TALIB, out int outBegIdx, out _, period);
|
||||
for (int i = QL.Length - 1; i > outBegIdx * 15; i--)
|
||||
for (int i = QL.Length - 1; i > skip * 15; i--)
|
||||
{
|
||||
double QL_item = Math.Round(QL[i].v, digits: digits);
|
||||
double TA_item = Math.Round(TALIB[i - outBegIdx], digits: digits);
|
||||
@@ -438,7 +439,7 @@ public class Ta_Lib
|
||||
{
|
||||
WMA_Series QL = new(bars.Close, period, false);
|
||||
Core.Wma(inclose, 0, bars.Count - 1, TALIB, out int outBegIdx, out _, period);
|
||||
for (int i = QL.Length - 1; i > outBegIdx; i--)
|
||||
for (int i = QL.Length - 1; i > skip; i--)
|
||||
{
|
||||
double QL_item = Math.Round(QL[i].v, digits: digits);
|
||||
double TA_item = Math.Round(TALIB[i - outBegIdx], digits: digits);
|
||||
@@ -0,0 +1,158 @@
|
||||
using Xunit;
|
||||
using System;
|
||||
using Tulip;
|
||||
using QuanTAlib;
|
||||
|
||||
namespace Validations;
|
||||
public class Tulip_Test
|
||||
{
|
||||
private readonly GBM_Feed bars;
|
||||
private readonly Random rnd = new();
|
||||
private readonly int period, digits, skip;
|
||||
private readonly double[] outdata;
|
||||
private readonly double[] inopen;
|
||||
private readonly double[] inhigh;
|
||||
private readonly double[] inlow;
|
||||
private readonly double[] inclose;
|
||||
private readonly double[] involume;
|
||||
|
||||
public Tulip_Test()
|
||||
{
|
||||
bars = new(Bars: 5000, Volatility: 0.8, Drift: 0.0, Precision: 3);
|
||||
period = rnd.Next(28) + 3;
|
||||
skip = 600;
|
||||
digits = 5;
|
||||
|
||||
outdata = new double[bars.Count];
|
||||
inopen = bars.Open.v.ToArray();
|
||||
inhigh = bars.High.v.ToArray();
|
||||
inlow = bars.Low.v.ToArray();
|
||||
inclose = bars.Close.v.ToArray()!;
|
||||
involume = bars.Volume.v.ToArray()!;
|
||||
|
||||
}
|
||||
[Fact]
|
||||
public void AD()
|
||||
{
|
||||
double[][] arrin = {inhigh, inlow, inclose, involume };
|
||||
double[][] arrout = { outdata };
|
||||
ADL_Series QL = new(bars, false);
|
||||
Tulip.Indicators.ad.Run(inputs: arrin, options: new double[] { }, outputs: arrout);
|
||||
for (int i = QL.Length - 1; i > skip; i--)
|
||||
{
|
||||
double QL_item = Math.Round(QL[i].v, digits: digits);
|
||||
double TU_item = Math.Round(arrout[0][i], digits);
|
||||
Assert.Equal(TU_item!, QL_item);
|
||||
}
|
||||
}
|
||||
[Fact]
|
||||
public void ADD()
|
||||
{
|
||||
double[][] arrin = { inhigh, inlow };
|
||||
double[][] arrout = { outdata };
|
||||
ADD_Series QL = new(bars.High, bars.Low);
|
||||
Tulip.Indicators.add.Run(inputs: arrin, options: new double[] { period }, outputs: arrout);
|
||||
for (int i = QL.Length - 1; i > skip; i--)
|
||||
{
|
||||
double QL_item = Math.Round(QL[i].v, digits: digits);
|
||||
double TU_item = Math.Round(arrout[0][i], digits);
|
||||
Assert.Equal(TU_item!, QL_item);
|
||||
}
|
||||
}
|
||||
[Fact]
|
||||
public void ADOSC()
|
||||
{
|
||||
double[][] arrin = { inhigh, inlow, inclose, involume };
|
||||
double[][] arrout = { outdata };
|
||||
int s = 3;
|
||||
ADOSC_Series QL = new(bars, s, period, false);
|
||||
Tulip.Indicators.adosc.Run(inputs: arrin, options: new double[] { s, period }, outputs: arrout);
|
||||
for (int i = QL.Length - 1; i > skip; i--)
|
||||
{
|
||||
double QL_item = Math.Round(QL[i].v, digits: digits);
|
||||
double TU_item = Math.Round(arrout[0][i-period+1], digits);
|
||||
Assert.Equal(TU_item!, QL_item);
|
||||
}
|
||||
}
|
||||
[Fact]
|
||||
public void ATR()
|
||||
{
|
||||
double[][] arrin = { inhigh, inlow, inclose };
|
||||
double[][] arrout = { outdata };
|
||||
|
||||
ATR_Series QL = new(bars, period, false);
|
||||
Tulip.Indicators.atr.Run(inputs: arrin, options: new double[] { period }, outputs: arrout);
|
||||
for (int i = QL.Length - 1; i > skip; i--)
|
||||
{
|
||||
double QL_item = Math.Round(QL[i].v, digits: digits);
|
||||
double TU_item = Math.Round(arrout[0][i - period + 1], digits);
|
||||
Assert.Equal(TU_item!, QL_item);
|
||||
}
|
||||
}
|
||||
[Fact]
|
||||
public void BBANDS()
|
||||
{
|
||||
double[][] arrin = { inclose };
|
||||
double[] outmid = new double[bars.Count];
|
||||
double[] outlower = new double[bars.Count];
|
||||
double[] outupper = new double[bars.Count];
|
||||
double[][] arrout = { outlower, outmid, outupper};
|
||||
BBANDS_Series QL = new(bars.Close, period, 2, false);
|
||||
Tulip.Indicators.bbands.Run(inputs: arrin, options: new double[] { period, 2 }, outputs: arrout);
|
||||
for (int i = QL.Length - 1; i > skip; i--)
|
||||
{
|
||||
double QL_item = Math.Round(QL.Lower[i].v, digits: digits);
|
||||
double TU_item = Math.Round(outlower[i - period + 1], digits);
|
||||
Assert.Equal(TU_item!, QL_item);
|
||||
QL_item = Math.Round(QL.Mid[i].v, digits: digits);
|
||||
TU_item = Math.Round(outmid[i - period + 1], digits);
|
||||
Assert.Equal(TU_item!, QL_item);
|
||||
QL_item = Math.Round(QL.Upper[i].v, digits: digits);
|
||||
TU_item = Math.Round(outupper[i - period + 1], digits);
|
||||
Assert.Equal(TU_item!, QL_item);
|
||||
}
|
||||
}
|
||||
[Fact]
|
||||
public void EMA()
|
||||
{
|
||||
double[][] arrin = { inclose };
|
||||
double[][] arrout = { outdata };
|
||||
EMA_Series QL = new(bars.Close, period, false);
|
||||
Tulip.Indicators.ema.Run(inputs: arrin, options: new double[] { period }, outputs: arrout);
|
||||
for (int i = QL.Length - 1; i > skip; i--)
|
||||
{
|
||||
double QL_item = Math.Round(QL[i].v, digits: digits);
|
||||
double TU_item = Math.Round(arrout[0][i], digits);
|
||||
Assert.Equal(TU_item!, QL_item);
|
||||
}
|
||||
}
|
||||
[Fact]
|
||||
public void AVGPRICE()
|
||||
{
|
||||
double[][] arrin = { inopen, inhigh, inlow, inclose };
|
||||
double[][] arrout = { outdata };
|
||||
|
||||
TSeries QL = bars.OHLC4;
|
||||
Tulip.Indicators.avgprice.Run(inputs: arrin, options: new double[] { }, outputs: arrout);
|
||||
for (int i = QL.Length - 1; i > skip; i--)
|
||||
{
|
||||
double QL_item = Math.Round(QL[i].v, digits: digits);
|
||||
double TU_item = Math.Round(arrout[0][i], digits);
|
||||
Assert.Equal(TU_item!, QL_item);
|
||||
}
|
||||
}
|
||||
[Fact]
|
||||
public void SMA()
|
||||
{
|
||||
double[][] arrin = { inclose };
|
||||
double[][] arrout = { outdata };
|
||||
SMA_Series QL = new(bars.Close, period, false);
|
||||
Tulip.Indicators.sma.Run(inputs: arrin, options: new double[] { period }, outputs: arrout);
|
||||
for (int i = QL.Length - 1; i > skip; i--)
|
||||
{
|
||||
double QL_item = Math.Round(QL[i].v, digits: digits);
|
||||
double TU_item = Math.Round(arrout[0][i-period+1], digits);
|
||||
Assert.Equal(TU_item!, QL_item);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user