mirror of
https://github.com/mihakralj/QuanTAlib.git
synced 2026-08-26 06:18: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,169 @@
|
||||
using TradingPlatform.BusinessLayer;
|
||||
|
||||
namespace QuanTAlib.Tests;
|
||||
|
||||
public class LsmaIndicatorTests
|
||||
{
|
||||
[Fact]
|
||||
public void LsmaIndicator_Constructor_SetsDefaults()
|
||||
{
|
||||
var indicator = new LsmaIndicator();
|
||||
|
||||
Assert.Equal(25, indicator.Period);
|
||||
Assert.Equal(0, indicator.Offset);
|
||||
Assert.Equal(SourceType.Close, indicator.Source);
|
||||
Assert.True(indicator.ShowColdValues);
|
||||
Assert.Equal("LSMA - Least Squares Moving Average", indicator.Name);
|
||||
Assert.False(indicator.SeparateWindow);
|
||||
Assert.True(indicator.OnBackGround);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void LsmaIndicator_MinHistoryDepths_EqualsPeriod()
|
||||
{
|
||||
var indicator = new LsmaIndicator { Period = 20 };
|
||||
|
||||
Assert.Equal(0, LsmaIndicator.MinHistoryDepths);
|
||||
Assert.Equal(0, ((IWatchlistIndicator)indicator).MinHistoryDepths);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void LsmaIndicator_ShortName_IncludesPeriodOffsetAndSource()
|
||||
{
|
||||
var indicator = new LsmaIndicator { Period = 15, Offset = 2 };
|
||||
|
||||
Assert.Contains("LSMA", indicator.ShortName, StringComparison.Ordinal);
|
||||
Assert.Contains("15", indicator.ShortName, StringComparison.Ordinal);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void LsmaIndicator_SourceCodeLink_IsValid()
|
||||
{
|
||||
var indicator = new LsmaIndicator();
|
||||
|
||||
Assert.Contains("github.com", indicator.SourceCodeLink, StringComparison.Ordinal);
|
||||
Assert.Contains("Lsma.Quantower.cs", indicator.SourceCodeLink, StringComparison.Ordinal);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void LsmaIndicator_Initialize_CreatesInternalLsma()
|
||||
{
|
||||
var indicator = new LsmaIndicator { Period = 10 };
|
||||
|
||||
// Initialize should not throw
|
||||
indicator.Initialize();
|
||||
|
||||
// After init, line series should exist
|
||||
Assert.Single(indicator.LinesSeries);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void LsmaIndicator_ProcessUpdate_HistoricalBar_ComputesValue()
|
||||
{
|
||||
var indicator = new LsmaIndicator { Period = 3 };
|
||||
indicator.Initialize();
|
||||
|
||||
// Add historical data
|
||||
var now = DateTime.UtcNow;
|
||||
indicator.HistoricalData.AddBar(now, 100, 105, 95, 102);
|
||||
|
||||
// Process update
|
||||
var args = new UpdateArgs(UpdateReason.HistoricalBar);
|
||||
indicator.ProcessUpdate(args);
|
||||
|
||||
// Line series should have a value
|
||||
Assert.Equal(1, indicator.LinesSeries[0].Count);
|
||||
Assert.True(double.IsFinite(indicator.LinesSeries[0].GetValue(0)));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void LsmaIndicator_ProcessUpdate_NewBar_ComputesValue()
|
||||
{
|
||||
var indicator = new LsmaIndicator { Period = 3 };
|
||||
indicator.Initialize();
|
||||
|
||||
var now = DateTime.UtcNow;
|
||||
indicator.HistoricalData.AddBar(now, 100, 105, 95, 102);
|
||||
indicator.HistoricalData.AddBar(now.AddMinutes(1), 102, 108, 100, 106);
|
||||
|
||||
indicator.ProcessUpdate(new UpdateArgs(UpdateReason.HistoricalBar));
|
||||
indicator.ProcessUpdate(new UpdateArgs(UpdateReason.NewBar));
|
||||
|
||||
Assert.Equal(2, indicator.LinesSeries[0].Count);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void LsmaIndicator_ProcessUpdate_NewTick_ProcessesWithoutError()
|
||||
{
|
||||
var indicator = new LsmaIndicator { Period = 3 };
|
||||
indicator.Initialize();
|
||||
|
||||
var now = DateTime.UtcNow;
|
||||
indicator.HistoricalData.AddBar(now, 100, 105, 95, 102);
|
||||
|
||||
indicator.ProcessUpdate(new UpdateArgs(UpdateReason.HistoricalBar));
|
||||
double firstValue = indicator.LinesSeries[0].GetValue(0);
|
||||
|
||||
indicator.ProcessUpdate(new UpdateArgs(UpdateReason.NewTick));
|
||||
double secondValue = indicator.LinesSeries[0].GetValue(0);
|
||||
|
||||
Assert.True(double.IsFinite(firstValue));
|
||||
Assert.True(double.IsFinite(secondValue));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void LsmaIndicator_MultipleUpdates_ProducesCorrectSequence()
|
||||
{
|
||||
var indicator = new LsmaIndicator { Period = 3 };
|
||||
indicator.Initialize();
|
||||
|
||||
var now = DateTime.UtcNow;
|
||||
double[] closes = { 100, 102, 104, 103, 105 };
|
||||
|
||||
foreach (var close in closes)
|
||||
{
|
||||
indicator.HistoricalData.AddBar(now, close, close + 2, close - 2, close);
|
||||
indicator.ProcessUpdate(new UpdateArgs(UpdateReason.HistoricalBar));
|
||||
now = now.AddMinutes(1);
|
||||
}
|
||||
|
||||
// All values should be finite
|
||||
for (int i = 0; i < closes.Length; i++)
|
||||
{
|
||||
Assert.True(double.IsFinite(indicator.LinesSeries[0].GetValue(closes.Length - 1 - i)));
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void LsmaIndicator_DifferentSourceTypes_Work()
|
||||
{
|
||||
var sources = new[] { SourceType.Open, SourceType.High, SourceType.Low, SourceType.Close, SourceType.HL2, SourceType.HLC3 };
|
||||
|
||||
foreach (var source in sources)
|
||||
{
|
||||
var indicator = new LsmaIndicator { Period = 3, Source = source };
|
||||
indicator.Initialize();
|
||||
|
||||
var now = DateTime.UtcNow;
|
||||
indicator.HistoricalData.AddBar(now, 100, 110, 90, 105);
|
||||
indicator.ProcessUpdate(new UpdateArgs(UpdateReason.HistoricalBar));
|
||||
|
||||
Assert.True(double.IsFinite(indicator.LinesSeries[0].GetValue(0)),
|
||||
$"Source {source} should produce finite value");
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void LsmaIndicator_PeriodAndOffset_CanBeChanged()
|
||||
{
|
||||
var indicator = new LsmaIndicator { Period = 5, Offset = 0 };
|
||||
Assert.Equal(5, indicator.Period);
|
||||
Assert.Equal(0, indicator.Offset);
|
||||
|
||||
indicator.Period = 20;
|
||||
indicator.Offset = 2;
|
||||
Assert.Equal(20, indicator.Period);
|
||||
Assert.Equal(2, indicator.Offset);
|
||||
Assert.Equal(0, LsmaIndicator.MinHistoryDepths);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,303 @@
|
||||
|
||||
namespace QuanTAlib.Tests;
|
||||
|
||||
public class LsmaTests
|
||||
{
|
||||
[Fact]
|
||||
public void Constructor_InvalidPeriod_ThrowsArgumentException()
|
||||
{
|
||||
Assert.Throws<ArgumentException>(() => new Lsma(0));
|
||||
Assert.Throws<ArgumentException>(() => new Lsma(-1));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Constructor_ValidParameters_SetsProperties()
|
||||
{
|
||||
var lsma = new Lsma(14, 0);
|
||||
Assert.Equal("Lsma(14)", lsma.Name);
|
||||
Assert.False(lsma.IsHot);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Update_SingleValue_ReturnsSameValue()
|
||||
{
|
||||
var lsma = new Lsma(14);
|
||||
var result = lsma.Update(new TValue(DateTime.UtcNow, 100));
|
||||
Assert.Equal(100, result.Value);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Update_LinearTrend_ReturnsExactValue()
|
||||
{
|
||||
// For a perfect linear trend y = x, LSMA should return x
|
||||
const int period = 10;
|
||||
var lsma = new Lsma(period);
|
||||
|
||||
for (int i = 0; i < period * 2; i++)
|
||||
{
|
||||
var result = lsma.Update(new TValue(DateTime.UtcNow, i));
|
||||
if (i >= period) // After warmup
|
||||
{
|
||||
Assert.Equal(i, result.Value, 1e-9);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Update_ConstantValue_ReturnsSameValue()
|
||||
{
|
||||
const int period = 10;
|
||||
var lsma = new Lsma(period);
|
||||
const double value = 123.45;
|
||||
|
||||
for (int i = 0; i < period * 2; i++)
|
||||
{
|
||||
var result = lsma.Update(new TValue(DateTime.UtcNow, value));
|
||||
Assert.Equal(value, result.Value, 1e-9);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Update_WithOffset_ProjectsCorrectly()
|
||||
{
|
||||
// y = 2x + 1
|
||||
// At x=10, y=21. Slope=2, Intercept=1
|
||||
// LSMA(offset=1) should project to x=11 -> y=23
|
||||
|
||||
const int period = 5;
|
||||
const int offset = 1;
|
||||
var lsma = new Lsma(period, offset);
|
||||
|
||||
for (int i = 0; i < 20; i++)
|
||||
{
|
||||
double y = 2 * i + 1;
|
||||
var result = lsma.Update(new TValue(DateTime.UtcNow, y));
|
||||
|
||||
if (i >= period)
|
||||
{
|
||||
double expected = 2 * (i + offset) + 1;
|
||||
Assert.Equal(expected, result.Value, 1e-9);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Update_BarCorrection_UpdatesCorrectly()
|
||||
{
|
||||
var lsma = new Lsma(5);
|
||||
|
||||
// Fill buffer
|
||||
for (int i = 0; i < 5; i++)
|
||||
{
|
||||
lsma.Update(new TValue(DateTime.UtcNow, i));
|
||||
}
|
||||
|
||||
// New bar
|
||||
var result1 = lsma.Update(new TValue(DateTime.UtcNow, 10));
|
||||
|
||||
// Update same bar with different value
|
||||
var result2 = lsma.Update(new TValue(DateTime.UtcNow, 20), isNew: false);
|
||||
|
||||
Assert.NotEqual(result1.Value, result2.Value);
|
||||
|
||||
// Verify internal state by adding next bar
|
||||
// If state was corrupted, this would fail
|
||||
var result3 = lsma.Update(new TValue(DateTime.UtcNow, 30));
|
||||
Assert.True(double.IsFinite(result3.Value));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Update_NaN_HandlesGracefully()
|
||||
{
|
||||
var lsma = new Lsma(5);
|
||||
|
||||
lsma.Update(new TValue(DateTime.UtcNow, 1));
|
||||
lsma.Update(new TValue(DateTime.UtcNow, 2));
|
||||
var result = lsma.Update(new TValue(DateTime.UtcNow, double.NaN));
|
||||
|
||||
// Input sequence becomes: 1, 2, 2 (NaN replaced by last valid 2)
|
||||
// Regression on (2,1), (1,2), (0,2)
|
||||
// Result should be 2.166666667
|
||||
Assert.Equal(2.1666666666666665, result.Value, 1e-9);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Calculate_StaticMethod_MatchesObjectInstance()
|
||||
{
|
||||
const int period = 10;
|
||||
const int count = 100;
|
||||
var source = new TSeries();
|
||||
var gbm = new GBM(startPrice: 100, seed: 42);
|
||||
|
||||
for (int i = 0; i < count; i++)
|
||||
{
|
||||
var bar = gbm.Next();
|
||||
source.Add(bar.C);
|
||||
}
|
||||
|
||||
var lsma = new Lsma(period);
|
||||
var series1 = lsma.Update(source);
|
||||
var series2 = Lsma.Batch(source, period);
|
||||
|
||||
Assert.Equal(series1.Count, series2.Count);
|
||||
for (int i = 0; i < count; i++)
|
||||
{
|
||||
Assert.Equal(series1[i].Value, series2[i].Value, 1e-9);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Calculate_Span_MatchesSeries()
|
||||
{
|
||||
const int period = 10;
|
||||
const int count = 100;
|
||||
var values = new double[count];
|
||||
var output = new double[count];
|
||||
var gbm = new GBM(startPrice: 100, seed: 42);
|
||||
|
||||
for (int i = 0; i < count; i++)
|
||||
{
|
||||
var bar = gbm.Next();
|
||||
values[i] = bar.Close;
|
||||
}
|
||||
|
||||
Lsma.Batch(values, output, period);
|
||||
|
||||
var lsma = new Lsma(period);
|
||||
for (int i = 0; i < count; i++)
|
||||
{
|
||||
var result = lsma.Update(new TValue(DateTime.UtcNow, values[i]));
|
||||
Assert.Equal(result.Value, output[i], 1e-9);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Reset_ClearsState()
|
||||
{
|
||||
var lsma = new Lsma(5);
|
||||
for (int i = 0; i < 10; i++)
|
||||
{
|
||||
lsma.Update(new TValue(DateTime.UtcNow, i));
|
||||
}
|
||||
|
||||
Assert.True(lsma.IsHot);
|
||||
|
||||
lsma.Reset();
|
||||
|
||||
Assert.False(lsma.IsHot);
|
||||
Assert.Equal(0, lsma.Last.Value);
|
||||
|
||||
// Should behave like new instance
|
||||
var result = lsma.Update(new TValue(DateTime.UtcNow, 100));
|
||||
Assert.Equal(100, result.Value);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void IsHot_BecomesTrueWhenBufferFull()
|
||||
{
|
||||
const int period = 5;
|
||||
var lsma = new Lsma(period);
|
||||
|
||||
for (int i = 0; i < period; i++)
|
||||
{
|
||||
Assert.False(lsma.IsHot);
|
||||
lsma.Update(new TValue(DateTime.UtcNow, i));
|
||||
}
|
||||
|
||||
Assert.True(lsma.IsHot);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Chainability_Works()
|
||||
{
|
||||
var source = new TSeries();
|
||||
var lsma = new Lsma(source, 10);
|
||||
|
||||
source.Add(new TValue(DateTime.UtcNow, 100));
|
||||
Assert.Equal(100, lsma.Last.Value);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Dispose_UnsubscribesFromSource()
|
||||
{
|
||||
var source = new TSeries();
|
||||
var lsma = new Lsma(source, 5);
|
||||
|
||||
// Verify subscription works
|
||||
source.Add(new TValue(DateTime.UtcNow, 100));
|
||||
Assert.Equal(100, lsma.Last.Value);
|
||||
|
||||
// Dispose and verify unsubscription
|
||||
lsma.Dispose();
|
||||
|
||||
// Add more data - lsma should NOT update
|
||||
source.Add(new TValue(DateTime.UtcNow, 200));
|
||||
Assert.Equal(100, lsma.Last.Value); // Should remain at previous value
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Dispose_IsIdempotent()
|
||||
{
|
||||
var source = new TSeries();
|
||||
var lsma = new Lsma(source, 5);
|
||||
|
||||
source.Add(new TValue(DateTime.UtcNow, 100));
|
||||
|
||||
// Multiple Dispose calls should not throw
|
||||
// Suppressing S3966: Multiple Dispose calls are intentional to test idempotency
|
||||
#pragma warning disable S3966
|
||||
lsma.Dispose();
|
||||
lsma.Dispose();
|
||||
lsma.Dispose();
|
||||
#pragma warning restore S3966
|
||||
|
||||
// Verify still unsubscribed
|
||||
source.Add(new TValue(DateTime.UtcNow, 200));
|
||||
Assert.Equal(100, lsma.Last.Value);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async System.Threading.Tasks.Task Dispose_IsThreadSafe()
|
||||
{
|
||||
var source = new TSeries();
|
||||
var lsma = new Lsma(source, 5);
|
||||
|
||||
source.Add(new TValue(DateTime.UtcNow, 100));
|
||||
|
||||
// Dispose from multiple threads simultaneously
|
||||
var tasks = new System.Threading.Tasks.Task[10];
|
||||
for (int i = 0; i < tasks.Length; i++)
|
||||
{
|
||||
tasks[i] = System.Threading.Tasks.Task.Run(() => lsma.Dispose());
|
||||
}
|
||||
|
||||
await System.Threading.Tasks.Task.WhenAll(tasks);
|
||||
|
||||
// Verify unsubscribed
|
||||
source.Add(new TValue(DateTime.UtcNow, 200));
|
||||
Assert.Equal(100, lsma.Last.Value);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Dispose_WithoutSource_DoesNotThrow()
|
||||
{
|
||||
// Lsma created without source parameter
|
||||
var lsma = new Lsma(5);
|
||||
|
||||
// Should not throw even though there's no source to unsubscribe from
|
||||
// Suppressing S3966: Multiple Dispose calls are intentional to test idempotency
|
||||
#pragma warning disable S3966
|
||||
lsma.Dispose();
|
||||
lsma.Dispose(); // Idempotent
|
||||
#pragma warning restore S3966
|
||||
|
||||
// Verify state remains valid
|
||||
Assert.False(lsma.IsHot);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Constructor_NullSource_ThrowsArgumentNullException()
|
||||
{
|
||||
Assert.Throws<ArgumentNullException>(() => new Lsma(null!, 5));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,100 @@
|
||||
using Skender.Stock.Indicators;
|
||||
using Xunit.Abstractions;
|
||||
|
||||
using OoplesFinance.StockIndicators;
|
||||
using OoplesFinance.StockIndicators.Models;
|
||||
|
||||
namespace QuanTAlib.Tests;
|
||||
|
||||
public class LsmaValidationTests
|
||||
{
|
||||
private readonly ValidationTestData _testData;
|
||||
private readonly ITestOutputHelper _output;
|
||||
|
||||
public LsmaValidationTests(ITestOutputHelper output)
|
||||
{
|
||||
_output = output;
|
||||
_testData = new ValidationTestData();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Validate_Skender_Batch()
|
||||
{
|
||||
int[] periods = { 5, 10, 20, 50, 100 };
|
||||
|
||||
foreach (var period in periods)
|
||||
{
|
||||
// Calculate QuanTAlib LSMA (batch TSeries)
|
||||
var lsma = new global::QuanTAlib.Lsma(period);
|
||||
var qResult = lsma.Update(_testData.Data);
|
||||
|
||||
// Calculate Skender EPMA (Endpoint Moving Average = LSMA)
|
||||
var sResult = _testData.SkenderQuotes.GetEpma(period).ToList();
|
||||
|
||||
// Compare last 100 records
|
||||
ValidationHelper.VerifyData(qResult, sResult, x => x.Epma, tolerance: ValidationHelper.OoplesTolerance);
|
||||
}
|
||||
_output.WriteLine("LSMA Batch(TSeries) validated successfully against Skender");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Validate_Skender_Streaming()
|
||||
{
|
||||
int[] periods = { 5, 10, 20, 50, 100 };
|
||||
|
||||
foreach (var period in periods)
|
||||
{
|
||||
// Calculate QuanTAlib LSMA (streaming)
|
||||
var lsma = new global::QuanTAlib.Lsma(period);
|
||||
var qResults = new List<double>();
|
||||
foreach (var item in _testData.Data)
|
||||
{
|
||||
qResults.Add(lsma.Update(item).Value);
|
||||
}
|
||||
|
||||
// Calculate Skender EPMA
|
||||
var sResult = _testData.SkenderQuotes.GetEpma(period).ToList();
|
||||
|
||||
// Compare last 100 records
|
||||
ValidationHelper.VerifyData(qResults, sResult, x => x.Epma, tolerance: ValidationHelper.OoplesTolerance);
|
||||
}
|
||||
_output.WriteLine("LSMA Streaming validated successfully against Skender");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Validate_Skender_Span()
|
||||
{
|
||||
int[] periods = { 5, 10, 20, 50, 100 };
|
||||
|
||||
foreach (var period in periods)
|
||||
{
|
||||
// Calculate QuanTAlib LSMA (Span API)
|
||||
double[] qOutput = new double[_testData.RawData.Length];
|
||||
global::QuanTAlib.Lsma.Batch(_testData.RawData.Span, qOutput.AsSpan(), period);
|
||||
|
||||
// Calculate Skender EPMA
|
||||
var sResult = _testData.SkenderQuotes.GetEpma(period).ToList();
|
||||
|
||||
// Compare last 100 records
|
||||
ValidationHelper.VerifyData(qOutput, sResult, x => x.Epma, tolerance: ValidationHelper.OoplesTolerance);
|
||||
}
|
||||
_output.WriteLine("LSMA Span validated successfully against Skender");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Lsma_MatchesOoples_Structural()
|
||||
{
|
||||
var gbm = new GBM(startPrice: 100.0, mu: 0.02, sigma: 0.15, seed: 42);
|
||||
var bars = gbm.Fetch(500, DateTime.UtcNow.Ticks, TimeSpan.FromMinutes(1));
|
||||
var ooplesData = bars.Select(b => new TickerData
|
||||
{
|
||||
Date = new DateTime(b.Time, DateTimeKind.Utc),
|
||||
Open = b.Open, High = b.High, Low = b.Low,
|
||||
Close = b.Close, Volume = b.Volume
|
||||
}).ToList();
|
||||
var result = new StockData(ooplesData).CalculateAdaptiveLeastSquares();
|
||||
var values = result.CustomValuesList;
|
||||
int finiteCount = values.Count(v => double.IsFinite(v));
|
||||
Assert.True(finiteCount > 100, $"Expected >100 finite values, got {finiteCount}");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user