mirror of
https://github.com/mihakralj/QuanTAlib.git
synced 2026-08-01 03:07:43 +00:00
86fe32a682
Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com> Co-authored-by: aider (openrouter/anthropic/claude-sonnet-4) <aider@aider.chat> Co-authored-by: Warp <agent@warp.dev>
231 lines
7.2 KiB
C#
231 lines
7.2 KiB
C#
using TradingPlatform.BusinessLayer;
|
|
|
|
namespace QuanTAlib.Quantower.Tests;
|
|
|
|
public class VarianceIndicatorTests
|
|
{
|
|
[Fact]
|
|
public void VarianceIndicator_Constructor_SetsDefaults()
|
|
{
|
|
var indicator = new VarianceIndicator();
|
|
|
|
Assert.Equal(20, indicator.Period);
|
|
Assert.False(indicator.IsPopulation);
|
|
Assert.Equal(SourceType.Close, indicator.Source);
|
|
Assert.True(indicator.ShowColdValues);
|
|
Assert.Equal("Variance - Rolling Variance", indicator.Name);
|
|
Assert.True(indicator.SeparateWindow);
|
|
Assert.True(indicator.OnBackGround);
|
|
}
|
|
|
|
[Fact]
|
|
public void VarianceIndicator_MinHistoryDepths_EqualsZero()
|
|
{
|
|
var indicator = new VarianceIndicator();
|
|
|
|
Assert.Equal(0, VarianceIndicator.MinHistoryDepths);
|
|
Assert.Equal(0, ((IWatchlistIndicator)indicator).MinHistoryDepths);
|
|
}
|
|
|
|
[Fact]
|
|
public void VarianceIndicator_ShortName_IncludesPeriod()
|
|
{
|
|
var indicator = new VarianceIndicator { Period = 14 };
|
|
|
|
Assert.True(indicator.ShortName.Contains("Variance", StringComparison.Ordinal));
|
|
Assert.True(indicator.ShortName.Contains("14", StringComparison.Ordinal));
|
|
}
|
|
|
|
[Fact]
|
|
public void VarianceIndicator_Initialize_CreatesInternalVariance()
|
|
{
|
|
var indicator = new VarianceIndicator { Period = 10 };
|
|
|
|
// Initialize should not throw
|
|
indicator.Initialize();
|
|
|
|
// After init, line series should exist
|
|
Assert.Single(indicator.LinesSeries);
|
|
}
|
|
|
|
[Fact]
|
|
public void VarianceIndicator_ProcessUpdate_HistoricalBar_ComputesValue()
|
|
{
|
|
var indicator = new VarianceIndicator { Period = 5 };
|
|
indicator.Initialize();
|
|
|
|
// Add historical data
|
|
var now = DateTime.UtcNow;
|
|
indicator.HistoricalData.AddBar(now, 100, 105, 95, 102);
|
|
|
|
// Process update
|
|
var args = new UpdateArgs(UpdateReason.HistoricalBar);
|
|
indicator.ProcessUpdate(args);
|
|
|
|
// Line series should have a value
|
|
Assert.Equal(1, indicator.LinesSeries[0].Count);
|
|
Assert.True(double.IsFinite(indicator.LinesSeries[0].GetValue(0)));
|
|
}
|
|
|
|
[Fact]
|
|
public void VarianceIndicator_ProcessUpdate_NewBar_ComputesValue()
|
|
{
|
|
var indicator = new VarianceIndicator { Period = 5 };
|
|
indicator.Initialize();
|
|
|
|
var now = DateTime.UtcNow;
|
|
indicator.HistoricalData.AddBar(now, 100, 105, 95, 102);
|
|
indicator.HistoricalData.AddBar(now.AddMinutes(1), 102, 108, 100, 106);
|
|
|
|
indicator.ProcessUpdate(new UpdateArgs(UpdateReason.HistoricalBar));
|
|
indicator.ProcessUpdate(new UpdateArgs(UpdateReason.NewBar));
|
|
|
|
Assert.Equal(2, indicator.LinesSeries[0].Count);
|
|
}
|
|
|
|
[Fact]
|
|
public void VarianceIndicator_ProcessUpdate_NewTick_ProcessesWithoutError()
|
|
{
|
|
var indicator = new VarianceIndicator { Period = 5 };
|
|
indicator.Initialize();
|
|
|
|
// Should not throw an exception
|
|
indicator.ProcessUpdate(new UpdateArgs(UpdateReason.NewTick));
|
|
|
|
// Assert that the indicator still exists (method completed without exception)
|
|
Assert.NotNull(indicator);
|
|
}
|
|
|
|
[Fact]
|
|
public void VarianceIndicator_MultipleUpdates_ProducesCorrectSequence()
|
|
{
|
|
var indicator = new VarianceIndicator { Period = 5 };
|
|
indicator.Initialize();
|
|
|
|
var now = DateTime.UtcNow;
|
|
double[] closes = { 100, 102, 105, 103, 107, 110 };
|
|
|
|
foreach (var close in closes)
|
|
{
|
|
indicator.HistoricalData.AddBar(now, close, close + 2, close - 2, close);
|
|
indicator.ProcessUpdate(new UpdateArgs(UpdateReason.HistoricalBar));
|
|
now = now.AddMinutes(1);
|
|
}
|
|
|
|
// All values should be finite
|
|
for (int i = 0; i < closes.Length; i++)
|
|
{
|
|
Assert.True(double.IsFinite(indicator.LinesSeries[0].GetValue(closes.Length - 1 - i)));
|
|
}
|
|
}
|
|
|
|
[Fact]
|
|
public void VarianceIndicator_DifferentSourceTypes_Work()
|
|
{
|
|
var sources = new[] { SourceType.Open, SourceType.High, SourceType.Low, SourceType.Close, SourceType.HL2, SourceType.HLC3 };
|
|
|
|
foreach (var source in sources)
|
|
{
|
|
var indicator = new VarianceIndicator { Period = 5, Source = source };
|
|
indicator.Initialize();
|
|
|
|
var now = DateTime.UtcNow;
|
|
indicator.HistoricalData.AddBar(now, 100, 110, 90, 105);
|
|
indicator.ProcessUpdate(new UpdateArgs(UpdateReason.HistoricalBar));
|
|
|
|
Assert.True(double.IsFinite(indicator.LinesSeries[0].GetValue(0)),
|
|
$"Source {source} should produce finite value");
|
|
}
|
|
}
|
|
|
|
[Fact]
|
|
public void VarianceIndicator_Period_CanBeChanged()
|
|
{
|
|
var indicator = new VarianceIndicator { Period = 10 };
|
|
|
|
Assert.Equal(10, indicator.Period);
|
|
|
|
indicator.Period = 20;
|
|
Assert.Equal(20, indicator.Period);
|
|
}
|
|
|
|
[Fact]
|
|
public void VarianceIndicator_IsPopulation_CanBeChanged()
|
|
{
|
|
var indicator = new VarianceIndicator { IsPopulation = false };
|
|
|
|
Assert.False(indicator.IsPopulation);
|
|
|
|
indicator.IsPopulation = true;
|
|
Assert.True(indicator.IsPopulation);
|
|
}
|
|
|
|
[Fact]
|
|
public void VarianceIndicator_Source_CanBeChanged()
|
|
{
|
|
var indicator = new VarianceIndicator { Source = SourceType.Close };
|
|
|
|
Assert.Equal(SourceType.Close, indicator.Source);
|
|
|
|
indicator.Source = SourceType.Open;
|
|
Assert.Equal(SourceType.Open, indicator.Source);
|
|
}
|
|
|
|
[Fact]
|
|
public void VarianceIndicator_ShowColdValues_CanBeChanged()
|
|
{
|
|
var indicator = new VarianceIndicator { ShowColdValues = true };
|
|
|
|
Assert.True(indicator.ShowColdValues);
|
|
|
|
indicator.ShowColdValues = false;
|
|
Assert.False(indicator.ShowColdValues);
|
|
}
|
|
|
|
[Fact]
|
|
public void VarianceIndicator_ShortName_UpdatesWhenPeriodChanges()
|
|
{
|
|
var indicator = new VarianceIndicator { Period = 10 };
|
|
string initialName = indicator.ShortName;
|
|
|
|
Assert.True(initialName.Contains("10", StringComparison.Ordinal));
|
|
|
|
indicator.Period = 20;
|
|
string updatedName = indicator.ShortName;
|
|
|
|
Assert.True(updatedName.Contains("20", StringComparison.Ordinal));
|
|
}
|
|
|
|
[Fact]
|
|
public void VarianceIndicator_ProcessUpdate_IgnoresNonBarUpdates()
|
|
{
|
|
var indicator = new VarianceIndicator { Period = 5 };
|
|
indicator.Initialize();
|
|
|
|
var now = DateTime.UtcNow;
|
|
indicator.HistoricalData.AddBar(now, 100, 105, 95, 102);
|
|
|
|
// Process historical bar first
|
|
indicator.ProcessUpdate(new UpdateArgs(UpdateReason.HistoricalBar));
|
|
|
|
// Process other update reasons - should not throw
|
|
indicator.ProcessUpdate(new UpdateArgs(UpdateReason.NewTick));
|
|
|
|
// Assert that the indicator still exists (method completed without exception)
|
|
Assert.NotNull(indicator);
|
|
}
|
|
|
|
[Fact]
|
|
public void VarianceIndicator_LineSeries_HasCorrectProperties()
|
|
{
|
|
var indicator = new VarianceIndicator { Period = 10 };
|
|
indicator.Initialize();
|
|
|
|
var lineSeries = indicator.LinesSeries[0];
|
|
|
|
Assert.Equal("Variance", lineSeries.Name);
|
|
Assert.Equal(2, lineSeries.Width);
|
|
Assert.Equal(LineStyle.Solid, lineSeries.Style);
|
|
}
|
|
}
|