mirror of
https://github.com/mihakralj/QuanTAlib.git
synced 2026-07-30 02:27:43 +00:00
875998b288
- Enhanced WMA indicator with event-driven capabilities using ITValuePublisher interface. - Created a new TODO file listing various indicators and their corresponding libraries. - Added unit tests for DEMA, HMA, TEMA, and WMA indicators to ensure proper functionality. - Implemented tests for handling new bars, ticks, and historical data updates across indicators. - Verified that indicators correctly compute values and handle different source types.
178 lines
5.5 KiB
C#
178 lines
5.5 KiB
C#
using Xunit;
|
|
using TradingPlatform.BusinessLayer;
|
|
|
|
namespace QuanTAlib.Tests;
|
|
|
|
public class TemaIndicatorTests
|
|
{
|
|
[Fact]
|
|
public void TemaIndicator_Constructor_SetsDefaults()
|
|
{
|
|
var indicator = new TemaIndicator();
|
|
|
|
Assert.Equal(10, indicator.Period);
|
|
Assert.Equal(SourceType.Close, indicator.Source);
|
|
Assert.True(indicator.ShowColdValues);
|
|
Assert.Equal("TEMA - Triple Exponential Moving Average", indicator.Name);
|
|
Assert.False(indicator.SeparateWindow);
|
|
Assert.True(indicator.OnBackGround);
|
|
}
|
|
|
|
[Fact]
|
|
public void TemaIndicator_MinHistoryDepths_EqualsPeriod()
|
|
{
|
|
var indicator = new TemaIndicator { Period = 20 };
|
|
|
|
Assert.Equal(20, indicator.MinHistoryDepths);
|
|
Assert.Equal(20, ((IWatchlistIndicator)indicator).MinHistoryDepths);
|
|
}
|
|
|
|
[Fact]
|
|
public void TemaIndicator_ShortName_IncludesPeriodAndSource()
|
|
{
|
|
var indicator = new TemaIndicator { Period = 15 };
|
|
|
|
Assert.Contains("TEMA", indicator.ShortName);
|
|
Assert.Contains("15", indicator.ShortName);
|
|
}
|
|
|
|
[Fact]
|
|
public void TemaIndicator_SourceCodeLink_IsValid()
|
|
{
|
|
var indicator = new TemaIndicator();
|
|
|
|
Assert.Contains("github.com", indicator.SourceCodeLink);
|
|
Assert.Contains("Tema.Quantower.cs", indicator.SourceCodeLink);
|
|
}
|
|
|
|
[Fact]
|
|
public void TemaIndicator_Initialize_CreatesInternalTema()
|
|
{
|
|
var indicator = new TemaIndicator { Period = 10 };
|
|
|
|
// Initialize should not throw
|
|
indicator.Initialize();
|
|
|
|
// After init, line series should exist
|
|
Assert.Single(indicator.LinesSeries);
|
|
}
|
|
|
|
[Fact]
|
|
public void TemaIndicator_ProcessUpdate_HistoricalBar_ComputesValue()
|
|
{
|
|
var indicator = new TemaIndicator { Period = 3 };
|
|
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 TemaIndicator_ProcessUpdate_NewBar_ComputesValue()
|
|
{
|
|
var indicator = new TemaIndicator { Period = 3 };
|
|
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 TemaIndicator_ProcessUpdate_NewTick_ProcessesWithoutError()
|
|
{
|
|
var indicator = new TemaIndicator { Period = 3 };
|
|
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.IsFinite(firstValue));
|
|
Assert.True(double.IsFinite(secondValue));
|
|
}
|
|
|
|
[Fact]
|
|
public void TemaIndicator_OnPaintChart_DoesNotThrow()
|
|
{
|
|
var indicator = new TemaIndicator();
|
|
indicator.Initialize();
|
|
|
|
var method = indicator.GetType().GetMethod("OnPaintChart");
|
|
Assert.NotNull(method);
|
|
Assert.Equal(typeof(TemaIndicator), method.DeclaringType);
|
|
}
|
|
|
|
[Fact]
|
|
public void TemaIndicator_MultipleUpdates_ProducesCorrectTemaSequence()
|
|
{
|
|
var indicator = new TemaIndicator { Period = 3 };
|
|
indicator.Initialize();
|
|
|
|
var now = DateTime.UtcNow;
|
|
double[] closes = { 100, 102, 104, 103, 105 };
|
|
|
|
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 TemaIndicator_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 TemaIndicator { Period = 3, 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 TemaIndicator_Period_CanBeChanged()
|
|
{
|
|
var indicator = new TemaIndicator { Period = 5 };
|
|
Assert.Equal(5, indicator.Period);
|
|
|
|
indicator.Period = 20;
|
|
Assert.Equal(20, indicator.Period);
|
|
Assert.Equal(20, indicator.MinHistoryDepths);
|
|
}
|
|
}
|