mirror of
https://github.com/mihakralj/QuanTAlib.git
synced 2026-08-25 05:48:06 +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,304 @@
|
||||
using TradingPlatform.BusinessLayer;
|
||||
using QuanTAlib;
|
||||
|
||||
namespace QuanTAlib.Tests;
|
||||
|
||||
public class HlvIndicatorTests
|
||||
{
|
||||
[Fact]
|
||||
public void HlvIndicator_Constructor_SetsDefaults()
|
||||
{
|
||||
var indicator = new HlvIndicator();
|
||||
|
||||
Assert.Equal(20, indicator.Period);
|
||||
Assert.True(indicator.Annualize);
|
||||
Assert.Equal(252, indicator.AnnualPeriods);
|
||||
Assert.True(indicator.ShowColdValues);
|
||||
Assert.Equal("HLV - High-Low Volatility (Parkinson)", indicator.Name);
|
||||
Assert.True(indicator.SeparateWindow);
|
||||
Assert.True(indicator.OnBackGround);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void HlvIndicator_ShortName_IncludesParameters()
|
||||
{
|
||||
var indicator = new HlvIndicator { Period = 14 };
|
||||
Assert.Contains("HLV", indicator.ShortName, StringComparison.Ordinal);
|
||||
Assert.Contains("14", indicator.ShortName, StringComparison.Ordinal);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void HlvIndicator_MinHistoryDepths_EqualsZero()
|
||||
{
|
||||
var indicator = new HlvIndicator();
|
||||
|
||||
Assert.Equal(0, HlvIndicator.MinHistoryDepths);
|
||||
Assert.Equal(0, ((IWatchlistIndicator)indicator).MinHistoryDepths);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void HlvIndicator_Initialize_CreatesInternalHlv()
|
||||
{
|
||||
var indicator = new HlvIndicator();
|
||||
|
||||
// Initialize should not throw
|
||||
indicator.Initialize();
|
||||
|
||||
// After init, line series should exist
|
||||
Assert.Single(indicator.LinesSeries);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void HlvIndicator_ProcessUpdate_HistoricalBar_ComputesValue()
|
||||
{
|
||||
var indicator = new HlvIndicator { Period = 10 };
|
||||
indicator.Initialize();
|
||||
|
||||
// Add historical data with varying volatility
|
||||
var now = DateTime.UtcNow;
|
||||
for (int i = 0; i < 30; i++)
|
||||
{
|
||||
double basePrice = 100 + i;
|
||||
double range = 2 + (i % 5); // Varying ranges
|
||||
indicator.HistoricalData.AddBar(now.AddMinutes(i), basePrice, basePrice + range, basePrice - range, basePrice + 1, 1000);
|
||||
|
||||
// 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 val = indicator.LinesSeries[0].GetValue(0);
|
||||
Assert.True(double.IsFinite(val));
|
||||
Assert.True(val >= 0, "Volatility should be non-negative");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void HlvIndicator_ProcessUpdate_NewBar_ComputesValue()
|
||||
{
|
||||
var indicator = new HlvIndicator { Period = 10 };
|
||||
indicator.Initialize();
|
||||
|
||||
var now = DateTime.UtcNow;
|
||||
for (int i = 0; i < 30; i++)
|
||||
{
|
||||
double basePrice = 100 + i;
|
||||
indicator.HistoricalData.AddBar(now.AddMinutes(i), basePrice, basePrice + 5, basePrice - 5, basePrice + 2, 1000);
|
||||
}
|
||||
|
||||
indicator.ProcessUpdate(new UpdateArgs(UpdateReason.HistoricalBar));
|
||||
|
||||
// Add new bar with larger range
|
||||
indicator.HistoricalData.AddBar(now.AddMinutes(30), 120, 135, 105, 125, 1500);
|
||||
indicator.ProcessUpdate(new UpdateArgs(UpdateReason.NewBar));
|
||||
|
||||
Assert.Equal(2, indicator.LinesSeries[0].Count);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void HlvIndicator_DifferentPeriods_Work()
|
||||
{
|
||||
int[] periods = { 5, 10, 14, 20 };
|
||||
|
||||
foreach (var period in periods)
|
||||
{
|
||||
var indicator = new HlvIndicator { Period = period };
|
||||
indicator.Initialize();
|
||||
|
||||
var now = DateTime.UtcNow;
|
||||
for (int i = 0; i < 50; i++)
|
||||
{
|
||||
double basePrice = 100 + i;
|
||||
double range = 3 + (i % 4);
|
||||
indicator.HistoricalData.AddBar(now.AddMinutes(i), basePrice, basePrice + range, basePrice - range, basePrice + 1, 1000);
|
||||
indicator.ProcessUpdate(new UpdateArgs(UpdateReason.HistoricalBar));
|
||||
}
|
||||
|
||||
double val = indicator.LinesSeries[0].GetValue(0);
|
||||
Assert.True(double.IsFinite(val), $"Period {period} should produce finite value");
|
||||
Assert.True(val >= 0, $"Period {period} should produce non-negative value");
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void HlvIndicator_Period_CanBeChanged()
|
||||
{
|
||||
var indicator = new HlvIndicator();
|
||||
Assert.Equal(20, indicator.Period);
|
||||
|
||||
indicator.Period = 14;
|
||||
Assert.Equal(14, indicator.Period);
|
||||
|
||||
indicator.Period = 10;
|
||||
Assert.Equal(10, indicator.Period);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void HlvIndicator_Annualize_CanBeToggled()
|
||||
{
|
||||
var indicator = new HlvIndicator();
|
||||
Assert.True(indicator.Annualize);
|
||||
|
||||
indicator.Annualize = false;
|
||||
Assert.False(indicator.Annualize);
|
||||
|
||||
indicator.Annualize = true;
|
||||
Assert.True(indicator.Annualize);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void HlvIndicator_AnnualPeriods_CanBeChanged()
|
||||
{
|
||||
var indicator = new HlvIndicator();
|
||||
Assert.Equal(252, indicator.AnnualPeriods);
|
||||
|
||||
indicator.AnnualPeriods = 365;
|
||||
Assert.Equal(365, indicator.AnnualPeriods);
|
||||
|
||||
indicator.AnnualPeriods = 52;
|
||||
Assert.Equal(52, indicator.AnnualPeriods);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void HlvIndicator_ShowColdValues_CanBeToggled()
|
||||
{
|
||||
var indicator = new HlvIndicator();
|
||||
Assert.True(indicator.ShowColdValues);
|
||||
|
||||
indicator.ShowColdValues = false;
|
||||
Assert.False(indicator.ShowColdValues);
|
||||
|
||||
indicator.ShowColdValues = true;
|
||||
Assert.True(indicator.ShowColdValues);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void HlvIndicator_SourceCodeLink_IsValid()
|
||||
{
|
||||
var indicator = new HlvIndicator();
|
||||
Assert.Contains("github.com", indicator.SourceCodeLink, StringComparison.Ordinal);
|
||||
Assert.Contains("Hlv.Quantower.cs", indicator.SourceCodeLink, StringComparison.Ordinal);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void HlvIndicator_HighVolatility_ProducesHigherValue()
|
||||
{
|
||||
var indicator1 = new HlvIndicator { Period = 10, Annualize = false };
|
||||
var indicator2 = new HlvIndicator { Period = 10, Annualize = false };
|
||||
indicator1.Initialize();
|
||||
indicator2.Initialize();
|
||||
|
||||
var now = DateTime.UtcNow;
|
||||
|
||||
// Indicator 1: low volatility (narrow range)
|
||||
for (int i = 0; i < 30; i++)
|
||||
{
|
||||
double basePrice = 100;
|
||||
indicator1.HistoricalData.AddBar(now.AddMinutes(i), basePrice, basePrice + 1, basePrice - 1, basePrice + 0.5, 1000);
|
||||
indicator1.ProcessUpdate(new UpdateArgs(UpdateReason.HistoricalBar));
|
||||
}
|
||||
|
||||
// Indicator 2: high volatility (wide range)
|
||||
for (int i = 0; i < 30; i++)
|
||||
{
|
||||
double basePrice = 100;
|
||||
indicator2.HistoricalData.AddBar(now.AddMinutes(i), basePrice, basePrice + 10, basePrice - 10, basePrice + 2, 1000);
|
||||
indicator2.ProcessUpdate(new UpdateArgs(UpdateReason.HistoricalBar));
|
||||
}
|
||||
|
||||
double lowVol = indicator1.LinesSeries[0].GetValue(0);
|
||||
double highVol = indicator2.LinesSeries[0].GetValue(0);
|
||||
|
||||
Assert.True(double.IsFinite(lowVol));
|
||||
Assert.True(double.IsFinite(highVol));
|
||||
Assert.True(highVol > lowVol, "Higher volatility bars should produce higher HLV value");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void HlvIndicator_AnnualizedValue_IsScaled()
|
||||
{
|
||||
var indicatorRaw = new HlvIndicator { Period = 10, Annualize = false };
|
||||
var indicatorAnn = new HlvIndicator { Period = 10, Annualize = true, AnnualPeriods = 252 };
|
||||
indicatorRaw.Initialize();
|
||||
indicatorAnn.Initialize();
|
||||
|
||||
var now = DateTime.UtcNow;
|
||||
|
||||
// Same data for both
|
||||
for (int i = 0; i < 30; i++)
|
||||
{
|
||||
double basePrice = 100 + i * 0.5;
|
||||
indicatorRaw.HistoricalData.AddBar(now.AddMinutes(i), basePrice, basePrice + 3, basePrice - 3, basePrice + 1, 1000);
|
||||
indicatorRaw.ProcessUpdate(new UpdateArgs(UpdateReason.HistoricalBar));
|
||||
|
||||
indicatorAnn.HistoricalData.AddBar(now.AddMinutes(i), basePrice, basePrice + 3, basePrice - 3, basePrice + 1, 1000);
|
||||
indicatorAnn.ProcessUpdate(new UpdateArgs(UpdateReason.HistoricalBar));
|
||||
}
|
||||
|
||||
double rawValue = indicatorRaw.LinesSeries[0].GetValue(0);
|
||||
double annValue = indicatorAnn.LinesSeries[0].GetValue(0);
|
||||
|
||||
Assert.True(double.IsFinite(rawValue));
|
||||
Assert.True(double.IsFinite(annValue));
|
||||
|
||||
// Annualized should be approximately sqrt(252) times larger
|
||||
double expectedRatio = Math.Sqrt(252);
|
||||
double actualRatio = annValue / rawValue;
|
||||
|
||||
Assert.True(Math.Abs(actualRatio - expectedRatio) < 0.01,
|
||||
$"Annualized value should be ~{expectedRatio:F2}× raw, got {actualRatio:F2}×");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void HlvIndicator_OnlyUsesHighLow_IgnoresOpenClose()
|
||||
{
|
||||
// Test that HLV only uses High-Low (not Open-Close)
|
||||
var indicator1 = new HlvIndicator { Period = 10, Annualize = false };
|
||||
var indicator2 = new HlvIndicator { Period = 10, Annualize = false };
|
||||
indicator1.Initialize();
|
||||
indicator2.Initialize();
|
||||
|
||||
var now = DateTime.UtcNow;
|
||||
|
||||
// Same high/low range but different open/close
|
||||
for (int i = 0; i < 30; i++)
|
||||
{
|
||||
// Indicator 1: open = close (doji pattern)
|
||||
indicator1.HistoricalData.AddBar(now.AddMinutes(i), 100, 105, 95, 100, 1000);
|
||||
indicator1.ProcessUpdate(new UpdateArgs(UpdateReason.HistoricalBar));
|
||||
|
||||
// Indicator 2: open != close (directional move)
|
||||
indicator2.HistoricalData.AddBar(now.AddMinutes(i), 98, 105, 95, 104, 1000);
|
||||
indicator2.ProcessUpdate(new UpdateArgs(UpdateReason.HistoricalBar));
|
||||
}
|
||||
|
||||
double val1 = indicator1.LinesSeries[0].GetValue(0);
|
||||
double val2 = indicator2.LinesSeries[0].GetValue(0);
|
||||
|
||||
Assert.True(double.IsFinite(val1));
|
||||
Assert.True(double.IsFinite(val2));
|
||||
// HLV should be identical since H-L range is the same
|
||||
Assert.Equal(val1, val2, 10);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void HlvIndicator_ConstantPrice_ProducesZeroVolatility()
|
||||
{
|
||||
var indicator = new HlvIndicator { Period = 10, Annualize = false };
|
||||
indicator.Initialize();
|
||||
|
||||
var now = DateTime.UtcNow;
|
||||
|
||||
// Constant price (no volatility)
|
||||
for (int i = 0; i < 30; i++)
|
||||
{
|
||||
indicator.HistoricalData.AddBar(now.AddMinutes(i), 100, 100, 100, 100, 1000);
|
||||
indicator.ProcessUpdate(new UpdateArgs(UpdateReason.HistoricalBar));
|
||||
}
|
||||
|
||||
double val = indicator.LinesSeries[0].GetValue(0);
|
||||
|
||||
Assert.True(double.IsFinite(val));
|
||||
Assert.True(val < 0.001, "Constant price should produce near-zero volatility");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,649 @@
|
||||
namespace QuanTAlib.Tests;
|
||||
using Xunit;
|
||||
|
||||
public class HlvTests
|
||||
{
|
||||
private const double Tolerance = 1e-9;
|
||||
|
||||
private static TBarSeries GenerateTestData(int count = 100)
|
||||
{
|
||||
var gbm = new GBM(seed: 42);
|
||||
return gbm.Fetch(count, DateTime.UtcNow.Ticks, TimeSpan.FromMinutes(1));
|
||||
}
|
||||
|
||||
#region Constructor Tests
|
||||
|
||||
[Fact]
|
||||
public void Constructor_DefaultParameters_SetsCorrectValues()
|
||||
{
|
||||
var hlv = new Hlv();
|
||||
Assert.Equal(20, hlv.Period);
|
||||
Assert.True(hlv.Annualize);
|
||||
Assert.Equal(252, hlv.AnnualPeriods);
|
||||
Assert.Equal("Hlv(20)", hlv.Name);
|
||||
Assert.Equal(20, hlv.WarmupPeriod);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Constructor_CustomParameters_SetsCorrectValues()
|
||||
{
|
||||
var hlv = new Hlv(period: 10, annualize: false, annualPeriods: 365);
|
||||
Assert.Equal(10, hlv.Period);
|
||||
Assert.False(hlv.Annualize);
|
||||
Assert.Equal(365, hlv.AnnualPeriods);
|
||||
Assert.Equal("Hlv(10)", hlv.Name);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Constructor_ZeroPeriod_ThrowsArgumentException()
|
||||
{
|
||||
var ex = Assert.Throws<ArgumentException>(() => new Hlv(period: 0));
|
||||
Assert.Equal("period", ex.ParamName);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Constructor_NegativePeriod_ThrowsArgumentException()
|
||||
{
|
||||
var ex = Assert.Throws<ArgumentException>(() => new Hlv(period: -1));
|
||||
Assert.Equal("period", ex.ParamName);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Constructor_ZeroAnnualPeriodsWhenAnnualizing_ThrowsArgumentException()
|
||||
{
|
||||
var ex = Assert.Throws<ArgumentException>(() => new Hlv(period: 10, annualize: true, annualPeriods: 0));
|
||||
Assert.Equal("annualPeriods", ex.ParamName);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Constructor_ZeroAnnualPeriodsWhenNotAnnualizing_DoesNotThrow()
|
||||
{
|
||||
var hlv = new Hlv(period: 10, annualize: false, annualPeriods: 0);
|
||||
Assert.Equal(0, hlv.AnnualPeriods);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Basic Calculation Tests
|
||||
|
||||
[Fact]
|
||||
public void Update_SingleBar_ReturnsNonNegativeValue()
|
||||
{
|
||||
var hlv = new Hlv(period: 5);
|
||||
var bar = new TBar(DateTime.UtcNow, 100.0, 105.0, 98.0, 102.0, 1000);
|
||||
var result = hlv.Update(bar);
|
||||
|
||||
Assert.True(result.Value >= 0, "HLV should return non-negative values");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Update_MultipleBars_ReturnsCorrectCount()
|
||||
{
|
||||
var hlv = new Hlv(period: 5);
|
||||
var bars = GenerateTestData(10);
|
||||
|
||||
for (int i = 0; i < bars.Count; i++)
|
||||
{
|
||||
hlv.Update(bars[i]);
|
||||
}
|
||||
|
||||
Assert.True(hlv.IsHot, "Indicator should be hot after warmup period");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Update_ReturnsLastValue()
|
||||
{
|
||||
var hlv = new Hlv(period: 5);
|
||||
var bar = new TBar(DateTime.UtcNow, 100.0, 105.0, 98.0, 102.0, 1000);
|
||||
var result = hlv.Update(bar);
|
||||
|
||||
Assert.Equal(result.Value, hlv.Last.Value, Tolerance);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Update_WithoutAnnualization_ReturnsSmallerValues()
|
||||
{
|
||||
var hlvAnnual = new Hlv(period: 10, annualize: true, annualPeriods: 252);
|
||||
var hlvNoAnnual = new Hlv(period: 10, annualize: false);
|
||||
var bars = GenerateTestData(20);
|
||||
|
||||
double lastAnnual = 0;
|
||||
double lastNoAnnual = 0;
|
||||
for (int i = 0; i < bars.Count; i++)
|
||||
{
|
||||
lastAnnual = hlvAnnual.Update(bars[i]).Value;
|
||||
lastNoAnnual = hlvNoAnnual.Update(bars[i]).Value;
|
||||
}
|
||||
|
||||
// Annualized values should be larger by factor of sqrt(252)
|
||||
Assert.True(lastAnnual > lastNoAnnual, "Annualized values should be larger");
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region State Management Tests
|
||||
|
||||
[Fact]
|
||||
public void Update_IsNewTrue_AdvancesState()
|
||||
{
|
||||
var hlv = new Hlv(period: 5);
|
||||
var bar1 = new TBar(DateTime.UtcNow, 100.0, 105.0, 98.0, 102.0, 1000);
|
||||
var bar2 = new TBar(DateTime.UtcNow.AddMinutes(1), 102.0, 107.0, 100.0, 105.0, 1000);
|
||||
|
||||
hlv.Update(bar1, isNew: true);
|
||||
var result1 = hlv.Last.Value;
|
||||
|
||||
hlv.Update(bar2, isNew: true);
|
||||
var result2 = hlv.Last.Value;
|
||||
|
||||
Assert.NotEqual(result1, result2);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Update_IsNewFalse_UpdatesCurrentBar()
|
||||
{
|
||||
var hlv = new Hlv(period: 5);
|
||||
var bar1 = new TBar(DateTime.UtcNow, 100.0, 105.0, 98.0, 102.0, 1000);
|
||||
|
||||
hlv.Update(bar1, isNew: true);
|
||||
var firstValue = hlv.Last.Value;
|
||||
|
||||
// Update the same bar with different high-low values
|
||||
var bar1Updated = new TBar(DateTime.UtcNow, 100.0, 110.0, 95.0, 108.0, 1000);
|
||||
hlv.Update(bar1Updated, isNew: false);
|
||||
var updatedValue = hlv.Last.Value;
|
||||
|
||||
Assert.NotEqual(firstValue, updatedValue);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Update_IterativeCorrections_RestoresState()
|
||||
{
|
||||
var hlv = new Hlv(period: 5);
|
||||
var bars = GenerateTestData(10);
|
||||
|
||||
// Process first 5 bars
|
||||
for (int i = 0; i < 5; i++)
|
||||
{
|
||||
hlv.Update(bars[i], isNew: true);
|
||||
}
|
||||
|
||||
// Add bar 6 and correct multiple times
|
||||
hlv.Update(bars[5], isNew: true);
|
||||
hlv.Update(bars[5], isNew: false);
|
||||
hlv.Update(bars[5], isNew: false);
|
||||
hlv.Update(bars[5], isNew: false);
|
||||
|
||||
// Now continue with bar 7
|
||||
hlv.Update(bars[6], isNew: true);
|
||||
|
||||
// Create new instance and process same data
|
||||
var hlv2 = new Hlv(period: 5);
|
||||
for (int i = 0; i < 7; i++)
|
||||
{
|
||||
hlv2.Update(bars[i], isNew: true);
|
||||
}
|
||||
|
||||
Assert.Equal(hlv.Last.Value, hlv2.Last.Value, Tolerance);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region IsHot and Warmup Tests
|
||||
|
||||
[Fact]
|
||||
public void IsHot_BeforeWarmup_ReturnsFalse()
|
||||
{
|
||||
var hlv = new Hlv(period: 10);
|
||||
var bars = GenerateTestData(5);
|
||||
|
||||
for (int i = 0; i < bars.Count; i++)
|
||||
{
|
||||
hlv.Update(bars[i]);
|
||||
}
|
||||
|
||||
Assert.False(hlv.IsHot);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void IsHot_AfterWarmup_ReturnsTrue()
|
||||
{
|
||||
var hlv = new Hlv(period: 10);
|
||||
var bars = GenerateTestData(15);
|
||||
|
||||
for (int i = 0; i < bars.Count; i++)
|
||||
{
|
||||
hlv.Update(bars[i]);
|
||||
}
|
||||
|
||||
Assert.True(hlv.IsHot);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void IsHot_ExactlyAtWarmup_ReturnsTrue()
|
||||
{
|
||||
var hlv = new Hlv(period: 10);
|
||||
var bars = GenerateTestData(10);
|
||||
|
||||
for (int i = 0; i < bars.Count; i++)
|
||||
{
|
||||
hlv.Update(bars[i]);
|
||||
}
|
||||
|
||||
Assert.True(hlv.IsHot);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Reset Tests
|
||||
|
||||
[Fact]
|
||||
public void Reset_ClearsState()
|
||||
{
|
||||
var hlv = new Hlv(period: 5);
|
||||
var bars = GenerateTestData(10);
|
||||
|
||||
for (int i = 0; i < bars.Count; i++)
|
||||
{
|
||||
hlv.Update(bars[i]);
|
||||
}
|
||||
|
||||
hlv.Reset();
|
||||
|
||||
Assert.False(hlv.IsHot);
|
||||
Assert.Equal(0, hlv.Last.Value);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Reset_AllowsReprocessing()
|
||||
{
|
||||
var hlv = new Hlv(period: 5);
|
||||
var bars = GenerateTestData(10);
|
||||
|
||||
// First pass
|
||||
for (int i = 0; i < bars.Count; i++)
|
||||
{
|
||||
hlv.Update(bars[i]);
|
||||
}
|
||||
var firstResult = hlv.Last.Value;
|
||||
|
||||
// Reset and second pass
|
||||
hlv.Reset();
|
||||
for (int i = 0; i < bars.Count; i++)
|
||||
{
|
||||
hlv.Update(bars[i]);
|
||||
}
|
||||
var secondResult = hlv.Last.Value;
|
||||
|
||||
Assert.Equal(firstResult, secondResult, Tolerance);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Robustness Tests
|
||||
|
||||
[Fact]
|
||||
public void Update_WithNaNValues_UsesLastValidEstimator()
|
||||
{
|
||||
var hlv = new Hlv(period: 5);
|
||||
var bars = GenerateTestData(10);
|
||||
|
||||
for (int i = 0; i < bars.Count; i++)
|
||||
{
|
||||
hlv.Update(bars[i]);
|
||||
}
|
||||
var valueBeforeInvalid = hlv.Last.Value;
|
||||
|
||||
// Bar with NaN high - should use last valid Parkinson estimator
|
||||
var nanBar = new TBar(DateTime.UtcNow, 100.0, double.NaN, 98.0, 102.0, 1000);
|
||||
var result = hlv.Update(nanBar);
|
||||
|
||||
// Result should be finite and close to previous (RMA smoothed)
|
||||
Assert.True(double.IsFinite(result.Value), "Result should be finite when using last valid estimator");
|
||||
Assert.True(result.Value >= 0, "Volatility should be non-negative");
|
||||
double relativeDiff = Math.Abs(result.Value - valueBeforeInvalid) / valueBeforeInvalid;
|
||||
Assert.True(relativeDiff < 0.2, $"Value should be similar to previous: {valueBeforeInvalid} vs {result.Value}");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Update_WithInfinityValues_UsesLastValidEstimator()
|
||||
{
|
||||
var hlv = new Hlv(period: 5);
|
||||
var bars = GenerateTestData(10);
|
||||
|
||||
for (int i = 0; i < bars.Count; i++)
|
||||
{
|
||||
hlv.Update(bars[i]);
|
||||
}
|
||||
var valueBeforeInvalid = hlv.Last.Value;
|
||||
|
||||
// Bar with infinity - should use last valid Parkinson estimator
|
||||
var infBar = new TBar(DateTime.UtcNow, 100.0, double.PositiveInfinity, 98.0, 102.0, 1000);
|
||||
var result = hlv.Update(infBar);
|
||||
|
||||
Assert.True(double.IsFinite(result.Value), "Result should be finite when using last valid estimator");
|
||||
Assert.True(result.Value >= 0, "Volatility should be non-negative");
|
||||
double relativeDiff = Math.Abs(result.Value - valueBeforeInvalid) / valueBeforeInvalid;
|
||||
Assert.True(relativeDiff < 0.2, $"Value should be similar to previous: {valueBeforeInvalid} vs {result.Value}");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Update_WithZeroPrices_UsesLastValidEstimator()
|
||||
{
|
||||
var hlv = new Hlv(period: 5);
|
||||
var bars = GenerateTestData(10);
|
||||
|
||||
for (int i = 0; i < bars.Count; i++)
|
||||
{
|
||||
hlv.Update(bars[i]);
|
||||
}
|
||||
var valueBeforeInvalid = hlv.Last.Value;
|
||||
|
||||
// Bar with zero low (invalid for log) - should use last valid Parkinson estimator
|
||||
var zeroBar = new TBar(DateTime.UtcNow, 100.0, 105.0, 0.0, 102.0, 1000);
|
||||
var result = hlv.Update(zeroBar);
|
||||
|
||||
Assert.True(double.IsFinite(result.Value), "Result should be finite when using last valid estimator");
|
||||
Assert.True(result.Value >= 0, "Volatility should be non-negative");
|
||||
double relativeDiff = Math.Abs(result.Value - valueBeforeInvalid) / valueBeforeInvalid;
|
||||
Assert.True(relativeDiff < 0.2, $"Value should be similar to previous: {valueBeforeInvalid} vs {result.Value}");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Update_WithNegativePrices_UsesLastValidEstimator()
|
||||
{
|
||||
var hlv = new Hlv(period: 5);
|
||||
var bars = GenerateTestData(10);
|
||||
|
||||
for (int i = 0; i < bars.Count; i++)
|
||||
{
|
||||
hlv.Update(bars[i]);
|
||||
}
|
||||
var valueBeforeInvalid = hlv.Last.Value;
|
||||
|
||||
// Bar with negative price - should use last valid Parkinson estimator
|
||||
var negBar = new TBar(DateTime.UtcNow, 100.0, 105.0, -98.0, 102.0, 1000);
|
||||
var result = hlv.Update(negBar);
|
||||
|
||||
Assert.True(double.IsFinite(result.Value), "Result should be finite when using last valid estimator");
|
||||
Assert.True(result.Value >= 0, "Volatility should be non-negative");
|
||||
double relativeDiff = Math.Abs(result.Value - valueBeforeInvalid) / valueBeforeInvalid;
|
||||
Assert.True(relativeDiff < 0.2, $"Value should be similar to previous: {valueBeforeInvalid} vs {result.Value}");
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Batch and Series Tests
|
||||
|
||||
[Fact]
|
||||
public void Batch_MatchesStreamingResults()
|
||||
{
|
||||
const int dataCount = 100;
|
||||
var bars = GenerateTestData(dataCount);
|
||||
|
||||
// Streaming
|
||||
var hlvStreaming = new Hlv(period: 10);
|
||||
var streamingResults = new double[dataCount];
|
||||
for (int i = 0; i < dataCount; i++)
|
||||
{
|
||||
streamingResults[i] = hlvStreaming.Update(bars[i]).Value;
|
||||
}
|
||||
|
||||
// Batch (HLV only uses high-low)
|
||||
var highs = new double[dataCount];
|
||||
var lows = new double[dataCount];
|
||||
var batchResults = new double[dataCount];
|
||||
|
||||
for (int i = 0; i < dataCount; i++)
|
||||
{
|
||||
highs[i] = bars[i].High;
|
||||
lows[i] = bars[i].Low;
|
||||
}
|
||||
|
||||
Hlv.Batch(highs, lows, batchResults, period: 10);
|
||||
|
||||
// Compare last 50 values (after warmup)
|
||||
for (int i = 50; i < dataCount; i++)
|
||||
{
|
||||
Assert.Equal(streamingResults[i], batchResults[i], Tolerance);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Calculate_TBarSeries_ReturnsCorrectLength()
|
||||
{
|
||||
const int dataCount = 50;
|
||||
var barSeries = GenerateTestData(dataCount);
|
||||
|
||||
var result = Hlv.Batch(barSeries, period: 10);
|
||||
|
||||
Assert.Equal(dataCount, result.Count);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Update_TBarSeries_MatchesStreamingResults()
|
||||
{
|
||||
const int dataCount = 50;
|
||||
var barSeries = GenerateTestData(dataCount);
|
||||
|
||||
// Series update
|
||||
var hlvSeries = new Hlv(period: 10);
|
||||
var seriesResult = hlvSeries.Update(barSeries);
|
||||
|
||||
// Streaming
|
||||
var hlvStreaming = new Hlv(period: 10);
|
||||
var streamingResults = new double[dataCount];
|
||||
for (int i = 0; i < dataCount; i++)
|
||||
{
|
||||
streamingResults[i] = hlvStreaming.Update(barSeries[i]).Value;
|
||||
}
|
||||
|
||||
// Compare last 30 values
|
||||
for (int i = 20; i < dataCount; i++)
|
||||
{
|
||||
Assert.Equal(streamingResults[i], seriesResult.Values[i], Tolerance);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Batch_EmptyInput_DoesNotThrow()
|
||||
{
|
||||
var highs = Array.Empty<double>();
|
||||
var lows = Array.Empty<double>();
|
||||
var output = Array.Empty<double>();
|
||||
|
||||
// Should not throw
|
||||
Hlv.Batch(highs, lows, output, period: 10);
|
||||
Assert.Empty(output);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Batch_MismatchedLengths_ThrowsArgumentException()
|
||||
{
|
||||
var highs = new double[10];
|
||||
var lows = new double[5]; // Mismatched
|
||||
var output = new double[10];
|
||||
|
||||
var ex = Assert.Throws<ArgumentException>(() =>
|
||||
Hlv.Batch(highs, lows, output, period: 10));
|
||||
Assert.Equal("low", ex.ParamName);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Batch_OutputTooShort_ThrowsArgumentException()
|
||||
{
|
||||
var highs = new double[10];
|
||||
var lows = new double[10];
|
||||
var output = new double[5]; // Too short
|
||||
|
||||
var ex = Assert.Throws<ArgumentException>(() =>
|
||||
Hlv.Batch(highs, lows, output, period: 10));
|
||||
Assert.Equal("output", ex.ParamName);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Batch_InvalidPeriod_ThrowsArgumentException()
|
||||
{
|
||||
var highs = new double[10];
|
||||
var lows = new double[10];
|
||||
var output = new double[10];
|
||||
|
||||
var ex = Assert.Throws<ArgumentException>(() =>
|
||||
Hlv.Batch(highs, lows, output, period: 0));
|
||||
Assert.Equal("period", ex.ParamName);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Event Publishing Tests
|
||||
|
||||
[Fact]
|
||||
public void Update_PublishesEvent()
|
||||
{
|
||||
var hlv = new Hlv(period: 5);
|
||||
bool eventFired = false;
|
||||
hlv.Pub += (object? sender, in TValueEventArgs args) => eventFired = true;
|
||||
|
||||
var bar = new TBar(DateTime.UtcNow, 100.0, 105.0, 98.0, 102.0, 1000);
|
||||
hlv.Update(bar);
|
||||
|
||||
Assert.True(eventFired);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ChainedIndicator_ReceivesValues()
|
||||
{
|
||||
var source = new Hlv(period: 5);
|
||||
var downstream = new Sma(source, period: 3);
|
||||
|
||||
var bars = GenerateTestData(10);
|
||||
for (int i = 0; i < bars.Count; i++)
|
||||
{
|
||||
source.Update(bars[i]);
|
||||
}
|
||||
|
||||
Assert.True(downstream.Last.Value > 0, "Downstream indicator should receive values");
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region TValue Update Tests
|
||||
|
||||
[Fact]
|
||||
public void Update_TValue_TreatsAsPrecomputedEstimator()
|
||||
{
|
||||
var hlv1 = new Hlv(period: 5);
|
||||
var hlv2 = new Hlv(period: 5);
|
||||
|
||||
// For hlv1, use bar data
|
||||
var bar = new TBar(DateTime.UtcNow, 100.0, 105.0, 98.0, 102.0, 1000);
|
||||
hlv1.Update(bar);
|
||||
|
||||
// For hlv2, use pre-computed Parkinson estimator value
|
||||
// Compute manually: (1/(4*ln(2))) * (ln(105)-ln(98))^2
|
||||
double lnH = Math.Log(105.0);
|
||||
double lnL = Math.Log(98.0);
|
||||
double hlRange = lnH - lnL;
|
||||
double C_4LN2_INV = 0.36067376022224085; // 1 / (4 * ln(2))
|
||||
double pkEstimator = C_4LN2_INV * hlRange * hlRange;
|
||||
|
||||
var tvalue = new TValue(bar.Time, pkEstimator);
|
||||
hlv2.Update(tvalue);
|
||||
|
||||
Assert.Equal(hlv1.Last.Value, hlv2.Last.Value, Tolerance);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Additional Tests
|
||||
|
||||
[Fact]
|
||||
public void LargeDataset_Performance()
|
||||
{
|
||||
var hlv = new Hlv(period: 20);
|
||||
var bars = GenerateTestData(5000);
|
||||
|
||||
for (int i = 0; i < bars.Count; i++)
|
||||
{
|
||||
var result = hlv.Update(bars[i]);
|
||||
Assert.True(double.IsFinite(result.Value));
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void DifferentParameters_ProduceDistinctValues()
|
||||
{
|
||||
var bars = GenerateTestData(50);
|
||||
|
||||
var hlv1 = new Hlv(period: 10);
|
||||
var hlv2 = new Hlv(period: 20);
|
||||
var hlv3 = new Hlv(period: 10, annualize: false);
|
||||
|
||||
for (int i = 0; i < bars.Count; i++)
|
||||
{
|
||||
hlv1.Update(bars[i]);
|
||||
hlv2.Update(bars[i]);
|
||||
hlv3.Update(bars[i]);
|
||||
}
|
||||
|
||||
Assert.True(double.IsFinite(hlv1.Last.Value));
|
||||
Assert.True(double.IsFinite(hlv2.Last.Value));
|
||||
Assert.True(double.IsFinite(hlv3.Last.Value));
|
||||
// Different parameters should produce different values
|
||||
Assert.NotEqual(hlv1.Last.Value, hlv2.Last.Value);
|
||||
Assert.NotEqual(hlv1.Last.Value, hlv3.Last.Value);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void StaticCalculate_Works()
|
||||
{
|
||||
var bars = GenerateTestData(100);
|
||||
|
||||
var result = Hlv.Batch(bars, period: 14);
|
||||
|
||||
Assert.Equal(100, result.Count);
|
||||
Assert.True(double.IsFinite(result[result.Count - 1].Value));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void StaticCalculate_ValidatesInput()
|
||||
{
|
||||
var bars = GenerateTestData(10);
|
||||
|
||||
Assert.Throws<ArgumentException>(() => Hlv.Batch(bars, period: 0));
|
||||
Assert.Throws<ArgumentException>(() => Hlv.Batch(bars, period: -1));
|
||||
Assert.Throws<ArgumentException>(() => Hlv.Batch(bars, period: 10, annualize: true, annualPeriods: 0));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Prime_Works()
|
||||
{
|
||||
var hlv = new Hlv(period: 5);
|
||||
var values = new double[] { 0.001, 0.002, 0.0015, 0.0018, 0.0012, 0.0022 };
|
||||
|
||||
hlv.Prime(values);
|
||||
|
||||
Assert.True(hlv.IsHot);
|
||||
Assert.True(double.IsFinite(hlv.Last.Value));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Hlv_OnlyUsesHighLow_NotOpenClose()
|
||||
{
|
||||
// HLV (Parkinson) only uses High-Low, so changing Open/Close shouldn't affect result
|
||||
var hlv1 = new Hlv(period: 5);
|
||||
var hlv2 = new Hlv(period: 5);
|
||||
|
||||
// Bar with same High-Low but different Open-Close
|
||||
var bar1 = new TBar(DateTime.UtcNow, 100.0, 105.0, 98.0, 102.0, 1000);
|
||||
var bar2 = new TBar(DateTime.UtcNow, 99.0, 105.0, 98.0, 104.0, 1000); // Different O/C
|
||||
|
||||
var result1 = hlv1.Update(bar1).Value;
|
||||
var result2 = hlv2.Update(bar2).Value;
|
||||
|
||||
// Results should be identical since only H-L matters
|
||||
Assert.Equal(result1, result2, Tolerance);
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
@@ -0,0 +1,647 @@
|
||||
namespace QuanTAlib.Test;
|
||||
|
||||
using Xunit;
|
||||
|
||||
/// <summary>
|
||||
/// Validation tests for HLV (High-Low Volatility / Parkinson Volatility).
|
||||
/// HLV is a range-based volatility estimator using only High-Low prices.
|
||||
/// Formula: parkinsonEstimator = (1/(4*ln(2))) * (lnH - lnL)²
|
||||
/// RMA smoothing with bias correction applied.
|
||||
/// </summary>
|
||||
public class HlvValidationTests
|
||||
{
|
||||
private static TBarSeries GenerateTestData(int count = 100)
|
||||
{
|
||||
var gbm = new GBM(seed: 42);
|
||||
return gbm.Fetch(count, DateTime.UtcNow.Ticks, TimeSpan.FromMinutes(1));
|
||||
}
|
||||
|
||||
// === Mathematical Validation ===
|
||||
|
||||
/// <summary>
|
||||
/// Validates the Parkinson coefficient: 1/(4*ln(2)) ≈ 0.36067376
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public void Hlv_ParkinsonCoefficient_IsCorrect()
|
||||
{
|
||||
double expectedCoeff = 1.0 / (4.0 * Math.Log(2));
|
||||
Assert.Equal(0.36067376022224085, expectedCoeff, 10);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Validates RMA decay formula: decay = 1 - (1/period)
|
||||
/// </summary>
|
||||
[Theory]
|
||||
[InlineData(14, 0.928571428571429)] // 1 - 1/14 = 13/14
|
||||
[InlineData(20, 0.95)] // 1 - 1/20 = 19/20
|
||||
[InlineData(10, 0.9)] // 1 - 1/10 = 9/10
|
||||
public void Hlv_RmaDecay_IsCorrect(int period, double expectedDecay)
|
||||
{
|
||||
double decay = 1.0 - 1.0 / period;
|
||||
Assert.Equal(expectedDecay, decay, 10);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Validates Parkinson estimator formula: (1/(4*ln(2))) * (lnH - lnL)²
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public void Hlv_ParkinsonEstimatorFormula_IsCorrect()
|
||||
{
|
||||
double high = 105.0;
|
||||
double low = 95.0;
|
||||
|
||||
double lnH = Math.Log(high);
|
||||
double lnL = Math.Log(low);
|
||||
|
||||
double coeff = 1.0 / (4.0 * Math.Log(2));
|
||||
double expectedPk = coeff * Math.Pow(lnH - lnL, 2);
|
||||
|
||||
// Manual calculation
|
||||
// lnH - lnL = ln(105/95) ≈ 0.1001
|
||||
// (lnH - lnL)² ≈ 0.01002
|
||||
// coeff ≈ 0.36067
|
||||
// Pk ≈ 0.36067 * 0.01002 ≈ 0.00361
|
||||
|
||||
Assert.True(expectedPk > 0, "Parkinson estimator should be positive for bars with range");
|
||||
Assert.True(expectedPk < 0.1, "Parkinson estimator should be small for 10% range");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Validates that flat bar (H=L) produces zero Parkinson estimator.
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public void Hlv_FlatBar_ProducesZeroPk()
|
||||
{
|
||||
double price = 100.0;
|
||||
double lnH = Math.Log(price);
|
||||
double lnL = Math.Log(price);
|
||||
|
||||
double coeff = 1.0 / (4.0 * Math.Log(2));
|
||||
double pk = coeff * Math.Pow(lnH - lnL, 2); // 0
|
||||
|
||||
Assert.Equal(0.0, pk, 15);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Validates bias correction formula: corrected = raw / (1 - decay^n)
|
||||
/// </summary>
|
||||
[Theory]
|
||||
[InlineData(14, 5)] // Early in warmup
|
||||
[InlineData(14, 14)] // At warmup
|
||||
[InlineData(14, 50)] // Well past warmup
|
||||
[InlineData(14, 100)] // Very late - correction should be minimal
|
||||
public void Hlv_BiasCorrection_WorksCorrectly(int period, int count)
|
||||
{
|
||||
double decay = 1.0 - 1.0 / period;
|
||||
double e = Math.Pow(decay, count);
|
||||
double correctionFactor = 1.0 / (1.0 - e);
|
||||
|
||||
// Early: large correction needed
|
||||
// Later: correction approaches 1.0
|
||||
if (count < period)
|
||||
{
|
||||
Assert.True(correctionFactor > 1.05, "Early values should need significant correction");
|
||||
}
|
||||
else if (count > period * 5)
|
||||
{
|
||||
Assert.True(correctionFactor < 1.01, "Very late values should need minimal correction");
|
||||
}
|
||||
else if (count > period * 2)
|
||||
{
|
||||
Assert.True(correctionFactor < 1.1, "Late values should need small correction");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Validates annualization factor: √(annualPeriods)
|
||||
/// </summary>
|
||||
[Theory]
|
||||
[InlineData(252, 15.8745078663875)] // Daily trading days
|
||||
[InlineData(365, 19.1049731745428)] // Calendar days
|
||||
[InlineData(52, 7.21110255092798)] // Weekly
|
||||
[InlineData(12, 3.46410161513775)] // Monthly
|
||||
public void Hlv_AnnualizationFactor_IsCorrect(int annualPeriods, double expectedFactor)
|
||||
{
|
||||
double factor = Math.Sqrt(annualPeriods);
|
||||
Assert.Equal(expectedFactor, factor, 10);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Validates that wider range produces higher Parkinson estimator.
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public void Hlv_WiderRange_ProducesHigherPk()
|
||||
{
|
||||
// Narrow range bar
|
||||
double narrowPk = ComputeParkinsonEstimator(101, 99);
|
||||
|
||||
// Wide range bar
|
||||
double widePk = ComputeParkinsonEstimator(110, 90);
|
||||
|
||||
Assert.True(widePk > narrowPk,
|
||||
"Wider range should produce higher Parkinson estimator");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Validates that HLV only uses High-Low (ignores Open-Close).
|
||||
/// Same H-L range with different O-C should produce identical results.
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public void Hlv_OnlyUsesHighLow_IgnoresOpenClose()
|
||||
{
|
||||
var hlv1 = new Hlv(14, annualize: false);
|
||||
var hlv2 = new Hlv(14, annualize: false);
|
||||
|
||||
for (int i = 0; i < 30; i++)
|
||||
{
|
||||
// Same high/low range but different open/close
|
||||
// Indicator 1: doji pattern (open = close)
|
||||
var bar1 = new TBar(
|
||||
DateTime.UtcNow.AddMinutes(i).Ticks,
|
||||
100.0, 105.0, 95.0, 100.0, 1000.0
|
||||
);
|
||||
hlv1.Update(bar1);
|
||||
|
||||
// Indicator 2: directional move (open != close)
|
||||
var bar2 = new TBar(
|
||||
DateTime.UtcNow.AddMinutes(i).Ticks,
|
||||
98.0, 105.0, 95.0, 104.0, 1000.0
|
||||
);
|
||||
hlv2.Update(bar2);
|
||||
}
|
||||
|
||||
// HLV should be identical since H-L range is the same
|
||||
Assert.Equal(hlv1.Last.Value, hlv2.Last.Value, 10);
|
||||
}
|
||||
|
||||
// === Consistency Tests ===
|
||||
|
||||
/// <summary>
|
||||
/// Validates streaming and batch produce identical results.
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public void Hlv_StreamingMatchesBatch()
|
||||
{
|
||||
var bars = GenerateTestData(100);
|
||||
|
||||
// Streaming calculation
|
||||
var streamingHlv = new Hlv(14);
|
||||
for (int i = 0; i < bars.Count; i++)
|
||||
{
|
||||
streamingHlv.Update(bars[i]);
|
||||
}
|
||||
|
||||
// Batch calculation
|
||||
var batchResult = Hlv.Batch(bars, 14);
|
||||
|
||||
// Compare last values
|
||||
Assert.Equal(batchResult.Last.Value, streamingHlv.Last.Value, 8);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Validates TBarSeries input matches TBar streaming.
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public void Hlv_TBarSeriesInput_MatchesStreaming()
|
||||
{
|
||||
var bars = GenerateTestData(100);
|
||||
|
||||
// Streaming
|
||||
var streamingHlv = new Hlv(14);
|
||||
for (int i = 0; i < bars.Count; i++)
|
||||
{
|
||||
streamingHlv.Update(bars[i]);
|
||||
}
|
||||
|
||||
// TBarSeries batch
|
||||
var batchHlv = new Hlv(14);
|
||||
var batchResult = batchHlv.Update(bars);
|
||||
|
||||
Assert.Equal(batchResult.Last.Value, streamingHlv.Last.Value, 10);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Validates Span batch matches streaming.
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public void Hlv_SpanBatch_MatchesStreaming()
|
||||
{
|
||||
var bars = GenerateTestData(100);
|
||||
|
||||
// Streaming
|
||||
var streamingHlv = new Hlv(14);
|
||||
for (int i = 0; i < bars.Count; i++)
|
||||
{
|
||||
streamingHlv.Update(bars[i]);
|
||||
}
|
||||
|
||||
// Extract H-L arrays
|
||||
var highs = new double[bars.Count];
|
||||
var lows = new double[bars.Count];
|
||||
for (int i = 0; i < bars.Count; i++)
|
||||
{
|
||||
highs[i] = bars[i].High;
|
||||
lows[i] = bars[i].Low;
|
||||
}
|
||||
|
||||
// Span batch
|
||||
var output = new double[bars.Count];
|
||||
Hlv.Batch(highs, lows, output, 14);
|
||||
|
||||
Assert.Equal(output[^1], streamingHlv.Last.Value, 10);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Validates annualized output is scaled correctly.
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public void Hlv_Annualized_ScaledCorrectly()
|
||||
{
|
||||
var bars = GenerateTestData(50);
|
||||
|
||||
// Non-annualized
|
||||
var hlvRaw = new Hlv(14, annualize: false);
|
||||
|
||||
// Annualized (default 252 periods)
|
||||
var hlvAnn = new Hlv(14, annualize: true, annualPeriods: 252);
|
||||
|
||||
for (int i = 0; i < bars.Count; i++)
|
||||
{
|
||||
hlvRaw.Update(bars[i]);
|
||||
hlvAnn.Update(bars[i]);
|
||||
}
|
||||
|
||||
double expectedRatio = Math.Sqrt(252);
|
||||
double actualRatio = hlvAnn.Last.Value / hlvRaw.Last.Value;
|
||||
|
||||
Assert.Equal(expectedRatio, actualRatio, 6);
|
||||
}
|
||||
|
||||
// === Parameter Sensitivity ===
|
||||
|
||||
/// <summary>
|
||||
/// Validates shorter period produces more responsive volatility.
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public void Hlv_ShorterPeriod_MoreResponsive()
|
||||
{
|
||||
var bars = GenerateTestData(50);
|
||||
|
||||
var hlvShort = new Hlv(5);
|
||||
var hlvLong = new Hlv(20);
|
||||
|
||||
var shortResults = new List<double>();
|
||||
var longResults = new List<double>();
|
||||
|
||||
for (int i = 0; i < bars.Count; i++)
|
||||
{
|
||||
hlvShort.Update(bars[i]);
|
||||
hlvLong.Update(bars[i]);
|
||||
|
||||
if (hlvShort.IsHot && hlvLong.IsHot)
|
||||
{
|
||||
shortResults.Add(hlvShort.Last.Value);
|
||||
longResults.Add(hlvLong.Last.Value);
|
||||
}
|
||||
}
|
||||
|
||||
// Shorter period should have higher variance in results
|
||||
double shortVar = Variance(shortResults);
|
||||
double longVar = Variance(longResults);
|
||||
|
||||
Assert.True(shortResults.Count > 0, "Should have hot results");
|
||||
Assert.True(shortVar > longVar * 0.5,
|
||||
"Shorter period should generally be more variable");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Validates different periods produce different results.
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public void Hlv_DifferentPeriods_ProduceDifferentResults()
|
||||
{
|
||||
var bars = GenerateTestData(50);
|
||||
|
||||
var hlv10 = new Hlv(10);
|
||||
var hlv14 = new Hlv(14);
|
||||
var hlv20 = new Hlv(20);
|
||||
|
||||
for (int i = 0; i < bars.Count; i++)
|
||||
{
|
||||
hlv10.Update(bars[i]);
|
||||
hlv14.Update(bars[i]);
|
||||
hlv20.Update(bars[i]);
|
||||
}
|
||||
|
||||
Assert.NotEqual(hlv10.Last.Value, hlv14.Last.Value);
|
||||
Assert.NotEqual(hlv14.Last.Value, hlv20.Last.Value);
|
||||
}
|
||||
|
||||
// === Edge Cases ===
|
||||
|
||||
/// <summary>
|
||||
/// Validates handling of very small ranges (tight consolidation).
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public void Hlv_VerySmallRanges_HandledCorrectly()
|
||||
{
|
||||
var hlv = new Hlv(14);
|
||||
|
||||
for (int i = 0; i < 30; i++)
|
||||
{
|
||||
var bar = new TBar(
|
||||
DateTime.UtcNow.AddMinutes(i).Ticks,
|
||||
100.0, 100.001, 99.999, 100.0, 1000.0
|
||||
);
|
||||
hlv.Update(bar);
|
||||
}
|
||||
|
||||
Assert.True(double.IsFinite(hlv.Last.Value));
|
||||
Assert.True(hlv.Last.Value >= 0, "Volatility should be non-negative");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Validates handling of very large ranges (high volatility).
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public void Hlv_VeryLargeRanges_HandledCorrectly()
|
||||
{
|
||||
var hlv = new Hlv(14);
|
||||
|
||||
for (int i = 0; i < 30; i++)
|
||||
{
|
||||
var bar = new TBar(
|
||||
DateTime.UtcNow.AddMinutes(i).Ticks,
|
||||
100.0, 200.0, 50.0, 150.0, 1000.0
|
||||
);
|
||||
hlv.Update(bar);
|
||||
}
|
||||
|
||||
Assert.True(double.IsFinite(hlv.Last.Value));
|
||||
Assert.True(hlv.Last.Value > 0, "High volatility should produce positive value");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Validates handling of constant bars (zero volatility).
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public void Hlv_ConstantBars_ProducesMinimalVolatility()
|
||||
{
|
||||
var hlv = new Hlv(14);
|
||||
|
||||
for (int i = 0; i < 30; i++)
|
||||
{
|
||||
var bar = new TBar(
|
||||
DateTime.UtcNow.AddMinutes(i).Ticks,
|
||||
100.0, 100.0, 100.0, 100.0, 1000.0
|
||||
);
|
||||
hlv.Update(bar);
|
||||
}
|
||||
|
||||
Assert.True(double.IsFinite(hlv.Last.Value));
|
||||
Assert.True(hlv.Last.Value < 0.001, "Constant price should produce near-zero volatility");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Validates warmup period calculation.
|
||||
/// </summary>
|
||||
[Theory]
|
||||
[InlineData(10)]
|
||||
[InlineData(14)]
|
||||
[InlineData(20)]
|
||||
public void Hlv_WarmupPeriod_IsCorrect(int period)
|
||||
{
|
||||
var hlv = new Hlv(period);
|
||||
Assert.Equal(period, hlv.WarmupPeriod);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Validates output is always non-negative (volatility property).
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public void Hlv_Output_IsNonNegative()
|
||||
{
|
||||
var bars = GenerateTestData(100);
|
||||
var hlv = new Hlv(14);
|
||||
|
||||
for (int i = 0; i < bars.Count; i++)
|
||||
{
|
||||
hlv.Update(bars[i]);
|
||||
if (hlv.IsHot)
|
||||
{
|
||||
Assert.True(hlv.Last.Value >= 0,
|
||||
$"Volatility should be non-negative at bar {i}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Validates bar correction works correctly.
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public void Hlv_BarCorrection_WorksCorrectly()
|
||||
{
|
||||
var hlv = new Hlv(14);
|
||||
var bars = GenerateTestData(30);
|
||||
|
||||
// Feed initial bars
|
||||
for (int i = 0; i < 20; i++)
|
||||
{
|
||||
hlv.Update(bars[i], isNew: true);
|
||||
}
|
||||
|
||||
// Add new bar
|
||||
hlv.Update(bars[20], isNew: true);
|
||||
double afterNew = hlv.Last.Value;
|
||||
|
||||
// Correct with different bar (much higher volatility)
|
||||
var correctedBar = new TBar(
|
||||
bars[20].Time,
|
||||
100, 200, 50, 150, 1000
|
||||
);
|
||||
hlv.Update(correctedBar, isNew: false);
|
||||
double afterCorrection = hlv.Last.Value;
|
||||
|
||||
// Restore original
|
||||
hlv.Update(bars[20], isNew: false);
|
||||
double afterRestore = hlv.Last.Value;
|
||||
|
||||
Assert.NotEqual(afterNew, afterCorrection);
|
||||
Assert.Equal(afterNew, afterRestore, 10);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Validates iterative corrections converge to same result.
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public void Hlv_IterativeCorrections_Converge()
|
||||
{
|
||||
var hlv = new Hlv(14);
|
||||
var bars = GenerateTestData(30);
|
||||
|
||||
// Feed bars and make corrections
|
||||
for (int i = 0; i < 20; i++)
|
||||
{
|
||||
hlv.Update(bars[i], isNew: true);
|
||||
}
|
||||
|
||||
// Multiple corrections on same bar
|
||||
for (int j = 0; j < 5; j++)
|
||||
{
|
||||
var tempBar = new TBar(
|
||||
bars[19].Time,
|
||||
100 + j, 110 + j, 90 + j, 105 + j, 1000
|
||||
);
|
||||
hlv.Update(tempBar, isNew: false);
|
||||
}
|
||||
|
||||
// Final correction back to original
|
||||
hlv.Update(bars[19], isNew: false);
|
||||
double afterCorrections = hlv.Last.Value;
|
||||
|
||||
// Fresh calculation
|
||||
var hlvFresh = new Hlv(14);
|
||||
for (int i = 0; i < 20; i++)
|
||||
{
|
||||
hlvFresh.Update(bars[i], isNew: true);
|
||||
}
|
||||
double freshValue = hlvFresh.Last.Value;
|
||||
|
||||
Assert.Equal(freshValue, afterCorrections, 10);
|
||||
}
|
||||
|
||||
// === Comparison with Theoretical Properties ===
|
||||
|
||||
/// <summary>
|
||||
/// Validates HLV stability over repeated runs with same seed.
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public void Hlv_Stability_ConsistentOverRepeatedRuns()
|
||||
{
|
||||
// Multiple runs with same seed should produce identical results
|
||||
var results = new List<double>();
|
||||
|
||||
for (int run = 0; run < 3; run++)
|
||||
{
|
||||
var gbm = new GBM(seed: 42);
|
||||
var bars = gbm.Fetch(100, DateTime.UtcNow.Ticks, TimeSpan.FromMinutes(1));
|
||||
var hlv = new Hlv(14);
|
||||
|
||||
for (int i = 0; i < bars.Count; i++)
|
||||
{
|
||||
hlv.Update(bars[i]);
|
||||
}
|
||||
results.Add(hlv.Last.Value);
|
||||
}
|
||||
|
||||
// All runs should be identical
|
||||
Assert.Equal(results[0], results[1], 15);
|
||||
Assert.Equal(results[1], results[2], 15);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Validates HLV responds to volatility regime changes.
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public void Hlv_RespondsToVolatilityRegimeChange()
|
||||
{
|
||||
var hlv = new Hlv(10);
|
||||
|
||||
// Low volatility regime
|
||||
for (int i = 0; i < 20; i++)
|
||||
{
|
||||
var bar = new TBar(
|
||||
DateTime.UtcNow.AddMinutes(i).Ticks,
|
||||
100.0, 101.0, 99.0, 100.0, 1000.0 // 2% range
|
||||
);
|
||||
hlv.Update(bar);
|
||||
}
|
||||
double lowVolValue = hlv.Last.Value;
|
||||
|
||||
// High volatility regime
|
||||
for (int i = 20; i < 40; i++)
|
||||
{
|
||||
var bar = new TBar(
|
||||
DateTime.UtcNow.AddMinutes(i).Ticks,
|
||||
100.0, 110.0, 90.0, 100.0, 1000.0 // 20% range
|
||||
);
|
||||
hlv.Update(bar);
|
||||
}
|
||||
double highVolValue = hlv.Last.Value;
|
||||
|
||||
Assert.True(highVolValue > lowVolValue * 2,
|
||||
"HLV should significantly increase with higher volatility regime");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Validates HLV vs GKV: same range, HLV ignores O-C while GKV uses it.
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public void Hlv_VsGkv_DifferentBehavior()
|
||||
{
|
||||
var hlv = new Hlv(14, annualize: false);
|
||||
var gkv = new Gkv(14, annualize: false);
|
||||
|
||||
// Same bars
|
||||
for (int i = 0; i < 30; i++)
|
||||
{
|
||||
// Directional bar (O != C)
|
||||
var bar = new TBar(
|
||||
DateTime.UtcNow.AddMinutes(i).Ticks,
|
||||
100.0, 105.0, 95.0, 104.0, 1000.0
|
||||
);
|
||||
hlv.Update(bar);
|
||||
gkv.Update(bar);
|
||||
}
|
||||
|
||||
// Both should produce positive values
|
||||
Assert.True(hlv.Last.Value > 0);
|
||||
Assert.True(gkv.Last.Value > 0);
|
||||
|
||||
// They should be different since GKV uses O-C term
|
||||
Assert.NotEqual(hlv.Last.Value, gkv.Last.Value);
|
||||
}
|
||||
|
||||
// === Efficiency Comparison ===
|
||||
|
||||
/// <summary>
|
||||
/// Validates Parkinson efficiency factor is approximately 5.2x close-to-close.
|
||||
/// This is a theoretical property - we just verify HLV produces reasonable values.
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public void Hlv_ProducesReasonableVolatilityEstimate()
|
||||
{
|
||||
var bars = GenerateTestData(100);
|
||||
var hlv = new Hlv(14, annualize: false);
|
||||
|
||||
for (int i = 0; i < bars.Count; i++)
|
||||
{
|
||||
hlv.Update(bars[i]);
|
||||
}
|
||||
|
||||
// HLV should be positive and finite
|
||||
Assert.True(double.IsFinite(hlv.Last.Value));
|
||||
Assert.True(hlv.Last.Value > 0);
|
||||
Assert.True(hlv.Last.Value < 10, "Raw volatility should be reasonable (< 1000%)");
|
||||
}
|
||||
|
||||
// === Helper Methods ===
|
||||
|
||||
private static double ComputeParkinsonEstimator(double high, double low)
|
||||
{
|
||||
double lnH = Math.Log(high);
|
||||
double lnL = Math.Log(low);
|
||||
double coeff = 1.0 / (4.0 * Math.Log(2));
|
||||
return coeff * Math.Pow(lnH - lnL, 2);
|
||||
}
|
||||
|
||||
private static double Variance(List<double> values)
|
||||
{
|
||||
if (values.Count == 0)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
double mean = values.Average();
|
||||
return values.Average(v => Math.Pow(v - mean, 2));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user