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); + } + } +} diff --git a/Tests/Validations/EMA_Test.cs b/Tests/Validations/EMA_Test.cs new file mode 100644 index 00000000..cc20f75a --- /dev/null +++ b/Tests/Validations/EMA_Test.cs @@ -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 IEnumerablequotes; + 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); + } + } +} diff --git a/Tests/Validations/SMA_Test.cs b/Tests/Validations/SMA_Test.cs new file mode 100644 index 00000000..eaaf3d7c --- /dev/null +++ b/Tests/Validations/SMA_Test.cs @@ -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 IEnumerablequotes; + 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); + } + } +} diff --git a/Tests/Validations/Pandas_TA.cs b/Tests/Validations/Trends/Pandas_TA.cs similarity index 84% rename from Tests/Validations/Pandas_TA.cs rename to Tests/Validations/Trends/Pandas_TA.cs index 4225dea4..cfe24689 100644 --- a/Tests/Validations/Pandas_TA.cs +++ b/Tests/Validations/Trends/Pandas_TA.cs @@ -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); + } + } + } \ No newline at end of file diff --git a/Tests/Validations/Skender_Stock.cs b/Tests/Validations/Trends/Skender.cs similarity index 88% rename from Tests/Validations/Skender_Stock.cs rename to Tests/Validations/Trends/Skender.cs index 27837c89..b1796f86 100644 --- a/Tests/Validations/Skender_Stock.cs +++ b/Tests/Validations/Trends/Skender.cs @@ -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 IEnumerablequotes; + - 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); } - } - -} + } + +} diff --git a/Tests/Validations/TA_LIB.cs b/Tests/Validations/Trends/TA_LIB.cs similarity index 88% rename from Tests/Validations/TA_LIB.cs rename to Tests/Validations/Trends/TA_LIB.cs index 06edb0e6..43b77fbb 100644 --- a/Tests/Validations/TA_LIB.cs +++ b/Tests/Validations/Trends/TA_LIB.cs @@ -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); diff --git a/Tests/Validations/Trends/Tulip.cs b/Tests/Validations/Trends/Tulip.cs new file mode 100644 index 00000000..fd36c4e8 --- /dev/null +++ b/Tests/Validations/Trends/Tulip.cs @@ -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); + } + } +} diff --git a/docs/SMA.md b/docs/SMA.md new file mode 100644 index 00000000..3f5eca99 --- /dev/null +++ b/docs/SMA.md @@ -0,0 +1,40 @@ +# SMA: Simple Moving Average +SMA is one of the most basic trend-following indicators used in Technical Analysis. It is calculated as the *unweighted mean* of the previous $p$ (period) data-points. + + +## Calculation + +SMA is a rolling calculation looking backwards from the position ${n}$ and is denoted as ${SMA}_{p}{(data)}$ where $p$ represents the period and $data$ represents the list of data points: +$$ +SMA_p{(data)} = \frac{1}{p}\sum_{i=n-p+1}^{n} data_i +$$ +When calculating the value of next $SMA_{p,next}$ while knowing all previous SMA values, SMA calculation can be reduced to: +$$ +SMA_{p,next} = SMA_{p,prev}+\frac{1}{p}\left( data_{n+1}-data_{n+1-p}\right) +$$ + +## Implementation + +``` csharp +SMA_Series mean = new(source: data, period: p, useNaN: false); + +QuanTA fluent = data.SMA(period: p); +``` + +## Parameters + +- `TSeries source` - List of value tuples (DateTime, double) +- `int period` - Integer representing the period of SMA +- `bool useNaN` - if true, initial values from 1 to period-1 will be replaced with NaN. If false, the initial calculation will return values for SMA(length) instead of SMA(period) + +## Sample chart + +picture of SMA + +## Comparison & Validation + +Validation tests +Performance tests + +## References + - https://www.tradingtechnologies.com/help/x-study/technical-indicator-definitions/simple-moving-average-sma/ \ No newline at end of file diff --git a/docs/comparing trends.ipynb b/docs/comparing trends.ipynb deleted file mode 100644 index ae2cbc6c..00000000 --- a/docs/comparing trends.ipynb +++ /dev/null @@ -1,137 +0,0 @@ -{ - "cells": [ - { - "cell_type": "code", - "execution_count": 1, - "metadata": { - "dotnet_interactive": { - "language": "csharp" - }, - "vscode": { - "languageId": "dotnet-interactive.csharp" - } - }, - "outputs": [ - { - "data": { - "text/html": [ - "" - ] - }, - "metadata": {}, - "output_type": "display_data" - }, - { - "data": { - "text/plain": [ - "Loading extensions from `C:\\Users\\miha\\.nuget\\packages\\plotly.net.interactive\\3.0.2\\interactive-extensions\\dotnet\\Plotly.NET.Interactive.dll`" - ] - }, - "metadata": {}, - "output_type": "display_data" - } - ], - "source": [ - "//#r \"nuget: QuanTAlib;\"\n", - "\n", - "#r \"nuget: Plotly.NET;\"\n", - "#r \"nuget: Plotly.NET.Interactive;\"\n", - "#r \"..\\Source\\bin\\Debug\\net6.0\\QuanTAlib.dll\"\n", - "\n", - "using QuanTAlib;\n", - "using Plotly.NET;\n", - "using Plotly.NET.LayoutObjects;" - ] - }, - { - "cell_type": "code", - "execution_count": 10, - "metadata": { - "dotnet_interactive": { - "language": "csharp" - }, - "vscode": { - "languageId": "dotnet-interactive.csharp" - } - }, - "outputs": [ - { - "data": { - "text/html": [ - "\n", - "\n", - " \r\n", - "\r\n", - "\n", - " \n", - "\n" - ] - }, - "metadata": {}, - "output_type": "display_data" - } - ], - "source": [ - "int period = 25;\n", - "GBM_Feed feed = new(50, Volatility: 1);\n", - "TSeries data = feed.OHLC4;\n", - "//Listraw = new() { 0, 1, 1 ,1 ,1, 1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0}; \n", - "//TSeries data = new();\n", - "//for (int i=0; i (data.t, data.v, false, data.GetType().Name).WithLineStyle(Width: 4, Color: Color.fromString(\"red\"));\n", - "var line1 = Chart2D.Chart.Line (calc1.t, calc1.v, false, \"EMA_SMA\").WithLineStyle(Width: 2, Color: Color.fromString(\"purple\"));\n", - "var line2 = Chart2D.Chart.Line (calc2.t, calc2.v, false, \"True EMA\").WithLineStyle(Width: 2, Color: Color.fromString(\"blue\"));\n", - "\n", - "var chart = Chart.Combine(new []{line0, line1, line2})\n", - " .WithSize(1200,600)\n", - " .WithMargin(Margin.init (30,10,40,30,1,false))\n", - " .WithXAxisRangeSlider(RangeSlider.init(Visible:false));\n", - "\n", - "chart" - ] - } - ], - "metadata": { - "kernelspec": { - "display_name": ".NET (C#)", - "language": "C#", - "name": ".net-csharp" - }, - "language_info": { - "file_extension": ".cs", - "mimetype": "text/x-csharp", - "name": "C#", - "pygments_lexer": "csharp", - "version": "9.0" - }, - "orig_nbformat": 4 - }, - "nbformat": 4, - "nbformat_minor": 2 -} diff --git a/docs/crossovers.ipynb b/docs/crossovers.ipynb deleted file mode 100644 index 1a8821f6..00000000 --- a/docs/crossovers.ipynb +++ /dev/null @@ -1,149 +0,0 @@ -{ - "cells": [ - { - "cell_type": "code", - "execution_count": 11, - "metadata": { - "dotnet_interactive": { - "language": "csharp" - }, - "vscode": { - "languageId": "dotnet-interactive.csharp" - } - }, - "outputs": [ - { - "data": { - "text/html": [ - " " - ] - }, - "metadata": {}, - "output_type": "display_data" - } - ], - "source": [ - "#r \"nuget: QuanTAlib;\"\n", - "#r \"nuget: Plotly.NET;\"\n", - "#r \"nuget: Plotly.NET.Interactive;\"\n", - "\n", - "using QuanTAlib;\n", - "using Plotly.NET;\n", - "using Plotly.NET.LayoutObjects;\n" - ] - }, - { - "cell_type": "code", - "execution_count": 12, - "metadata": { - "dotnet_interactive": { - "language": "csharp" - }, - "vscode": { - "languageId": "dotnet-interactive.csharp" - } - }, - "outputs": [], - "source": [ - "String Sym = \"IBM\";\n", - "Alphavantage_Feed data = new(Symbol: Sym);\n", - "ZLEMA_Series calc1 = new(data.OHLC4,20);\n", - "HMA_Series calc2 = new(data.OHLC4,20);\n", - "HEMA_Series calc3 = new(data.OHLC4,20);" - ] - }, - { - "cell_type": "code", - "execution_count": 17, - "metadata": { - "dotnet_interactive": { - "language": "csharp" - }, - "vscode": { - "languageId": "dotnet-interactive.csharp" - } - }, - "outputs": [ - { - "data": { - "text/html": [ - "\n", - "\n", - " \r\n", - "\r\n", - "\n", - " \n", - "\n" - ] - }, - "metadata": {}, - "output_type": "display_data" - } - ], - "source": [ - "var layout = Layout.init( \n", - " PlotBGColor : Color.fromString(\"#1e1e1e\"),\n", - " PaperBGColor : Color.fromString(\"#1e1e1e\"),\n", - " Font:Font.init(Size:10, Color: Color.fromString(\"#ffffff\")));\n", - "\n", - "var yAxis = LinearAxis.init (\n", - " GridColor:Color.fromString(\"#252525\")); \n", - "\n", - "var candles = Chart2D.Chart.Candlestick (data.Open.v, data.High.v, data.Low.v, data.Close.v, data.Open.t, \"\");\n", - "var line1 = Chart2D.Chart.Line (calc1.t, calc1.v, false, calc1.GetType().Name).WithLineStyle(Width: 2, Color: Color.fromString(\"yellow\"));\n", - "var line2 = Chart2D.Chart.Line (calc2.t, calc2.v, false, calc2.GetType().Name).WithLineStyle(Width: 3, Color: Color.fromString(\"red\"));\n", - "var line3 = Chart2D.Chart.Line (calc3.t, calc3.v, false, calc3.GetType().Name).WithLineStyle(Width: 2, Color: Color.fromString(\"blue\"));\n", - "var chart = Chart.Combine(new []{candles, line1, line2, line3})\n", - " .WithSize(1200,600)\n", - " .WithMargin(Margin.init (30,10,40,30,1,false))\n", - " .WithXAxisRangeSlider(RangeSlider.init(Visible:false))\n", - " .WithYAxis(yAxis)\n", - " .WithXAxis(yAxis)\n", - " .WithTitle(Sym)\n", - " .WithLayout(layout);\n", - "\n", - "chart" - ] - } - ], - "metadata": { - "kernelspec": { - "display_name": ".NET (C#)", - "language": "C#", - "name": ".net-csharp" - }, - "language_info": { - "file_extension": ".cs", - "mimetype": "text/x-csharp", - "name": "C#", - "pygments_lexer": "csharp", - "version": "9.0" - }, - "orig_nbformat": 4 - }, - "nbformat": 4, - "nbformat_minor": 2 -} diff --git a/docs/ema study.ipynb b/docs/ema study.ipynb deleted file mode 100644 index 49fde29c..00000000 --- a/docs/ema study.ipynb +++ /dev/null @@ -1,249 +0,0 @@ -{ - "cells": [ - { - "cell_type": "code", - "execution_count": 44, - "metadata": { - "dotnet_interactive": { - "language": "csharp" - }, - "vscode": { - "languageId": "dotnet-interactive.csharp" - } - }, - "outputs": [ - { - "data": { - "text/html": [ - " " - ] - }, - "metadata": {}, - "output_type": "display_data" - } - ], - "source": [ - "#r \"nuget: Plotly.NET;\"\n", - "#r \"nuget: Plotly.NET.ImageExport;\"\n", - "\n", - "using Plotly.NET;\n", - "using Plotly.NET.ImageExport;\n", - "\n", - "double ema(double data, double prev_ema, int period, double attenuator = 0.0) {\n", - " double k = 2.0 / (period + 1);\n", - " return ((data*k) + (prev_ema*(1-k)))/(1-attenuator);\n", - "}\n", - "\n", - "Listdata = new() { 1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0}; \n", - "List x = new() { 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16};\n", - "List es = new();\n", - "List e1 = new();\n", - "List e2 = new();\n", - "List e3 = new();" - ] - }, - { - "attachments": {}, - "cell_type": "markdown", - "metadata": {}, - "source": [ - "EMA is a convergent moving average where we don't need to keep a list of past values around; all that is needed to calculate the next value of EMA is a previous value, factor k [ k = 2.0/(period+1) ] and new value.\n", - "\n", - "The core question is: how to START the EMA sequence?\n", - "\n", - "The most direct way is to start the EMA sequence with an arbitrary number - either zero or the first value are commonly accepted 'igniters' of the calculation. Except both starting values are wrong: \n", - "- starting EMA with the First Value creates overshooting EMA, requiring 50+ bars to converge to the correct series.\n", - "- starting EMA with a Zero undershoots the series, requiring 50+ bars to climb back to the correct series.\n", - "- starting EMA with a short SMA warming sequence at the beginning creates even more problems: transition from SMA to EMA is non-linear and results are not predictable.\n", - "\n", - "But there is a better way.\n", - "\n", - "I found an article on [David Owen's blog](https://blog.fugue88.ws/archives/2017-01/The-correct-way-to-start-an-Exponential-Moving-Average-EMA) that explains the math behind attenuation of early elements of EMA series.\n", - "\n", - "Below is the simple comparison of four approaches:\n", - "\n", - "- low EMA (series assumes that pre-value was 0)\n", - "- high EMA (series assumes that pre-value was same as the first value)\n", - "- SMA-EMA (series first calculates SMA for the duration of period and then uses the last SMA to calculate EMA)\n", - "- fixed EMA (uses diminishing attenuation to keep EMA between overshooting and undershooting EMA )\n" - ] - }, - { - "cell_type": "code", - "execution_count": 47, - "metadata": { - "dotnet_interactive": { - "language": "csharp" - }, - "vscode": { - "languageId": "dotnet-interactive.csharp" - } - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "data\t low\t high\t s_ema fixed\t\r\n", - "1\t 0.222\t 1.000\t 1.000\t 1.000\t\r\n", - "0\t 0.173\t 0.778\t 0.500\t 0.560\t\r\n", - "0\t 0.134\t 0.605\t 0.333\t 0.393\t\r\n", - "0\t 0.105\t 0.471\t 0.250\t 0.294\t\r\n", - "0\t 0.081\t 0.366\t 0.200\t 0.226\t\r\n", - "0\t 0.063\t 0.285\t 0.167\t 0.175\t\r\n", - "1\t 0.271\t 0.444\t 0.286\t 0.358\t\r\n", - "1\t 0.433\t 0.567\t 0.375\t 0.501\t\r\n", - "1\t 0.559\t 0.663\t 0.514\t 0.611\t\r\n", - "1\t 0.657\t 0.738\t 0.622\t 0.698\t\r\n", - "1\t 0.733\t 0.796\t 0.706\t 0.765\t\r\n", - "0\t 0.570\t 0.619\t 0.549\t 0.595\t\r\n", - "0\t 0.444\t 0.482\t 0.427\t 0.463\t\r\n", - "0\t 0.345\t 0.375\t 0.332\t 0.360\t\r\n", - "0\t 0.268\t 0.291\t 0.258\t 0.280\t\r\n", - "0\t 0.209\t 0.227\t 0.201\t 0.218\t\r\n" - ] - } - ], - "source": [ - "int period = 18;\n", - "\n", - "double k = 2.0 / (period + 1);\n", - "double attenuator = ( 1 - k ) * 0.5;\n", - "double factor = 1;\n", - "double ema1 = 0; // start too low\n", - "double ema2 = data[0]; // start too high\n", - "double ema3 = data[0]*(1-k-attenuator)/(1-k); \n", - "double emas = data[0];\n", - "Console.WriteLine($\"data\\t low\\t high\\t s_ema fixed\\t\");\n", - "for (int i=0; i \n", - " \r\n", - "\r\n", - "\n", - " \n", - " \n" - ] - }, - "metadata": {}, - "output_type": "display_data" - } - ], - "source": [ - "var d = Chart2D.Chart.Line (x, data, true, \"Data\").WithLineStyle(Width: 4, Color: Color.fromString(\"blue\"));\n", - "var le = Chart2D.Chart.Line (x, es, true, \"s_ema\").WithLineStyle(Width: 2, Color: Color.fromString(\"green\"));\n", - "var le1 = Chart2D.Chart.Line (x, e1, true, \"low\").WithLineStyle(Width: 2, Color: Color.fromString(\"red\"));\n", - "var le2 = Chart2D.Chart.Line (x, e2, true, \"high\").WithLineStyle(Width: 2, Color: Color.fromString(\"red\"));\n", - "var le3 = Chart2D.Chart.Line (x, e3, true, \"fixed\").WithLineStyle(Width: 3, Color: Color.fromString(\"purple\"));\n", - "var chart = Chart.Combine(new []{d,le,le1,le2,le3})\n", - " .WithSize(1200,600)\n", - " .WithMargin(Margin.init (30,10,40,30,1,false))\n", - " .WithXAxisRangeSlider(RangeSlider.init(Visible:false));\n", - "\n", - "chart.SavePNG(\"ema_study\");\n", - "chart" - ] - }, - { - "attachments": {}, - "cell_type": "markdown", - "metadata": { - "dotnet_interactive": { - "language": "csharp" - }, - "vscode": { - "languageId": "dotnet-interactive.csharp" - } - }, - "source": [ - "" - ] - } - ], - "metadata": { - "kernelspec": { - "display_name": ".NET (C#)", - "language": "C#", - "name": ".net-csharp" - }, - "language_info": { - "file_extension": ".cs", - "mimetype": "text/x-csharp", - "name": "C#", - "pygments_lexer": "csharp", - "version": "9.0" - }, - "orig_nbformat": 4, - "vscode": { - "interpreter": { - "hash": "aee8b7b246df8f9039afb4144a1f6fd8d2ca17a180786b69acc140d282b71a49" - } - } - }, - "nbformat": 4, - "nbformat_minor": 2 -} diff --git a/docs/ema_study.png b/docs/ema_study.png deleted file mode 100644 index d5feee0b..00000000 Binary files a/docs/ema_study.png and /dev/null differ diff --git a/docs/getting_started.ipynb b/docs/getting_started.ipynb index ede92014..bf8c469c 100644 --- a/docs/getting_started.ipynb +++ b/docs/getting_started.ipynb @@ -2,7 +2,14 @@ "cells": [ { "cell_type": "markdown", - "metadata": {}, + "metadata": { + "dotnet_interactive": { + "language": "csharp" + }, + "polyglot_notebook": { + "kernelName": "csharp" + } + }, "source": [ "# Quick Start\n", "\n", @@ -17,35 +24,16 @@ }, { "cell_type": "code", - "execution_count": 1, + "execution_count": null, "metadata": { "dotnet_interactive": { "language": "csharp" }, "vscode": { - "languageId": "dotnet-interactive.csharp" + "languageId": "polyglot-notebook" } }, - "outputs": [ - { - "data": { - "text/html": [ - " " - ] - }, - "metadata": {}, - "output_type": "display_data" - }, - { - "ename": "Error", - "evalue": "(3,1): error CS0246: The type or namespace name 'Yahoo_Feed' could not be found (are you missing a using directive or an assembly reference?)\r\n(10,15): error CS0019: Operator '<' cannot be applied to operands of type 'int' and 'method group'", - "output_type": "error", - "traceback": [ - "(3,1): error CS0246: The type or namespace name 'Yahoo_Feed' could not be found (are you missing a using directive or an assembly reference?)\r\n", - "(10,15): error CS0019: Operator '<' cannot be applied to operands of type 'int' and 'method group'" - ] - } - ], + "outputs": [], "source": [ "#r \"nuget:QuanTAlib;\"\n", "using QuanTAlib;\n", @@ -63,7 +51,14 @@ }, { "cell_type": "markdown", - "metadata": {}, + "metadata": { + "dotnet_interactive": { + "language": "csharp" + }, + "polyglot_notebook": { + "kernelName": "csharp" + } + }, "source": [ "## Understanding QuanTAlib data model\n", "\n", @@ -72,26 +67,16 @@ }, { "cell_type": "code", - "execution_count": 10, + "execution_count": null, "metadata": { "dotnet_interactive": { "language": "csharp" }, "vscode": { - "languageId": "dotnet-interactive.csharp" + "languageId": "polyglot-notebook" } }, - "outputs": [ - { - "data": { - "text/html": [ - "" - ] - }, - "metadata": {}, - "output_type": "display_data" - } - ], + "outputs": [], "source": [ "var item1 = (DateTime.Today, 105.3); // (DateTime, Value) tuple\n", "double item2 = 293.1; // a simple double\n", @@ -107,66 +92,60 @@ }, { "cell_type": "markdown", - "metadata": {}, + "metadata": { + "dotnet_interactive": { + "language": "csharp" + }, + "polyglot_notebook": { + "kernelName": "csharp" + } + }, "source": [ "TSeries list can display only values (without timestamps) or only timestamps (without values) by using `.v` or `.t` properties" ] }, { "cell_type": "code", - "execution_count": 11, + "execution_count": null, "metadata": { "dotnet_interactive": { "language": "csharp" }, "vscode": { - "languageId": "dotnet-interactive.csharp" + "languageId": "polyglot-notebook" } }, - "outputs": [ - { - "data": { - "text/html": [ - "
index Item1 Item2 0 2022-11-10 00:00:00Z 105.31 2022-11-10 15:47:46Z 293.12 2022-11-10 15:47:46Z 03 2022-11-07 15:47:46Z 10" - ] - }, - "metadata": {}, - "output_type": "display_data" - } - ], + "outputs": [], "source": [ "data.v" ] }, { "cell_type": "markdown", - "metadata": {}, + "metadata": { + "dotnet_interactive": { + "language": "csharp" + }, + "polyglot_notebook": { + "kernelName": "csharp" + } + }, "source": [ "The last element on the list can be accessed by .Last() or by [^1] - and using `.t` (time) and `.v` (value) properties. Also, casting a TSeries into (double) will return the value of the last element" ] }, { "cell_type": "code", - "execution_count": 12, + "execution_count": null, "metadata": { "dotnet_interactive": { "language": "csharp" }, "vscode": { - "languageId": "dotnet-interactive.csharp" + "languageId": "polyglot-notebook" } }, - "outputs": [ - { - "data": { - "text/html": [ - "
index value 0 105.31 293.12 03 1010" - ] - }, - "metadata": {}, - "output_type": "display_data" - } - ], + "outputs": [], "source": [ "bool IsTheSame = data.Last().v == data[^1].v;\n", "double lastvalue = data;\n", @@ -176,33 +155,30 @@ }, { "cell_type": "markdown", - "metadata": {}, + "metadata": { + "dotnet_interactive": { + "language": "csharp" + }, + "polyglot_notebook": { + "kernelName": "csharp" + } + }, "source": [ "All indicators are just modified TSeries classes; they get all required input during class construction (source of the datafeed, period...) and they automatically subscribe to events of the datafeed. Whenever datafeed gets a new value, indicator will calculate its own value. Indicators are also event publishers, so other indicators can subscribe to their results, chaining indicators together:" ] }, { "cell_type": "code", - "execution_count": 13, + "execution_count": null, "metadata": { "dotnet_interactive": { "language": "csharp" }, "vscode": { - "languageId": "dotnet-interactive.csharp" + "languageId": "polyglot-notebook" } }, - "outputs": [ - { - "data": { - "text/html": [ - "" - ] - }, - "metadata": {}, - "output_type": "display_data" - } - ], + "outputs": [], "source": [ "TSeries t1 = new() {0,1,2,3,4,5,6,7,8,9}; // t1 is loaded with data and activated as a publisher\n", "EMA_Series t2 = new(t1, 3); // t2 will auto-load all history of t1 and wait for events from t1\n", @@ -218,7 +194,14 @@ }, { "cell_type": "markdown", - "metadata": {}, + "metadata": { + "dotnet_interactive": { + "language": "csharp" + }, + "polyglot_notebook": { + "kernelName": "csharp" + } + }, "source": [ "# MACD compounded indicator\n", "\n", @@ -227,26 +210,16 @@ }, { "cell_type": "code", - "execution_count": 15, + "execution_count": null, "metadata": { "dotnet_interactive": { "language": "csharp" }, "vscode": { - "languageId": "dotnet-interactive.csharp" + "languageId": "polyglot-notebook" } }, - "outputs": [ - { - "data": { - "text/html": [ - "
index value 0 Infinity1 0.66666666666666662 0.33333333333333333 0.24 0.142857142857142855 0.11111111111111116 0.090909090909090917 0.076923076923076938 0.066666666666666679 0.05882352941176470510 0.25" - ] - }, - "metadata": {}, - "output_type": "display_data" - } - ], + "outputs": [], "source": [ "Yahoo_Feed aapl = new(\"AAPL\", 100);\n", "TSeries close = aapl.Close; // close will get data from history\n", @@ -266,14 +239,33 @@ "language": "C#", "name": ".net-csharp" }, - "language_info": { - "file_extension": ".cs", - "mimetype": "text/x-csharp", - "name": "C#", - "pygments_lexer": "csharp", - "version": "9.0" - }, - "orig_nbformat": 4 + "polyglot_notebook": { + "kernelInfo": { + "defaultKernelName": "csharp", + "items": [ + { + "aliases": [ + "c#", + "C#" + ], + "languageName": "C#", + "name": "csharp" + }, + { + "aliases": [ + "frontend" + ], + "languageName": null, + "name": "vscode" + }, + { + "aliases": [], + "languageName": "KQL", + "name": "kql" + } + ] + } + } }, "nbformat": 4, "nbformat_minor": 2 diff --git a/docs/index.html b/docs/index.html index 10426a54..1cd0381d 100644 --- a/docs/index.html +++ b/docs/index.html @@ -6,20 +6,26 @@ - + + - - - + + + +
index value 0 01 02 03 04 05 06 07 08 09 010 011 012 0.1354358974359001813 -0.0389795435334099314 -0.1773100843141110215 -0.2403067115230409516 -0.0824705567361498817 -0.4789844849024081418 -0.902071504185661519 -1.3489730137363423(51 more)