mirror of
https://github.com/mihakralj/QuanTAlib.git
synced 2026-08-25 22:08:05 +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,168 @@
|
||||
using TradingPlatform.BusinessLayer;
|
||||
|
||||
namespace QuanTAlib.Tests;
|
||||
|
||||
public class EmaIndicatorTests
|
||||
{
|
||||
[Fact]
|
||||
public void EmaIndicator_Constructor_SetsDefaults()
|
||||
{
|
||||
var indicator = new EmaIndicator();
|
||||
|
||||
Assert.Equal(10, indicator.Period);
|
||||
Assert.Equal(SourceType.Close, indicator.Source);
|
||||
Assert.True(indicator.ShowColdValues);
|
||||
Assert.Equal("EMA - Exponential Moving Average", indicator.Name);
|
||||
Assert.False(indicator.SeparateWindow);
|
||||
Assert.True(indicator.OnBackGround);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void EmaIndicator_MinHistoryDepths_EqualsZero()
|
||||
{
|
||||
var indicator = new EmaIndicator { Period = 20 };
|
||||
|
||||
Assert.Equal(0, EmaIndicator.MinHistoryDepths);
|
||||
Assert.Equal(0, ((IWatchlistIndicator)indicator).MinHistoryDepths);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void EmaIndicator_ShortName_IncludesPeriodAndSource()
|
||||
{
|
||||
var indicator = new EmaIndicator { Period = 15 };
|
||||
|
||||
Assert.Contains("EMA", indicator.ShortName, StringComparison.Ordinal);
|
||||
Assert.Contains("15", indicator.ShortName, StringComparison.Ordinal);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void EmaIndicator_Initialize_CreatesInternalEma()
|
||||
{
|
||||
var indicator = new EmaIndicator { Period = 10 };
|
||||
|
||||
// Initialize should not throw
|
||||
indicator.Initialize();
|
||||
|
||||
// After init, line series should exist
|
||||
Assert.Single(indicator.LinesSeries);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void EmaIndicator_ProcessUpdate_HistoricalBar_ComputesValue()
|
||||
{
|
||||
var indicator = new EmaIndicator { 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 EmaIndicator_ProcessUpdate_NewBar_ComputesValue()
|
||||
{
|
||||
var indicator = new EmaIndicator { Period = 3 };
|
||||
indicator.Initialize();
|
||||
|
||||
// Add historical data
|
||||
var now = DateTime.UtcNow;
|
||||
indicator.HistoricalData.AddBar(now, 100, 105, 95, 102);
|
||||
indicator.HistoricalData.AddBar(now.AddMinutes(1), 102, 108, 100, 106);
|
||||
|
||||
// Process first update
|
||||
indicator.ProcessUpdate(new UpdateArgs(UpdateReason.HistoricalBar));
|
||||
indicator.ProcessUpdate(new UpdateArgs(UpdateReason.NewBar));
|
||||
|
||||
// Line series should have values
|
||||
Assert.Equal(2, indicator.LinesSeries[0].Count);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void EmaIndicator_ProcessUpdate_NewTick_ProcessesWithoutError()
|
||||
{
|
||||
var indicator = new EmaIndicator { Period = 3 };
|
||||
indicator.Initialize();
|
||||
|
||||
// Add historical data
|
||||
var now = DateTime.UtcNow;
|
||||
indicator.HistoricalData.AddBar(now, 100, 105, 95, 102);
|
||||
|
||||
// Process historical bar first
|
||||
indicator.ProcessUpdate(new UpdateArgs(UpdateReason.HistoricalBar));
|
||||
double firstValue = indicator.LinesSeries[0].GetValue(0);
|
||||
|
||||
// Update with new tick (same bar data - simulates intrabar update)
|
||||
indicator.ProcessUpdate(new UpdateArgs(UpdateReason.NewTick));
|
||||
double secondValue = indicator.LinesSeries[0].GetValue(0);
|
||||
|
||||
// Both values should be finite
|
||||
Assert.True(double.IsFinite(firstValue));
|
||||
Assert.True(double.IsFinite(secondValue));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void EmaIndicator_MultipleUpdates_ProducesCorrectEmaSequence()
|
||||
{
|
||||
var indicator = new EmaIndicator { Period = 3 };
|
||||
indicator.Initialize();
|
||||
|
||||
var now = DateTime.UtcNow;
|
||||
double[] closes = { 100, 102, 104, 103, 105, 107, 106 };
|
||||
|
||||
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)));
|
||||
}
|
||||
|
||||
// EMA should be smoothing the values
|
||||
// Last EMA value should be between first and last close
|
||||
double lastEma = indicator.LinesSeries[0].GetValue(0);
|
||||
Assert.True(lastEma >= 100 && lastEma <= 110);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void EmaIndicator_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 EmaIndicator { 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 EmaIndicator_Period_CanBeChanged()
|
||||
{
|
||||
var indicator = new EmaIndicator { Period = 5 };
|
||||
Assert.Equal(5, indicator.Period);
|
||||
|
||||
indicator.Period = 20;
|
||||
Assert.Equal(20, indicator.Period);
|
||||
Assert.Equal(0, EmaIndicator.MinHistoryDepths);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,707 @@
|
||||
namespace QuanTAlib.Tests;
|
||||
|
||||
public class EmaTests
|
||||
{
|
||||
[Fact]
|
||||
public void Ema_Constructor_Period_ValidatesInput()
|
||||
{
|
||||
Assert.Throws<ArgumentOutOfRangeException>(() => new Ema(0));
|
||||
Assert.Throws<ArgumentOutOfRangeException>(() => new Ema(-1));
|
||||
|
||||
var ema = new Ema(10);
|
||||
Assert.NotNull(ema);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Ema_Constructor_Alpha_ValidatesInput()
|
||||
{
|
||||
Assert.Throws<ArgumentException>(() => new Ema(0.0));
|
||||
Assert.Throws<ArgumentException>(() => new Ema(-0.1));
|
||||
Assert.Throws<ArgumentException>(() => new Ema(1.1));
|
||||
|
||||
var ema = new Ema(0.5);
|
||||
Assert.NotNull(ema);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Ema_Calc_ReturnsValue()
|
||||
{
|
||||
var ema = new Ema(10);
|
||||
|
||||
Assert.Equal(0, ema.Last.Value);
|
||||
|
||||
TValue result = ema.Update(new TValue(DateTime.UtcNow, 100));
|
||||
|
||||
Assert.True(result.Value > 0);
|
||||
Assert.Equal(result.Value, ema.Last.Value);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Ema_Calc_IsNew_AcceptsParameter()
|
||||
{
|
||||
var ema = new Ema(10);
|
||||
|
||||
ema.Update(new TValue(DateTime.UtcNow, 100), isNew: true);
|
||||
double value1 = ema.Last.Value;
|
||||
|
||||
ema.Update(new TValue(DateTime.UtcNow, 105), isNew: true);
|
||||
double value2 = ema.Last.Value;
|
||||
|
||||
// Values should change with new bars
|
||||
Assert.NotEqual(value1, value2);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Ema_Calc_IsNew_False_UpdatesValue()
|
||||
{
|
||||
var ema = new Ema(10);
|
||||
|
||||
ema.Update(new TValue(DateTime.UtcNow, 100));
|
||||
ema.Update(new TValue(DateTime.UtcNow, 110), isNew: true);
|
||||
double beforeUpdate = ema.Last.Value;
|
||||
|
||||
ema.Update(new TValue(DateTime.UtcNow, 120), isNew: false);
|
||||
double afterUpdate = ema.Last.Value;
|
||||
|
||||
// Update should change the value
|
||||
Assert.NotEqual(beforeUpdate, afterUpdate);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Ema_Reset_ClearsState()
|
||||
{
|
||||
var ema = new Ema(10);
|
||||
|
||||
ema.Update(new TValue(DateTime.UtcNow, 100));
|
||||
ema.Update(new TValue(DateTime.UtcNow, 105));
|
||||
double valueBefore = ema.Last.Value;
|
||||
|
||||
ema.Reset();
|
||||
|
||||
Assert.Equal(0, ema.Last.Value);
|
||||
|
||||
// After reset, should accept new values
|
||||
ema.Update(new TValue(DateTime.UtcNow, 50));
|
||||
Assert.NotEqual(0, ema.Last.Value);
|
||||
Assert.NotEqual(valueBefore, ema.Last.Value);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Ema_Properties_Accessible()
|
||||
{
|
||||
var ema = new Ema(10);
|
||||
|
||||
Assert.Equal(0, ema.Last.Value);
|
||||
Assert.False(ema.IsHot);
|
||||
|
||||
ema.Update(new TValue(DateTime.UtcNow, 100));
|
||||
|
||||
Assert.NotEqual(0, ema.Last.Value);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Ema_IsHot_BecomesTrueAt95PercentCoverage()
|
||||
{
|
||||
var ema = new Ema(10);
|
||||
|
||||
// Initially IsHot should be false
|
||||
Assert.False(ema.IsHot);
|
||||
|
||||
// IsHot triggers at 95% coverage (E <= 0.05)
|
||||
// E = (1 - alpha)^N where alpha = 2 / (period + 1)
|
||||
// For period 10: alpha = 2/11 ≈ 0.1818, (1-alpha) ≈ 0.8182
|
||||
// N = ln(0.05) / ln(0.8182) ≈ 14.93, so ~15 bars
|
||||
|
||||
int steps = 0;
|
||||
while (!ema.IsHot && steps < 1000)
|
||||
{
|
||||
ema.Update(new TValue(DateTime.UtcNow, 100));
|
||||
steps++;
|
||||
}
|
||||
|
||||
Assert.True(ema.IsHot);
|
||||
Assert.True(steps > 0);
|
||||
// For period 10, should become hot around 15 bars
|
||||
Assert.InRange(steps, 14, 16);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Ema_IsHot_IsPeriodDependent()
|
||||
{
|
||||
// Test that different periods result in different warmup times
|
||||
// Formula: N = ln(0.05) / ln((p-1)/(p+1))
|
||||
|
||||
int[] periods = [10, 20, 50, 100];
|
||||
int[] expectedSteps = new int[periods.Length];
|
||||
|
||||
for (int i = 0; i < periods.Length; i++)
|
||||
{
|
||||
int period = periods[i];
|
||||
var ema = new Ema(period);
|
||||
|
||||
int steps = 0;
|
||||
while (!ema.IsHot && steps < 500)
|
||||
{
|
||||
ema.Update(new TValue(DateTime.UtcNow, 100));
|
||||
steps++;
|
||||
}
|
||||
|
||||
expectedSteps[i] = steps;
|
||||
}
|
||||
|
||||
// Verify warmup times increase with period
|
||||
// Period 10 → ~15 bars, Period 20 → ~30 bars, Period 50 → ~75 bars, Period 100 → ~150 bars
|
||||
Assert.True(expectedSteps[0] < expectedSteps[1], $"Period 10 ({expectedSteps[0]}) should be less than Period 20 ({expectedSteps[1]})");
|
||||
Assert.True(expectedSteps[1] < expectedSteps[2], $"Period 20 ({expectedSteps[1]}) should be less than Period 50 ({expectedSteps[2]})");
|
||||
Assert.True(expectedSteps[2] < expectedSteps[3], $"Period 50 ({expectedSteps[2]}) should be less than Period 100 ({expectedSteps[3]})");
|
||||
|
||||
// Verify approximate expected values (N ≈ 1.5 * period for 95% coverage)
|
||||
Assert.InRange(expectedSteps[0], 14, 17); // Period 10 → ~15
|
||||
Assert.InRange(expectedSteps[1], 28, 32); // Period 20 → ~30
|
||||
Assert.InRange(expectedSteps[2], 73, 78); // Period 50 → ~75
|
||||
Assert.InRange(expectedSteps[3], 147, 153); // Period 100 → ~150
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Ema_PeriodEquivalence_BothConstructorsWork()
|
||||
{
|
||||
const int period = 20;
|
||||
double alpha = 2.0 / (period + 1);
|
||||
|
||||
var emaPeriod = new Ema(period);
|
||||
var emaAlpha = new Ema(alpha);
|
||||
|
||||
// Both should accept Calc calls and produce same result
|
||||
TValue result1 = emaPeriod.Update(new TValue(DateTime.UtcNow, 100));
|
||||
TValue result2 = emaAlpha.Update(new TValue(DateTime.UtcNow, 100));
|
||||
|
||||
Assert.Equal(result1.Value, result2.Value, 1e-10);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Ema_IterativeCorrections_RestoreToOriginalState()
|
||||
{
|
||||
var ema = new Ema(10);
|
||||
var gbm = new GBM(startPrice: 100.0, mu: 0.02, sigma: 0.1);
|
||||
|
||||
// Feed 10 new values
|
||||
TValue tenthInput = default;
|
||||
for (int i = 0; i < 10; i++)
|
||||
{
|
||||
var bar = gbm.Next(isNew: true);
|
||||
tenthInput = new TValue(bar.Time, bar.Close);
|
||||
ema.Update(tenthInput, isNew: true);
|
||||
}
|
||||
|
||||
// Remember EMA state after 10 values
|
||||
double emaAfterTen = ema.Last.Value;
|
||||
|
||||
// Generate 9 corrections with isNew=false (different values)
|
||||
for (int i = 0; i < 9; i++)
|
||||
{
|
||||
var bar = gbm.Next(isNew: false);
|
||||
ema.Update(new TValue(bar.Time, bar.Close), isNew: false);
|
||||
}
|
||||
|
||||
// Feed the remembered 10th input again with isNew=false
|
||||
TValue finalEma = ema.Update(tenthInput, isNew: false);
|
||||
|
||||
// EMA should match the original state after 10 values
|
||||
Assert.Equal(emaAfterTen, finalEma.Value, 1e-10);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Ema_BatchCalc_MatchesIterativeCalc()
|
||||
{
|
||||
var emaIterative = new Ema(10);
|
||||
var emaBatch = new Ema(10);
|
||||
var gbm = new GBM(startPrice: 100.0, mu: 0.02, sigma: 0.1);
|
||||
|
||||
// Generate data
|
||||
var series = new TSeries();
|
||||
for (int i = 0; i < 100; i++)
|
||||
{
|
||||
var bar = gbm.Next(isNew: true);
|
||||
series.Add(bar.Time, bar.Close);
|
||||
}
|
||||
|
||||
Assert.True(series.Count > 0);
|
||||
|
||||
// Calculate iteratively
|
||||
var iterativeResults = new TSeries();
|
||||
foreach (var item in series)
|
||||
{
|
||||
iterativeResults.Add(emaIterative.Update(item));
|
||||
}
|
||||
|
||||
// Calculate batch
|
||||
var batchResults = emaBatch.Update(series);
|
||||
|
||||
// Compare
|
||||
Assert.Equal(iterativeResults.Count, batchResults.Count);
|
||||
for (int i = 0; i < iterativeResults.Count; i++)
|
||||
{
|
||||
Assert.Equal(iterativeResults[i].Value, batchResults[i].Value, 1e-10);
|
||||
Assert.Equal(iterativeResults[i].Time, batchResults[i].Time);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Ema_Result_ImplicitConversionToDouble()
|
||||
{
|
||||
var ema = new Ema(10);
|
||||
ema.Update(new TValue(DateTime.UtcNow, 100));
|
||||
|
||||
// This should compile and work because TValue has implicit conversion to double
|
||||
double result = ema.Last.Value;
|
||||
|
||||
Assert.Equal(100.0, result, 1e-10);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Ema_NaN_Input_UsesLastValidValue()
|
||||
{
|
||||
var ema = new Ema(10);
|
||||
|
||||
// Feed some valid values
|
||||
ema.Update(new TValue(DateTime.UtcNow, 100));
|
||||
ema.Update(new TValue(DateTime.UtcNow, 110));
|
||||
|
||||
// Feed NaN - should use last valid value (110)
|
||||
var resultAfterNaN = ema.Update(new TValue(DateTime.UtcNow, double.NaN));
|
||||
|
||||
// Result should be finite (not NaN)
|
||||
Assert.True(double.IsFinite(resultAfterNaN.Value));
|
||||
// EMA should continue to evolve (may differ slightly due to substitution)
|
||||
Assert.NotEqual(0, resultAfterNaN.Value);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Ema_Infinity_Input_UsesLastValidValue()
|
||||
{
|
||||
var ema = new Ema(10);
|
||||
|
||||
// Feed some valid values
|
||||
ema.Update(new TValue(DateTime.UtcNow, 100));
|
||||
ema.Update(new TValue(DateTime.UtcNow, 110));
|
||||
|
||||
// Feed positive infinity - should use last valid value
|
||||
var resultAfterPosInf = ema.Update(new TValue(DateTime.UtcNow, double.PositiveInfinity));
|
||||
Assert.True(double.IsFinite(resultAfterPosInf.Value));
|
||||
|
||||
// Feed negative infinity - should use last valid value
|
||||
var resultAfterNegInf = ema.Update(new TValue(DateTime.UtcNow, double.NegativeInfinity));
|
||||
Assert.True(double.IsFinite(resultAfterNegInf.Value));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Ema_MultipleNaN_ContinuesWithLastValid()
|
||||
{
|
||||
var ema = new Ema(10);
|
||||
|
||||
// Feed valid values
|
||||
ema.Update(new TValue(DateTime.UtcNow, 100));
|
||||
ema.Update(new TValue(DateTime.UtcNow, 110));
|
||||
ema.Update(new TValue(DateTime.UtcNow, 120));
|
||||
|
||||
// Feed multiple NaN values
|
||||
var r1 = ema.Update(new TValue(DateTime.UtcNow, double.NaN));
|
||||
var r2 = ema.Update(new TValue(DateTime.UtcNow, double.NaN));
|
||||
var r3 = ema.Update(new TValue(DateTime.UtcNow, double.NaN));
|
||||
|
||||
// All results should be finite
|
||||
Assert.True(double.IsFinite(r1.Value));
|
||||
Assert.True(double.IsFinite(r2.Value));
|
||||
Assert.True(double.IsFinite(r3.Value));
|
||||
|
||||
// EMA should converge toward last valid value (120) with repeated substitution
|
||||
// Values should be getting closer to 120
|
||||
Assert.True(r3.Value > r1.Value || Math.Abs(r3.Value - 120) < Math.Abs(r1.Value - 120));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Ema_BatchCalc_HandlesNaN()
|
||||
{
|
||||
var ema = new Ema(10);
|
||||
|
||||
// Create series with NaN values interspersed
|
||||
var series = new TSeries();
|
||||
series.Add(DateTime.UtcNow.Ticks, 100);
|
||||
series.Add(DateTime.UtcNow.Ticks + 1, 110);
|
||||
series.Add(DateTime.UtcNow.Ticks + 2, double.NaN);
|
||||
series.Add(DateTime.UtcNow.Ticks + 3, 120);
|
||||
series.Add(DateTime.UtcNow.Ticks + 4, double.PositiveInfinity);
|
||||
series.Add(DateTime.UtcNow.Ticks + 5, 130);
|
||||
|
||||
var results = ema.Update(series);
|
||||
|
||||
// All results should be finite
|
||||
foreach (var result in results)
|
||||
{
|
||||
Assert.True(double.IsFinite(result.Value), $"Expected finite value but got {result.Value}");
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Ema_Reset_ClearsLastValidValue()
|
||||
{
|
||||
var ema = new Ema(10);
|
||||
|
||||
// Feed values including NaN
|
||||
ema.Update(new TValue(DateTime.UtcNow, 100));
|
||||
ema.Update(new TValue(DateTime.UtcNow, double.NaN));
|
||||
|
||||
// Reset
|
||||
ema.Reset();
|
||||
|
||||
// After reset, first valid value should establish new baseline
|
||||
var result = ema.Update(new TValue(DateTime.UtcNow, 50));
|
||||
Assert.Equal(50.0, result.Value, 1e-10);
|
||||
}
|
||||
|
||||
// ============== Span API Tests ==============
|
||||
|
||||
[Fact]
|
||||
public void Ema_SpanBatch_Period_ValidatesInput()
|
||||
{
|
||||
double[] source = [1, 2, 3, 4, 5];
|
||||
double[] output = new double[5];
|
||||
double[] wrongSizeOutput = new double[3];
|
||||
|
||||
// Period must be > 0
|
||||
Assert.Throws<ArgumentException>(() => Ema.Batch(source.AsSpan(), output.AsSpan(), 0));
|
||||
Assert.Throws<ArgumentException>(() => Ema.Batch(source.AsSpan(), output.AsSpan(), -1));
|
||||
|
||||
// Output must be same length as source
|
||||
Assert.Throws<ArgumentException>(() => Ema.Batch(source.AsSpan(), wrongSizeOutput.AsSpan(), 3));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Ema_SpanBatch_Alpha_ValidatesInput()
|
||||
{
|
||||
double[] source = [1, 2, 3, 4, 5];
|
||||
double[] output = new double[5];
|
||||
|
||||
// Alpha must be > 0 and <= 1
|
||||
Assert.Throws<ArgumentOutOfRangeException>(() => Ema.Batch(source.AsSpan(), output.AsSpan(), 0.0));
|
||||
Assert.Throws<ArgumentOutOfRangeException>(() => Ema.Batch(source.AsSpan(), output.AsSpan(), -0.1));
|
||||
Assert.Throws<ArgumentOutOfRangeException>(() => Ema.Batch(source.AsSpan(), output.AsSpan(), 1.1));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Ema_SpanBatch_MatchesTSeriesBatch()
|
||||
{
|
||||
var series = new TSeries();
|
||||
double[] source = new double[100];
|
||||
double[] output = new double[100];
|
||||
|
||||
var gbm = new GBM(startPrice: 100.0, mu: 0.02, sigma: 0.1, seed: 42);
|
||||
for (int i = 0; i < 100; i++)
|
||||
{
|
||||
var bar = gbm.Next(isNew: true);
|
||||
source[i] = bar.Close;
|
||||
series.Add(bar.Time, bar.Close);
|
||||
}
|
||||
|
||||
// Calculate with TSeries API
|
||||
var tseriesResult = Ema.Batch(series, 10);
|
||||
|
||||
// Calculate with Span API
|
||||
Ema.Batch(source.AsSpan(), output.AsSpan(), 10);
|
||||
|
||||
// Compare results - allow small tolerance due to bias correction differences
|
||||
for (int i = 0; i < 100; i++)
|
||||
{
|
||||
Assert.Equal(tseriesResult[i].Value, output[i], 1e-9);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Ema_SpanBatch_PeriodAndAlphaEquivalent()
|
||||
{
|
||||
double[] source = [10, 20, 30, 40, 50, 60, 70, 80, 90, 100];
|
||||
double[] outputPeriod = new double[10];
|
||||
double[] outputAlpha = new double[10];
|
||||
|
||||
int period = 5;
|
||||
double alpha = 2.0 / (period + 1);
|
||||
|
||||
Ema.Batch(source.AsSpan(), outputPeriod.AsSpan(), period);
|
||||
Ema.Batch(source.AsSpan(), outputAlpha.AsSpan(), alpha);
|
||||
|
||||
// Results should be identical
|
||||
for (int i = 0; i < 10; i++)
|
||||
{
|
||||
Assert.Equal(outputPeriod[i], outputAlpha[i], 1e-10);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Ema_SpanBatch_ZeroAllocation()
|
||||
{
|
||||
double[] source = new double[10000];
|
||||
double[] output = new double[10000];
|
||||
|
||||
var gbm = new GBM(startPrice: 100, mu: 0.05, sigma: 0.2, seed: 42);
|
||||
for (int i = 0; i < source.Length; i++)
|
||||
{
|
||||
source[i] = gbm.Next().Close;
|
||||
}
|
||||
|
||||
// Warm up
|
||||
Ema.Batch(source.AsSpan(), output.AsSpan(), 100);
|
||||
|
||||
// This test verifies the method runs without throwing
|
||||
Assert.True(double.IsFinite(output[^1]));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Ema_SpanBatch_HandlesNaN()
|
||||
{
|
||||
double[] source = [100, 110, double.NaN, 120, 130];
|
||||
double[] output = new double[5];
|
||||
|
||||
Ema.Batch(source.AsSpan(), output.AsSpan(), 3);
|
||||
|
||||
// All outputs should be finite
|
||||
foreach (var val in output)
|
||||
{
|
||||
Assert.True(double.IsFinite(val), $"Expected finite value but got {val}");
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Ema_SpanBatch_BiasCorrection_Works()
|
||||
{
|
||||
double[] source = [100, 100, 100, 100, 100];
|
||||
double[] output = new double[5];
|
||||
|
||||
Ema.Batch(source.AsSpan(), output.AsSpan(), 3);
|
||||
|
||||
// With bias correction, first value should equal input
|
||||
Assert.Equal(100.0, output[0], 1e-10);
|
||||
|
||||
// All values should converge to 100 since input is constant
|
||||
foreach (var val in output)
|
||||
{
|
||||
Assert.Equal(100.0, val, 1e-9);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Ema_SpanBatch_Alpha_DirectUsage()
|
||||
{
|
||||
double[] source = [10, 20, 30, 40, 50];
|
||||
double[] output = new double[5];
|
||||
|
||||
// Use alpha = 0.5 directly
|
||||
Ema.Batch(source.AsSpan(), output.AsSpan(), 0.5);
|
||||
|
||||
// Results should be finite and reasonable
|
||||
Assert.True(double.IsFinite(output[^1]));
|
||||
Assert.True(output[^1] > 10 && output[^1] <= 50);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Chainability_Works()
|
||||
{
|
||||
var source = new TSeries();
|
||||
var ema = new Ema(source, 10);
|
||||
|
||||
source.Add(new TValue(DateTime.UtcNow, 100));
|
||||
Assert.Equal(100, ema.Last.Value, 1e-10);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Prime_SetsStateCorrectly()
|
||||
{
|
||||
var ema = new Ema(5);
|
||||
double[] history = [10, 20, 30, 40, 50];
|
||||
|
||||
ema.Prime(history);
|
||||
|
||||
// EMA(5) of 10,20,30,40,50
|
||||
// Alpha = 2/6 = 1/3
|
||||
// 10 -> 10
|
||||
// 20 -> 10 + 1/3(10) = 13.33...
|
||||
// ...
|
||||
// We can verify against a fresh EMA fed with same data
|
||||
var verifyEma = new Ema(5);
|
||||
foreach (var val in history)
|
||||
{
|
||||
verifyEma.Update(new TValue(DateTime.UtcNow, val));
|
||||
}
|
||||
|
||||
Assert.Equal(verifyEma.Last.Value, ema.Last.Value, 1e-10);
|
||||
Assert.Equal(verifyEma.IsHot, ema.IsHot);
|
||||
|
||||
// Verify it continues correctly
|
||||
ema.Update(new TValue(DateTime.UtcNow, 60));
|
||||
verifyEma.Update(new TValue(DateTime.UtcNow, 60));
|
||||
Assert.Equal(verifyEma.Last.Value, ema.Last.Value, 1e-10);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Prime_HandlesNaN_InHistory()
|
||||
{
|
||||
var ema = new Ema(5);
|
||||
double[] history = [10, 20, double.NaN, 40, 50];
|
||||
|
||||
ema.Prime(history);
|
||||
|
||||
var verifyEma = new Ema(5);
|
||||
foreach (var val in history)
|
||||
{
|
||||
verifyEma.Update(new TValue(DateTime.UtcNow, val));
|
||||
}
|
||||
|
||||
Assert.Equal(verifyEma.Last.Value, ema.Last.Value, 1e-10);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Prime_AllNaNs_ReturnsNaN()
|
||||
{
|
||||
var ema = new Ema(5);
|
||||
double[] history = [double.NaN, double.NaN, double.NaN];
|
||||
|
||||
ema.Prime(history);
|
||||
|
||||
Assert.True(double.IsNaN(ema.Last.Value));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Calculate_ReturnsCorrectResultsAndHotIndicator()
|
||||
{
|
||||
var series = new TSeries();
|
||||
for (int i = 1; i <= 20; i++)
|
||||
{
|
||||
series.Add(DateTime.UtcNow, i * 10);
|
||||
}
|
||||
|
||||
// EMA(5)
|
||||
var (results, indicator) = Ema.Calculate(series, 5);
|
||||
|
||||
// Check results
|
||||
Assert.Equal(20, results.Count);
|
||||
|
||||
// Verify against standard calculation
|
||||
var verifyEma = new Ema(5);
|
||||
var verifyResults = verifyEma.Update(series);
|
||||
|
||||
Assert.Equal(verifyResults.Last.Value, results.Last.Value, 1e-10);
|
||||
Assert.Equal(verifyEma.Last.Value, indicator.Last.Value, 1e-10);
|
||||
|
||||
// Check indicator state
|
||||
Assert.True(indicator.IsHot);
|
||||
|
||||
// Verify indicator continues correctly
|
||||
indicator.Update(new TValue(DateTime.UtcNow, 210));
|
||||
verifyEma.Update(new TValue(DateTime.UtcNow, 210));
|
||||
Assert.Equal(verifyEma.Last.Value, indicator.Last.Value, 1e-10);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Ema_Batch_AllNaNs_ReturnsNaN()
|
||||
{
|
||||
double[] source = [double.NaN, double.NaN, double.NaN];
|
||||
double[] output = new double[3];
|
||||
|
||||
Ema.Batch(source.AsSpan(), output.AsSpan(), 5);
|
||||
|
||||
// Should be all NaNs, not 0s
|
||||
foreach (var val in output)
|
||||
{
|
||||
Assert.True(double.IsNaN(val), $"Expected NaN but got {val}");
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Ema_AllModes_ProduceSameResult()
|
||||
{
|
||||
// Arrange
|
||||
int period = 10;
|
||||
var gbm = new GBM(startPrice: 100, mu: 0.05, sigma: 0.2, seed: 123);
|
||||
var bars = gbm.Fetch(1000, DateTime.UtcNow.Ticks, TimeSpan.FromMinutes(1));
|
||||
var series = bars.Close;
|
||||
|
||||
// 1. Batch Mode
|
||||
var batchSeries = Ema.Batch(series, period);
|
||||
double expected = batchSeries.Last.Value;
|
||||
|
||||
// 2. Span Mode
|
||||
var tValues = series.Values.ToArray(); // Need array for Span modification safety if any
|
||||
var spanInput = new ReadOnlySpan<double>(tValues);
|
||||
var spanOutput = new double[tValues.Length];
|
||||
Ema.Batch(spanInput, spanOutput, period);
|
||||
double spanResult = spanOutput[^1];
|
||||
|
||||
// 3. Streaming Mode
|
||||
var streamingInd = new Ema(period);
|
||||
for (int i = 0; i < series.Count; i++)
|
||||
{
|
||||
streamingInd.Update(series[i]);
|
||||
}
|
||||
double streamingResult = streamingInd.Last.Value;
|
||||
|
||||
// 4. Eventing Mode
|
||||
var pubSource = new TSeries();
|
||||
var eventingInd = new Ema(pubSource, period);
|
||||
for (int i = 0; i < series.Count; i++)
|
||||
{
|
||||
pubSource.Add(series[i]);
|
||||
}
|
||||
double eventingResult = eventingInd.Last.Value;
|
||||
|
||||
// Assert
|
||||
// Precision 9 due to potential accumulation differences in loop vs batch optimizations
|
||||
Assert.Equal(expected, spanResult, precision: 9);
|
||||
Assert.Equal(expected, streamingResult, precision: 9);
|
||||
Assert.Equal(expected, eventingResult, precision: 9);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Prime_SingleValue_SetsState()
|
||||
{
|
||||
var ema = new Ema(5);
|
||||
double[] history = [100];
|
||||
|
||||
ema.Prime(history);
|
||||
|
||||
// Single value should be returned as-is (bias-corrected to itself)
|
||||
Assert.Equal(100.0, ema.Last.Value, 1e-10);
|
||||
Assert.False(ema.IsHot); // Not hot with only 1 value
|
||||
|
||||
// Verify against streaming
|
||||
var verifyEma = new Ema(5);
|
||||
verifyEma.Update(new TValue(DateTime.UtcNow, 100));
|
||||
Assert.Equal(verifyEma.Last.Value, ema.Last.Value, 1e-10);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Prime_ThenUpdate_StateWorksCorrectly()
|
||||
{
|
||||
var ema = new Ema(5);
|
||||
double[] history = [10, 20, 30, 40, 50];
|
||||
|
||||
ema.Prime(history);
|
||||
double afterPrime = ema.Last.Value;
|
||||
|
||||
// After Prime, an isNew=true should advance the state
|
||||
ema.Update(new TValue(DateTime.UtcNow, 60), isNew: true);
|
||||
double afterNewBar = ema.Last.Value;
|
||||
|
||||
// Values should be different
|
||||
Assert.NotEqual(afterPrime, afterNewBar);
|
||||
|
||||
// isNew=false with a different value should recalculate from previous state
|
||||
ema.Update(new TValue(DateTime.UtcNow, 70), isNew: false);
|
||||
double afterCorrection = ema.Last.Value;
|
||||
|
||||
// Correction with 70 should give different result than 60
|
||||
Assert.NotEqual(afterNewBar, afterCorrection);
|
||||
|
||||
// isNew=false with original value (60) should restore to afterNewBar
|
||||
ema.Update(new TValue(DateTime.UtcNow, 60), isNew: false);
|
||||
Assert.Equal(afterNewBar, ema.Last.Value, 1e-10);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,316 @@
|
||||
using OoplesFinance.StockIndicators;
|
||||
using OoplesFinance.StockIndicators.Models;
|
||||
using Skender.Stock.Indicators;
|
||||
using TALib;
|
||||
using Xunit.Abstractions;
|
||||
|
||||
namespace QuanTAlib.Tests;
|
||||
|
||||
public sealed class EmaValidationTests : IDisposable
|
||||
{
|
||||
private readonly ValidationTestData _testData;
|
||||
private readonly ITestOutputHelper _output;
|
||||
private bool _disposed;
|
||||
|
||||
public EmaValidationTests(ITestOutputHelper output)
|
||||
{
|
||||
_output = output;
|
||||
_testData = new ValidationTestData();
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
Dispose(true);
|
||||
}
|
||||
|
||||
private void Dispose(bool disposing)
|
||||
{
|
||||
if (_disposed)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
_disposed = true;
|
||||
|
||||
if (disposing)
|
||||
{
|
||||
_testData?.Dispose();
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Validate_Skender_Batch()
|
||||
{
|
||||
int[] periods = { 5, 10, 20, 50, 100 };
|
||||
|
||||
foreach (var period in periods)
|
||||
{
|
||||
// Calculate QuanTAlib EMA (batch TSeries)
|
||||
var ema = new global::QuanTAlib.Ema(period);
|
||||
var qResult = ema.Update(_testData.Data);
|
||||
|
||||
// Calculate Skender EMA
|
||||
var sResult = _testData.SkenderQuotes.GetEma(period).ToList();
|
||||
|
||||
// Compare last 100 records
|
||||
ValidationHelper.VerifyData(qResult, sResult, (s) => s.Ema);
|
||||
}
|
||||
_output.WriteLine("EMA Batch(TSeries) validated successfully against Skender");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Validate_Skender_Streaming()
|
||||
{
|
||||
int[] periods = { 5, 10, 20, 50, 100 };
|
||||
|
||||
foreach (var period in periods)
|
||||
{
|
||||
// Calculate QuanTAlib EMA (streaming)
|
||||
var ema = new global::QuanTAlib.Ema(period);
|
||||
var qResults = new List<double>();
|
||||
foreach (var item in _testData.Data)
|
||||
{
|
||||
qResults.Add(ema.Update(item).Value);
|
||||
}
|
||||
|
||||
// Calculate Skender EMA
|
||||
var sResult = _testData.SkenderQuotes.GetEma(period).ToList();
|
||||
|
||||
// Compare last 100 records
|
||||
ValidationHelper.VerifyData(qResults, sResult, (s) => s.Ema);
|
||||
}
|
||||
_output.WriteLine("EMA Streaming validated successfully against Skender");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Validate_Skender_Span()
|
||||
{
|
||||
int[] periods = { 5, 10, 20, 50, 100 };
|
||||
|
||||
// Prepare data for Span API
|
||||
double[] sourceData = _testData.RawData.ToArray();
|
||||
|
||||
foreach (var period in periods)
|
||||
{
|
||||
// Calculate QuanTAlib EMA (Span API)
|
||||
double[] qOutput = new double[sourceData.Length];
|
||||
global::QuanTAlib.Ema.Batch(sourceData.AsSpan(), qOutput.AsSpan(), period);
|
||||
|
||||
// Calculate Skender EMA
|
||||
var sResult = _testData.SkenderQuotes.GetEma(period).ToList();
|
||||
|
||||
// Compare last 100 records
|
||||
ValidationHelper.VerifyData(qOutput, sResult, (s) => s.Ema);
|
||||
}
|
||||
_output.WriteLine("EMA Span validated successfully against Skender");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Validate_Talib_Batch()
|
||||
{
|
||||
int[] periods = { 5, 10, 20, 50, 100 };
|
||||
|
||||
// Prepare data for TA-Lib (double[])
|
||||
double[] tData = _testData.RawData.ToArray();
|
||||
double[] output = new double[tData.Length];
|
||||
|
||||
foreach (var period in periods)
|
||||
{
|
||||
// Calculate QuanTAlib EMA (batch TSeries)
|
||||
var ema = new global::QuanTAlib.Ema(period);
|
||||
var qResult = ema.Update(_testData.Data);
|
||||
|
||||
// Calculate TA-Lib EMA
|
||||
var retCode = TALib.Functions.Ema<double>(tData, 0..^0, output, out var outRange, period);
|
||||
Assert.Equal(TALib.Core.RetCode.Success, retCode);
|
||||
|
||||
int lookback = TALib.Functions.EmaLookback(period);
|
||||
|
||||
// Compare last 100 records
|
||||
ValidationHelper.VerifyData(qResult, output, outRange, lookback);
|
||||
}
|
||||
_output.WriteLine("EMA Batch(TSeries) validated successfully against TA-Lib");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Validate_Talib_Streaming()
|
||||
{
|
||||
int[] periods = { 5, 10, 20, 50, 100 };
|
||||
|
||||
// Prepare data for TA-Lib (double[])
|
||||
double[] tData = _testData.RawData.ToArray();
|
||||
double[] output = new double[tData.Length];
|
||||
|
||||
foreach (var period in periods)
|
||||
{
|
||||
// Calculate QuanTAlib EMA (streaming)
|
||||
var ema = new global::QuanTAlib.Ema(period);
|
||||
var qResults = new List<double>();
|
||||
foreach (var item in _testData.Data)
|
||||
{
|
||||
qResults.Add(ema.Update(item).Value);
|
||||
}
|
||||
|
||||
// Calculate TA-Lib EMA
|
||||
var retCode = TALib.Functions.Ema<double>(tData, 0..^0, output, out var outRange, period);
|
||||
Assert.Equal(TALib.Core.RetCode.Success, retCode);
|
||||
|
||||
int lookback = TALib.Functions.EmaLookback(period);
|
||||
|
||||
// Compare last 100 records
|
||||
ValidationHelper.VerifyData(qResults, output, outRange, lookback);
|
||||
}
|
||||
_output.WriteLine("EMA Streaming validated successfully against TA-Lib");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Validate_Talib_Span()
|
||||
{
|
||||
int[] periods = { 5, 10, 20, 50, 100 };
|
||||
|
||||
// Prepare data
|
||||
double[] sourceData = _testData.RawData.ToArray();
|
||||
double[] talibOutput = new double[sourceData.Length];
|
||||
|
||||
foreach (var period in periods)
|
||||
{
|
||||
// Calculate QuanTAlib EMA (Span API)
|
||||
double[] qOutput = new double[sourceData.Length];
|
||||
global::QuanTAlib.Ema.Batch(sourceData.AsSpan(), qOutput.AsSpan(), period);
|
||||
|
||||
// Calculate TA-Lib EMA
|
||||
var retCode = TALib.Functions.Ema<double>(sourceData, 0..^0, talibOutput, out var outRange, period);
|
||||
Assert.Equal(TALib.Core.RetCode.Success, retCode);
|
||||
|
||||
int lookback = TALib.Functions.EmaLookback(period);
|
||||
|
||||
// Compare last 100 records
|
||||
ValidationHelper.VerifyData(qOutput, talibOutput, outRange, lookback);
|
||||
}
|
||||
_output.WriteLine("EMA Span validated successfully against TA-Lib");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Validate_Tulip_Batch()
|
||||
{
|
||||
int[] periods = { 5, 10, 20, 50, 100 };
|
||||
|
||||
// Prepare data for Tulip (double[])
|
||||
double[] tData = _testData.RawData.ToArray();
|
||||
|
||||
foreach (var period in periods)
|
||||
{
|
||||
// Calculate QuanTAlib EMA (batch TSeries)
|
||||
var ema = new global::QuanTAlib.Ema(period);
|
||||
var qResult = ema.Update(_testData.Data);
|
||||
|
||||
// Calculate Tulip EMA
|
||||
var emaIndicator = Tulip.Indicators.ema;
|
||||
double[][] inputs = { tData };
|
||||
double[] options = { period };
|
||||
double[][] outputs = { new double[tData.Length] };
|
||||
|
||||
emaIndicator.Run(inputs, options, outputs);
|
||||
var tResult = outputs[0];
|
||||
|
||||
// Compare last 100 records
|
||||
ValidationHelper.VerifyData(qResult, tResult, 0);
|
||||
}
|
||||
_output.WriteLine("EMA Batch(TSeries) validated successfully against Tulip");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Validate_Tulip_Streaming()
|
||||
{
|
||||
int[] periods = { 5, 10, 20, 50, 100 };
|
||||
|
||||
// Prepare data for Tulip (double[])
|
||||
double[] tData = _testData.RawData.ToArray();
|
||||
|
||||
foreach (var period in periods)
|
||||
{
|
||||
// Calculate QuanTAlib EMA (streaming)
|
||||
var ema = new global::QuanTAlib.Ema(period);
|
||||
var qResults = new List<double>();
|
||||
foreach (var item in _testData.Data)
|
||||
{
|
||||
qResults.Add(ema.Update(item).Value);
|
||||
}
|
||||
|
||||
// Calculate Tulip EMA
|
||||
var emaIndicator = Tulip.Indicators.ema;
|
||||
double[][] inputs = { tData };
|
||||
double[] options = { period };
|
||||
double[][] outputs = { new double[tData.Length] };
|
||||
|
||||
emaIndicator.Run(inputs, options, outputs);
|
||||
var tResult = outputs[0];
|
||||
|
||||
// Compare last 100 records
|
||||
ValidationHelper.VerifyData(qResults, tResult, 0);
|
||||
}
|
||||
_output.WriteLine("EMA Streaming validated successfully against Tulip");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Validate_Tulip_Span()
|
||||
{
|
||||
int[] periods = { 5, 10, 20, 50, 100 };
|
||||
|
||||
// Prepare data
|
||||
double[] sourceData = _testData.RawData.ToArray();
|
||||
|
||||
foreach (var period in periods)
|
||||
{
|
||||
// Calculate QuanTAlib EMA (Span API)
|
||||
double[] qOutput = new double[sourceData.Length];
|
||||
global::QuanTAlib.Ema.Batch(sourceData.AsSpan(), qOutput.AsSpan(), period);
|
||||
|
||||
// Calculate Tulip EMA
|
||||
var emaIndicator = Tulip.Indicators.ema;
|
||||
double[][] inputs = { sourceData };
|
||||
double[] options = { period };
|
||||
double[][] outputs = { new double[sourceData.Length] };
|
||||
|
||||
emaIndicator.Run(inputs, options, outputs);
|
||||
var tResult = outputs[0];
|
||||
|
||||
// Compare last 100 records
|
||||
ValidationHelper.VerifyData(qOutput, tResult, 0);
|
||||
}
|
||||
_output.WriteLine("EMA Span validated successfully against Tulip");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Validate_Against_Ooples()
|
||||
{
|
||||
int[] periods = { 5, 10, 20, 50, 100 };
|
||||
|
||||
// Prepare data for Ooples (List<TickerData>)
|
||||
var ooplesData = _testData.SkenderQuotes.Select(q => new TickerData
|
||||
{
|
||||
Date = q.Date,
|
||||
Close = (double)q.Close,
|
||||
High = (double)q.High,
|
||||
Low = (double)q.Low,
|
||||
Open = (double)q.Open,
|
||||
Volume = (double)q.Volume
|
||||
}).ToList();
|
||||
|
||||
foreach (var period in periods)
|
||||
{
|
||||
// Calculate QuanTAlib EMA
|
||||
var ema = new global::QuanTAlib.Ema(period);
|
||||
var qResult = ema.Update(_testData.Data);
|
||||
|
||||
// Calculate Ooples EMA
|
||||
var stockData = new StockData(ooplesData);
|
||||
var oResult = stockData.CalculateExponentialMovingAverage(period);
|
||||
var oValues = oResult.OutputValues.Values.First();
|
||||
|
||||
// Compare
|
||||
ValidationHelper.VerifyData(qResult, oValues, (s) => s, tolerance: ValidationHelper.OoplesTolerance);
|
||||
}
|
||||
_output.WriteLine("EMA validated successfully against Ooples");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user