mirror of
https://github.com/mihakralj/QuanTAlib.git
synced 2026-08-24 21:48:03 +00:00
feat: implement SuperTrend indicator with tests and documentation
This commit is contained in:
@@ -0,0 +1,80 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Skender.Stock.Indicators;
|
||||
using Xunit;
|
||||
using QuanTAlib.Tests;
|
||||
|
||||
namespace QuanTAlib;
|
||||
|
||||
public class SuperValidationTests : IDisposable
|
||||
{
|
||||
private readonly ValidationTestData _data;
|
||||
|
||||
public SuperValidationTests()
|
||||
{
|
||||
_data = new ValidationTestData();
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
Dispose(true);
|
||||
GC.SuppressFinalize(this);
|
||||
}
|
||||
|
||||
protected virtual void Dispose(bool disposing)
|
||||
{
|
||||
if (disposing)
|
||||
{
|
||||
_data.Dispose();
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void MatchesSkender()
|
||||
{
|
||||
var super = new Super(10, 3.0);
|
||||
var results = new List<double>();
|
||||
var upper = new List<double>();
|
||||
var lower = new List<double>();
|
||||
|
||||
for (int i = 0; i < _data.Bars.Count; i++)
|
||||
{
|
||||
var res = super.Update(_data.Bars[i]);
|
||||
results.Add(res.Value);
|
||||
upper.Add(super.UpperBand.Value);
|
||||
lower.Add(super.LowerBand.Value);
|
||||
}
|
||||
|
||||
// Skender uses GetSuperTrend
|
||||
var skenderResults = _data.SkenderQuotes.GetSuperTrend(10, 3.0).ToList();
|
||||
|
||||
Assert.Equal(_data.Bars.Count, skenderResults.Count);
|
||||
|
||||
for (int i = 0; i < _data.Bars.Count; i++)
|
||||
{
|
||||
// Skender returns null for warmup
|
||||
if (skenderResults[i].SuperTrend == null)
|
||||
{
|
||||
Assert.True(double.IsNaN(results[i]));
|
||||
continue;
|
||||
}
|
||||
|
||||
Assert.Equal((double)skenderResults[i].SuperTrend!, results[i], 1e-7);
|
||||
|
||||
if (skenderResults[i].UpperBand != null)
|
||||
{
|
||||
Assert.Equal((double)skenderResults[i].UpperBand!, upper[i], 1e-7);
|
||||
}
|
||||
|
||||
if (skenderResults[i].LowerBand != null)
|
||||
{
|
||||
Assert.Equal((double)skenderResults[i].LowerBand!, lower[i], 1e-7);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Note: OoplesFinance implementation of SuperTrend diverges significantly from Skender and QuanTAlib.
|
||||
// This is likely due to different initialization logic for ATR or the SuperTrend state itself.
|
||||
// Therefore, we do not validate against Ooples for SuperTrend.
|
||||
}
|
||||
Reference in New Issue
Block a user