mirror of
https://github.com/mihakralj/QuanTAlib.git
synced 2026-08-26 06:18: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,154 @@
|
||||
using TradingPlatform.BusinessLayer;
|
||||
|
||||
namespace QuanTAlib.Tests;
|
||||
|
||||
public class DemaIndicatorTests
|
||||
{
|
||||
[Fact]
|
||||
public void DemaIndicator_Constructor_SetsDefaults()
|
||||
{
|
||||
var indicator = new DemaIndicator();
|
||||
|
||||
Assert.Equal(10, indicator.Period);
|
||||
Assert.Equal(SourceType.Close, indicator.Source);
|
||||
Assert.True(indicator.ShowColdValues);
|
||||
Assert.Equal("DEMA - Double Exponential Moving Average", indicator.Name);
|
||||
Assert.False(indicator.SeparateWindow);
|
||||
Assert.True(indicator.OnBackGround);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void DemaIndicator_MinHistoryDepths_EqualsPeriod()
|
||||
{
|
||||
var indicator = new DemaIndicator { Period = 20 };
|
||||
|
||||
Assert.Equal(0, DemaIndicator.MinHistoryDepths);
|
||||
Assert.Equal(0, ((IWatchlistIndicator)indicator).MinHistoryDepths);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void DemaIndicator_ShortName_IncludesPeriodAndSource()
|
||||
{
|
||||
var indicator = new DemaIndicator { Period = 15 };
|
||||
|
||||
Assert.Contains("DEMA", indicator.ShortName, StringComparison.Ordinal);
|
||||
Assert.Contains("15", indicator.ShortName, StringComparison.Ordinal);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void DemaIndicator_SourceCodeLink_IsValid()
|
||||
{
|
||||
var indicator = new DemaIndicator();
|
||||
|
||||
Assert.Contains("github.com", indicator.SourceCodeLink, StringComparison.Ordinal);
|
||||
Assert.Contains("Dema.Quantower.cs", indicator.SourceCodeLink, StringComparison.Ordinal);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void DemaIndicator_Initialize_CreatesInternalDema()
|
||||
{
|
||||
var indicator = new DemaIndicator { Period = 10 };
|
||||
|
||||
// Initialize should not throw
|
||||
indicator.Initialize();
|
||||
|
||||
// After init, line series should exist
|
||||
Assert.Single(indicator.LinesSeries);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void DemaIndicator_ProcessUpdate_HistoricalBar_ComputesValue()
|
||||
{
|
||||
var indicator = new DemaIndicator { 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 DemaIndicator_ProcessUpdate_NewBar_ComputesValue()
|
||||
{
|
||||
var indicator = new DemaIndicator { Period = 3 };
|
||||
indicator.Initialize();
|
||||
|
||||
var now = DateTime.UtcNow;
|
||||
indicator.HistoricalData.AddBar(now, 100, 105, 95, 102);
|
||||
indicator.HistoricalData.AddBar(now.AddMinutes(1), 102, 108, 100, 106);
|
||||
|
||||
indicator.ProcessUpdate(new UpdateArgs(UpdateReason.HistoricalBar));
|
||||
indicator.ProcessUpdate(new UpdateArgs(UpdateReason.NewBar));
|
||||
|
||||
Assert.Equal(2, indicator.LinesSeries[0].Count);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void DemaIndicator_ProcessUpdate_NewTick_ProcessesWithoutError()
|
||||
{
|
||||
var indicator = new DemaIndicator { Period = 3 };
|
||||
indicator.Initialize();
|
||||
|
||||
var now = DateTime.UtcNow;
|
||||
indicator.HistoricalData.AddBar(now, 100, 105, 95, 102);
|
||||
|
||||
indicator.ProcessUpdate(new UpdateArgs(UpdateReason.HistoricalBar));
|
||||
double firstValue = indicator.LinesSeries[0].GetValue(0);
|
||||
|
||||
indicator.ProcessUpdate(new UpdateArgs(UpdateReason.NewTick));
|
||||
double secondValue = indicator.LinesSeries[0].GetValue(0);
|
||||
|
||||
Assert.True(double.IsFinite(firstValue));
|
||||
Assert.True(double.IsFinite(secondValue));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void DemaIndicator_MultipleUpdates_ProducesCorrectDemaSequence()
|
||||
{
|
||||
var indicator = new DemaIndicator { Period = 3 };
|
||||
indicator.Initialize();
|
||||
|
||||
var now = DateTime.UtcNow;
|
||||
double[] closes = { 100, 102, 104, 103, 105 };
|
||||
|
||||
foreach (var close in closes)
|
||||
{
|
||||
indicator.HistoricalData.AddBar(now, close, close + 2, close - 2, close);
|
||||
indicator.ProcessUpdate(new UpdateArgs(UpdateReason.HistoricalBar));
|
||||
now = now.AddMinutes(1);
|
||||
}
|
||||
|
||||
// All values should be finite
|
||||
for (int i = 0; i < closes.Length; i++)
|
||||
{
|
||||
Assert.True(double.IsFinite(indicator.LinesSeries[0].GetValue(closes.Length - 1 - i)));
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void DemaIndicator_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 DemaIndicator { 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");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,329 @@
|
||||
|
||||
namespace QuanTAlib.Tests;
|
||||
|
||||
public class DemaTests
|
||||
{
|
||||
[Fact]
|
||||
public void Dema_Matches_ManualCalculation()
|
||||
{
|
||||
// Arrange
|
||||
const int period = 10;
|
||||
var dema = new Dema(period);
|
||||
var ema1 = new Ema(period);
|
||||
var ema2 = new Ema(period);
|
||||
var gbm = new GBM(startPrice: 100, mu: 0.05, sigma: 0.2, seed: 123);
|
||||
|
||||
// Act & Assert
|
||||
for (int i = 0; i < 100; i++)
|
||||
{
|
||||
var bar = gbm.Next(isNew: true);
|
||||
var tVal = new TValue(bar.Time, bar.Close);
|
||||
|
||||
var dVal = dema.Update(tVal);
|
||||
|
||||
var e1Val = ema1.Update(tVal);
|
||||
var e2Val = ema2.Update(e1Val);
|
||||
double expected = 2 * e1Val.Value - e2Val.Value;
|
||||
|
||||
Assert.Equal(expected, dVal.Value, 1e-9);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void StaticCalculate_Matches_ObjectUpdate()
|
||||
{
|
||||
// Arrange
|
||||
const int period = 10;
|
||||
var source = new TSeries();
|
||||
|
||||
var gbm = new GBM(startPrice: 100, mu: 0.05, sigma: 0.2, seed: 123);
|
||||
for (int i = 0; i < 100; i++)
|
||||
{
|
||||
var bar = gbm.Next(isNew: true);
|
||||
source.Add(new TValue(bar.Time, bar.Close));
|
||||
}
|
||||
|
||||
// Act
|
||||
var demaSeries = Dema.Batch(source, period);
|
||||
var demaObj = new Dema(period);
|
||||
|
||||
// Assert
|
||||
for (int i = 0; i < source.Count; i++)
|
||||
{
|
||||
var val = demaObj.Update(source[i]);
|
||||
Assert.Equal(val.Value, demaSeries[i].Value, 1e-9);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ZeroAllocCalculate_Matches_ObjectUpdate()
|
||||
{
|
||||
// Arrange
|
||||
const int period = 10;
|
||||
const int count = 100;
|
||||
var source = new double[count];
|
||||
var output = new double[count];
|
||||
|
||||
var gbm = new GBM(startPrice: 100, mu: 0.05, sigma: 0.2, seed: 123);
|
||||
for (int i = 0; i < count; i++)
|
||||
{
|
||||
source[i] = gbm.Next().Close;
|
||||
}
|
||||
|
||||
// Act
|
||||
Dema.Batch(source, output, period);
|
||||
var demaObj = new Dema(period);
|
||||
|
||||
// Assert
|
||||
for (int i = 0; i < count; i++)
|
||||
{
|
||||
var val = demaObj.Update(new TValue(DateTime.UtcNow, source[i]));
|
||||
Assert.Equal(val.Value, output[i], 1e-9);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Alpha_Constructor_Matches_Period_Constructor()
|
||||
{
|
||||
// Arrange
|
||||
const int period = 10;
|
||||
double alpha = 2.0 / (period + 1);
|
||||
var demaPeriod = new Dema(period);
|
||||
var demaAlpha = new Dema(alpha);
|
||||
|
||||
var gbm = new GBM(startPrice: 100, mu: 0.05, sigma: 0.2, seed: 123);
|
||||
|
||||
// Act & Assert
|
||||
for (int i = 0; i < 100; i++)
|
||||
{
|
||||
var bar = gbm.Next(isNew: true);
|
||||
var tVal = new TValue(bar.Time, bar.Close);
|
||||
|
||||
var pVal = demaPeriod.Update(tVal);
|
||||
var aVal = demaAlpha.Update(tVal);
|
||||
|
||||
Assert.Equal(pVal.Value, aVal.Value, 1e-9);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Alpha_Constructor_Sets_WarmupPeriod()
|
||||
{
|
||||
const int period = 10;
|
||||
double alpha = 2.0 / (period + 1);
|
||||
var dema = new Dema(alpha);
|
||||
Assert.Equal(period, dema.WarmupPeriod);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void StaticCalculate_Alpha_Matches_ObjectUpdate()
|
||||
{
|
||||
// Arrange
|
||||
const double alpha = 0.15;
|
||||
var source = new TSeries();
|
||||
|
||||
var gbm = new GBM(startPrice: 100, mu: 0.05, sigma: 0.2, seed: 123);
|
||||
for (int i = 0; i < 100; i++)
|
||||
{
|
||||
var bar = gbm.Next(isNew: true);
|
||||
source.Add(new TValue(bar.Time, bar.Close));
|
||||
}
|
||||
|
||||
// Act
|
||||
var demaSeries = Dema.Batch(source, alpha);
|
||||
var demaObj = new Dema(alpha);
|
||||
|
||||
// Assert
|
||||
for (int i = 0; i < source.Count; i++)
|
||||
{
|
||||
var val = demaObj.Update(source[i]);
|
||||
Assert.Equal(val.Value, demaSeries[i].Value, 1e-9);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ZeroAllocCalculate_Alpha_Matches_ObjectUpdate()
|
||||
{
|
||||
// Arrange
|
||||
const double alpha = 0.15;
|
||||
const int count = 100;
|
||||
var source = new double[count];
|
||||
var output = new double[count];
|
||||
|
||||
var gbm = new GBM(startPrice: 100, mu: 0.05, sigma: 0.2, seed: 123);
|
||||
for (int i = 0; i < count; i++)
|
||||
{
|
||||
source[i] = gbm.Next().Close;
|
||||
}
|
||||
|
||||
// Act
|
||||
Dema.Batch(source, output, alpha);
|
||||
var demaObj = new Dema(alpha);
|
||||
|
||||
// Assert
|
||||
for (int i = 0; i < count; i++)
|
||||
{
|
||||
var val = demaObj.Update(new TValue(DateTime.UtcNow, source[i]));
|
||||
Assert.Equal(val.Value, output[i], 1e-9);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Dema_Constructor_ValidatesInput()
|
||||
{
|
||||
Assert.Throws<ArgumentException>(() => new Dema(0));
|
||||
Assert.Throws<ArgumentException>(() => new Dema(-1));
|
||||
Assert.Throws<ArgumentException>(() => new Dema(0.0));
|
||||
Assert.Throws<ArgumentException>(() => new Dema(1.1));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Dema_Calc_IsNew_AcceptsParameter()
|
||||
{
|
||||
var dema = new Dema(10);
|
||||
dema.Update(new TValue(DateTime.UtcNow, 100), isNew: true);
|
||||
Assert.Equal(100, dema.Last.Value);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Dema_Reset_ClearsState()
|
||||
{
|
||||
var dema = new Dema(10);
|
||||
dema.Update(new TValue(DateTime.UtcNow, 100));
|
||||
dema.Update(new TValue(DateTime.UtcNow, 110));
|
||||
|
||||
dema.Reset();
|
||||
|
||||
Assert.Equal(0, dema.Last.Value);
|
||||
Assert.False(dema.IsHot);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Dema_IterativeCorrections_RestoreToOriginalState()
|
||||
{
|
||||
var dema = new Dema(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);
|
||||
dema.Update(tenthInput, isNew: true);
|
||||
}
|
||||
|
||||
// Remember state after 10 values
|
||||
double valueAfterTen = dema.Last.Value;
|
||||
|
||||
// Generate 9 corrections with isNew=false (different values)
|
||||
for (int i = 0; i < 9; i++)
|
||||
{
|
||||
var bar = gbm.Next(isNew: false);
|
||||
dema.Update(new TValue(bar.Time, bar.Close), isNew: false);
|
||||
}
|
||||
|
||||
// Feed the remembered 10th input again with isNew=false
|
||||
TValue finalValue = dema.Update(tenthInput, isNew: false);
|
||||
|
||||
// Should match the original state after 10 values
|
||||
Assert.Equal(valueAfterTen, finalValue.Value, 1e-9);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Dema_NaN_Input_UsesLastValidValue()
|
||||
{
|
||||
var dema = new Dema(10);
|
||||
dema.Update(new TValue(DateTime.UtcNow, 100));
|
||||
dema.Update(new TValue(DateTime.UtcNow, 110));
|
||||
|
||||
var resultAfterNaN = dema.Update(new TValue(DateTime.UtcNow, double.NaN));
|
||||
|
||||
Assert.True(double.IsFinite(resultAfterNaN.Value));
|
||||
Assert.NotEqual(0, resultAfterNaN.Value);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Dema_SpanCalc_ValidatesInput()
|
||||
{
|
||||
double[] source = [1, 2, 3, 4, 5];
|
||||
double[] output = new double[5];
|
||||
double[] wrongSizeOutput = new double[3];
|
||||
|
||||
Assert.Throws<ArgumentException>(() => Dema.Batch(source.AsSpan(), output.AsSpan(), 0));
|
||||
Assert.Throws<ArgumentException>(() => Dema.Batch(source.AsSpan(), wrongSizeOutput.AsSpan(), 3));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Dema_SpanCalc_HandlesNaN()
|
||||
{
|
||||
double[] source = [100, 110, double.NaN, 120, 130];
|
||||
double[] output = new double[5];
|
||||
|
||||
Dema.Batch(source.AsSpan(), output.AsSpan(), 3);
|
||||
|
||||
foreach (var val in output)
|
||||
{
|
||||
Assert.True(double.IsFinite(val));
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Dema_AllModes_ProduceSameResult()
|
||||
{
|
||||
// Arrange
|
||||
const 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 = Dema.Batch(series, period);
|
||||
double expected = batchSeries.Last.Value;
|
||||
|
||||
// 2. Span Mode
|
||||
var tValues = series.Values.ToArray();
|
||||
var spanInput = new ReadOnlySpan<double>(tValues);
|
||||
var spanOutput = new double[tValues.Length];
|
||||
Dema.Batch(spanInput, spanOutput, period);
|
||||
double spanResult = spanOutput[^1];
|
||||
|
||||
// 3. Streaming Mode
|
||||
var streamingInd = new Dema(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 Dema(pubSource, period);
|
||||
for (int i = 0; i < series.Count; i++)
|
||||
{
|
||||
pubSource.Add(series[i]);
|
||||
}
|
||||
double eventingResult = eventingInd.Last.Value;
|
||||
|
||||
// Assert
|
||||
Assert.Equal(expected, spanResult, precision: 9);
|
||||
Assert.Equal(expected, streamingResult, precision: 9);
|
||||
Assert.Equal(expected, eventingResult, precision: 9);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void StaticCalculate_HandlesInitialNaN_Correctly()
|
||||
{
|
||||
double[] source = { double.NaN, double.NaN, 10.0, 11.0, 12.0 };
|
||||
double[] output = new double[source.Length];
|
||||
|
||||
Dema.Batch(source, output, 3);
|
||||
|
||||
// We expect the first two outputs to be NaN because the input was NaN
|
||||
Assert.True(double.IsNaN(output[0]), $"Output[0] should be NaN, but was {output[0]}");
|
||||
Assert.True(double.IsNaN(output[1]), $"Output[1] should be NaN, but was {output[1]}");
|
||||
|
||||
// The first valid value is 10.0.
|
||||
Assert.Equal(10.0, output[2], 1e-9);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,192 @@
|
||||
using Skender.Stock.Indicators;
|
||||
using TALib;
|
||||
using Xunit.Abstractions;
|
||||
|
||||
namespace QuanTAlib.Tests;
|
||||
|
||||
public sealed class DemaValidationTests : IDisposable
|
||||
{
|
||||
private readonly ValidationTestData _testData;
|
||||
private readonly ITestOutputHelper _output;
|
||||
private bool _disposed;
|
||||
|
||||
public DemaValidationTests(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 DEMA (batch TSeries)
|
||||
var dema = new global::QuanTAlib.Dema(period);
|
||||
var qResult = dema.Update(_testData.Data);
|
||||
|
||||
// Calculate Skender DEMA
|
||||
var sResult = _testData.SkenderQuotes.GetDema(period).ToList();
|
||||
|
||||
// Compare last 100 records
|
||||
ValidationHelper.VerifyData(qResult, sResult, (s) => s.Dema);
|
||||
}
|
||||
_output.WriteLine("DEMA Batch(TSeries) validated successfully against Skender.Stock.Indicators");
|
||||
}
|
||||
|
||||
[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 DEMA (batch TSeries)
|
||||
var dema = new global::QuanTAlib.Dema(period);
|
||||
var qResult = dema.Update(_testData.Data);
|
||||
|
||||
// Calculate TA-Lib DEMA
|
||||
var retCode = TALib.Functions.Dema<double>(tData, 0..^0, output, out var outRange, period);
|
||||
Assert.Equal(TALib.Core.RetCode.Success, retCode);
|
||||
|
||||
int lookback = TALib.Functions.DemaLookback(period);
|
||||
|
||||
// Compare last 100 records
|
||||
ValidationHelper.VerifyData(qResult, output, outRange, lookback);
|
||||
}
|
||||
_output.WriteLine("DEMA Batch(TSeries) 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 DEMA (batch TSeries)
|
||||
var dema = new global::QuanTAlib.Dema(period);
|
||||
var qResult = dema.Update(_testData.Data);
|
||||
|
||||
// Calculate Tulip DEMA
|
||||
var demaIndicator = Tulip.Indicators.dema;
|
||||
double[][] inputs = { tData };
|
||||
double[] options = { period };
|
||||
|
||||
// Tulip DEMA lookback is usually period-1 for EMA, but DEMA is 2*EMA - EMA(EMA)
|
||||
// Let's rely on the output length to align.
|
||||
// Tulip DEMA lookback is same as EMA lookback? No, it involves double smoothing.
|
||||
// Actually, Tulip's DEMA implementation might have a specific lookback.
|
||||
// We'll calculate it based on output length.
|
||||
|
||||
// Tulip.Indicators.dema.Run expects outputs to be sized correctly.
|
||||
// We'll use a large buffer and resize if needed, or just calculate lookback.
|
||||
// For DEMA(n), lookback is roughly n-1 (same as EMA).
|
||||
// Wait, DEMA uses EMA(EMA), so it might be 2*(n-1)?
|
||||
// Let's try with n-1 first, if it fails we adjust.
|
||||
// Actually, TA-Lib DEMA lookback is 2*(period-1).
|
||||
// Let's assume Tulip is similar.
|
||||
int lookback = 2 * (period - 1);
|
||||
double[][] outputs = { new double[tData.Length - lookback] };
|
||||
|
||||
demaIndicator.Run(inputs, options, outputs);
|
||||
var tResult = outputs[0];
|
||||
|
||||
// Compare last 100 records
|
||||
ValidationHelper.VerifyData(qResult, tResult, lookback);
|
||||
}
|
||||
_output.WriteLine("DEMA Batch(TSeries) validated successfully against Tulip");
|
||||
}
|
||||
|
||||
[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 DEMA (Span API)
|
||||
double[] qOutput = new double[sourceData.Length];
|
||||
global::QuanTAlib.Dema.Batch(sourceData.AsSpan(), qOutput.AsSpan(), period);
|
||||
|
||||
// Calculate TA-Lib DEMA
|
||||
var retCode = TALib.Functions.Dema<double>(sourceData, 0..^0, talibOutput, out var outRange, period);
|
||||
Assert.Equal(TALib.Core.RetCode.Success, retCode);
|
||||
|
||||
int lookback = TALib.Functions.DemaLookback(period);
|
||||
|
||||
// Compare last 100 records
|
||||
ValidationHelper.VerifyData(qOutput, talibOutput, outRange, lookback);
|
||||
}
|
||||
_output.WriteLine("DEMA Span validated successfully against TA-Lib");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Validate_Against_Ooples()
|
||||
{
|
||||
// Ooples Finance implementation of DEMA is standard:
|
||||
// DEMA = 2 * EMA(n) - EMA(EMA(n))
|
||||
// We validate that our Dema class matches this composition using our own Ema class.
|
||||
|
||||
int[] periods = { 5, 10, 14, 20 };
|
||||
|
||||
foreach (var period in periods)
|
||||
{
|
||||
var dema = new Dema(period);
|
||||
var ema1 = new Ema(period);
|
||||
var ema2 = new Ema(period);
|
||||
|
||||
for (int i = 0; i < _testData.Data.Count; i++)
|
||||
{
|
||||
var item = _testData.Data[i];
|
||||
|
||||
// QuanTAlib DEMA
|
||||
var qVal = dema.Update(item);
|
||||
|
||||
// Manual DEMA (Ooples logic)
|
||||
var e1 = ema1.Update(item);
|
||||
var e2 = ema2.Update(e1); // EMA of EMA
|
||||
double ooplesVal = 2 * e1.Value - e2.Value;
|
||||
|
||||
// Compare
|
||||
// Note: There might be tiny differences due to floating point operations order
|
||||
// or internal state handling optimization in Dema class vs composed Ema classes.
|
||||
Assert.Equal(ooplesVal, qVal.Value, ValidationHelper.DefaultTolerance);
|
||||
}
|
||||
}
|
||||
_output.WriteLine("DEMA validated successfully against Ooples logic (2*EMA - EMA(EMA))");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user