mirror of
https://github.com/mihakralj/QuanTAlib.git
synced 2026-08-24 13:38:05 +00:00
docs: remove C# Implementation Considerations sections, clean up temp scripts, reorganize test files
- Remove 'C# Implementation Considerations' sections from 34 indicator .md files - Delete 29 temp PowerShell scripts (_fix_mojibake.ps1, _hex_scan.ps1, etc.) - Move test files into tests/ subdirectories for consistent project structure - Add trader-focused bullet points to indicator documentation
This commit is contained in:
@@ -0,0 +1,40 @@
|
||||
using TradingPlatform.BusinessLayer;
|
||||
|
||||
namespace QuanTAlib.Quantower.Tests;
|
||||
|
||||
public class HtitIndicatorTests
|
||||
{
|
||||
[Fact]
|
||||
public void Indicator_Initializes_Correctly()
|
||||
{
|
||||
var indicator = new HtitIndicator();
|
||||
indicator.Initialize();
|
||||
Assert.Equal("HTIT - Ehlers Hilbert Transform Instantaneous Trend", indicator.Name);
|
||||
Assert.StartsWith("HTIT", indicator.ShortName, StringComparison.Ordinal);
|
||||
Assert.Contains("Close", indicator.ShortName, StringComparison.Ordinal);
|
||||
Assert.Equal(0, HtitIndicator.MinHistoryDepths);
|
||||
Assert.Single(indicator.LinesSeries);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Indicator_Updates_Correctly()
|
||||
{
|
||||
var indicator = new HtitIndicator();
|
||||
indicator.Initialize();
|
||||
|
||||
// Warmup
|
||||
for (int i = 0; i < 100; i++)
|
||||
{
|
||||
var time = DateTime.UtcNow.AddMinutes(i);
|
||||
indicator.HistoricalData.AddBar(time, 100 + i, 100 + i, 100 + i, 100 + i);
|
||||
|
||||
var args = new UpdateArgs(UpdateReason.NewBar);
|
||||
indicator.ProcessUpdate(args);
|
||||
}
|
||||
|
||||
// Check if value is set (should be non-zero after warmup)
|
||||
var result = indicator.LinesSeries[0].GetValue();
|
||||
Assert.NotEqual(0, result);
|
||||
Assert.False(double.IsNaN(result));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,195 @@
|
||||
|
||||
namespace QuanTAlib.Tests;
|
||||
|
||||
public class HtitTests
|
||||
{
|
||||
private readonly GBM _gbm;
|
||||
|
||||
public HtitTests()
|
||||
{
|
||||
_gbm = new GBM();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void IsHot_BecomesTrue_AfterWarmup()
|
||||
{
|
||||
var htit = new Htit();
|
||||
for (int i = 0; i < 12; i++)
|
||||
{
|
||||
Assert.False(htit.IsHot);
|
||||
htit.Update(new TValue(DateTime.UtcNow.Ticks, 100.0));
|
||||
}
|
||||
Assert.True(htit.IsHot);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Update_Matches_Calculate()
|
||||
{
|
||||
var htit = new Htit();
|
||||
var data = _gbm.Fetch(100, DateTime.UtcNow.Ticks, TimeSpan.FromMinutes(1)).Close;
|
||||
var series = data;
|
||||
|
||||
var resultSeries = htit.Update(series);
|
||||
|
||||
// Reset and calculate streaming
|
||||
htit.Reset();
|
||||
var streamingResults = new List<double>();
|
||||
foreach (var item in data)
|
||||
{
|
||||
streamingResults.Add(htit.Update(item).Value);
|
||||
}
|
||||
|
||||
for (int i = 0; i < resultSeries.Count; i++)
|
||||
{
|
||||
Assert.Equal(resultSeries.Values[i], streamingResults[i], 1e-9);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Calculate_Span_Matches_Update()
|
||||
{
|
||||
var htit = new Htit();
|
||||
var data = _gbm.Fetch(100, DateTime.UtcNow.Ticks, TimeSpan.FromMinutes(1)).Close;
|
||||
var series = data;
|
||||
|
||||
var resultSeries = htit.Update(series);
|
||||
|
||||
var spanInput = data.Values.ToArray();
|
||||
var spanOutput = new double[spanInput.Length];
|
||||
|
||||
Htit.Batch(spanInput, spanOutput);
|
||||
|
||||
for (int i = 0; i < resultSeries.Count; i++)
|
||||
{
|
||||
Assert.Equal(resultSeries.Values[i], spanOutput[i], 1e-9);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Handles_NaN()
|
||||
{
|
||||
var htit = new Htit();
|
||||
htit.Update(new TValue(DateTime.UtcNow.Ticks, 100.0));
|
||||
htit.Update(new TValue(DateTime.UtcNow.Ticks, double.NaN));
|
||||
|
||||
Assert.Equal(100.0, htit.Last.Value);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Htit_Calc_IsNew_AcceptsParameter()
|
||||
{
|
||||
var htit = new Htit();
|
||||
htit.Update(new TValue(DateTime.UtcNow, 100), isNew: true);
|
||||
Assert.Equal(100, htit.Last.Value);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Htit_Reset_ClearsState()
|
||||
{
|
||||
var htit = new Htit();
|
||||
htit.Update(new TValue(DateTime.UtcNow, 100));
|
||||
htit.Update(new TValue(DateTime.UtcNow, 110));
|
||||
|
||||
htit.Reset();
|
||||
|
||||
Assert.True(double.IsNaN(htit.Last.Value));
|
||||
Assert.False(htit.IsHot);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Htit_IterativeCorrections_RestoreToOriginalState()
|
||||
{
|
||||
var htit = new Htit();
|
||||
var gbm = new GBM(startPrice: 100.0, mu: 0.02, sigma: 0.1);
|
||||
|
||||
// Feed 20 new values (needs > 12 for warmup)
|
||||
TValue lastInput = default;
|
||||
for (int i = 0; i < 20; i++)
|
||||
{
|
||||
var bar = gbm.Next(isNew: true);
|
||||
lastInput = new TValue(bar.Time, bar.Close);
|
||||
htit.Update(lastInput, isNew: true);
|
||||
}
|
||||
|
||||
// Remember state after 20 values
|
||||
double valueAfterTwenty = htit.Last.Value;
|
||||
|
||||
// Generate 9 corrections with isNew=false (different values)
|
||||
for (int i = 0; i < 9; i++)
|
||||
{
|
||||
var bar = gbm.Next(isNew: false);
|
||||
htit.Update(new TValue(bar.Time, bar.Close), isNew: false);
|
||||
}
|
||||
|
||||
// Feed the remembered 20th input again with isNew=false
|
||||
TValue finalValue = htit.Update(lastInput, isNew: false);
|
||||
|
||||
// Should match the original state after 20 values
|
||||
Assert.Equal(valueAfterTwenty, finalValue.Value, 1e-9);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Htit_SpanCalc_ValidatesInput()
|
||||
{
|
||||
double[] source = [1, 2, 3, 4, 5];
|
||||
double[] wrongSizeOutput = new double[3];
|
||||
|
||||
Assert.Throws<ArgumentException>(() => Htit.Batch(source.AsSpan(), wrongSizeOutput.AsSpan()));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Htit_SpanCalc_HandlesNaN()
|
||||
{
|
||||
double[] source = [100, 110, double.NaN, 120, 130];
|
||||
double[] output = new double[5];
|
||||
|
||||
Htit.Batch(source.AsSpan(), output.AsSpan());
|
||||
|
||||
foreach (var val in output)
|
||||
{
|
||||
Assert.True(double.IsFinite(val));
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Htit_AllModes_ProduceSameResult()
|
||||
{
|
||||
// Arrange
|
||||
var gbm = new GBM(startPrice: 100, mu: 0.05, sigma: 0.2, seed: 123);
|
||||
var bars = gbm.Fetch(1000, DateTime.UtcNow.Ticks, TimeSpan.FromMinutes(1));
|
||||
var series = bars.Close;
|
||||
|
||||
// 1. Batch Mode
|
||||
var batchSeries = Htit.Batch(series);
|
||||
double expected = batchSeries.Last.Value;
|
||||
|
||||
// 2. Span Mode
|
||||
var tValues = series.Values.ToArray();
|
||||
var spanInput = new ReadOnlySpan<double>(tValues);
|
||||
var spanOutput = new double[tValues.Length];
|
||||
Htit.Batch(spanInput, spanOutput);
|
||||
double spanResult = spanOutput[^1];
|
||||
|
||||
// 3. Streaming Mode
|
||||
var streamingInd = new Htit();
|
||||
for (int i = 0; i < series.Count; i++)
|
||||
{
|
||||
streamingInd.Update(series[i]);
|
||||
}
|
||||
double streamingResult = streamingInd.Last.Value;
|
||||
|
||||
// 4. Eventing Mode
|
||||
var pubSource = new TSeries();
|
||||
var eventingInd = new Htit(pubSource);
|
||||
for (int i = 0; i < series.Count; i++)
|
||||
{
|
||||
pubSource.Add(series[i]);
|
||||
}
|
||||
double eventingResult = eventingInd.Last.Value;
|
||||
|
||||
// Assert
|
||||
Assert.Equal(expected, spanResult, precision: 9);
|
||||
Assert.Equal(expected, streamingResult, precision: 9);
|
||||
Assert.Equal(expected, eventingResult, precision: 9);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,166 @@
|
||||
using Skender.Stock.Indicators;
|
||||
using OoplesFinance.StockIndicators;
|
||||
using OoplesFinance.StockIndicators.Models;
|
||||
using TALib;
|
||||
|
||||
namespace QuanTAlib.Tests;
|
||||
|
||||
public sealed class HtitValidationTests : IDisposable
|
||||
{
|
||||
private readonly ValidationTestData _data;
|
||||
private bool _disposed;
|
||||
|
||||
public HtitValidationTests()
|
||||
{
|
||||
_data = new ValidationTestData(10000);
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
Dispose(true);
|
||||
}
|
||||
|
||||
private void Dispose(bool disposing)
|
||||
{
|
||||
if (_disposed)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
_disposed = true;
|
||||
|
||||
if (disposing)
|
||||
{
|
||||
_data?.Dispose();
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Validate_TaLib()
|
||||
{
|
||||
// Calculate TA-Lib HTIT
|
||||
var input = _data.RawData.Span;
|
||||
var output = new double[input.Length];
|
||||
var retCode = TALib.Functions.HtTrendline(input, 0..^0, output, out var outRange);
|
||||
|
||||
Assert.Equal(TALib.Core.RetCode.Success, retCode);
|
||||
|
||||
// Calculate QuanTAlib HTIT
|
||||
var htit = new Htit();
|
||||
var quantalibResults = htit.Update(_data.Data);
|
||||
|
||||
// Compare results
|
||||
// TA-Lib HT_TRENDLINE has a lookback of 63
|
||||
for (int i = quantalibResults.Count - 100; i < quantalibResults.Count; i++)
|
||||
{
|
||||
if (i >= outRange.Start.Value)
|
||||
{
|
||||
double talibValue = output[i - outRange.Start.Value];
|
||||
double quantalibValue = quantalibResults.Values[i];
|
||||
double diff = Math.Abs(talibValue - quantalibValue);
|
||||
double relError = talibValue == 0.0 ? diff : diff / Math.Abs(talibValue);
|
||||
Assert.True(relError < ValidationHelper.RelativeTolerance, $"Relative error {relError} too high at index {i}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Validate_Skender_Batch()
|
||||
{
|
||||
// Calculate Skender HTIT
|
||||
var skenderResults = _data.SkenderQuotes.GetHtTrendline().ToList();
|
||||
|
||||
// Calculate QuanTAlib HTIT
|
||||
var htit = new Htit();
|
||||
var series = _data.Data;
|
||||
var quantalibResults = htit.Update(series);
|
||||
|
||||
// Compare results
|
||||
// Skip warmup period (Skender needs 100 periods for convergence, but we can check after 50)
|
||||
for (int i = quantalibResults.Count - 100; i < quantalibResults.Count; i++)
|
||||
{
|
||||
double skenderValue = skenderResults[i].Trendline ?? double.NaN;
|
||||
double quantalibValue = quantalibResults.Values[i];
|
||||
|
||||
if (!double.IsNaN(skenderValue))
|
||||
{
|
||||
// Skender implementation differs slightly (~0.32%) from TA-Lib/QuanTAlib.
|
||||
// QuanTAlib matches TA-Lib (reference) with 1e-6 precision.
|
||||
// The divergence in Skender is likely due to implementation details or smoothing differences.
|
||||
double diff = Math.Abs(skenderValue - quantalibValue);
|
||||
double relError = diff / skenderValue;
|
||||
Assert.True(relError < ValidationHelper.RelativeTolerance, $"Relative error {relError} too high at index {i}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Validate_Skender_Streaming()
|
||||
{
|
||||
// Calculate Skender HTIT
|
||||
var skenderResults = _data.SkenderQuotes.GetHtTrendline().ToList();
|
||||
|
||||
// Calculate QuanTAlib HTIT Streaming
|
||||
var htit = new Htit();
|
||||
var streamingResults = new List<double>();
|
||||
|
||||
foreach (var item in _data.Data)
|
||||
{
|
||||
streamingResults.Add(htit.Update(item).Value);
|
||||
}
|
||||
|
||||
// Compare results
|
||||
for (int i = streamingResults.Count - 100; i < streamingResults.Count; i++)
|
||||
{
|
||||
double skenderValue = skenderResults[i].Trendline ?? double.NaN;
|
||||
double quantalibValue = streamingResults[i];
|
||||
|
||||
if (!double.IsNaN(skenderValue))
|
||||
{
|
||||
// Skender implementation differs slightly (~0.32%) from TA-Lib/QuanTAlib
|
||||
double diff = Math.Abs(skenderValue - quantalibValue);
|
||||
double relError = diff / skenderValue;
|
||||
Assert.True(relError < ValidationHelper.RelativeTolerance, $"Relative error {relError} too high at index {i}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Validate_Ooples()
|
||||
{
|
||||
// Prepare data for Ooples
|
||||
var ooplesData = _data.SkenderQuotes.Select(q => new TickerData
|
||||
{
|
||||
Date = q.Date,
|
||||
Open = (double)q.Open,
|
||||
High = (double)q.High,
|
||||
Low = (double)q.Low,
|
||||
Close = (double)q.Close,
|
||||
Volume = (double)q.Volume
|
||||
}).ToList();
|
||||
|
||||
// Calculate Ooples HTIT
|
||||
var stockData = new StockData(ooplesData);
|
||||
var oResult = stockData.CalculateEhlersInstantaneousTrendlineV1();
|
||||
var oValues = oResult.OutputValues["Eit"];
|
||||
|
||||
// Calculate QuanTAlib HTIT
|
||||
var htit = new Htit();
|
||||
var quantalibResults = htit.Update(_data.Data);
|
||||
|
||||
// Compare results
|
||||
// Ooples might have different warmup or calculation details
|
||||
// We'll check for correlation or close values after warmup
|
||||
for (int i = quantalibResults.Count - 100; i < quantalibResults.Count; i++)
|
||||
{
|
||||
double ooplesValue = oValues[i];
|
||||
double quantalibValue = quantalibResults.Values[i];
|
||||
|
||||
// Ooples V1 differs slightly (~0.25%) from TA-Lib/QuanTAlib.
|
||||
// QuanTAlib matches TA-Lib (reference) with 1e-6 precision.
|
||||
double diff = Math.Abs(ooplesValue - quantalibValue);
|
||||
double relError = diff / ooplesValue;
|
||||
Assert.True(relError < ValidationHelper.RelativeTolerance, $"Relative error {relError} too high at index {i}");
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user