mirror of
https://github.com/mihakralj/QuanTAlib.git
synced 2026-08-25 13:58:04 +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,176 @@
|
||||
using TradingPlatform.BusinessLayer;
|
||||
using QuanTAlib;
|
||||
|
||||
namespace QuanTAlib.Tests;
|
||||
|
||||
public class JvoltyIndicatorTests
|
||||
{
|
||||
[Fact]
|
||||
public void JvoltyIndicator_Constructor_SetsDefaults()
|
||||
{
|
||||
var indicator = new JvoltyIndicator();
|
||||
|
||||
Assert.Equal(14, indicator.Period);
|
||||
Assert.Equal(SourceType.Close, indicator.Source);
|
||||
Assert.True(indicator.ShowColdValues);
|
||||
Assert.Equal("JVOLTY - Jurik Volatility", indicator.Name);
|
||||
Assert.True(indicator.SeparateWindow);
|
||||
Assert.True(indicator.OnBackGround);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void JvoltyIndicator_ShortName_IncludesParameters()
|
||||
{
|
||||
var indicator = new JvoltyIndicator { Period = 20 };
|
||||
Assert.Contains("JVOLTY", indicator.ShortName, StringComparison.Ordinal);
|
||||
Assert.Contains("20", indicator.ShortName, StringComparison.Ordinal);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void JvoltyIndicator_MinHistoryDepths_EqualsZero()
|
||||
{
|
||||
var indicator = new JvoltyIndicator();
|
||||
|
||||
Assert.Equal(0, JvoltyIndicator.MinHistoryDepths);
|
||||
Assert.Equal(0, ((IWatchlistIndicator)indicator).MinHistoryDepths);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void JvoltyIndicator_Initialize_CreatesInternalJvolty()
|
||||
{
|
||||
var indicator = new JvoltyIndicator();
|
||||
|
||||
// Initialize should not throw
|
||||
indicator.Initialize();
|
||||
|
||||
// After init, line series should exist
|
||||
Assert.Single(indicator.LinesSeries);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void JvoltyIndicator_ProcessUpdate_HistoricalBar_ComputesValue()
|
||||
{
|
||||
var indicator = new JvoltyIndicator { Period = 5 };
|
||||
indicator.Initialize();
|
||||
|
||||
// Add historical data with volatility
|
||||
var now = DateTime.UtcNow;
|
||||
for (int i = 0; i < 30; i++)
|
||||
{
|
||||
double basePrice = 100 + i * 2 + (i % 2 == 0 ? 5 : -5); // Add some volatility
|
||||
indicator.HistoricalData.AddBar(now.AddMinutes(i), basePrice, basePrice + 5, basePrice - 5, basePrice + 2, 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 >= 1.0); // Jvolty minimum is 1.0
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void JvoltyIndicator_ProcessUpdate_NewBar_ComputesValue()
|
||||
{
|
||||
var indicator = new JvoltyIndicator { Period = 5 };
|
||||
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
|
||||
indicator.HistoricalData.AddBar(now.AddMinutes(30), 120, 128, 115, 125, 1500);
|
||||
indicator.ProcessUpdate(new UpdateArgs(UpdateReason.NewBar));
|
||||
|
||||
Assert.Equal(2, indicator.LinesSeries[0].Count);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void JvoltyIndicator_DifferentPeriods_Work()
|
||||
{
|
||||
int[] periods = { 5, 10, 14, 20, 50 };
|
||||
|
||||
foreach (var period in periods)
|
||||
{
|
||||
var indicator = new JvoltyIndicator { Period = period };
|
||||
indicator.Initialize();
|
||||
|
||||
var now = DateTime.UtcNow;
|
||||
for (int i = 0; i < 60; i++)
|
||||
{
|
||||
double basePrice = 100 + i + (i % 3 == 0 ? 10 : -5); // Add volatility
|
||||
indicator.HistoricalData.AddBar(now.AddMinutes(i), basePrice, basePrice + 5, basePrice - 5, basePrice + 2, 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 >= 1.0, $"Period {period} should produce Jvolty >= 1.0");
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void JvoltyIndicator_DifferentSourceTypes_Work()
|
||||
{
|
||||
SourceType[] sources = { SourceType.Close, SourceType.High, SourceType.Low, SourceType.HL2, SourceType.HLC3 };
|
||||
|
||||
foreach (var source in sources)
|
||||
{
|
||||
var indicator = new JvoltyIndicator { Source = source };
|
||||
indicator.Initialize();
|
||||
|
||||
var now = DateTime.UtcNow;
|
||||
for (int i = 0; i < 40; 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));
|
||||
}
|
||||
|
||||
double val = indicator.LinesSeries[0].GetValue(0);
|
||||
Assert.True(double.IsFinite(val), $"Source {source} should produce finite value");
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void JvoltyIndicator_Period_CanBeChanged()
|
||||
{
|
||||
var indicator = new JvoltyIndicator();
|
||||
Assert.Equal(14, indicator.Period);
|
||||
|
||||
indicator.Period = 20;
|
||||
Assert.Equal(20, indicator.Period);
|
||||
|
||||
indicator.Period = 50;
|
||||
Assert.Equal(50, indicator.Period);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void JvoltyIndicator_ShowColdValues_CanBeToggled()
|
||||
{
|
||||
var indicator = new JvoltyIndicator();
|
||||
Assert.True(indicator.ShowColdValues);
|
||||
|
||||
indicator.ShowColdValues = false;
|
||||
Assert.False(indicator.ShowColdValues);
|
||||
|
||||
indicator.ShowColdValues = true;
|
||||
Assert.True(indicator.ShowColdValues);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void JvoltyIndicator_SourceCodeLink_IsValid()
|
||||
{
|
||||
var indicator = new JvoltyIndicator();
|
||||
Assert.Contains("github.com", indicator.SourceCodeLink, StringComparison.Ordinal);
|
||||
Assert.Contains("Jvolty.Quantower.cs", indicator.SourceCodeLink, StringComparison.Ordinal);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,653 @@
|
||||
namespace QuanTAlib.Tests;
|
||||
|
||||
public class JvoltyTests
|
||||
{
|
||||
private const double Tolerance = 1e-9;
|
||||
|
||||
private static TSeries GenerateTestData(int count = 100)
|
||||
{
|
||||
var gbm = new GBM(seed: 42);
|
||||
var bars = gbm.Fetch(count, DateTime.UtcNow.Ticks, TimeSpan.FromMinutes(1));
|
||||
var series = new TSeries(count);
|
||||
for (int i = 0; i < bars.Count; i++)
|
||||
{
|
||||
series.Add(new TValue(bars[i].Time, bars[i].Close));
|
||||
}
|
||||
return series;
|
||||
}
|
||||
|
||||
// ============== Constructor & Parameter Validation ==============
|
||||
|
||||
[Fact]
|
||||
public void Constructor_ValidatesInput()
|
||||
{
|
||||
Assert.Throws<ArgumentOutOfRangeException>(() => new Jvolty(0));
|
||||
Assert.Throws<ArgumentOutOfRangeException>(() => new Jvolty(-1));
|
||||
|
||||
var jvolty = new Jvolty(10);
|
||||
Assert.NotNull(jvolty);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Constructor_SetsCorrectName()
|
||||
{
|
||||
var jvolty = new Jvolty(7);
|
||||
Assert.Equal("Jvolty(7)", jvolty.Name);
|
||||
Assert.True(jvolty.WarmupPeriod > 0);
|
||||
|
||||
var jvolty2 = new Jvolty(14);
|
||||
Assert.Equal("Jvolty(14)", jvolty2.Name);
|
||||
}
|
||||
|
||||
// ============== Basic Functionality ==============
|
||||
|
||||
[Fact]
|
||||
public void BasicCalculation_DoesNotCrash()
|
||||
{
|
||||
var jvolty = new Jvolty(10);
|
||||
var series = GenerateTestData(100);
|
||||
|
||||
foreach (var value in series)
|
||||
{
|
||||
jvolty.Update(value);
|
||||
}
|
||||
|
||||
Assert.True(double.IsFinite(jvolty.Last.Value));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Calc_ReturnsValue()
|
||||
{
|
||||
var jvolty = new Jvolty(10);
|
||||
var input = new TValue(DateTime.UtcNow, 100.0);
|
||||
|
||||
Assert.InRange(jvolty.Last.Value, -Tolerance, Tolerance); // Initially zero
|
||||
|
||||
TValue result = jvolty.Update(input);
|
||||
|
||||
Assert.True(result.Value >= 1.0); // Minimum volatility is 1.0
|
||||
Assert.Equal(result.Value, jvolty.Last.Value, Tolerance);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void FirstValue_ReturnsMinimumVolatility()
|
||||
{
|
||||
var jvolty = new Jvolty(10);
|
||||
var input = new TValue(DateTime.UtcNow, 100.0);
|
||||
|
||||
TValue result = jvolty.Update(input);
|
||||
|
||||
Assert.Equal(1.0, result.Value, Tolerance); // First bar returns minimum volatility
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Properties_Accessible()
|
||||
{
|
||||
var jvolty = new Jvolty(10);
|
||||
|
||||
Assert.InRange(jvolty.Last.Value, -Tolerance, Tolerance); // Initially zero
|
||||
Assert.False(jvolty.IsHot);
|
||||
Assert.Contains("Jvolty", jvolty.Name, StringComparison.Ordinal);
|
||||
Assert.True(jvolty.WarmupPeriod > 0);
|
||||
|
||||
var input = new TValue(DateTime.UtcNow, 100.0);
|
||||
jvolty.Update(input);
|
||||
|
||||
Assert.True(Math.Abs(jvolty.Last.Value) > Tolerance); // No longer zero
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void BandProperties_Accessible()
|
||||
{
|
||||
var jvolty = new Jvolty(10);
|
||||
var input = new TValue(DateTime.UtcNow, 100.0);
|
||||
|
||||
jvolty.Update(input);
|
||||
|
||||
// After first bar, bands should be initialized to the input value
|
||||
Assert.Equal(100.0, jvolty.UpperBand, Tolerance);
|
||||
Assert.Equal(100.0, jvolty.LowerBand, Tolerance);
|
||||
}
|
||||
|
||||
// ============== State Management & Bar Correction ==============
|
||||
|
||||
[Fact]
|
||||
public void Calc_IsNew_AcceptsParameter()
|
||||
{
|
||||
var jvolty = new Jvolty(10);
|
||||
var series = GenerateTestData(50);
|
||||
|
||||
// Feed enough values to build up volatility history
|
||||
for (int i = 0; i < 49; i++)
|
||||
{
|
||||
jvolty.Update(series[i], isNew: true);
|
||||
}
|
||||
double valueBefore = jvolty.Last.Value;
|
||||
|
||||
// Add one more value with isNew=true
|
||||
jvolty.Update(series[49], isNew: true);
|
||||
double valueAfter = jvolty.Last.Value;
|
||||
|
||||
// Both should be valid volatility values
|
||||
Assert.True(double.IsFinite(valueBefore));
|
||||
Assert.True(double.IsFinite(valueAfter));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Calc_IsNew_False_UpdatesValue()
|
||||
{
|
||||
var jvolty = new Jvolty(10);
|
||||
var series = GenerateTestData(50);
|
||||
|
||||
// Feed enough bars to have meaningful volatility
|
||||
for (int i = 0; i < 49; i++)
|
||||
{
|
||||
jvolty.Update(series[i], isNew: true);
|
||||
}
|
||||
|
||||
// Add one more value with isNew=true
|
||||
jvolty.Update(series[49], isNew: true);
|
||||
double beforeUpdate = jvolty.Last.Value;
|
||||
|
||||
// Update same bar with different value (isNew=false)
|
||||
var modifiedInput = new TValue(series[49].Time, series[49].Value + 50.0);
|
||||
jvolty.Update(modifiedInput, isNew: false);
|
||||
double afterUpdate = jvolty.Last.Value;
|
||||
|
||||
// Values should be different after the correction
|
||||
Assert.True(Math.Abs(beforeUpdate - afterUpdate) > Tolerance);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void IsNew_Consistency()
|
||||
{
|
||||
var jvolty = new Jvolty(10);
|
||||
var series = GenerateTestData(100);
|
||||
|
||||
// Feed first 99
|
||||
for (int i = 0; i < 99; i++)
|
||||
{
|
||||
jvolty.Update(series[i]);
|
||||
}
|
||||
|
||||
// Update with 100th point (isNew=true)
|
||||
jvolty.Update(series[99], true);
|
||||
|
||||
// Update with modified 100th point (isNew=false)
|
||||
var modifiedInput = new TValue(series[99].Time, series[99].Value + 50.0);
|
||||
double val2 = jvolty.Update(modifiedInput, false).Value;
|
||||
|
||||
// Create new instance and feed up to modified
|
||||
var jvolty2 = new Jvolty(10);
|
||||
for (int i = 0; i < 99; i++)
|
||||
{
|
||||
jvolty2.Update(series[i]);
|
||||
}
|
||||
double val3 = jvolty2.Update(modifiedInput, true).Value;
|
||||
|
||||
Assert.Equal(val3, val2, Tolerance);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void IterativeCorrections_RestoreToOriginalState()
|
||||
{
|
||||
var jvolty = new Jvolty(5);
|
||||
var series = GenerateTestData(20);
|
||||
|
||||
// Feed 10 new values
|
||||
TValue tenthValue = default;
|
||||
for (int i = 0; i < 10; i++)
|
||||
{
|
||||
tenthValue = series[i];
|
||||
jvolty.Update(tenthValue, isNew: true);
|
||||
}
|
||||
|
||||
// Remember state after 10 values
|
||||
double stateAfterTen = jvolty.Last.Value;
|
||||
|
||||
// Generate 9 corrections with isNew=false (different values)
|
||||
for (int i = 10; i < 19; i++)
|
||||
{
|
||||
jvolty.Update(series[i], isNew: false);
|
||||
}
|
||||
|
||||
// Feed the remembered 10th value again with isNew=false
|
||||
TValue finalResult = jvolty.Update(tenthValue, isNew: false);
|
||||
|
||||
// State should match the original state after 10 values
|
||||
Assert.Equal(stateAfterTen, finalResult.Value, Tolerance);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Reset_Works()
|
||||
{
|
||||
var jvolty = new Jvolty(10);
|
||||
var series = GenerateTestData(50);
|
||||
|
||||
foreach (var value in series)
|
||||
{
|
||||
jvolty.Update(value);
|
||||
}
|
||||
|
||||
double lastVal = jvolty.Last.Value;
|
||||
Assert.True(Math.Abs(lastVal) > Tolerance); // Not zero
|
||||
|
||||
jvolty.Reset();
|
||||
Assert.InRange(jvolty.Last.Value, -Tolerance, Tolerance); // Reset to zero
|
||||
Assert.False(jvolty.IsHot);
|
||||
|
||||
// After reset, should accept new values
|
||||
jvolty.Update(series[0]);
|
||||
Assert.True(Math.Abs(jvolty.Last.Value) > Tolerance); // No longer zero
|
||||
}
|
||||
|
||||
// ============== Warmup & Convergence ==============
|
||||
|
||||
[Fact]
|
||||
public void IsHot_BecomesTrueAfterWarmup()
|
||||
{
|
||||
var jvolty = new Jvolty(5);
|
||||
|
||||
Assert.False(jvolty.IsHot);
|
||||
|
||||
var series = GenerateTestData(200);
|
||||
|
||||
int steps = 0;
|
||||
while (!jvolty.IsHot && steps < series.Count)
|
||||
{
|
||||
jvolty.Update(series[steps]);
|
||||
steps++;
|
||||
}
|
||||
|
||||
Assert.True(jvolty.IsHot);
|
||||
Assert.True(steps > 0);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void WarmupPeriod_IsPositive()
|
||||
{
|
||||
var jvolty = new Jvolty(10);
|
||||
Assert.True(jvolty.WarmupPeriod > 0);
|
||||
|
||||
var jvolty2 = new Jvolty(20);
|
||||
Assert.True(jvolty2.WarmupPeriod > 0);
|
||||
|
||||
// WarmupPeriod should increase with the period parameter
|
||||
Assert.True(jvolty2.WarmupPeriod >= jvolty.WarmupPeriod);
|
||||
}
|
||||
|
||||
// ============== NaN/Infinity Handling ==============
|
||||
|
||||
[Fact]
|
||||
public void NaN_Input_UsesLastValidValue()
|
||||
{
|
||||
var jvolty = new Jvolty(5);
|
||||
|
||||
var input1 = new TValue(DateTime.UtcNow, 100.0);
|
||||
jvolty.Update(input1);
|
||||
|
||||
var input2 = new TValue(DateTime.UtcNow.AddMinutes(1), 110.0);
|
||||
jvolty.Update(input2);
|
||||
|
||||
// Feed NaN value
|
||||
var inputWithNaN = new TValue(DateTime.UtcNow.AddMinutes(2), double.NaN);
|
||||
var resultAfterNaN = jvolty.Update(inputWithNaN);
|
||||
|
||||
// Result should be finite
|
||||
Assert.True(double.IsFinite(resultAfterNaN.Value));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Infinity_Input_UsesLastValidValue()
|
||||
{
|
||||
var jvolty = new Jvolty(5);
|
||||
|
||||
var input1 = new TValue(DateTime.UtcNow, 100.0);
|
||||
jvolty.Update(input1);
|
||||
|
||||
var input2 = new TValue(DateTime.UtcNow.AddMinutes(1), 110.0);
|
||||
jvolty.Update(input2);
|
||||
|
||||
// Feed Infinity value
|
||||
var inputWithInf = new TValue(DateTime.UtcNow.AddMinutes(2), double.PositiveInfinity);
|
||||
var resultAfterInf = jvolty.Update(inputWithInf);
|
||||
|
||||
// Result should be finite
|
||||
Assert.True(double.IsFinite(resultAfterInf.Value));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void BatchNaN_Safe()
|
||||
{
|
||||
var jvolty = new Jvolty(5);
|
||||
var series = GenerateTestData(20);
|
||||
|
||||
// Feed some values
|
||||
for (int i = 0; i < 10; i++)
|
||||
{
|
||||
jvolty.Update(series[i]);
|
||||
}
|
||||
|
||||
// Feed multiple NaN values
|
||||
for (int i = 0; i < 5; i++)
|
||||
{
|
||||
var nanInput = new TValue(DateTime.UtcNow.AddMinutes(10 + i), double.NaN);
|
||||
var result = jvolty.Update(nanInput);
|
||||
Assert.True(double.IsFinite(result.Value));
|
||||
}
|
||||
}
|
||||
|
||||
// ============== Consistency Tests ==============
|
||||
|
||||
[Fact]
|
||||
public void BatchCalc_MatchesIterativeCalc()
|
||||
{
|
||||
var jvoltyIterative = new Jvolty(10);
|
||||
var series = GenerateTestData(100);
|
||||
|
||||
// Calculate iteratively
|
||||
var iterativeResults = new TSeries();
|
||||
foreach (var value in series)
|
||||
{
|
||||
iterativeResults.Add(jvoltyIterative.Update(value));
|
||||
}
|
||||
|
||||
// Calculate batch
|
||||
var batchResults = Jvolty.Batch(series, 10);
|
||||
|
||||
// Compare
|
||||
Assert.Equal(iterativeResults.Count, batchResults.Count);
|
||||
for (int i = 0; i < iterativeResults.Count; i++)
|
||||
{
|
||||
Assert.Equal(iterativeResults[i].Value, batchResults[i].Value, Tolerance);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void TSeries_Update_MatchesStreaming()
|
||||
{
|
||||
var jvolty1 = new Jvolty(10);
|
||||
var jvolty2 = new Jvolty(10);
|
||||
var series = GenerateTestData(100);
|
||||
|
||||
// Streaming
|
||||
foreach (var value in series)
|
||||
{
|
||||
jvolty1.Update(value);
|
||||
}
|
||||
|
||||
// Batch
|
||||
jvolty2.Update(series);
|
||||
|
||||
Assert.Equal(jvolty1.Last.Value, jvolty2.Last.Value, Tolerance);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SpanCalc_MatchesStreaming()
|
||||
{
|
||||
var jvolty = new Jvolty(10);
|
||||
var series = GenerateTestData(100);
|
||||
|
||||
// Stream all values first
|
||||
foreach (var value in series)
|
||||
{
|
||||
jvolty.Update(value);
|
||||
}
|
||||
double streamingLast = jvolty.Last.Value;
|
||||
|
||||
// Span calculation
|
||||
var output = new double[series.Count];
|
||||
Jvolty.Batch(series.Values, output, 10);
|
||||
|
||||
// Compare last value (after warmup)
|
||||
Assert.Equal(streamingLast, output[series.Count - 1], 1e-6);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Chainability_Works()
|
||||
{
|
||||
var jvolty = new Jvolty(10);
|
||||
var series = GenerateTestData(50);
|
||||
|
||||
var result = jvolty.Update(series);
|
||||
Assert.Equal(50, result.Count);
|
||||
Assert.Equal(jvolty.Last.Value, result.Last.Value);
|
||||
}
|
||||
|
||||
// ============== Static Batch Method ==============
|
||||
|
||||
[Fact]
|
||||
public void StaticBatch_Works()
|
||||
{
|
||||
var series = GenerateTestData(50);
|
||||
|
||||
var results = Jvolty.Batch(series, 10);
|
||||
|
||||
Assert.Equal(50, results.Count);
|
||||
Assert.True(double.IsFinite(results.Last.Value));
|
||||
}
|
||||
|
||||
// ============== Span API Tests ==============
|
||||
|
||||
[Fact]
|
||||
public void Calculate_ValidatesLengths()
|
||||
{
|
||||
var source = new double[10];
|
||||
var output = new double[5]; // Wrong size
|
||||
|
||||
var ex = Assert.Throws<ArgumentException>(() => Jvolty.Batch(source, output, 10));
|
||||
Assert.Equal("output", ex.ParamName);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Calculate_EmptySource_NoException()
|
||||
{
|
||||
var source = Array.Empty<double>();
|
||||
var output = Array.Empty<double>();
|
||||
|
||||
var exception = Record.Exception(() => Jvolty.Batch(source, output, 10));
|
||||
Assert.Null(exception);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Calculate_InvalidPeriod_ThrowsArgumentException()
|
||||
{
|
||||
var source = new double[10];
|
||||
var output = new double[10];
|
||||
|
||||
Assert.Throws<ArgumentOutOfRangeException>(() => Jvolty.Batch(source, output, 0));
|
||||
}
|
||||
|
||||
// ============== Edge Cases ==============
|
||||
|
||||
[Fact]
|
||||
public void SingleValue_ReturnsValidResult()
|
||||
{
|
||||
var jvolty = new Jvolty(10);
|
||||
var input = new TValue(DateTime.UtcNow, 100.0);
|
||||
|
||||
var result = jvolty.Update(input);
|
||||
|
||||
Assert.True(double.IsFinite(result.Value));
|
||||
Assert.Equal(1.0, result.Value, Tolerance); // First bar = minimum volatility
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Period1_Works()
|
||||
{
|
||||
var jvolty = new Jvolty(1);
|
||||
var series = GenerateTestData(10);
|
||||
|
||||
foreach (var value in series)
|
||||
{
|
||||
var result = jvolty.Update(value);
|
||||
Assert.True(double.IsFinite(result.Value));
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void FlatValues_MinimumVolatility()
|
||||
{
|
||||
var jvolty = new Jvolty(5);
|
||||
|
||||
// All values are the same
|
||||
for (int i = 0; i < 50; i++)
|
||||
{
|
||||
var input = new TValue(DateTime.UtcNow.AddMinutes(i), 100.0);
|
||||
jvolty.Update(input);
|
||||
}
|
||||
|
||||
// Volatility should be at minimum (1.0) for flat values
|
||||
Assert.Equal(1.0, jvolty.Last.Value, Tolerance);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void HighVolatility_IncreasesExponent()
|
||||
{
|
||||
var jvolty = new Jvolty(10);
|
||||
|
||||
// Start with stable values
|
||||
for (int i = 0; i < 20; i++)
|
||||
{
|
||||
var input = new TValue(DateTime.UtcNow.AddMinutes(i), 100.0 + (i * 0.1));
|
||||
jvolty.Update(input);
|
||||
}
|
||||
|
||||
double lowVolatility = jvolty.Last.Value;
|
||||
|
||||
// Create high volatility spike
|
||||
var spike = new TValue(DateTime.UtcNow.AddMinutes(21), 150.0);
|
||||
jvolty.Update(spike);
|
||||
|
||||
double highVolatility = jvolty.Last.Value;
|
||||
|
||||
// High volatility should be greater than low volatility
|
||||
Assert.True(highVolatility > lowVolatility);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Bands_TrackPrice()
|
||||
{
|
||||
var jvolty = new Jvolty(10);
|
||||
|
||||
// Feed increasing prices
|
||||
for (int i = 0; i < 20; i++)
|
||||
{
|
||||
var input = new TValue(DateTime.UtcNow.AddMinutes(i), 100.0 + i);
|
||||
jvolty.Update(input);
|
||||
}
|
||||
|
||||
// Upper band should track the highest recent prices
|
||||
Assert.True(jvolty.UpperBand > 100.0);
|
||||
// Lower band should lag behind due to adaptive decay
|
||||
Assert.True(jvolty.LowerBand < jvolty.UpperBand);
|
||||
}
|
||||
|
||||
// ============== Event Publishing ==============
|
||||
|
||||
[Fact]
|
||||
public void PubEvent_Fires()
|
||||
{
|
||||
var jvolty = new Jvolty(10);
|
||||
bool eventFired = false;
|
||||
|
||||
jvolty.Pub += (object? sender, in TValueEventArgs args) => eventFired = true;
|
||||
|
||||
var input = new TValue(DateTime.UtcNow, 100.0);
|
||||
jvolty.Update(input);
|
||||
|
||||
Assert.True(eventFired);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void EventChaining_Works()
|
||||
{
|
||||
var series = GenerateTestData(50);
|
||||
|
||||
var jvolty = new Jvolty(10);
|
||||
var sma = new Sma(jvolty, 5); // Chain SMA to Jvolty output
|
||||
|
||||
foreach (var value in series)
|
||||
{
|
||||
jvolty.Update(value);
|
||||
}
|
||||
|
||||
Assert.True(double.IsFinite(sma.Last.Value));
|
||||
}
|
||||
|
||||
// ============== Additional Tests ==============
|
||||
|
||||
[Fact]
|
||||
public void LargeDataset_Completes()
|
||||
{
|
||||
var jvolty = new Jvolty(20);
|
||||
var series = GenerateTestData(5000);
|
||||
|
||||
foreach (var value in series)
|
||||
{
|
||||
jvolty.Update(value);
|
||||
}
|
||||
|
||||
Assert.True(jvolty.IsHot);
|
||||
Assert.True(double.IsFinite(jvolty.Last.Value));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void DifferentPeriods_ProduceValidValues()
|
||||
{
|
||||
var series = GenerateTestData(200);
|
||||
|
||||
var jvolty1 = new Jvolty(5);
|
||||
var jvolty2 = new Jvolty(10);
|
||||
var jvolty3 = new Jvolty(20);
|
||||
|
||||
foreach (var value in series)
|
||||
{
|
||||
jvolty1.Update(value);
|
||||
jvolty2.Update(value);
|
||||
jvolty3.Update(value);
|
||||
}
|
||||
|
||||
Assert.True(double.IsFinite(jvolty1.Last.Value));
|
||||
Assert.True(double.IsFinite(jvolty2.Last.Value));
|
||||
Assert.True(double.IsFinite(jvolty3.Last.Value));
|
||||
Assert.True(jvolty1.Last.Value >= 1.0);
|
||||
Assert.True(jvolty2.Last.Value >= 1.0);
|
||||
Assert.True(jvolty3.Last.Value >= 1.0);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SourceChaining_Works()
|
||||
{
|
||||
var series = GenerateTestData(200);
|
||||
|
||||
// Create source TSeries that publishes events
|
||||
var sourceSeries = new TSeries();
|
||||
var jvolty = new Jvolty(sourceSeries, 10);
|
||||
|
||||
// Feed data through the source (need enough for warmup)
|
||||
foreach (var value in series)
|
||||
{
|
||||
sourceSeries.Add(value);
|
||||
}
|
||||
|
||||
// Should have valid output
|
||||
Assert.True(double.IsFinite(jvolty.Last.Value));
|
||||
Assert.True(jvolty.Last.Value >= 1.0); // Minimum volatility
|
||||
}
|
||||
|
||||
#pragma warning disable S2699 // Test contains Assert.True and Assert.InRange - analyzer false positive
|
||||
[Fact]
|
||||
public void Prime_Works()
|
||||
{
|
||||
var jvolty = new Jvolty(5);
|
||||
var values = new double[] { 100.0, 101.0, 99.5, 102.0, 98.0, 103.0 };
|
||||
|
||||
jvolty.Prime(values);
|
||||
|
||||
double lastValue = jvolty.Last.Value;
|
||||
Assert.True(double.IsFinite(lastValue), "Last value should be finite after Prime");
|
||||
Assert.InRange(lastValue, 1.0, double.MaxValue); // Volatility >= minimum (1.0)
|
||||
}
|
||||
#pragma warning restore S2699
|
||||
}
|
||||
@@ -0,0 +1,183 @@
|
||||
// Jvolty: Mathematical property validation tests
|
||||
// Jvolty is a proprietary Jurik Research indicator — no external library equivalents exist.
|
||||
// Validation uses mathematical property testing against known volatility band behaviors.
|
||||
|
||||
namespace QuanTAlib.Tests;
|
||||
|
||||
using Xunit;
|
||||
|
||||
public class JvoltyValidationTests
|
||||
{
|
||||
private const int DefaultPeriod = 10;
|
||||
private const int TestDataLength = 500;
|
||||
|
||||
[Fact]
|
||||
public void Jvolty_Output_IsFiniteForGbmData()
|
||||
{
|
||||
var series = new GBM(sigma: 0.5, seed: 123).Fetch(TestDataLength, DateTime.UtcNow.Ticks, TimeSpan.FromMinutes(1)).Close;
|
||||
var jvolty = new Jvolty(DefaultPeriod);
|
||||
|
||||
for (int i = 0; i < series.Count; i++)
|
||||
{
|
||||
var result = jvolty.Update(series[i], isNew: true);
|
||||
Assert.True(double.IsFinite(result.Value),
|
||||
$"Jvolty output must be finite at bar {i}, got {result.Value}");
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Jvolty_Output_IsPositive_AfterWarmup()
|
||||
{
|
||||
var series = new GBM(sigma: 0.5, seed: 123).Fetch(TestDataLength, DateTime.UtcNow.Ticks, TimeSpan.FromMinutes(1)).Close;
|
||||
var jvolty = new Jvolty(DefaultPeriod);
|
||||
|
||||
for (int i = 0; i < series.Count; i++)
|
||||
{
|
||||
var result = jvolty.Update(series[i], isNew: true);
|
||||
if (jvolty.IsHot)
|
||||
{
|
||||
Assert.True(result.Value >= 1.0,
|
||||
$"Jvolty output must be >= 1.0 after warmup at bar {i}, got {result.Value}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Jvolty_ConstantSeries_MinimumVolatility()
|
||||
{
|
||||
var jvolty = new Jvolty(DefaultPeriod);
|
||||
double price = 100.0;
|
||||
|
||||
// Feed constant-price values
|
||||
for (int i = 0; i < 300; i++)
|
||||
{
|
||||
jvolty.Update(new TValue(DateTime.UtcNow.AddMinutes(i), price), isNew: true);
|
||||
}
|
||||
|
||||
// Constant series should produce minimum volatility (d = 1.0)
|
||||
Assert.Equal(1.0, jvolty.Last.Value, precision: 1);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Jvolty_UpperBand_GreaterOrEqualLowerBand()
|
||||
{
|
||||
var series = new GBM(sigma: 0.5, seed: 123).Fetch(TestDataLength, DateTime.UtcNow.Ticks, TimeSpan.FromMinutes(1)).Close;
|
||||
var jvolty = new Jvolty(DefaultPeriod);
|
||||
|
||||
for (int i = 0; i < series.Count; i++)
|
||||
{
|
||||
jvolty.Update(series[i], isNew: true);
|
||||
|
||||
Assert.True(jvolty.UpperBand >= jvolty.LowerBand,
|
||||
$"UpperBand ({jvolty.UpperBand}) must be >= LowerBand ({jvolty.LowerBand}) at bar {i}");
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Jvolty_HighVolatility_ProducesHigherExponent()
|
||||
{
|
||||
// Low volatility data
|
||||
var lowVolSeries = new GBM(sigma: 0.01, seed: 123).Fetch(300, DateTime.UtcNow.Ticks, TimeSpan.FromMinutes(1)).Close;
|
||||
var lowJvolty = new Jvolty(DefaultPeriod);
|
||||
for (int i = 0; i < lowVolSeries.Count; i++)
|
||||
{
|
||||
lowJvolty.Update(lowVolSeries[i], isNew: true);
|
||||
}
|
||||
double lowVolResult = lowJvolty.Last.Value;
|
||||
|
||||
// High volatility data
|
||||
var highVolSeries = new GBM(sigma: 2.0, seed: 123).Fetch(300, DateTime.UtcNow.Ticks, TimeSpan.FromMinutes(1)).Close;
|
||||
var highJvolty = new Jvolty(DefaultPeriod);
|
||||
for (int i = 0; i < highVolSeries.Count; i++)
|
||||
{
|
||||
highJvolty.Update(highVolSeries[i], isNew: true);
|
||||
}
|
||||
double highVolResult = highJvolty.Last.Value;
|
||||
|
||||
// High volatility data should generally produce higher exponent values
|
||||
// (This is a statistical property, not guaranteed per-sample)
|
||||
Assert.True(highVolResult >= 1.0, "High vol result should be >= 1.0");
|
||||
Assert.True(lowVolResult >= 1.0, "Low vol result should be >= 1.0");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Jvolty_BatchAndStreaming_ProduceSameResults()
|
||||
{
|
||||
var series = new GBM(sigma: 0.5, seed: 123).Fetch(TestDataLength, DateTime.UtcNow.Ticks, TimeSpan.FromMinutes(1)).Close;
|
||||
|
||||
// Batch
|
||||
var batchResults = Jvolty.Batch(series, DefaultPeriod);
|
||||
|
||||
// Streaming
|
||||
var streamJvolty = new Jvolty(DefaultPeriod);
|
||||
var streamResults = new double[series.Count];
|
||||
for (int i = 0; i < series.Count; i++)
|
||||
{
|
||||
var result = streamJvolty.Update(series[i], isNew: true);
|
||||
streamResults[i] = result.Value;
|
||||
}
|
||||
|
||||
Assert.Equal(batchResults.Count, series.Count);
|
||||
for (int i = 0; i < series.Count; i++)
|
||||
{
|
||||
Assert.Equal(batchResults.Values[i], streamResults[i], precision: 10);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Jvolty_SpanAndStreaming_ProduceSameResults()
|
||||
{
|
||||
var series = new GBM(sigma: 0.5, seed: 123).Fetch(TestDataLength, DateTime.UtcNow.Ticks, TimeSpan.FromMinutes(1)).Close;
|
||||
var spanOutput = new double[series.Count];
|
||||
|
||||
Jvolty.Batch(series.Values, spanOutput, DefaultPeriod);
|
||||
|
||||
// Streaming
|
||||
var streamJvolty = new Jvolty(DefaultPeriod);
|
||||
for (int i = 0; i < series.Count; i++)
|
||||
{
|
||||
streamJvolty.Update(series[i], isNew: true);
|
||||
Assert.Equal(spanOutput[i], streamJvolty.Last.Value, precision: 10);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Jvolty_DifferentPeriods_ProduceDifferentResults()
|
||||
{
|
||||
var series = new GBM(sigma: 0.5, seed: 123).Fetch(200, DateTime.UtcNow.Ticks, TimeSpan.FromMinutes(1)).Close;
|
||||
|
||||
var jvolty5 = new Jvolty(5);
|
||||
var jvolty50 = new Jvolty(50);
|
||||
|
||||
for (int i = 0; i < series.Count; i++)
|
||||
{
|
||||
jvolty5.Update(series[i], isNew: true);
|
||||
jvolty50.Update(series[i], isNew: true);
|
||||
}
|
||||
|
||||
// Different periods should produce different results
|
||||
Assert.NotEqual(jvolty5.Last.Value, jvolty50.Last.Value);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Jvolty_BarCorrection_IsNewFalse_RestoresState()
|
||||
{
|
||||
var series = new GBM(sigma: 0.5, seed: 123).Fetch(50, DateTime.UtcNow.Ticks, TimeSpan.FromMinutes(1)).Close;
|
||||
var jvolty = new Jvolty(DefaultPeriod);
|
||||
|
||||
// Process 30 bars
|
||||
for (int i = 0; i < 30; i++)
|
||||
{
|
||||
jvolty.Update(series[i], isNew: true);
|
||||
}
|
||||
|
||||
// Update bar 30 (isNew=true) then correct it (isNew=false)
|
||||
jvolty.Update(series[30], isNew: true);
|
||||
double afterNew = jvolty.Last.Value;
|
||||
|
||||
jvolty.Update(series[30], isNew: false);
|
||||
double afterCorrection = jvolty.Last.Value;
|
||||
|
||||
Assert.Equal(afterNew, afterCorrection, precision: 10);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user