mirror of
https://github.com/mihakralj/QuanTAlib.git
synced 2026-08-21 12:08: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,112 @@
|
||||
using TradingPlatform.BusinessLayer;
|
||||
using QuanTAlib;
|
||||
|
||||
namespace QuanTAlib.Tests;
|
||||
|
||||
public sealed class BbbIndicatorTests
|
||||
{
|
||||
[Fact]
|
||||
public void BbbIndicator_Constructor_SetsDefaults()
|
||||
{
|
||||
var indicator = new BbbIndicator();
|
||||
|
||||
Assert.Equal(20, indicator.Period);
|
||||
Assert.Equal(2.0, indicator.Multiplier);
|
||||
Assert.True(indicator.ShowColdValues);
|
||||
Assert.Equal("BBB - Bollinger %B", indicator.Name);
|
||||
Assert.True(indicator.SeparateWindow);
|
||||
Assert.True(indicator.OnBackGround);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void BbbIndicator_MinHistoryDepths_EqualsZero()
|
||||
{
|
||||
var indicator = new BbbIndicator { Period = 20 };
|
||||
|
||||
Assert.Equal(0, BbbIndicator.MinHistoryDepths);
|
||||
IWatchlistIndicator watchlistIndicator = indicator;
|
||||
Assert.Equal(0, watchlistIndicator.MinHistoryDepths);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void BbbIndicator_ShortName_IncludesParameters()
|
||||
{
|
||||
var indicator = new BbbIndicator { Period = 10, Multiplier = 2.5 };
|
||||
indicator.Initialize();
|
||||
|
||||
Assert.Contains("BBB", indicator.ShortName, StringComparison.Ordinal);
|
||||
Assert.Contains("10", indicator.ShortName, StringComparison.Ordinal);
|
||||
Assert.Contains("2.5", indicator.ShortName, StringComparison.Ordinal);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void BbbIndicator_SourceCodeLink_IsValid()
|
||||
{
|
||||
var indicator = new BbbIndicator();
|
||||
|
||||
Assert.Contains("github.com", indicator.SourceCodeLink, StringComparison.Ordinal);
|
||||
Assert.Contains("Bbb.Quantower.cs", indicator.SourceCodeLink, StringComparison.Ordinal);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void BbbIndicator_Initialize_CreatesInternalBbb()
|
||||
{
|
||||
var indicator = new BbbIndicator { Period = 20, Multiplier = 2.0 };
|
||||
|
||||
indicator.Initialize();
|
||||
|
||||
Assert.Single(indicator.LinesSeries);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void BbbIndicator_ProcessUpdate_HistoricalBar_ComputesValue()
|
||||
{
|
||||
var indicator = new BbbIndicator { Period = 5, Multiplier = 2.0 };
|
||||
indicator.Initialize();
|
||||
|
||||
var now = DateTime.UtcNow;
|
||||
for (int i = 0; i < 20; i++)
|
||||
{
|
||||
indicator.HistoricalData.AddBar(now.AddMinutes(i), 100 + i, 110 + i, 90 + i, 105 + i);
|
||||
|
||||
var args = new UpdateArgs(UpdateReason.HistoricalBar);
|
||||
indicator.ProcessUpdate(args);
|
||||
}
|
||||
|
||||
double value = indicator.LinesSeries[0].GetValue(0);
|
||||
Assert.True(double.IsFinite(value));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void BbbIndicator_ProcessUpdate_NewBar_ComputesValue()
|
||||
{
|
||||
var indicator = new BbbIndicator { Period = 5, Multiplier = 2.0 };
|
||||
indicator.Initialize();
|
||||
|
||||
var now = DateTime.UtcNow;
|
||||
for (int i = 0; i < 20; i++)
|
||||
{
|
||||
indicator.HistoricalData.AddBar(now.AddMinutes(i), 100 + i, 110 + i, 90 + i, 105 + i);
|
||||
}
|
||||
|
||||
indicator.ProcessUpdate(new UpdateArgs(UpdateReason.HistoricalBar));
|
||||
|
||||
indicator.HistoricalData.AddBar(now.AddMinutes(20), 120, 130, 110, 125);
|
||||
indicator.ProcessUpdate(new UpdateArgs(UpdateReason.NewBar));
|
||||
|
||||
Assert.Equal(2, indicator.LinesSeries[0].Count);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void BbbIndicator_Parameters_CanBeChanged()
|
||||
{
|
||||
var indicator = new BbbIndicator { Period = 20, Multiplier = 2.0 };
|
||||
|
||||
indicator.Period = 10;
|
||||
indicator.Multiplier = 1.5;
|
||||
|
||||
Assert.Equal(10, indicator.Period);
|
||||
Assert.Equal(1.5, indicator.Multiplier);
|
||||
Assert.Equal(0, BbbIndicator.MinHistoryDepths);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,211 @@
|
||||
using Xunit;
|
||||
|
||||
namespace QuanTAlib.Tests;
|
||||
|
||||
public sealed class BbbTests
|
||||
{
|
||||
[Fact]
|
||||
public void Constructor_ValidParameters()
|
||||
{
|
||||
var bbb = new Bbb(period: 20, multiplier: 2.0);
|
||||
|
||||
Assert.NotNull(bbb);
|
||||
Assert.Equal("Bbb(20,2.0)", bbb.Name);
|
||||
Assert.Equal(20, bbb.WarmupPeriod);
|
||||
Assert.False(bbb.IsHot);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Constructor_InvalidPeriod_Throws()
|
||||
{
|
||||
var ex = Assert.Throws<ArgumentException>(() => new Bbb(period: 0, multiplier: 2.0));
|
||||
Assert.Equal("period", ex.ParamName);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Constructor_InvalidMultiplier_Throws()
|
||||
{
|
||||
var ex = Assert.Throws<ArgumentException>(() => new Bbb(period: 20, multiplier: 0.0));
|
||||
Assert.Equal("multiplier", ex.ParamName);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ZeroWidth_ReturnsNeutral()
|
||||
{
|
||||
var bbb = new Bbb(period: 3, multiplier: 2.0);
|
||||
DateTime time = DateTime.UtcNow;
|
||||
|
||||
bbb.Update(new TValue(time, 10.0), isNew: true);
|
||||
bbb.Update(new TValue(time.AddSeconds(1), 10.0), isNew: true);
|
||||
var result = bbb.Update(new TValue(time.AddSeconds(2), 10.0), isNew: true);
|
||||
|
||||
Assert.Equal(0.5, result.Value, 10);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void PercentB_AtMiddle_IsHalf()
|
||||
{
|
||||
var bbb = new Bbb(period: 3, multiplier: 2.0);
|
||||
DateTime time = DateTime.UtcNow;
|
||||
|
||||
// Window [0, 3, 1.5] has mean 1.5 and non-zero stddev.
|
||||
bbb.Update(new TValue(time, 0.0), isNew: true);
|
||||
bbb.Update(new TValue(time.AddSeconds(1), 3.0), isNew: true);
|
||||
var result = bbb.Update(new TValue(time.AddSeconds(2), 1.5), isNew: true);
|
||||
|
||||
Assert.Equal(0.5, result.Value, 10);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void IsNew_False_RollsBackCorrectly()
|
||||
{
|
||||
var bbb = new Bbb(period: 3, multiplier: 2.0);
|
||||
DateTime time = DateTime.UtcNow;
|
||||
|
||||
bbb.Update(new TValue(time, 10.0), isNew: true);
|
||||
bbb.Update(new TValue(time.AddSeconds(1), 12.0), isNew: true);
|
||||
bbb.Update(new TValue(time.AddSeconds(2), 14.0), isNew: true);
|
||||
double before = bbb.Last.Value;
|
||||
|
||||
bbb.Update(new TValue(time.AddSeconds(2), 15.0), isNew: false);
|
||||
double after = bbb.Last.Value;
|
||||
|
||||
Assert.NotEqual(before, after);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void NaN_HandledGracefully()
|
||||
{
|
||||
var bbb = new Bbb(period: 3, multiplier: 2.0);
|
||||
DateTime time = DateTime.UtcNow;
|
||||
|
||||
bbb.Update(new TValue(time, 10.0), isNew: true);
|
||||
bbb.Update(new TValue(time.AddSeconds(1), 12.0), isNew: true);
|
||||
bbb.Update(new TValue(time.AddSeconds(2), double.NaN), isNew: true);
|
||||
|
||||
Assert.True(double.IsFinite(bbb.Last.Value));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Infinity_HandledGracefully()
|
||||
{
|
||||
var bbb = new Bbb(period: 3, multiplier: 2.0);
|
||||
DateTime time = DateTime.UtcNow;
|
||||
|
||||
bbb.Update(new TValue(time, 10.0), isNew: true);
|
||||
bbb.Update(new TValue(time.AddSeconds(1), 12.0), isNew: true);
|
||||
bbb.Update(new TValue(time.AddSeconds(2), double.PositiveInfinity), isNew: true);
|
||||
|
||||
Assert.True(double.IsFinite(bbb.Last.Value));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void WarmupPeriod_IsHotTransition()
|
||||
{
|
||||
var bbb = new Bbb(period: 5, multiplier: 2.0);
|
||||
DateTime time = DateTime.UtcNow;
|
||||
|
||||
for (int i = 0; i < 4; i++)
|
||||
{
|
||||
bbb.Update(new TValue(time.AddSeconds(i), 10.0 + i));
|
||||
Assert.False(bbb.IsHot);
|
||||
}
|
||||
|
||||
bbb.Update(new TValue(time.AddSeconds(4), 14.0));
|
||||
Assert.True(bbb.IsHot);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void UpdateTSeries_ReturnsValidSeries()
|
||||
{
|
||||
int period = 5;
|
||||
var bbb = new Bbb(period, multiplier: 2.0);
|
||||
var gbm = new GBM(startPrice: 100.0, mu: 0.02, sigma: 0.1, seed: 42);
|
||||
var bars = gbm.Fetch(50, DateTime.UtcNow.Ticks, TimeSpan.FromMinutes(1));
|
||||
TSeries source = bars.Close;
|
||||
|
||||
TSeries result = bbb.Update(source);
|
||||
|
||||
Assert.Equal(source.Count, result.Count);
|
||||
Assert.True(bbb.IsHot);
|
||||
|
||||
var streaming = new Bbb(period, multiplier: 2.0);
|
||||
for (int i = Math.Max(0, source.Count - period); i < source.Count; i++)
|
||||
{
|
||||
streaming.Update(source[i], isNew: true);
|
||||
}
|
||||
Assert.Equal(streaming.Last.Value, result[^1].Value, 8);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Batch_MatchesStreaming()
|
||||
{
|
||||
var gbm = new GBM(startPrice: 100.0, mu: 0.02, sigma: 0.1, seed: 7);
|
||||
var bars = gbm.Fetch(200, DateTime.UtcNow.Ticks, TimeSpan.FromMinutes(1));
|
||||
TSeries source = bars.Close;
|
||||
|
||||
var streaming = new Bbb(period: 20, multiplier: 2.0);
|
||||
foreach (var item in source)
|
||||
{
|
||||
streaming.Update(item);
|
||||
}
|
||||
|
||||
TSeries batch = Bbb.Batch(source, period: 20, multiplier: 2.0);
|
||||
|
||||
Assert.Equal(batch[^1].Value, streaming.Last.Value, 8);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SpanBatch_EmptyArrays_DoesNotThrow()
|
||||
{
|
||||
double[] source = [];
|
||||
double[] output = [];
|
||||
|
||||
var ex = Record.Exception(() => Bbb.Batch(source.AsSpan(), output.AsSpan(), 20, 2.0));
|
||||
Assert.Null(ex);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SpanBatch_InvalidLength_Throws()
|
||||
{
|
||||
double[] source = new double[10];
|
||||
double[] output = new double[9];
|
||||
|
||||
var ex = Assert.Throws<ArgumentException>(() => Bbb.Batch(source.AsSpan(), output.AsSpan(), 20, 2.0));
|
||||
Assert.Equal("output", ex.ParamName);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SpanBatch_InvalidPeriod_Throws()
|
||||
{
|
||||
double[] source = new double[10];
|
||||
double[] output = new double[10];
|
||||
|
||||
var ex = Assert.Throws<ArgumentException>(() => Bbb.Batch(source.AsSpan(), output.AsSpan(), 0, 2.0));
|
||||
Assert.Equal("period", ex.ParamName);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SpanBatch_InvalidMultiplier_Throws()
|
||||
{
|
||||
double[] source = new double[10];
|
||||
double[] output = new double[10];
|
||||
|
||||
var ex = Assert.Throws<ArgumentException>(() => Bbb.Batch(source.AsSpan(), output.AsSpan(), 20, 0.0));
|
||||
Assert.Equal("multiplier", ex.ParamName);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Calculate_ReturnsResultsAndHotIndicator()
|
||||
{
|
||||
var gbm = new GBM(startPrice: 100, mu: 0.02, sigma: 0.1, seed: 42);
|
||||
var bars = gbm.Fetch(50, DateTime.UtcNow.Ticks, TimeSpan.FromMinutes(1));
|
||||
TSeries source = bars.Close;
|
||||
|
||||
var (results, indicator) = Bbb.Calculate(source, period: 5, multiplier: 2.0);
|
||||
|
||||
Assert.Equal(50, results.Count);
|
||||
Assert.True(indicator.IsHot);
|
||||
Assert.True(double.IsFinite(indicator.Last.Value));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,108 @@
|
||||
using OoplesFinance.StockIndicators;
|
||||
using OoplesFinance.StockIndicators.Models;
|
||||
using Skender.Stock.Indicators;
|
||||
using Xunit.Abstractions;
|
||||
|
||||
namespace QuanTAlib.Tests;
|
||||
|
||||
public sealed class BbbValidationTests : IDisposable
|
||||
{
|
||||
private readonly ValidationTestData _testData;
|
||||
private readonly ITestOutputHelper _output;
|
||||
private bool _disposed;
|
||||
|
||||
public BbbValidationTests(ITestOutputHelper output)
|
||||
{
|
||||
_output = output;
|
||||
_testData = new ValidationTestData();
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
Dispose(true);
|
||||
}
|
||||
|
||||
private void Dispose(bool disposing)
|
||||
{
|
||||
if (_disposed)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
_disposed = true;
|
||||
|
||||
if (disposing)
|
||||
{
|
||||
_testData?.Dispose();
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Validate_Streaming_Batch_Span_Agree()
|
||||
{
|
||||
int period = 20;
|
||||
double multiplier = 2.0;
|
||||
|
||||
// Streaming
|
||||
var streaming = new Bbb(period, multiplier);
|
||||
var streamValues = new List<double>(_testData.Data.Count);
|
||||
foreach (var item in _testData.Data)
|
||||
{
|
||||
streamValues.Add(streaming.Update(item).Value);
|
||||
}
|
||||
|
||||
// Batch (TSeries)
|
||||
TSeries batchSeries = Bbb.Batch(_testData.Data, period, multiplier);
|
||||
|
||||
// Span
|
||||
double[] src = _testData.RawData.ToArray();
|
||||
double[] spanOutput = new double[src.Length];
|
||||
Bbb.Batch(src.AsSpan(), spanOutput.AsSpan(), period, multiplier);
|
||||
|
||||
// Compare last 200 samples for stability
|
||||
int start = Math.Max(0, src.Length - 200);
|
||||
for (int i = start; i < src.Length; i++)
|
||||
{
|
||||
Assert.Equal(batchSeries[i].Value, streamValues[i], 9);
|
||||
Assert.Equal(batchSeries[i].Value, spanOutput[i], 9);
|
||||
}
|
||||
|
||||
_output.WriteLine("BBB validation: streaming, batch, and span outputs agree.");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Validate_Skender_PercentB()
|
||||
{
|
||||
int[] periods = { 5, 10, 20, 50, 100 };
|
||||
double multiplier = 2.0;
|
||||
|
||||
foreach (var period in periods)
|
||||
{
|
||||
// QuanTAlib
|
||||
var bbb = new Bbb(period, multiplier);
|
||||
var qResult = bbb.Update(_testData.Data);
|
||||
|
||||
// Skender Bollinger Bands PercentB
|
||||
var sResult = _testData.SkenderQuotes.GetBollingerBands(period, multiplier).ToList();
|
||||
|
||||
ValidationHelper.VerifyData(qResult, sResult, s => s.PercentB);
|
||||
}
|
||||
|
||||
_output.WriteLine("BBB validated successfully against Skender PercentB.");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Bbb_MatchesOoples_Structural()
|
||||
{
|
||||
// CalculateBollingerBandsPercentB — structural test
|
||||
var ooplesData = _testData.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();
|
||||
|
||||
var result = new StockData(ooplesData).CalculateBollingerBandsPercentB();
|
||||
var values = result.CustomValuesList;
|
||||
|
||||
int finiteCount = values.Count(v => double.IsFinite(v));
|
||||
Assert.True(finiteCount > 100, $"Expected >100 finite Ooples BBB values, got {finiteCount}");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user