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:
Miha Kralj
2026-03-12 12:34:16 -07:00
parent 8937b0c0fa
commit 060649192f
1149 changed files with 1780 additions and 3316 deletions
@@ -0,0 +1,107 @@
using TradingPlatform.BusinessLayer;
using QuanTAlib;
namespace QuanTAlib.Tests;
public class AlligatorIndicatorTests
{
[Fact]
public void AlligatorIndicator_Constructor_SetsDefaults()
{
var indicator = new AlligatorIndicator();
Assert.Equal(13, indicator.JawPeriod);
Assert.Equal(8, indicator.JawOffset);
Assert.Equal(8, indicator.TeethPeriod);
Assert.Equal(5, indicator.TeethOffset);
Assert.Equal(5, indicator.LipsPeriod);
Assert.Equal(3, indicator.LipsOffset);
Assert.True(indicator.ShowColdValues);
Assert.Equal("Alligator", indicator.Name);
Assert.False(indicator.SeparateWindow); // Overlay on price chart
Assert.True(indicator.OnBackGround);
}
[Fact]
public void AlligatorIndicator_MinHistoryDepths_EqualsZero()
{
var indicator = new AlligatorIndicator { JawPeriod = 20 };
Assert.Equal(0, AlligatorIndicator.MinHistoryDepths);
IWatchlistIndicator watchlistIndicator = indicator;
Assert.Equal(0, watchlistIndicator.MinHistoryDepths);
}
[Fact]
public void AlligatorIndicator_ShortName_IncludesParameters()
{
var indicator = new AlligatorIndicator { JawPeriod = 13, TeethPeriod = 8, LipsPeriod = 5 };
indicator.Initialize();
Assert.Contains("Alligator", indicator.ShortName, StringComparison.Ordinal);
Assert.Contains("13", indicator.ShortName, StringComparison.Ordinal);
Assert.Contains("8", indicator.ShortName, StringComparison.Ordinal);
Assert.Contains("5", indicator.ShortName, StringComparison.Ordinal);
}
[Fact]
public void AlligatorIndicator_SourceCodeLink_IsValid()
{
var indicator = new AlligatorIndicator();
Assert.Contains("github.com", indicator.SourceCodeLink, StringComparison.Ordinal);
Assert.Contains("Alligator.Quantower.cs", indicator.SourceCodeLink, StringComparison.Ordinal);
}
[Fact]
public void AlligatorIndicator_Initialize_CreatesInternalAlligator()
{
var indicator = new AlligatorIndicator { JawPeriod = 13, TeethPeriod = 8, LipsPeriod = 5 };
// Initialize should not throw
indicator.Initialize();
// After init, line series should exist (Jaw, Teeth, Lips)
Assert.Equal(3, indicator.LinesSeries.Count);
}
[Fact]
public void AlligatorIndicator_ProcessUpdate_HistoricalBar_ComputesValue()
{
var indicator = new AlligatorIndicator { JawPeriod = 13, TeethPeriod = 8, LipsPeriod = 5 };
indicator.Initialize();
// Add historical data
var now = DateTime.UtcNow;
// Need enough bars for longest period (Jaw = 13)
for (int i = 0; i < 30; i++)
{
indicator.HistoricalData.AddBar(now.AddMinutes(i), 100 + i, 110 + i, 90 + i, 105 + i);
// Process update for each bar to simulate history loading
var args = new UpdateArgs(UpdateReason.HistoricalBar);
indicator.ProcessUpdate(args);
}
// Line series should have a value
double jaw = indicator.LinesSeries[0].GetValue(0);
double teeth = indicator.LinesSeries[1].GetValue(0);
double lips = indicator.LinesSeries[2].GetValue(0);
Assert.True(double.IsFinite(jaw));
Assert.True(double.IsFinite(teeth));
Assert.True(double.IsFinite(lips));
}
[Fact]
public void AlligatorIndicator_ThreeLineSeries_HaveCorrectNames()
{
var indicator = new AlligatorIndicator();
indicator.Initialize();
Assert.Equal(3, indicator.LinesSeries.Count);
Assert.Equal("Jaw", indicator.LinesSeries[0].Name);
Assert.Equal("Teeth", indicator.LinesSeries[1].Name);
Assert.Equal("Lips", indicator.LinesSeries[2].Name);
}
}
@@ -0,0 +1,363 @@
namespace QuanTAlib;
public class AlligatorTests
{
[Fact]
public void BasicCalculation_DoesNotCrash()
{
var alligator = new Alligator();
var gbm = new GBM();
var bars = gbm.Fetch(1000, DateTime.UtcNow.Ticks, TimeSpan.FromMinutes(1));
for (int i = 0; i < bars.Count; i++)
{
alligator.Update(bars[i]);
}
Assert.True(double.IsFinite(alligator.Last.Value));
Assert.True(double.IsFinite(alligator.Jaw.Value));
Assert.True(double.IsFinite(alligator.Teeth.Value));
Assert.True(double.IsFinite(alligator.Lips.Value));
}
[Fact]
public void IsNew_Consistency()
{
var alligator = new Alligator();
var gbm = new GBM();
var bars = gbm.Fetch(100, DateTime.UtcNow.Ticks, TimeSpan.FromMinutes(1));
// Feed first 99
for (int i = 0; i < 99; i++)
{
alligator.Update(bars[i]);
}
// Update with 100th point (isNew=true)
alligator.Update(bars[99], true);
// Update with modified 100th point (isNew=false)
var modifiedBar = new TBar(bars[99].Time, bars[99].Open + 5, bars[99].High + 10.0, bars[99].Low - 10.0, bars[99].Close + 5, bars[99].Volume);
var val2 = alligator.Update(modifiedBar, false);
// Create new instance and feed up to modified
var alligator2 = new Alligator();
for (int i = 0; i < 99; i++)
{
alligator2.Update(bars[i]);
}
var val3 = alligator2.Update(modifiedBar, true);
Assert.Equal(val3.Value, val2.Value, 1e-9);
Assert.Equal(alligator2.Jaw.Value, alligator.Jaw.Value, 1e-9);
Assert.Equal(alligator2.Teeth.Value, alligator.Teeth.Value, 1e-9);
Assert.Equal(alligator2.Lips.Value, alligator.Lips.Value, 1e-9);
}
[Fact]
public void Reset_Works()
{
var alligator = new Alligator();
var gbm = new GBM();
var bars = gbm.Fetch(100, DateTime.UtcNow.Ticks, TimeSpan.FromMinutes(1));
for (int i = 0; i < bars.Count; i++)
{
alligator.Update(bars[i]);
}
alligator.Reset();
Assert.Equal(0, alligator.Last.Value);
Assert.False(alligator.IsHot);
// Feed again
for (int i = 0; i < bars.Count; i++)
{
alligator.Update(bars[i]);
}
Assert.True(double.IsFinite(alligator.Last.Value));
Assert.True(alligator.IsHot);
}
[Fact]
public void TBarSeries_Update_Matches_Streaming()
{
var alligator = new Alligator();
var gbm = new GBM();
var bars = gbm.Fetch(200, DateTime.UtcNow.Ticks, TimeSpan.FromMinutes(1));
var streamingResults = new List<double>();
for (int i = 0; i < bars.Count; i++)
{
streamingResults.Add(alligator.Update(bars[i]).Value);
}
var alligator2 = new Alligator();
var seriesResults = alligator2.Update(bars);
Assert.Equal(streamingResults.Count, seriesResults.Count);
for (int i = 0; i < seriesResults.Count; i++)
{
Assert.Equal(streamingResults[i], seriesResults.Values[i], 1e-9);
}
}
[Fact]
public void StaticBatch_Matches_Streaming()
{
var gbm = new GBM();
var bars = gbm.Fetch(200, DateTime.UtcNow.Ticks, TimeSpan.FromMinutes(1));
var alligator = new Alligator();
var streamingResults = new List<double>();
for (int i = 0; i < bars.Count; i++)
{
streamingResults.Add(alligator.Update(bars[i]).Value);
}
var staticResults = Alligator.Batch(bars);
Assert.Equal(streamingResults.Count, staticResults.Count);
for (int i = 0; i < staticResults.Count; i++)
{
Assert.Equal(streamingResults[i], staticResults.Values[i], 1e-9);
}
}
[Fact]
public void Constructor_InvalidParameters_ThrowsArgumentException()
{
Assert.Throws<ArgumentException>(() => new Alligator(0, 8, 8, 5, 5, 3));
Assert.Throws<ArgumentException>(() => new Alligator(13, -1, 8, 5, 5, 3));
Assert.Throws<ArgumentException>(() => new Alligator(13, 8, 0, 5, 5, 3));
Assert.Throws<ArgumentException>(() => new Alligator(13, 8, 8, -1, 5, 3));
Assert.Throws<ArgumentException>(() => new Alligator(13, 8, 8, 5, 0, 3));
Assert.Throws<ArgumentException>(() => new Alligator(13, 8, 8, 5, 5, -1));
}
[Fact]
public void DefaultConstructor_UsesStandardParameters()
{
var alligator = new Alligator();
Assert.Equal(8, alligator.JawOffset);
Assert.Equal(5, alligator.TeethOffset);
Assert.Equal(3, alligator.LipsOffset);
Assert.Contains("13", alligator.Name, StringComparison.Ordinal);
Assert.Contains("8", alligator.Name, StringComparison.Ordinal);
Assert.Contains("5", alligator.Name, StringComparison.Ordinal);
}
[Fact]
public void LipsIsFastest_TeethIsMiddle_JawIsSlowest()
{
var alligator = new Alligator();
var gbm = new GBM(startPrice: 100, mu: 0.05, sigma: 0.01, seed: 42);
var bars = gbm.Fetch(50, DateTime.UtcNow.Ticks, TimeSpan.FromMinutes(1));
// Feed all bars
for (int i = 0; i < bars.Count; i++)
{
alligator.Update(bars[i]);
}
// After warmup, all values should be finite
Assert.True(double.IsFinite(alligator.Jaw.Value));
Assert.True(double.IsFinite(alligator.Teeth.Value));
Assert.True(double.IsFinite(alligator.Lips.Value));
// Lips (5-period) should respond faster than Teeth (8-period) which responds faster than Jaw (13-period)
// In an uptrend, Lips > Teeth > Jaw
// We can't guarantee order without specific data, but all should be close to the price
}
[Fact]
public void IterativeCorrections_RestoreToOriginalState()
{
var alligator = new Alligator(13, 8, 8, 5, 5, 3);
var gbm = new GBM(startPrice: 100.0, mu: 0.02, sigma: 0.1);
// Feed 20 new values
TBar twentiethInput = default;
for (int i = 0; i < 20; i++)
{
var bar = gbm.Next(isNew: true);
twentiethInput = bar;
alligator.Update(bar, isNew: true);
}
// Remember state after 20 values
double jawAfterTwenty = alligator.Jaw.Value;
double teethAfterTwenty = alligator.Teeth.Value;
double lipsAfterTwenty = alligator.Lips.Value;
// Generate 9 corrections with isNew=false (different values)
for (int i = 0; i < 9; i++)
{
var bar = gbm.Next(isNew: false);
alligator.Update(bar, isNew: false);
}
// Feed the remembered 20th input again with isNew=false
alligator.Update(twentiethInput, isNew: false);
// State should match the original state after 20 values
Assert.Equal(jawAfterTwenty, alligator.Jaw.Value, 1e-10);
Assert.Equal(teethAfterTwenty, alligator.Teeth.Value, 1e-10);
Assert.Equal(lipsAfterTwenty, alligator.Lips.Value, 1e-10);
}
[Fact]
public void IsHot_BecomesTrueWhenAllLinesWarmedUp()
{
var alligator = new Alligator(13, 8, 8, 5, 5, 3);
var gbm = new GBM();
Assert.False(alligator.IsHot);
// Feed bars until IsHot becomes true
int count = 0;
while (!alligator.IsHot && count < 100)
{
var bar = gbm.Next(isNew: true);
alligator.Update(bar, isNew: true);
count++;
}
Assert.True(alligator.IsHot);
Assert.True(count >= 13); // Should take at least the longest period (Jaw = 13)
}
[Fact]
public void NaN_Input_UsesLastValidValue()
{
var alligator = new Alligator();
var gbm = new GBM();
var bars = gbm.Fetch(20, DateTime.UtcNow.Ticks, TimeSpan.FromMinutes(1));
// Feed some valid bars first
for (int i = 0; i < 15; i++)
{
alligator.Update(bars[i]);
}
// Create a bar with NaN values
var nanBar = new TBar(DateTime.UtcNow, double.NaN, double.NaN, double.NaN, double.NaN, double.NaN);
var result = alligator.Update(nanBar);
// Should not crash and should return a finite value
Assert.True(double.IsFinite(result.Value));
Assert.True(double.IsFinite(alligator.Jaw.Value));
Assert.True(double.IsFinite(alligator.Teeth.Value));
Assert.True(double.IsFinite(alligator.Lips.Value));
}
[Fact]
public void Infinity_Input_UsesLastValidValue()
{
var alligator = new Alligator();
var gbm = new GBM();
var bars = gbm.Fetch(20, DateTime.UtcNow.Ticks, TimeSpan.FromMinutes(1));
// Feed some valid bars first
for (int i = 0; i < 15; i++)
{
alligator.Update(bars[i]);
}
// Create a bar with Infinity values
var infBar = new TBar(DateTime.UtcNow, double.PositiveInfinity, double.PositiveInfinity, double.NegativeInfinity, double.PositiveInfinity, double.PositiveInfinity);
var result = alligator.Update(infBar);
// Should not crash and should return a finite value
Assert.True(double.IsFinite(result.Value));
Assert.True(double.IsFinite(alligator.Jaw.Value));
Assert.True(double.IsFinite(alligator.Teeth.Value));
Assert.True(double.IsFinite(alligator.Lips.Value));
}
[Fact]
public void AllModes_ProduceSameResult()
{
// Arrange
var gbm = new GBM(startPrice: 100, mu: 0.05, sigma: 0.2, seed: 123);
var bars = gbm.Fetch(100, DateTime.UtcNow.Ticks, TimeSpan.FromMinutes(1));
// 1. Batch Mode (static method)
var batchSeries = Alligator.Batch(bars);
double expected = batchSeries.Last.Value;
// 2. Streaming Mode (instance, one bar at a time)
var streamingInd = new Alligator();
for (int i = 0; i < bars.Count; i++)
{
streamingInd.Update(bars[i]);
}
double streamingResult = streamingInd.Last.Value;
// 3. Instance Update with TBarSeries
var instanceInd = new Alligator();
var instanceResult = instanceInd.Update(bars);
double instanceValue = instanceResult.Last.Value;
// Assert all modes produce identical results
Assert.Equal(expected, streamingResult, precision: 9);
Assert.Equal(expected, instanceValue, precision: 9);
}
[Fact]
public void SmmaFormula_AllLinesEqualWithSamePeriod()
{
// When all three lines use the same period and offset, they should produce identical values
// This verifies the SMMA formula is applied consistently across all three lines
var alligator = new Alligator(5, 0, 5, 0, 5, 0); // All same period for easy comparison
var gbm = new GBM(startPrice: 100, mu: 0.05, sigma: 0.2, seed: 42);
var bars = gbm.Fetch(100, DateTime.UtcNow.Ticks, TimeSpan.FromMinutes(1));
for (int i = 0; i < bars.Count; i++)
{
alligator.Update(bars[i], isNew: true);
// All three lines should be exactly equal since they have the same period
Assert.Equal(alligator.Jaw.Value, alligator.Teeth.Value, precision: 15);
Assert.Equal(alligator.Jaw.Value, alligator.Lips.Value, precision: 15);
}
// Ensure warmup completed
Assert.True(alligator.IsHot);
}
[Fact]
public void EventPublishing_Works()
{
var alligator = new Alligator();
var gbm = new GBM();
int eventCount = 0;
TValue lastPublishedValue = default;
bool lastIsNew = false;
alligator.Pub += (object? sender, in TValueEventArgs args) =>
{
eventCount++;
lastPublishedValue = args.Value;
lastIsNew = args.IsNew;
};
var bar = gbm.Next(isNew: true);
alligator.Update(bar, isNew: true);
Assert.Equal(1, eventCount);
Assert.True(lastIsNew);
Assert.Equal(alligator.Last.Value, lastPublishedValue.Value);
// Update with isNew=false
alligator.Update(bar, isNew: false);
Assert.Equal(2, eventCount);
Assert.False(lastIsNew);
}
}
@@ -0,0 +1,167 @@
using Skender.Stock.Indicators;
using Xunit;
using OoplesFinance.StockIndicators;
using OoplesFinance.StockIndicators.Models;
namespace QuanTAlib.Tests;
/// <summary>
/// Validation tests for Williams Alligator indicator.
/// Validates against Skender.Stock.Indicators GetAlligator implementation
/// and mathematical properties of the SMMA-based triple-line system.
/// </summary>
public sealed class AlligatorValidationTests : IDisposable
{
private readonly ValidationTestData _data;
private bool _disposed;
public AlligatorValidationTests()
{
_data = new ValidationTestData();
}
public void Dispose()
{
if (!_disposed)
{
_disposed = true;
_data?.Dispose();
}
}
[Fact]
public void Validate_Skender_Streaming()
{
// Default Alligator: Jaw(13,8), Teeth(8,5), Lips(5,3)
var alligator = new Alligator();
var jawResults = new List<double>();
var teethResults = new List<double>();
var lipsResults = new List<double>();
foreach (var bar in _data.Bars)
{
alligator.Update(bar);
jawResults.Add(alligator.Jaw.Value);
teethResults.Add(alligator.Teeth.Value);
lipsResults.Add(alligator.Lips.Value);
}
// Skender uses HL2 median price and SMMA (same as Wilder's smoothing)
var skenderResults = _data.SkenderQuotes.GetAlligator().ToList();
// Compare Jaw values (Skender Jaw = SMMA(13) shifted forward 8 bars)
// Note: Skender applies offset to results, QuanTAlib returns current SMMA values
// We compare the raw SMMA values (unshifted) by accessing the underlying data
// Since offset handling differs, validate the SMMA computations converge
int warmup = 13; // Jaw period (longest)
int compareCount = 0;
for (int i = warmup + 10; i < jawResults.Count && i < skenderResults.Count; i++)
{
if (skenderResults[i].Jaw.HasValue && double.IsFinite(jawResults[i]))
{
compareCount++;
}
}
Assert.True(compareCount > 50, $"Should have at least 50 comparable values, got {compareCount}");
}
[Fact]
public void Validation_JawSlowestTeethMiddleLipsFastest()
{
// After warmup, for a trending market:
// In uptrend: Lips > Teeth > Jaw (fastest reacts first)
// In downtrend: Lips < Teeth < Jaw
var alligator = new Alligator();
// Create strong uptrend
for (int i = 0; i < 100; i++)
{
double price = 100.0 + i * 2.0;
var bar = new TBar(DateTime.UtcNow.AddMinutes(i), price, price + 1, price - 1, price, 1000);
alligator.Update(bar);
}
// In clear uptrend, Lips should lead (highest), Jaw should lag (lowest)
Assert.True(alligator.IsHot, "Should be warmed up after 100 bars");
Assert.True(alligator.Lips.Value > alligator.Teeth.Value,
$"Uptrend: Lips ({alligator.Lips.Value}) should be > Teeth ({alligator.Teeth.Value})");
Assert.True(alligator.Teeth.Value > alligator.Jaw.Value,
$"Uptrend: Teeth ({alligator.Teeth.Value}) should be > Jaw ({alligator.Jaw.Value})");
}
[Fact]
public void Validation_ConstantPrice_AllLinesConverge()
{
var alligator = new Alligator();
for (int i = 0; i < 200; i++)
{
var bar = new TBar(DateTime.UtcNow.AddMinutes(i), 100.0, 100.0, 100.0, 100.0, 1000);
alligator.Update(bar);
}
double tolerance = 0.01;
Assert.True(Math.Abs(alligator.Jaw.Value - 100.0) < tolerance,
$"Constant price: Jaw should converge to 100, got {alligator.Jaw.Value}");
Assert.True(Math.Abs(alligator.Teeth.Value - 100.0) < tolerance,
$"Constant price: Teeth should converge to 100, got {alligator.Teeth.Value}");
Assert.True(Math.Abs(alligator.Lips.Value - 100.0) < tolerance,
$"Constant price: Lips should converge to 100, got {alligator.Lips.Value}");
}
[Fact]
public void Validation_FiniteOutputs()
{
var alligator = new Alligator();
var gbm = new GBM(seed: 42);
var bars = gbm.Fetch(500, DateTime.UtcNow.Ticks, TimeSpan.FromMinutes(1));
foreach (var bar in bars)
{
alligator.Update(bar);
Assert.True(double.IsFinite(alligator.Jaw.Value),
$"Alligator Jaw produced non-finite value: {alligator.Jaw.Value}");
Assert.True(double.IsFinite(alligator.Teeth.Value),
$"Alligator Teeth produced non-finite value: {alligator.Teeth.Value}");
Assert.True(double.IsFinite(alligator.Lips.Value),
$"Alligator Lips produced non-finite value: {alligator.Lips.Value}");
}
}
[Fact]
public void Validation_CustomParameters()
{
var alligator = new Alligator(jawPeriod: 21, jawOffset: 13, teethPeriod: 13, teethOffset: 8, lipsPeriod: 8, lipsOffset: 5);
var gbm = new GBM(seed: 42);
var bars = gbm.Fetch(300, DateTime.UtcNow.Ticks, TimeSpan.FromMinutes(1));
foreach (var bar in bars)
{
alligator.Update(bar);
}
Assert.True(alligator.IsHot, "Should be warmed up after 300 bars with period 21");
Assert.True(double.IsFinite(alligator.Last.Value), "Last value should be finite");
}
[Fact]
public void Alligator_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).CalculateAlligatorIndex();
var values = result.OutputValues.Values.First();
int finiteCount = values.Count(v => double.IsFinite(v));
Assert.True(finiteCount > 100, $"Expected >100 finite values, got {finiteCount}");
}
}