mirror of
https://github.com/mihakralj/QuanTAlib.git
synced 2026-08-07 21:47:43 +00:00
dfeb23bf3d
- Implemented SgmaIndicator class in C# with properties for Period, Degree, and Source. - Added unit tests for SgmaIndicator covering constructor defaults, initialization, and various update scenarios. - Created a new Quantower adapter for the SGMA indicator, including input parameters and line series setup. - Removed legacy SGMA implementation and tests to streamline the codebase. - Updated project files to include new indicator and tests in the build process. - Generated a missing indicators report and outlined a plan for oscillator documentation rewrite.
136 lines
4.5 KiB
C#
136 lines
4.5 KiB
C#
using TradingPlatform.BusinessLayer;
|
|
|
|
namespace QuanTAlib.Tests;
|
|
|
|
public sealed class GrangerIndicatorTests
|
|
{
|
|
[Fact]
|
|
public void GrangerIndicator_Constructor_SetsDefaults()
|
|
{
|
|
var indicator = new GrangerIndicator();
|
|
|
|
Assert.Equal(20, indicator.Period);
|
|
Assert.Equal(SourceType.Close, indicator.Source);
|
|
Assert.Equal(SourceType.Open, indicator.Source2);
|
|
Assert.True(indicator.ShowColdValues);
|
|
Assert.Equal("GRANGER - Granger Causality F-Statistic", indicator.Name);
|
|
Assert.True(indicator.SeparateWindow);
|
|
Assert.True(indicator.OnBackGround);
|
|
}
|
|
|
|
[Fact]
|
|
public void GrangerIndicator_MinHistoryDepths_EqualsTwo()
|
|
{
|
|
var indicator = new GrangerIndicator();
|
|
|
|
Assert.Equal(2, GrangerIndicator.MinHistoryDepths);
|
|
Assert.Equal(2, ((IWatchlistIndicator)indicator).MinHistoryDepths);
|
|
}
|
|
|
|
[Fact]
|
|
public void GrangerIndicator_ShortName_IncludesPeriodAndSources()
|
|
{
|
|
var indicator = new GrangerIndicator { Period = 20 };
|
|
|
|
Assert.Contains("GRANGER", indicator.ShortName, StringComparison.Ordinal);
|
|
Assert.Contains("20", indicator.ShortName, StringComparison.Ordinal);
|
|
}
|
|
|
|
[Fact]
|
|
public void GrangerIndicator_Initialize_CreatesInternalGranger()
|
|
{
|
|
var indicator = new GrangerIndicator { Period = 10 };
|
|
|
|
indicator.Initialize();
|
|
|
|
Assert.Single(indicator.LinesSeries);
|
|
}
|
|
|
|
[Fact]
|
|
public void GrangerIndicator_ProcessUpdate_HistoricalBar_ComputesValue()
|
|
{
|
|
var indicator = new GrangerIndicator { Period = 5 };
|
|
indicator.Initialize();
|
|
|
|
var now = DateTime.UtcNow;
|
|
indicator.HistoricalData.AddBar(now, 100, 105, 95, 102);
|
|
|
|
var args = new UpdateArgs(UpdateReason.HistoricalBar);
|
|
indicator.ProcessUpdate(args);
|
|
|
|
Assert.Equal(1, indicator.LinesSeries[0].Count);
|
|
}
|
|
|
|
[Fact]
|
|
public void GrangerIndicator_ProcessUpdate_NewBar_ComputesValue()
|
|
{
|
|
var indicator = new GrangerIndicator { 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 GrangerIndicator_ProcessUpdate_NewTick_ProcessesWithoutError()
|
|
{
|
|
var indicator = new GrangerIndicator { Period = 5 };
|
|
indicator.Initialize();
|
|
|
|
var now = DateTime.UtcNow;
|
|
indicator.HistoricalData.AddBar(now, 100, 105, 95, 102);
|
|
|
|
indicator.ProcessUpdate(new UpdateArgs(UpdateReason.HistoricalBar));
|
|
double firstValue = indicator.LinesSeries[0].GetValue(0);
|
|
|
|
indicator.ProcessUpdate(new UpdateArgs(UpdateReason.NewTick));
|
|
double secondValue = indicator.LinesSeries[0].GetValue(0);
|
|
|
|
Assert.True(double.IsNaN(firstValue) || double.IsFinite(firstValue));
|
|
Assert.True(double.IsNaN(secondValue) || double.IsFinite(secondValue));
|
|
}
|
|
|
|
[Fact]
|
|
public void GrangerIndicator_MultipleUpdates_ProducesSequence()
|
|
{
|
|
var indicator = new GrangerIndicator { Period = 5 };
|
|
indicator.Initialize();
|
|
|
|
var now = DateTime.UtcNow;
|
|
double[] opens = { 100, 101, 102, 103, 104, 105 };
|
|
double[] closes = { 100, 101, 102, 103, 104, 105 };
|
|
|
|
for (int i = 0; i < opens.Length; i++)
|
|
{
|
|
indicator.HistoricalData.AddBar(now.AddMinutes(i), opens[i], opens[i] + 5, opens[i] - 5, closes[i]);
|
|
indicator.ProcessUpdate(new UpdateArgs(i == 0 ? UpdateReason.HistoricalBar : UpdateReason.NewBar));
|
|
}
|
|
|
|
Assert.Equal(opens.Length, indicator.LinesSeries[0].Count);
|
|
}
|
|
|
|
[Fact]
|
|
public void GrangerIndicator_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 GrangerIndicator { Period = 5, Source = source, Source2 = SourceType.Close };
|
|
indicator.Initialize();
|
|
|
|
var now = DateTime.UtcNow;
|
|
indicator.HistoricalData.AddBar(now, 100, 110, 90, 105);
|
|
indicator.ProcessUpdate(new UpdateArgs(UpdateReason.HistoricalBar));
|
|
|
|
Assert.Equal(1, indicator.LinesSeries[0].Count);
|
|
}
|
|
}
|
|
}
|