using System; using System.Collections.Generic; using System.Linq; using Skender.Stock.Indicators; using TALib; using Tulip; using Xunit; using Xunit.Abstractions; using QuanTAlib; namespace QuanTAlib.Tests; public class SmaValidationTests { private readonly TBarSeries _bars; private readonly TSeries _data; private readonly List _skenderQuotes; private readonly ITestOutputHelper _output; public SmaValidationTests(ITestOutputHelper output) { _output = output; // 1. Generate 1000 records using GBM feed var gbm = new GBM(startPrice: 100.0, mu: 0.05, sigma: 0.2); _bars = gbm.Fetch(1000, DateTime.UtcNow.Ticks, TimeSpan.FromMinutes(1)); // 2. Extract Close TSeries _data = _bars.Close; // 3. Prepare data for Skender (List) _skenderQuotes = new List(); for (int i = 0; i < _bars.Count; i++) { _skenderQuotes.Add(new Quote { Date = new DateTime(_bars.Open.Times[i], DateTimeKind.Utc), Open = (decimal)_bars.Open[i].Value, High = (decimal)_bars.High[i].Value, Low = (decimal)_bars.Low[i].Value, Close = (decimal)_bars.Close[i].Value, Volume = (decimal)_bars.Volume[i].Value }); } } [Fact] public void Validate_Skender() { int[] periods = { 5, 10, 20, 50, 100 }; foreach (var period in periods) { // Calculate QuanTAlib SMA var sma = new global::QuanTAlib.Sma(period); var qResult = sma.Update(_data); // Calculate Skender SMA var sResult = _skenderQuotes.GetSma(period).ToList(); // Compare last 100 records VerifyData(qResult, sResult); } _output.WriteLine("SMA validated successfully against Skender"); } [Fact] public void Validate_Talib() { int[] periods = { 5, 10, 20, 50, 100 }; // Prepare data for TA-Lib (double[]) double[] tData = _data.Select(x => x.Value).ToArray(); double[] output = new double[tData.Length]; foreach (var period in periods) { // Calculate QuanTAlib SMA var sma = new global::QuanTAlib.Sma(period); var qResult = sma.Update(_data); // Calculate TA-Lib SMA var retCode = TALib.Functions.Sma(tData, 0..^0, output, out var outRange, period); // Check success Assert.Equal(Core.RetCode.Success, retCode); // TA-Lib skips the lookback period, so output[0] corresponds to input[lookback] int lookback = TALib.Functions.SmaLookback(period); // Compare last 100 records VerifyData_Talib(qResult, output, outRange, lookback); } _output.WriteLine("SMA validated successfully against TA-Lib"); } [Fact] public void Validate_Tulip() { int[] periods = { 5, 10, 20, 50, 100 }; // Prepare data for Tulip (double[]) double[] tData = _data.Select(x => x.Value).ToArray(); foreach (var period in periods) { // Calculate QuanTAlib SMA var sma = new global::QuanTAlib.Sma(period); var qResult = sma.Update(_data); // Calculate Tulip SMA - Tulip returns fewer elements (skips lookback) var smaIndicator = Tulip.Indicators.sma; double[][] inputs = { tData }; double[] options = { (double)period }; int lookback = period - 1; double[][] outputs = { new double[tData.Length - lookback] }; smaIndicator.Run(inputs, options, outputs); var tResult = outputs[0]; // Compare last 100 records (accounting for lookback offset) VerifyData_Tulip(qResult, tResult, lookback); } _output.WriteLine("SMA validated successfully against Tulip"); } private static void VerifyData_Tulip(TSeries qSeries, double[] tOutput, int lookback) { int count = qSeries.Count; int skip = count - 100; // Last 100 records for (int i = skip; i < count; i++) { double qValue = qSeries[i].Value; // Tulip skips lookback, so output[0] = input[lookback] if (i < lookback) continue; int tIndex = i - lookback; if (tIndex >= tOutput.Length) continue; double tValue = tOutput[tIndex]; // Assert equality with tolerance Assert.Equal(tValue, qValue, 1e-6); } } private static void VerifyData(TSeries qSeries, List sSeries) { // Ensure we have enough data Assert.Equal(qSeries.Count, sSeries.Count); int count = qSeries.Count; int skip = count - 100; // Last 100 records for (int i = skip; i < count; i++) { double qValue = qSeries[i].Value; double? sValue = sSeries[i].Sma; // Skip if Skender returns null (warmup period) if (!sValue.HasValue) continue; // Assert equality with tolerance Assert.Equal(sValue.Value, qValue, 1e-6); } } private static void VerifyData_Talib(TSeries qSeries, double[] tOutput, Range outRange, int lookback) { int count = qSeries.Count; int skip = count - 100; // Last 100 records // outRange.End.Value is the number of elements written to tOutput int validCount = outRange.End.Value - outRange.Start.Value; for (int i = skip; i < count; i++) { double qValue = qSeries[i].Value; // Calculate index in tOutput // If i < lookback, we don't have a value from TA-Lib if (i < lookback) continue; int tIndex = i - lookback; // Check if tIndex is within valid range if (tIndex >= validCount) continue; double tValue = tOutput[tIndex]; // Assert equality with tolerance Assert.Equal(tValue, qValue, 1e-6); } } }