mirror of
https://github.com/mihakralj/QuanTAlib.git
synced 2026-08-21 20:18:05 +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:
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,188 @@
|
||||
// Jvoltyn: Mathematical property validation tests
|
||||
// Jvoltyn is a proprietary Jurik Research indicator — no external library equivalents exist.
|
||||
// Validation uses mathematical property testing: normalized output must be in [0, 100].
|
||||
|
||||
namespace QuanTAlib.Tests;
|
||||
|
||||
using Xunit;
|
||||
|
||||
public class JvoltynValidationTests
|
||||
{
|
||||
private const int DefaultPeriod = 10;
|
||||
private const int TestDataLength = 500;
|
||||
|
||||
[Fact]
|
||||
public void Jvoltyn_Output_IsFiniteForGbmData()
|
||||
{
|
||||
var series = new GBM(sigma: 0.5, seed: 123).Fetch(TestDataLength, DateTime.UtcNow.Ticks, TimeSpan.FromMinutes(1)).Close;
|
||||
var jvoltyn = new Jvoltyn(DefaultPeriod);
|
||||
|
||||
for (int i = 0; i < series.Count; i++)
|
||||
{
|
||||
var result = jvoltyn.Update(series[i], isNew: true);
|
||||
Assert.True(double.IsFinite(result.Value),
|
||||
$"Jvoltyn output must be finite at bar {i}, got {result.Value}");
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Jvoltyn_Output_InRange0To100_AfterWarmup()
|
||||
{
|
||||
var series = new GBM(sigma: 0.5, seed: 123).Fetch(TestDataLength, DateTime.UtcNow.Ticks, TimeSpan.FromMinutes(1)).Close;
|
||||
var jvoltyn = new Jvoltyn(DefaultPeriod);
|
||||
|
||||
for (int i = 0; i < series.Count; i++)
|
||||
{
|
||||
var result = jvoltyn.Update(series[i], isNew: true);
|
||||
if (jvoltyn.IsHot)
|
||||
{
|
||||
Assert.True(result.Value >= -0.01 && result.Value <= 100.01,
|
||||
$"Jvoltyn output must be in [0, 100] after warmup at bar {i}, got {result.Value}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Jvoltyn_ConstantSeries_ZeroNormalizedVolatility()
|
||||
{
|
||||
var jvoltyn = new Jvoltyn(DefaultPeriod);
|
||||
double price = 100.0;
|
||||
|
||||
// Feed constant-price values
|
||||
for (int i = 0; i < 300; i++)
|
||||
{
|
||||
jvoltyn.Update(new TValue(DateTime.UtcNow.AddMinutes(i), price), isNew: true);
|
||||
}
|
||||
|
||||
// Constant series: d = 1 → normalized = (1-1)/(logParam-1)*100 = 0
|
||||
Assert.Equal(0.0, jvoltyn.Last.Value, precision: 1);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Jvoltyn_FirstBar_ReturnsZero()
|
||||
{
|
||||
var jvoltyn = new Jvoltyn(DefaultPeriod);
|
||||
|
||||
var result = jvoltyn.Update(new TValue(DateTime.UtcNow, 100.0), isNew: true);
|
||||
|
||||
// First bar initializes bands to price, d=1 → normalized=0
|
||||
Assert.Equal(0.0, result.Value, precision: 10);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Jvoltyn_UpperBand_GreaterOrEqualLowerBand()
|
||||
{
|
||||
var series = new GBM(sigma: 0.5, seed: 123).Fetch(TestDataLength, DateTime.UtcNow.Ticks, TimeSpan.FromMinutes(1)).Close;
|
||||
var jvoltyn = new Jvoltyn(DefaultPeriod);
|
||||
|
||||
for (int i = 0; i < series.Count; i++)
|
||||
{
|
||||
jvoltyn.Update(series[i], isNew: true);
|
||||
|
||||
Assert.True(jvoltyn.UpperBand >= jvoltyn.LowerBand,
|
||||
$"UpperBand ({jvoltyn.UpperBand}) must be >= LowerBand ({jvoltyn.LowerBand}) at bar {i}");
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Jvoltyn_RawVolatility_IsConsistentWithNormalized()
|
||||
{
|
||||
var series = new GBM(sigma: 0.5, seed: 123).Fetch(200, DateTime.UtcNow.Ticks, TimeSpan.FromMinutes(1)).Close;
|
||||
var jvoltyn = new Jvoltyn(DefaultPeriod);
|
||||
|
||||
// Calculate logParam manually to verify normalization
|
||||
double lengthParam = (DefaultPeriod - 1.0) / 2.0;
|
||||
double logParam = System.Math.Log(System.Math.Sqrt(lengthParam)) / System.Math.Log(2.0);
|
||||
logParam = (logParam + 2.0) < 0.0 ? 0.0 : (logParam + 2.0);
|
||||
double normFactor = System.Math.Abs(logParam - 1.0) > 1e-10 ? 100.0 / (logParam - 1.0) : 0.0;
|
||||
|
||||
for (int i = 0; i < series.Count; i++)
|
||||
{
|
||||
jvoltyn.Update(series[i], isNew: true);
|
||||
|
||||
if (i > 0) // Skip first bar initialization
|
||||
{
|
||||
double expectedNormalized = (jvoltyn.RawVolatility - 1.0) * normFactor;
|
||||
Assert.Equal(expectedNormalized, jvoltyn.Last.Value, precision: 8);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Jvoltyn_BatchAndStreaming_ProduceSameResults()
|
||||
{
|
||||
var series = new GBM(sigma: 0.5, seed: 123).Fetch(TestDataLength, DateTime.UtcNow.Ticks, TimeSpan.FromMinutes(1)).Close;
|
||||
|
||||
// Batch
|
||||
var batchResults = Jvoltyn.Batch(series, DefaultPeriod);
|
||||
|
||||
// Streaming
|
||||
var streamJvoltyn = new Jvoltyn(DefaultPeriod);
|
||||
var streamResults = new double[series.Count];
|
||||
for (int i = 0; i < series.Count; i++)
|
||||
{
|
||||
var result = streamJvoltyn.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 Jvoltyn_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];
|
||||
|
||||
Jvoltyn.Batch(series.Values, spanOutput, DefaultPeriod);
|
||||
|
||||
// Streaming
|
||||
var streamJvoltyn = new Jvoltyn(DefaultPeriod);
|
||||
for (int i = 0; i < series.Count; i++)
|
||||
{
|
||||
streamJvoltyn.Update(series[i], isNew: true);
|
||||
Assert.Equal(spanOutput[i], streamJvoltyn.Last.Value, precision: 10);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Jvoltyn_DifferentPeriods_ProduceDifferentResults()
|
||||
{
|
||||
var series = new GBM(sigma: 0.5, seed: 123).Fetch(200, DateTime.UtcNow.Ticks, TimeSpan.FromMinutes(1)).Close;
|
||||
|
||||
var jvoltyn5 = new Jvoltyn(5);
|
||||
var jvoltyn50 = new Jvoltyn(50);
|
||||
|
||||
for (int i = 0; i < series.Count; i++)
|
||||
{
|
||||
jvoltyn5.Update(series[i], isNew: true);
|
||||
jvoltyn50.Update(series[i], isNew: true);
|
||||
}
|
||||
|
||||
Assert.NotEqual(jvoltyn5.Last.Value, jvoltyn50.Last.Value);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Jvoltyn_BarCorrection_IsNewFalse_RestoresState()
|
||||
{
|
||||
var series = new GBM(sigma: 0.5, seed: 123).Fetch(50, DateTime.UtcNow.Ticks, TimeSpan.FromMinutes(1)).Close;
|
||||
var jvoltyn = new Jvoltyn(DefaultPeriod);
|
||||
|
||||
for (int i = 0; i < 30; i++)
|
||||
{
|
||||
jvoltyn.Update(series[i], isNew: true);
|
||||
}
|
||||
|
||||
jvoltyn.Update(series[30], isNew: true);
|
||||
double afterNew = jvoltyn.Last.Value;
|
||||
|
||||
jvoltyn.Update(series[30], isNew: false);
|
||||
double afterCorrection = jvoltyn.Last.Value;
|
||||
|
||||
Assert.Equal(afterNew, afterCorrection, precision: 10);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,208 @@
|
||||
// Massi: Mathematical property validation tests
|
||||
// Mass Index by Donald Dorsey. While Ooples has GetMassIndex(), the implementation
|
||||
// differences (EMA compensation, continuous vs discrete sum) make direct comparison
|
||||
// unreliable. Validation uses mathematical property testing instead.
|
||||
|
||||
namespace QuanTAlib.Tests;
|
||||
|
||||
using Xunit;
|
||||
|
||||
public class MassiValidationTests
|
||||
{
|
||||
private const int DefaultEmaLength = 9;
|
||||
private const int DefaultSumLength = 25;
|
||||
private const int TestDataLength = 500;
|
||||
|
||||
[Fact]
|
||||
public void Massi_Output_IsFiniteForGbmData()
|
||||
{
|
||||
var bars = new GBM(sigma: 0.5, seed: 123).Fetch(TestDataLength, DateTime.UtcNow.Ticks, TimeSpan.FromMinutes(1));
|
||||
var massi = new Massi(DefaultEmaLength, DefaultSumLength);
|
||||
|
||||
for (int i = 0; i < bars.Count; i++)
|
||||
{
|
||||
var result = massi.Update(bars[i], isNew: true);
|
||||
Assert.True(double.IsFinite(result.Value),
|
||||
$"Massi output must be finite at bar {i}, got {result.Value}");
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Massi_Output_IsPositive_AfterWarmup()
|
||||
{
|
||||
var bars = new GBM(sigma: 0.5, seed: 123).Fetch(TestDataLength, DateTime.UtcNow.Ticks, TimeSpan.FromMinutes(1));
|
||||
var massi = new Massi(DefaultEmaLength, DefaultSumLength);
|
||||
|
||||
for (int i = 0; i < bars.Count; i++)
|
||||
{
|
||||
var result = massi.Update(bars[i], isNew: true);
|
||||
if (massi.IsHot)
|
||||
{
|
||||
Assert.True(result.Value > 0,
|
||||
$"Massi output must be positive after warmup at bar {i}, got {result.Value}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Massi_ConstantRange_ConvergesToSumLength()
|
||||
{
|
||||
// When High-Low is constant, EMA1 = EMA2 after convergence,
|
||||
// so ratio = 1.0. Sum of 25 ratios = 25.0.
|
||||
var massi = new Massi(DefaultEmaLength, DefaultSumLength);
|
||||
|
||||
for (int i = 0; i < 300; i++)
|
||||
{
|
||||
var bar = new TBar(
|
||||
DateTime.UtcNow.AddMinutes(i),
|
||||
101, 101, 99, 100, 1000); // constant range = 2
|
||||
massi.Update(bar, isNew: true);
|
||||
}
|
||||
|
||||
// After convergence: ratio ≈ 1.0, sum ≈ 25.0
|
||||
Assert.Equal(DefaultSumLength, massi.Last.Value, tolerance: 0.5);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Massi_Ratio_ConvergesToOne_ForConstantRange()
|
||||
{
|
||||
var massi = new Massi(DefaultEmaLength, DefaultSumLength);
|
||||
|
||||
for (int i = 0; i < 300; i++)
|
||||
{
|
||||
var bar = new TBar(
|
||||
DateTime.UtcNow.AddMinutes(i),
|
||||
102, 102, 98, 100, 1000);
|
||||
massi.Update(bar, isNew: true);
|
||||
}
|
||||
|
||||
// EMA1/EMA2 should converge to 1.0 for constant range
|
||||
Assert.Equal(1.0, massi.Ratio, precision: 3);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Massi_Ema1_GreaterThanZero_ForPositiveRange()
|
||||
{
|
||||
var bars = new GBM(sigma: 0.5, seed: 123).Fetch(TestDataLength, DateTime.UtcNow.Ticks, TimeSpan.FromMinutes(1));
|
||||
var massi = new Massi(DefaultEmaLength, DefaultSumLength);
|
||||
|
||||
for (int i = 0; i < bars.Count; i++)
|
||||
{
|
||||
massi.Update(bars[i], isNew: true);
|
||||
if (massi.IsHot)
|
||||
{
|
||||
Assert.True(massi.Ema1 > 0,
|
||||
$"EMA1 must be > 0 at bar {i}, got {massi.Ema1}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Massi_Ema2_GreaterThanZero_ForPositiveRange()
|
||||
{
|
||||
var bars = new GBM(sigma: 0.5, seed: 123).Fetch(TestDataLength, DateTime.UtcNow.Ticks, TimeSpan.FromMinutes(1));
|
||||
var massi = new Massi(DefaultEmaLength, DefaultSumLength);
|
||||
|
||||
for (int i = 0; i < bars.Count; i++)
|
||||
{
|
||||
massi.Update(bars[i], isNew: true);
|
||||
if (massi.IsHot)
|
||||
{
|
||||
Assert.True(massi.Ema2 > 0,
|
||||
$"EMA2 must be > 0 at bar {i}, got {massi.Ema2}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Massi_BatchTBarSeries_MatchesStreaming()
|
||||
{
|
||||
var bars = new GBM(sigma: 0.5, seed: 123).Fetch(TestDataLength, DateTime.UtcNow.Ticks, TimeSpan.FromMinutes(1));
|
||||
|
||||
// Batch
|
||||
var batchResults = Massi.Batch(bars, DefaultEmaLength, DefaultSumLength);
|
||||
|
||||
// Streaming
|
||||
var streamMassi = new Massi(DefaultEmaLength, DefaultSumLength);
|
||||
var streamResults = new double[bars.Count];
|
||||
for (int i = 0; i < bars.Count; i++)
|
||||
{
|
||||
var result = streamMassi.Update(bars[i], isNew: true);
|
||||
streamResults[i] = result.Value;
|
||||
}
|
||||
|
||||
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 Massi_WideningRange_IncreasesValue()
|
||||
{
|
||||
var massi = new Massi(DefaultEmaLength, DefaultSumLength);
|
||||
|
||||
// Start with constant narrow range
|
||||
for (int i = 0; i < 100; i++)
|
||||
{
|
||||
var bar = new TBar(
|
||||
DateTime.UtcNow.AddMinutes(i),
|
||||
100.5, 100.5, 99.5, 100, 1000); // range = 1
|
||||
massi.Update(bar, isNew: true);
|
||||
}
|
||||
double narrowValue = massi.Last.Value;
|
||||
|
||||
// Abruptly widen the range
|
||||
for (int i = 100; i < 150; i++)
|
||||
{
|
||||
var bar = new TBar(
|
||||
DateTime.UtcNow.AddMinutes(i),
|
||||
110, 110, 90, 100, 1000); // range = 20
|
||||
massi.Update(bar, isNew: true);
|
||||
}
|
||||
double wideValue = massi.Last.Value;
|
||||
|
||||
// Widening range causes EMA1 to react faster than EMA2,
|
||||
// so ratio > 1 and MASSI increases
|
||||
Assert.True(wideValue > narrowValue,
|
||||
$"Widening range should increase MASSI: narrow={narrowValue}, wide={wideValue}");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Massi_DifferentParameters_ProduceDifferentResults()
|
||||
{
|
||||
var bars = new GBM(sigma: 0.5, seed: 123).Fetch(200, DateTime.UtcNow.Ticks, TimeSpan.FromMinutes(1));
|
||||
|
||||
var massi1 = new Massi(9, 25);
|
||||
var massi2 = new Massi(5, 10);
|
||||
|
||||
for (int i = 0; i < bars.Count; i++)
|
||||
{
|
||||
massi1.Update(bars[i], isNew: true);
|
||||
massi2.Update(bars[i], isNew: true);
|
||||
}
|
||||
|
||||
Assert.NotEqual(massi1.Last.Value, massi2.Last.Value);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Massi_BarCorrection_IsNewFalse_RestoresState()
|
||||
{
|
||||
var bars = new GBM(sigma: 0.5, seed: 123).Fetch(50, DateTime.UtcNow.Ticks, TimeSpan.FromMinutes(1));
|
||||
var massi = new Massi(DefaultEmaLength, DefaultSumLength);
|
||||
|
||||
for (int i = 0; i < 40; i++)
|
||||
{
|
||||
massi.Update(bars[i], isNew: true);
|
||||
}
|
||||
|
||||
massi.Update(bars[40], isNew: true);
|
||||
double afterNew = massi.Last.Value;
|
||||
|
||||
massi.Update(bars[40], isNew: false);
|
||||
double afterCorrection = massi.Last.Value;
|
||||
|
||||
Assert.Equal(afterNew, afterCorrection, precision: 10);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user