mirror of
https://github.com/mihakralj/QuanTAlib.git
synced 2026-08-25 13:58:04 +00:00
Add validation tests for various volume and momentum indicators
- Introduced Massi validation tests to ensure mathematical properties hold for the Mass Index indicator. - Added Va validation tests for Volume Accumulation, checking for finite outputs and correct accumulation behavior. - Implemented Vf validation tests for Volume Force, verifying outputs for rising and falling prices, and ensuring batch and streaming results match. - Created Vo validation tests for Volume Oscillator, confirming behavior with constant, increasing, and decreasing volumes. - Developed Vroc validation tests for Volume Rate of Change, validating outputs for constant volume and changes in volume. - Updated project file to include new momentum indicators (MACD and RSI) in the compilation.
This commit is contained in:
@@ -13,6 +13,14 @@ public class TtmTrendIndicatorTests
|
||||
Assert.Equal("TTM Trend", indicator.Name);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Constructor_SetsDescription()
|
||||
{
|
||||
var indicator = new TtmTrendIndicator();
|
||||
Assert.Contains("TTM Trend", indicator.Description, StringComparison.Ordinal);
|
||||
Assert.Contains("EMA", indicator.Description, StringComparison.Ordinal);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void DefaultPeriod_Is6()
|
||||
{
|
||||
@@ -20,6 +28,13 @@ public class TtmTrendIndicatorTests
|
||||
Assert.Equal(6, indicator.Period);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void DefaultShowColdValues_IsTrue()
|
||||
{
|
||||
var indicator = new TtmTrendIndicator();
|
||||
Assert.True(indicator.ShowColdValues);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ShortName_IncludesParameters()
|
||||
{
|
||||
@@ -50,6 +65,174 @@ public class TtmTrendIndicatorTests
|
||||
Assert.True(indicator.OnBackGround);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Constructor_AddsOneLineSeries()
|
||||
{
|
||||
var indicator = new TtmTrendIndicator();
|
||||
Assert.Single(indicator.LinesSeries);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Parameters_CanBeChanged()
|
||||
{
|
||||
var indicator = new TtmTrendIndicator { Period = 6 };
|
||||
|
||||
indicator.Period = 20;
|
||||
|
||||
Assert.Equal(20, indicator.Period);
|
||||
Assert.Equal(0, TtmTrendIndicator.MinHistoryDepths);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ShowColdValues_CanBeChanged()
|
||||
{
|
||||
var indicator = new TtmTrendIndicator();
|
||||
|
||||
indicator.ShowColdValues = false;
|
||||
|
||||
Assert.False(indicator.ShowColdValues);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Initialize_CreatesInternalIndicator()
|
||||
{
|
||||
var indicator = new TtmTrendIndicator { Period = 10 };
|
||||
|
||||
indicator.Initialize();
|
||||
|
||||
// Line series count should remain 1 after init
|
||||
Assert.Single(indicator.LinesSeries);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ProcessUpdate_HistoricalBar_ComputesValue()
|
||||
{
|
||||
var indicator = new TtmTrendIndicator { Period = 6 };
|
||||
indicator.Initialize();
|
||||
|
||||
var now = DateTime.UtcNow;
|
||||
for (int i = 0; i < 20; i++)
|
||||
{
|
||||
indicator.HistoricalData.AddBar(now.AddMinutes(i), 100 + i, 110 + i, 90 + i, 105 + i);
|
||||
|
||||
var args = new UpdateArgs(UpdateReason.HistoricalBar);
|
||||
indicator.ProcessUpdate(args);
|
||||
}
|
||||
|
||||
double value = indicator.LinesSeries[0].GetValue(0);
|
||||
Assert.True(double.IsFinite(value));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ProcessUpdate_NewBar_ComputesValue()
|
||||
{
|
||||
var indicator = new TtmTrendIndicator { Period = 6 };
|
||||
indicator.Initialize();
|
||||
|
||||
var now = DateTime.UtcNow;
|
||||
for (int i = 0; i < 10; i++)
|
||||
{
|
||||
indicator.HistoricalData.AddBar(now.AddMinutes(i), 100 + i, 110 + i, 90 + i, 105 + i);
|
||||
var args = new UpdateArgs(UpdateReason.HistoricalBar);
|
||||
indicator.ProcessUpdate(args);
|
||||
}
|
||||
|
||||
// Simulate a new bar
|
||||
indicator.HistoricalData.AddBar(now.AddMinutes(10), 110, 120, 100, 115);
|
||||
var newArgs = new UpdateArgs(UpdateReason.NewBar);
|
||||
indicator.ProcessUpdate(newArgs);
|
||||
|
||||
double value = indicator.LinesSeries[0].GetValue(0);
|
||||
Assert.True(double.IsFinite(value));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ProcessUpdate_BullishTrend_ProducesGreenMarker()
|
||||
{
|
||||
var indicator = new TtmTrendIndicator { Period = 2 };
|
||||
indicator.Initialize();
|
||||
|
||||
var now = DateTime.UtcNow;
|
||||
|
||||
// Feed strongly rising bars to trigger bullish trend (Trend == 1)
|
||||
indicator.HistoricalData.AddBar(now, 50.0, 55.0, 48.0, 52.0);
|
||||
indicator.ProcessUpdate(new UpdateArgs(UpdateReason.HistoricalBar));
|
||||
|
||||
indicator.HistoricalData.AddBar(now.AddMinutes(1), 60.0, 65.0, 58.0, 62.0);
|
||||
indicator.ProcessUpdate(new UpdateArgs(UpdateReason.HistoricalBar));
|
||||
|
||||
indicator.HistoricalData.AddBar(now.AddMinutes(2), 70.0, 75.0, 68.0, 72.0);
|
||||
indicator.ProcessUpdate(new UpdateArgs(UpdateReason.HistoricalBar));
|
||||
|
||||
indicator.HistoricalData.AddBar(now.AddMinutes(3), 80.0, 85.0, 78.0, 82.0);
|
||||
indicator.ProcessUpdate(new UpdateArgs(UpdateReason.HistoricalBar));
|
||||
|
||||
// Value should be finite after enough bars
|
||||
double value = indicator.LinesSeries[0].GetValue(0);
|
||||
Assert.True(double.IsFinite(value));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ProcessUpdate_BearishTrend_ProducesRedMarker()
|
||||
{
|
||||
var indicator = new TtmTrendIndicator { Period = 2 };
|
||||
indicator.Initialize();
|
||||
|
||||
var now = DateTime.UtcNow;
|
||||
|
||||
// Feed strongly falling bars to trigger bearish trend (Trend == -1)
|
||||
indicator.HistoricalData.AddBar(now, 100.0, 105.0, 98.0, 102.0);
|
||||
indicator.ProcessUpdate(new UpdateArgs(UpdateReason.HistoricalBar));
|
||||
|
||||
indicator.HistoricalData.AddBar(now.AddMinutes(1), 90.0, 95.0, 88.0, 92.0);
|
||||
indicator.ProcessUpdate(new UpdateArgs(UpdateReason.HistoricalBar));
|
||||
|
||||
indicator.HistoricalData.AddBar(now.AddMinutes(2), 80.0, 85.0, 78.0, 82.0);
|
||||
indicator.ProcessUpdate(new UpdateArgs(UpdateReason.HistoricalBar));
|
||||
|
||||
indicator.HistoricalData.AddBar(now.AddMinutes(3), 70.0, 75.0, 68.0, 72.0);
|
||||
indicator.ProcessUpdate(new UpdateArgs(UpdateReason.HistoricalBar));
|
||||
|
||||
double value = indicator.LinesSeries[0].GetValue(0);
|
||||
Assert.True(double.IsFinite(value));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ProcessUpdate_FlatPrices_ProducesGrayMarker()
|
||||
{
|
||||
var indicator = new TtmTrendIndicator { Period = 2 };
|
||||
indicator.Initialize();
|
||||
|
||||
var now = DateTime.UtcNow;
|
||||
|
||||
// Feed identical bars to get Trend == 0 (neutral)
|
||||
for (int i = 0; i < 5; i++)
|
||||
{
|
||||
indicator.HistoricalData.AddBar(now.AddMinutes(i), 100.0, 100.0, 100.0, 100.0);
|
||||
indicator.ProcessUpdate(new UpdateArgs(UpdateReason.HistoricalBar));
|
||||
}
|
||||
|
||||
double value = indicator.LinesSeries[0].GetValue(0);
|
||||
Assert.True(double.IsFinite(value));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ProcessUpdate_ColdValues_HiddenWhenDisabled()
|
||||
{
|
||||
var indicator = new TtmTrendIndicator { Period = 6, ShowColdValues = false };
|
||||
indicator.Initialize();
|
||||
|
||||
var now = DateTime.UtcNow;
|
||||
|
||||
// Only 1 bar — indicator should not yet be hot
|
||||
indicator.HistoricalData.AddBar(now, 100.0, 105.0, 98.0, 102.0);
|
||||
indicator.ProcessUpdate(new UpdateArgs(UpdateReason.HistoricalBar));
|
||||
|
||||
// With ShowColdValues=false, the cold value should not be set
|
||||
// (LineSeries.SetValue with isHot=false and showCold=false skips the value)
|
||||
Assert.Single(indicator.LinesSeries);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void CalculationIntegration_ProducesCorrectValues()
|
||||
{
|
||||
@@ -108,4 +291,43 @@ public class TtmTrendIndicatorTests
|
||||
Assert.Equal(default, ttm.Last);
|
||||
Assert.Equal(0, ttm.Trend);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ProcessUpdate_MultipleNewBars_AccumulatesValues()
|
||||
{
|
||||
var indicator = new TtmTrendIndicator { Period = 3 };
|
||||
indicator.Initialize();
|
||||
|
||||
var now = DateTime.UtcNow;
|
||||
|
||||
// Feed historical bars
|
||||
for (int i = 0; i < 5; i++)
|
||||
{
|
||||
indicator.HistoricalData.AddBar(now.AddMinutes(i), 100 + i * 2, 110 + i * 2, 90 + i * 2, 105 + i * 2);
|
||||
indicator.ProcessUpdate(new UpdateArgs(UpdateReason.HistoricalBar));
|
||||
}
|
||||
|
||||
// Feed new bars
|
||||
for (int i = 5; i < 8; i++)
|
||||
{
|
||||
indicator.HistoricalData.AddBar(now.AddMinutes(i), 100 + i * 2, 110 + i * 2, 90 + i * 2, 105 + i * 2);
|
||||
indicator.ProcessUpdate(new UpdateArgs(UpdateReason.NewBar));
|
||||
}
|
||||
|
||||
double value = indicator.LinesSeries[0].GetValue(0);
|
||||
Assert.True(double.IsFinite(value));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Initialize_AfterParameterChange_UsesNewPeriod()
|
||||
{
|
||||
var indicator = new TtmTrendIndicator { Period = 6 };
|
||||
indicator.Initialize();
|
||||
|
||||
// Change period and re-initialize
|
||||
indicator.Period = 20;
|
||||
indicator.Initialize();
|
||||
|
||||
Assert.Equal("TTM_TREND(20)", indicator.ShortName);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,229 @@
|
||||
// TtmTrend: Mathematical property validation tests
|
||||
// TTM Trend is a proprietary John Carter indicator — no external library equivalents exist.
|
||||
// Validation uses mathematical property testing against known EMA behaviors.
|
||||
|
||||
namespace QuanTAlib.Tests;
|
||||
|
||||
using Xunit;
|
||||
|
||||
public class TtmTrendValidationTests
|
||||
{
|
||||
private const int DefaultPeriod = 6;
|
||||
private const int TestDataLength = 500;
|
||||
|
||||
[Fact]
|
||||
public void TtmTrend_EmaOutput_IsFiniteForGbmData()
|
||||
{
|
||||
var bars = new GBM(sigma: 0.5, seed: 123).Fetch(TestDataLength, DateTime.UtcNow.Ticks, TimeSpan.FromMinutes(1));
|
||||
var ttm = new TtmTrend(DefaultPeriod);
|
||||
|
||||
for (int i = 0; i < bars.Count; i++)
|
||||
{
|
||||
var result = ttm.Update(bars[i], isNew: true);
|
||||
Assert.True(double.IsFinite(result.Value),
|
||||
$"TtmTrend output must be finite at bar {i}, got {result.Value}");
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void TtmTrend_TrendDirection_OnlyValidValues()
|
||||
{
|
||||
var bars = new GBM(sigma: 0.5, seed: 123).Fetch(TestDataLength, DateTime.UtcNow.Ticks, TimeSpan.FromMinutes(1));
|
||||
var ttm = new TtmTrend(DefaultPeriod);
|
||||
|
||||
for (int i = 0; i < bars.Count; i++)
|
||||
{
|
||||
ttm.Update(bars[i], isNew: true);
|
||||
|
||||
Assert.True(ttm.Trend is -1 or 0 or 1,
|
||||
$"Trend must be -1, 0, or 1 at bar {i}, got {ttm.Trend}");
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void TtmTrend_Strength_IsNonNegative()
|
||||
{
|
||||
var bars = new GBM(sigma: 0.5, seed: 123).Fetch(TestDataLength, DateTime.UtcNow.Ticks, TimeSpan.FromMinutes(1));
|
||||
var ttm = new TtmTrend(DefaultPeriod);
|
||||
|
||||
for (int i = 0; i < bars.Count; i++)
|
||||
{
|
||||
ttm.Update(bars[i], isNew: true);
|
||||
|
||||
Assert.True(ttm.Strength >= 0,
|
||||
$"Strength must be >= 0 at bar {i}, got {ttm.Strength}");
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void TtmTrend_RisingSequence_BullishTrend()
|
||||
{
|
||||
var ttm = new TtmTrend(DefaultPeriod);
|
||||
double basePrice = 100.0;
|
||||
|
||||
// Feed enough bars to warm up, then inject consistently rising prices
|
||||
for (int i = 0; i < 20; i++)
|
||||
{
|
||||
double price = basePrice + i * 2.0;
|
||||
var bar = new TBar(
|
||||
DateTime.UtcNow.AddMinutes(i),
|
||||
price - 0.5, price + 0.5, price - 0.5, price, 1000);
|
||||
ttm.Update(bar, isNew: true);
|
||||
}
|
||||
|
||||
// After a consistently rising sequence, trend should be bullish
|
||||
Assert.Equal(1, ttm.Trend);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void TtmTrend_FallingSequence_BearishTrend()
|
||||
{
|
||||
var ttm = new TtmTrend(DefaultPeriod);
|
||||
double basePrice = 200.0;
|
||||
|
||||
// Feed enough bars to warm up, then inject consistently falling prices
|
||||
for (int i = 0; i < 20; i++)
|
||||
{
|
||||
double price = basePrice - i * 2.0;
|
||||
var bar = new TBar(
|
||||
DateTime.UtcNow.AddMinutes(i),
|
||||
price + 0.5, price + 0.5, price - 0.5, price, 1000);
|
||||
ttm.Update(bar, isNew: true);
|
||||
}
|
||||
|
||||
// After a consistently falling sequence, trend should be bearish
|
||||
Assert.Equal(-1, ttm.Trend);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void TtmTrend_ConstantPrice_ZeroStrength()
|
||||
{
|
||||
var ttm = new TtmTrend(DefaultPeriod);
|
||||
double price = 100.0;
|
||||
|
||||
// Feed constant-price bars
|
||||
for (int i = 0; i < 20; i++)
|
||||
{
|
||||
var bar = new TBar(
|
||||
DateTime.UtcNow.AddMinutes(i),
|
||||
price, price, price, price, 1000);
|
||||
ttm.Update(bar, isNew: true);
|
||||
}
|
||||
|
||||
// Strength should be 0 for a constant series (no percent change)
|
||||
Assert.Equal(0.0, ttm.Strength, precision: 10);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void TtmTrend_EmaConvergesToConstant()
|
||||
{
|
||||
var ttm = new TtmTrend(DefaultPeriod);
|
||||
double targetPrice = 100.0;
|
||||
|
||||
// Start at 50, abruptly switch to constant 100
|
||||
for (int i = 0; i < 5; i++)
|
||||
{
|
||||
var bar = new TBar(
|
||||
DateTime.UtcNow.AddMinutes(i),
|
||||
50, 50, 50, 50, 1000);
|
||||
ttm.Update(bar, isNew: true);
|
||||
}
|
||||
|
||||
// Now feed constant 100 for many bars
|
||||
for (int i = 5; i < 100; i++)
|
||||
{
|
||||
var bar = new TBar(
|
||||
DateTime.UtcNow.AddMinutes(i),
|
||||
targetPrice, targetPrice, targetPrice, targetPrice, 1000);
|
||||
ttm.Update(bar, isNew: true);
|
||||
}
|
||||
|
||||
// EMA output should converge to the target price
|
||||
Assert.Equal(targetPrice, ttm.Last.Value, precision: 6);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void TtmTrend_BatchAndStreaming_ProduceSameResults()
|
||||
{
|
||||
var bars = new GBM(sigma: 0.5, seed: 123).Fetch(TestDataLength, DateTime.UtcNow.Ticks, TimeSpan.FromMinutes(1));
|
||||
|
||||
// Batch mode
|
||||
var batchResults = TtmTrend.Batch(bars, DefaultPeriod);
|
||||
|
||||
// Streaming mode
|
||||
var streamTtm = new TtmTrend(DefaultPeriod);
|
||||
var streamResults = new double[bars.Count];
|
||||
for (int i = 0; i < bars.Count; i++)
|
||||
{
|
||||
var result = streamTtm.Update(bars[i], isNew: true);
|
||||
streamResults[i] = result.Value;
|
||||
}
|
||||
|
||||
// Both must match
|
||||
Assert.Equal(batchResults.Count, bars.Count);
|
||||
for (int i = 0; i < bars.Count; i++)
|
||||
{
|
||||
Assert.Equal(batchResults.Values[i], streamResults[i], precision: 10);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void TtmTrend_DifferentPeriods_ProduceDifferentEmaSmoothing()
|
||||
{
|
||||
var bars = new GBM(sigma: 0.5, seed: 123).Fetch(100, DateTime.UtcNow.Ticks, TimeSpan.FromMinutes(1));
|
||||
|
||||
var ttm3 = new TtmTrend(period: 3);
|
||||
var ttm20 = new TtmTrend(period: 20);
|
||||
|
||||
for (int i = 0; i < bars.Count; i++)
|
||||
{
|
||||
ttm3.Update(bars[i], isNew: true);
|
||||
ttm20.Update(bars[i], isNew: true);
|
||||
}
|
||||
|
||||
// Different periods should produce different final values (except on trivially constant data)
|
||||
Assert.NotEqual(ttm3.Last.Value, ttm20.Last.Value);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void TtmTrend_IsHot_AfterWarmup()
|
||||
{
|
||||
var ttm = new TtmTrend(DefaultPeriod);
|
||||
|
||||
// First bar: not hot
|
||||
var bar1 = new TBar(DateTime.UtcNow, 100, 101, 99, 100, 1000);
|
||||
ttm.Update(bar1, isNew: true);
|
||||
Assert.False(ttm.IsHot);
|
||||
|
||||
// Second bar: should be hot (warmup period = 2)
|
||||
var bar2 = new TBar(DateTime.UtcNow.AddMinutes(1), 101, 102, 100, 101, 1000);
|
||||
ttm.Update(bar2, isNew: true);
|
||||
Assert.True(ttm.IsHot);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void TtmTrend_BarCorrection_IsNewFalse_RestoresState()
|
||||
{
|
||||
var bars = new GBM(sigma: 0.5, seed: 123).Fetch(50, DateTime.UtcNow.Ticks, TimeSpan.FromMinutes(1));
|
||||
var ttm = new TtmTrend(DefaultPeriod);
|
||||
|
||||
// Process 30 bars
|
||||
for (int i = 0; i < 30; i++)
|
||||
{
|
||||
ttm.Update(bars[i], isNew: true);
|
||||
}
|
||||
|
||||
_ = ttm.Last.Value;
|
||||
|
||||
// Update bar 30 (isNew=true) then correct it (isNew=false) with same value
|
||||
ttm.Update(bars[30], isNew: true);
|
||||
double afterNew = ttm.Last.Value;
|
||||
|
||||
// Correct with isNew=false using same bar
|
||||
ttm.Update(bars[30], isNew: false);
|
||||
double afterCorrection = ttm.Last.Value;
|
||||
|
||||
// Bar correction with same data should produce the same value
|
||||
Assert.Equal(afterNew, afterCorrection, precision: 10);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user