mirror of
https://github.com/mihakralj/QuanTAlib.git
synced 2026-08-23 13:08:04 +00:00
docs: remove C# Implementation Considerations sections, clean up temp scripts, reorganize test files
- Remove 'C# Implementation Considerations' sections from 34 indicator .md files - Delete 29 temp PowerShell scripts (_fix_mojibake.ps1, _hex_scan.ps1, etc.) - Move test files into tests/ subdirectories for consistent project structure - Add trader-focused bullet points to indicator documentation
This commit is contained in:
@@ -0,0 +1,189 @@
|
||||
using TradingPlatform.BusinessLayer;
|
||||
|
||||
namespace QuanTAlib.Tests;
|
||||
|
||||
public class EomIndicatorTests
|
||||
{
|
||||
[Fact]
|
||||
public void EomIndicator_Constructor_SetsDefaults()
|
||||
{
|
||||
var indicator = new EomIndicator();
|
||||
|
||||
Assert.Equal("EOM - Ease of Movement", indicator.Name);
|
||||
Assert.Equal(14, indicator.Period);
|
||||
Assert.Equal(10000, indicator.VolumeScale);
|
||||
Assert.True(indicator.SeparateWindow);
|
||||
Assert.True(indicator.OnBackGround);
|
||||
Assert.Equal(15, indicator.MinHistoryDepths); // Period + 1
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void EomIndicator_ShortName_ReflectsPeriod()
|
||||
{
|
||||
var indicator = new EomIndicator { Period = 20 };
|
||||
Assert.Equal("EOM(20)", indicator.ShortName);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void EomIndicator_MinHistoryDepths_EqualsPeriodPlusOne()
|
||||
{
|
||||
var indicator = new EomIndicator { Period = 26 };
|
||||
|
||||
Assert.Equal(27, indicator.MinHistoryDepths); // Period + 1
|
||||
Assert.Equal(27, ((IWatchlistIndicator)indicator).MinHistoryDepths);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void EomIndicator_Initialize_CreatesInternalEom()
|
||||
{
|
||||
var indicator = new EomIndicator();
|
||||
|
||||
// Initialize should not throw
|
||||
indicator.Initialize();
|
||||
|
||||
// After init, line series should exist
|
||||
Assert.Single(indicator.LinesSeries);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void EomIndicator_ProcessUpdate_HistoricalBar_ComputesValue()
|
||||
{
|
||||
var indicator = new EomIndicator();
|
||||
indicator.Initialize();
|
||||
|
||||
// Add historical data
|
||||
var now = DateTime.UtcNow;
|
||||
for (int i = 0; i < 30; i++)
|
||||
{
|
||||
indicator.HistoricalData.AddBar(now.AddMinutes(i), 100 + i, 110 + i, 90 + i, 105 + i, 1000 + (i * 100));
|
||||
|
||||
// Process update for each bar to simulate history loading
|
||||
var args = new UpdateArgs(UpdateReason.HistoricalBar);
|
||||
indicator.ProcessUpdate(args);
|
||||
}
|
||||
|
||||
// Line series should have a value
|
||||
double val = indicator.LinesSeries[0].GetValue(0);
|
||||
Assert.True(double.IsFinite(val));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void EomIndicator_ProcessUpdate_NewBar_ComputesValue()
|
||||
{
|
||||
var indicator = new EomIndicator();
|
||||
indicator.Initialize();
|
||||
|
||||
var now = DateTime.UtcNow;
|
||||
for (int i = 0; i < 30; i++)
|
||||
{
|
||||
indicator.HistoricalData.AddBar(now.AddMinutes(i), 100 + i, 110 + i, 90 + i, 105 + i, 1000 + (i * 100));
|
||||
}
|
||||
|
||||
indicator.ProcessUpdate(new UpdateArgs(UpdateReason.HistoricalBar));
|
||||
|
||||
// Add new bar
|
||||
indicator.HistoricalData.AddBar(now.AddMinutes(30), 130, 140, 120, 135, 1500);
|
||||
indicator.ProcessUpdate(new UpdateArgs(UpdateReason.NewBar));
|
||||
|
||||
Assert.Equal(2, indicator.LinesSeries[0].Count);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void EomIndicator_Value_IsFinite()
|
||||
{
|
||||
var indicator = new EomIndicator();
|
||||
indicator.Initialize();
|
||||
|
||||
var now = DateTime.UtcNow;
|
||||
for (int i = 0; i < 50; i++)
|
||||
{
|
||||
// Create varying price patterns with price ranges
|
||||
double open = 100 + i;
|
||||
double high = open + 10 + (i % 5);
|
||||
double low = open - 5;
|
||||
double close = (i % 2 == 0) ? high - 1 : low + 1;
|
||||
double volume = 1000 + (i * 100);
|
||||
|
||||
indicator.HistoricalData.AddBar(now.AddMinutes(i), open, high, low, close, volume);
|
||||
indicator.ProcessUpdate(new UpdateArgs(UpdateReason.HistoricalBar));
|
||||
}
|
||||
|
||||
double val = indicator.LinesSeries[0].GetValue(0);
|
||||
Assert.True(double.IsFinite(val), $"EOM value {val} should be finite");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void EomIndicator_PositiveValue_OnUpwardMovement()
|
||||
{
|
||||
var indicator = new EomIndicator { Period = 3 };
|
||||
indicator.Initialize();
|
||||
|
||||
var now = DateTime.UtcNow;
|
||||
|
||||
// First bar: baseline
|
||||
indicator.HistoricalData.AddBar(now, 100, 105, 95, 100, 1000);
|
||||
indicator.ProcessUpdate(new UpdateArgs(UpdateReason.HistoricalBar));
|
||||
|
||||
// Add bars with increasing midpoints (price moving up) with low volume (easy movement)
|
||||
for (int i = 1; i <= 10; i++)
|
||||
{
|
||||
double basePrice = 100 + (i * 5);
|
||||
indicator.HistoricalData.AddBar(now.AddMinutes(i), basePrice, basePrice + 10, basePrice - 10, basePrice + 5, 500);
|
||||
indicator.ProcessUpdate(new UpdateArgs(UpdateReason.HistoricalBar));
|
||||
}
|
||||
|
||||
double val = indicator.LinesSeries[0].GetValue(0);
|
||||
Assert.True(val > 0, $"EOM should be positive on sustained upward movement, got {val}");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void EomIndicator_NegativeValue_OnDownwardMovement()
|
||||
{
|
||||
var indicator = new EomIndicator { Period = 3 };
|
||||
indicator.Initialize();
|
||||
|
||||
var now = DateTime.UtcNow;
|
||||
|
||||
// First bar: baseline
|
||||
indicator.HistoricalData.AddBar(now, 150, 160, 140, 150, 1000);
|
||||
indicator.ProcessUpdate(new UpdateArgs(UpdateReason.HistoricalBar));
|
||||
|
||||
// Add bars with decreasing midpoints (price moving down) with low volume (easy movement)
|
||||
for (int i = 1; i <= 10; i++)
|
||||
{
|
||||
double basePrice = 150 - (i * 5);
|
||||
indicator.HistoricalData.AddBar(now.AddMinutes(i), basePrice, basePrice + 10, basePrice - 10, basePrice - 5, 500);
|
||||
indicator.ProcessUpdate(new UpdateArgs(UpdateReason.HistoricalBar));
|
||||
}
|
||||
|
||||
double val = indicator.LinesSeries[0].GetValue(0);
|
||||
Assert.True(val < 0, $"EOM should be negative on sustained downward movement, got {val}");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void EomIndicator_VolumeScale_AffectsOutput()
|
||||
{
|
||||
var indicator1 = new EomIndicator { Period = 5, VolumeScale = 10000 };
|
||||
var indicator2 = new EomIndicator { Period = 5, VolumeScale = 100000 };
|
||||
indicator1.Initialize();
|
||||
indicator2.Initialize();
|
||||
|
||||
var now = DateTime.UtcNow;
|
||||
for (int i = 0; i < 20; i++)
|
||||
{
|
||||
double basePrice = 100 + i;
|
||||
indicator1.HistoricalData.AddBar(now.AddMinutes(i), basePrice, basePrice + 5, basePrice - 5, basePrice + 2, 50000);
|
||||
indicator2.HistoricalData.AddBar(now.AddMinutes(i), basePrice, basePrice + 5, basePrice - 5, basePrice + 2, 50000);
|
||||
indicator1.ProcessUpdate(new UpdateArgs(UpdateReason.HistoricalBar));
|
||||
indicator2.ProcessUpdate(new UpdateArgs(UpdateReason.HistoricalBar));
|
||||
}
|
||||
|
||||
double val1 = indicator1.LinesSeries[0].GetValue(0);
|
||||
double val2 = indicator2.LinesSeries[0].GetValue(0);
|
||||
|
||||
// Different volume scales should produce different magnitude results
|
||||
Assert.NotEqual(val1, val2);
|
||||
Assert.True(double.IsFinite(val1));
|
||||
Assert.True(double.IsFinite(val2));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,369 @@
|
||||
using Xunit;
|
||||
|
||||
namespace QuanTAlib.Tests;
|
||||
|
||||
public class EomTests
|
||||
{
|
||||
private const int DefaultPeriod = 14;
|
||||
private const double DefaultVolumeScale = 10000;
|
||||
|
||||
[Fact]
|
||||
public void Constructor_DefaultParameters_CreatesValidIndicator()
|
||||
{
|
||||
var eom = new Eom();
|
||||
Assert.Equal($"Eom({DefaultPeriod},{DefaultVolumeScale:F0})", eom.Name);
|
||||
Assert.Equal(DefaultPeriod + 1, eom.WarmupPeriod);
|
||||
Assert.False(eom.IsHot);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Constructor_CustomParameters_CreatesValidIndicator()
|
||||
{
|
||||
var eom = new Eom(period: 20, volumeScale: 50000);
|
||||
Assert.Equal("Eom(20,50000)", eom.Name);
|
||||
Assert.Equal(21, eom.WarmupPeriod);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Constructor_InvalidPeriod_ThrowsArgumentException()
|
||||
{
|
||||
Assert.Throws<ArgumentException>(() => new Eom(period: 0));
|
||||
Assert.Throws<ArgumentException>(() => new Eom(period: -1));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Constructor_InvalidVolumeScale_ThrowsArgumentException()
|
||||
{
|
||||
Assert.Throws<ArgumentException>(() => new Eom(volumeScale: 0));
|
||||
Assert.Throws<ArgumentException>(() => new Eom(volumeScale: -1));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Update_WithTBar_ReturnsValidValue()
|
||||
{
|
||||
var eom = new Eom();
|
||||
var bar = new TBar(DateTime.UtcNow, 100, 110, 90, 105, 1000000);
|
||||
var result = eom.Update(bar);
|
||||
Assert.True(double.IsFinite(result.Value));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Update_WithTValue_ThrowsNotSupportedException()
|
||||
{
|
||||
var eom = new Eom();
|
||||
var value = new TValue(DateTime.UtcNow, 100);
|
||||
Assert.Throws<NotSupportedException>(() => eom.Update(value));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Update_FirstBar_ReturnsZero()
|
||||
{
|
||||
var eom = new Eom();
|
||||
var bar = new TBar(DateTime.UtcNow, 100, 110, 90, 105, 1000000);
|
||||
var result = eom.Update(bar);
|
||||
// First bar has no previous midpoint, so raw EOM is 0
|
||||
Assert.Equal(0.0, result.Value);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Update_PriceIncrease_ReturnsPositiveValue()
|
||||
{
|
||||
var eom = new Eom(period: 1, volumeScale: 10000);
|
||||
// First bar
|
||||
eom.Update(new TBar(DateTime.UtcNow, 100, 105, 95, 102, 100000));
|
||||
// Second bar with price increase
|
||||
var result = eom.Update(new TBar(DateTime.UtcNow, 102, 115, 100, 112, 100000));
|
||||
Assert.True(result.Value > 0, "Price increase should result in positive EOM");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Update_PriceDecrease_ReturnsNegativeValue()
|
||||
{
|
||||
var eom = new Eom(period: 1, volumeScale: 10000);
|
||||
// First bar
|
||||
eom.Update(new TBar(DateTime.UtcNow, 110, 115, 105, 112, 100000));
|
||||
// Second bar with price decrease
|
||||
var result = eom.Update(new TBar(DateTime.UtcNow, 108, 105, 90, 92, 100000));
|
||||
Assert.True(result.Value < 0, "Price decrease should result in negative EOM");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Update_IsNewTrue_AdvancesState()
|
||||
{
|
||||
var eom = new Eom();
|
||||
var bar1 = new TBar(DateTime.UtcNow, 100, 110, 90, 105, 1000000);
|
||||
var result1 = eom.Update(bar1, isNew: true);
|
||||
|
||||
var bar2 = new TBar(DateTime.UtcNow.AddMinutes(1), 105, 115, 95, 110, 1100000);
|
||||
var result2 = eom.Update(bar2, isNew: true);
|
||||
|
||||
Assert.NotEqual(result1.Time, result2.Time);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Update_IsNewFalse_UpdatesCurrentBar()
|
||||
{
|
||||
var eom = new Eom();
|
||||
var time = DateTime.UtcNow;
|
||||
var bar1 = new TBar(time, 100, 110, 90, 105, 1000000);
|
||||
eom.Update(bar1, isNew: true);
|
||||
|
||||
var bar2 = new TBar(time.AddMinutes(1), 105, 115, 95, 110, 1100000);
|
||||
var result1 = eom.Update(bar2, isNew: true);
|
||||
|
||||
// Update same bar with different values (using same time)
|
||||
var bar2Updated = new TBar(time.AddMinutes(1), 105, 120, 95, 118, 1200000);
|
||||
var result2 = eom.Update(bar2Updated, isNew: false);
|
||||
|
||||
Assert.Equal(result1.Time, result2.Time);
|
||||
Assert.NotEqual(result1.Value, result2.Value);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Update_IterativeCorrections_UpdatesCurrentValue()
|
||||
{
|
||||
var eom = new Eom(period: 3);
|
||||
var time = DateTime.UtcNow;
|
||||
|
||||
// Build up some state
|
||||
eom.Update(new TBar(time, 100, 110, 90, 105, 100000), isNew: true);
|
||||
eom.Update(new TBar(time.AddMinutes(1), 105, 115, 95, 110, 110000), isNew: true);
|
||||
|
||||
// Original bar 3
|
||||
var bar3 = new TBar(time.AddMinutes(2), 110, 120, 100, 115, 120000);
|
||||
var originalResult = eom.Update(bar3, isNew: true);
|
||||
|
||||
// Make a correction with different values
|
||||
var correctionBar = new TBar(time.AddMinutes(2), 100, 150, 80, 130, 200000);
|
||||
var correctedResult = eom.Update(correctionBar, isNew: false);
|
||||
|
||||
// Values should differ due to different bar data
|
||||
Assert.NotEqual(originalResult.Value, correctedResult.Value);
|
||||
|
||||
// Verify the correction actually changed the value
|
||||
Assert.True(double.IsFinite(correctedResult.Value));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Update_WarmupPeriod_IsHotBecomesTrueAfterWarmup()
|
||||
{
|
||||
var eom = new Eom(period: 3);
|
||||
var time = DateTime.UtcNow;
|
||||
|
||||
Assert.False(eom.IsHot);
|
||||
eom.Update(new TBar(time, 100, 110, 90, 105, 100000), isNew: true);
|
||||
Assert.False(eom.IsHot);
|
||||
eom.Update(new TBar(time.AddMinutes(1), 105, 115, 95, 110, 110000), isNew: true);
|
||||
Assert.False(eom.IsHot);
|
||||
eom.Update(new TBar(time.AddMinutes(2), 110, 120, 100, 115, 120000), isNew: true);
|
||||
|
||||
// After period bars, should be hot (count >= period and has prev midpoint)
|
||||
Assert.True(eom.IsHot);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Update_WithNaN_UsesLastValidValue()
|
||||
{
|
||||
var eom = new Eom(period: 3, volumeScale: 10000);
|
||||
|
||||
// Process some valid bars first
|
||||
eom.Update(new TBar(DateTime.UtcNow, 100, 105, 95, 102, 100000));
|
||||
eom.Update(new TBar(DateTime.UtcNow.AddMinutes(1), 102, 108, 98, 105, 110000));
|
||||
|
||||
// Process bar with NaN volume (will cause NaN in calculation)
|
||||
var nanBar = new TBar(DateTime.UtcNow.AddMinutes(2), 105, 110, 100, 108, double.NaN);
|
||||
var result = eom.Update(nanBar);
|
||||
|
||||
Assert.True(double.IsFinite(result.Value));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Update_ZeroPriceRange_ReturnsZeroEom()
|
||||
{
|
||||
var eom = new Eom(period: 1, volumeScale: 10000);
|
||||
eom.Update(new TBar(DateTime.UtcNow, 100, 100, 100, 100, 100000));
|
||||
var result = eom.Update(new TBar(DateTime.UtcNow.AddMinutes(1), 105, 105, 105, 105, 100000));
|
||||
Assert.Equal(0.0, result.Value);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Update_ZeroVolume_ReturnsZeroEom()
|
||||
{
|
||||
var eom = new Eom(period: 1, volumeScale: 10000);
|
||||
eom.Update(new TBar(DateTime.UtcNow, 100, 110, 90, 105, 100000));
|
||||
var result = eom.Update(new TBar(DateTime.UtcNow.AddMinutes(1), 105, 115, 95, 110, 0));
|
||||
Assert.Equal(0.0, result.Value);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Reset_ClearsState()
|
||||
{
|
||||
var eom = new Eom(period: 3);
|
||||
var time = DateTime.UtcNow;
|
||||
|
||||
// Process some bars
|
||||
eom.Update(new TBar(time, 100, 110, 90, 105, 100000), isNew: true);
|
||||
eom.Update(new TBar(time.AddMinutes(1), 105, 115, 95, 110, 110000), isNew: true);
|
||||
eom.Update(new TBar(time.AddMinutes(2), 110, 120, 100, 115, 120000), isNew: true);
|
||||
|
||||
Assert.True(eom.IsHot);
|
||||
|
||||
eom.Reset();
|
||||
|
||||
Assert.False(eom.IsHot);
|
||||
Assert.Equal(default, eom.Last);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void BatchCalculate_MatchesStreaming()
|
||||
{
|
||||
var bars = new TBarSeries();
|
||||
var gbm = new GBM(seed: 42);
|
||||
|
||||
for (int i = 0; i < 100; i++)
|
||||
{
|
||||
bars.Add(gbm.Next());
|
||||
}
|
||||
|
||||
// Streaming
|
||||
var eom = new Eom();
|
||||
var streamingValues = new List<double>();
|
||||
foreach (var bar in bars)
|
||||
{
|
||||
streamingValues.Add(eom.Update(bar).Value);
|
||||
}
|
||||
|
||||
// Batch
|
||||
var batchResult = Eom.Batch(bars);
|
||||
|
||||
Assert.Equal(bars.Count, batchResult.Count);
|
||||
for (int i = 0; i < bars.Count; i++)
|
||||
{
|
||||
Assert.Equal(streamingValues[i], batchResult[i].Value, 10);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SpanCalculate_MatchesStreaming()
|
||||
{
|
||||
var bars = new TBarSeries();
|
||||
var gbm = new GBM(seed: 42);
|
||||
|
||||
for (int i = 0; i < 100; i++)
|
||||
{
|
||||
bars.Add(gbm.Next());
|
||||
}
|
||||
|
||||
// Streaming
|
||||
var eom = new Eom();
|
||||
var streamingValues = new List<double>();
|
||||
foreach (var bar in bars)
|
||||
{
|
||||
streamingValues.Add(eom.Update(bar).Value);
|
||||
}
|
||||
|
||||
// Span
|
||||
var high = bars.High.Values.ToArray();
|
||||
var low = bars.Low.Values.ToArray();
|
||||
var volume = bars.Volume.Values.ToArray();
|
||||
var spanValues = new double[bars.Count];
|
||||
|
||||
Eom.Batch(high, low, volume, spanValues);
|
||||
|
||||
for (int i = 0; i < bars.Count; i++)
|
||||
{
|
||||
Assert.Equal(streamingValues[i], spanValues[i], 10);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SpanCalculate_InvalidLengths_ThrowsArgumentException()
|
||||
{
|
||||
var high = new double[100];
|
||||
var low = new double[99]; // Different length
|
||||
var volume = new double[100];
|
||||
var output = new double[100];
|
||||
|
||||
Assert.Throws<ArgumentException>(() => Eom.Batch(high, low, volume, output));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SpanCalculate_InvalidPeriod_ThrowsArgumentException()
|
||||
{
|
||||
var high = new double[100];
|
||||
var low = new double[100];
|
||||
var volume = new double[100];
|
||||
var output = new double[100];
|
||||
|
||||
Assert.Throws<ArgumentException>(() => Eom.Batch(high, low, volume, output, period: 0));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SpanCalculate_InvalidVolumeScale_ThrowsArgumentException()
|
||||
{
|
||||
var high = new double[100];
|
||||
var low = new double[100];
|
||||
var volume = new double[100];
|
||||
var output = new double[100];
|
||||
|
||||
Assert.Throws<ArgumentException>(() => Eom.Batch(high, low, volume, output, volumeScale: 0));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SpanCalculate_LargeData_UsesArrayPool()
|
||||
{
|
||||
int size = 1000; // > 256 threshold
|
||||
var high = new double[size];
|
||||
var low = new double[size];
|
||||
var volume = new double[size];
|
||||
var output = new double[size];
|
||||
|
||||
for (int i = 0; i < size; i++)
|
||||
{
|
||||
high[i] = 110 + i * 0.1;
|
||||
low[i] = 90 + i * 0.1;
|
||||
volume[i] = 100000;
|
||||
}
|
||||
|
||||
// Should not throw
|
||||
Eom.Batch(high, low, volume, output);
|
||||
Assert.True(double.IsFinite(output[size - 1]));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Event_PubFiresOnUpdate()
|
||||
{
|
||||
var eom = new Eom();
|
||||
TValue? receivedValue = null;
|
||||
bool receivedIsNew = false;
|
||||
|
||||
eom.Pub += (object? sender, in TValueEventArgs args) =>
|
||||
{
|
||||
receivedValue = args.Value;
|
||||
receivedIsNew = args.IsNew;
|
||||
};
|
||||
|
||||
var bar = new TBar(DateTime.UtcNow, 100, 110, 90, 105, 1000000);
|
||||
eom.Update(bar, isNew: true);
|
||||
|
||||
Assert.NotNull(receivedValue);
|
||||
Assert.True(receivedIsNew);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void VolumeScale_AffectsResult()
|
||||
{
|
||||
var eom1 = new Eom(period: 1, volumeScale: 10000);
|
||||
var eom2 = new Eom(period: 1, volumeScale: 100000);
|
||||
|
||||
var bar1 = new TBar(DateTime.UtcNow, 100, 110, 90, 105, 1000000);
|
||||
var bar2 = new TBar(DateTime.UtcNow.AddMinutes(1), 105, 120, 95, 115, 1000000);
|
||||
|
||||
eom1.Update(bar1); eom1.Update(bar2);
|
||||
eom2.Update(bar1); eom2.Update(bar2);
|
||||
|
||||
// Different volume scales should produce different results
|
||||
Assert.NotEqual(eom1.Last.Value, eom2.Last.Value);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,140 @@
|
||||
using Xunit.Abstractions;
|
||||
|
||||
namespace QuanTAlib.Tests;
|
||||
|
||||
/// <summary>
|
||||
/// Ease of Movement validation tests.
|
||||
/// Tulip has emv (Ease of Movement Value) but outputs raw unsmoothed values
|
||||
/// without volume scaling, while QuanTAlib applies SMA(period) smoothing with
|
||||
/// configurable volumeScale (default 10000). Direct comparison not possible.
|
||||
/// Skender, TA-Lib, and Ooples do not have EOM implementations.
|
||||
/// </summary>
|
||||
public sealed class EomValidationTests : IDisposable
|
||||
{
|
||||
private readonly ValidationTestData _data;
|
||||
private readonly ITestOutputHelper _output;
|
||||
private const int DefaultPeriod = 14;
|
||||
|
||||
public EomValidationTests(ITestOutputHelper output)
|
||||
{
|
||||
_data = new ValidationTestData();
|
||||
_output = output;
|
||||
}
|
||||
|
||||
public void Dispose() { /* nothing to dispose */ }
|
||||
|
||||
[Fact]
|
||||
public void Eom_Matches_Tulip_Directional_Agreement()
|
||||
{
|
||||
// Tulip emv: inputs={high, low, volume}, options={}, outputs={emv}
|
||||
// Tulip computes raw EMV without SMA smoothing or volumeScale division.
|
||||
// QuanTAlib EOM = SMA(raw_eom / volumeScale, period).
|
||||
// We can only verify directional agreement (sign correlation) after warmup.
|
||||
var high = _data.Bars.High.Values.ToArray();
|
||||
var low = _data.Bars.Low.Values.ToArray();
|
||||
var volume = _data.Bars.Volume.Values.ToArray();
|
||||
|
||||
var tulipIndicator = Tulip.Indicators.emv;
|
||||
double[][] inputs = { high, low, volume };
|
||||
double[] options = Array.Empty<double>();
|
||||
double[][] outputs = { new double[high.Length] };
|
||||
|
||||
tulipIndicator.Run(inputs, options, outputs);
|
||||
double[] tResult = outputs[0];
|
||||
int lookback = tulipIndicator.Start(options);
|
||||
|
||||
// QuanTAlib EOM with period=1 (no smoothing) for directional comparison
|
||||
var eom = new Eom(1);
|
||||
var qValues = new double[_data.Bars.Count];
|
||||
int idx = 0;
|
||||
foreach (var bar in _data.Bars)
|
||||
{
|
||||
qValues[idx++] = eom.Update(bar).Value;
|
||||
}
|
||||
|
||||
_output.WriteLine($"Tulip EMV lookback: {lookback}, output length: {tResult.Length}");
|
||||
|
||||
// Verify directional agreement (both positive or both negative) in most bars
|
||||
int agreementCount = 0;
|
||||
int totalCompared = 0;
|
||||
int startIdx = lookback + 5;
|
||||
for (int i = startIdx; i < qValues.Length && (i - lookback) < tResult.Length; i++)
|
||||
{
|
||||
double qValue = qValues[i];
|
||||
double tValue = tResult[i - lookback];
|
||||
|
||||
// Skip near-zero values where sign is meaningless
|
||||
if (Math.Abs(qValue) < 1e-10 || Math.Abs(tValue) < 1e-10)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
totalCompared++;
|
||||
if (Math.Sign(qValue) == Math.Sign(tValue))
|
||||
{
|
||||
agreementCount++;
|
||||
}
|
||||
}
|
||||
|
||||
double agreementRate = totalCompared > 0 ? (double)agreementCount / totalCompared : 0;
|
||||
_output.WriteLine($"Tulip EMV directional agreement: {agreementCount}/{totalCompared} ({agreementRate:P1})");
|
||||
|
||||
// With period=1, directional agreement should be high (>80%)
|
||||
Assert.True(agreementRate > 0.80,
|
||||
$"EOM directional agreement with Tulip EMV should be >80%, got {agreementRate:P1}");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Eom_Matches_Skender()
|
||||
{
|
||||
// Skender does not have Ease of Movement implementation
|
||||
Assert.True(true, "Skender does not have an Ease of Movement implementation");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Eom_Matches_Talib()
|
||||
{
|
||||
// TA-Lib does not have EOM/Ease of Movement
|
||||
Assert.True(true, "TA-Lib does not have an Ease of Movement implementation");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Eom_Streaming_Matches_Batch()
|
||||
{
|
||||
// Streaming
|
||||
var eom = new Eom(DefaultPeriod);
|
||||
var streamingValues = new List<double>();
|
||||
foreach (var bar in _data.Bars)
|
||||
{
|
||||
streamingValues.Add(eom.Update(bar).Value);
|
||||
}
|
||||
|
||||
// Batch
|
||||
var batchResult = Eom.Batch(_data.Bars, DefaultPeriod);
|
||||
var batchValues = batchResult.Values.ToArray();
|
||||
|
||||
ValidationHelper.VerifyData(streamingValues.ToArray(), batchValues, 0, 100, 1e-9);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Eom_Span_Matches_Streaming()
|
||||
{
|
||||
// Streaming
|
||||
var eom = new Eom(DefaultPeriod);
|
||||
var streamingValues = new List<double>();
|
||||
foreach (var bar in _data.Bars)
|
||||
{
|
||||
streamingValues.Add(eom.Update(bar).Value);
|
||||
}
|
||||
|
||||
// Span
|
||||
var high = _data.Bars.High.Values.ToArray();
|
||||
var low = _data.Bars.Low.Values.ToArray();
|
||||
var volume = _data.Bars.Volume.Values.ToArray();
|
||||
var spanValues = new double[high.Length];
|
||||
|
||||
Eom.Batch(high, low, volume, spanValues, DefaultPeriod);
|
||||
|
||||
ValidationHelper.VerifyData(streamingValues.ToArray(), spanValues, 0, 100, 1e-9);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user