mirror of
https://github.com/mihakralj/QuanTAlib.git
synced 2026-08-26 14:28: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,148 @@
|
||||
using TradingPlatform.BusinessLayer;
|
||||
|
||||
namespace QuanTAlib.Tests;
|
||||
|
||||
public class McnmaIndicatorTests
|
||||
{
|
||||
[Fact]
|
||||
public void McnmaIndicator_Constructor_SetsDefaults()
|
||||
{
|
||||
var indicator = new McnmaIndicator();
|
||||
|
||||
Assert.Equal(14, indicator.Period);
|
||||
Assert.Equal(SourceType.Close, indicator.Source);
|
||||
Assert.True(indicator.ShowColdValues);
|
||||
Assert.Equal("MCNMA - McNicholl EMA", indicator.Name);
|
||||
Assert.False(indicator.SeparateWindow);
|
||||
Assert.True(indicator.OnBackGround);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void McnmaIndicator_MinHistoryDepths_EqualsZero()
|
||||
{
|
||||
var indicator = new McnmaIndicator { Period = 20 };
|
||||
|
||||
Assert.Equal(0, McnmaIndicator.MinHistoryDepths);
|
||||
Assert.Equal(0, ((IWatchlistIndicator)indicator).MinHistoryDepths);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void McnmaIndicator_ShortName_IncludesPeriodAndSource()
|
||||
{
|
||||
var indicator = new McnmaIndicator { Period = 15 };
|
||||
|
||||
Assert.Contains("MCNMA", indicator.ShortName, StringComparison.Ordinal);
|
||||
Assert.Contains("15", indicator.ShortName, StringComparison.Ordinal);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void McnmaIndicator_SourceCodeLink_IsValid()
|
||||
{
|
||||
var indicator = new McnmaIndicator();
|
||||
|
||||
Assert.Contains("github.com", indicator.SourceCodeLink, StringComparison.Ordinal);
|
||||
Assert.Contains("Mcnma.Quantower.cs", indicator.SourceCodeLink, StringComparison.Ordinal);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void McnmaIndicator_Initialize_CreatesInternalMcnma()
|
||||
{
|
||||
var indicator = new McnmaIndicator { Period = 14 };
|
||||
|
||||
indicator.Initialize();
|
||||
|
||||
Assert.Single(indicator.LinesSeries);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void McnmaIndicator_ProcessUpdate_HistoricalBar_ComputesValue()
|
||||
{
|
||||
var indicator = new McnmaIndicator { Period = 3 };
|
||||
indicator.Initialize();
|
||||
|
||||
var now = DateTime.UtcNow;
|
||||
indicator.HistoricalData.AddBar(now, 100, 105, 95, 102);
|
||||
|
||||
var args = new UpdateArgs(UpdateReason.HistoricalBar);
|
||||
indicator.ProcessUpdate(args);
|
||||
|
||||
Assert.Equal(1, indicator.LinesSeries[0].Count);
|
||||
Assert.True(double.IsFinite(indicator.LinesSeries[0].GetValue(0)));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void McnmaIndicator_ProcessUpdate_NewBar_ComputesValue()
|
||||
{
|
||||
var indicator = new McnmaIndicator { 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 McnmaIndicator_ProcessUpdate_NewTick_ProcessesWithoutError()
|
||||
{
|
||||
var indicator = new McnmaIndicator { 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 McnmaIndicator_MultipleUpdates_ProducesCorrectSequence()
|
||||
{
|
||||
var indicator = new McnmaIndicator { 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);
|
||||
}
|
||||
|
||||
for (int i = 0; i < closes.Length; i++)
|
||||
{
|
||||
Assert.True(double.IsFinite(indicator.LinesSeries[0].GetValue(closes.Length - 1 - i)));
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void McnmaIndicator_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 McnmaIndicator { 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,323 @@
|
||||
namespace QuanTAlib.Tests;
|
||||
|
||||
public class McnmaTests
|
||||
{
|
||||
[Fact]
|
||||
public void Mcnma_Matches_ManualCalculation()
|
||||
{
|
||||
// Manual 6-EMA with first-value seeding (matches Pine exactly)
|
||||
const int period = 10;
|
||||
double alpha = 2.0 / (period + 1);
|
||||
double decay = 1.0 - alpha;
|
||||
var mcnma = new Mcnma(period);
|
||||
var gbm = new GBM(startPrice: 100, mu: 0.05, sigma: 0.2, seed: 123);
|
||||
|
||||
double e1 = 0, e2 = 0, e3 = 0, e4 = 0, e5 = 0, e6 = 0;
|
||||
bool init = false;
|
||||
|
||||
for (int i = 0; i < 100; i++)
|
||||
{
|
||||
var bar = gbm.Next(isNew: true);
|
||||
var tVal = new TValue(bar.Time, bar.Close);
|
||||
|
||||
var mVal = mcnma.Update(tVal);
|
||||
|
||||
double val = tVal.Value;
|
||||
if (!init)
|
||||
{
|
||||
e1 = e2 = e3 = e4 = e5 = e6 = val;
|
||||
init = true;
|
||||
Assert.Equal(val, mVal.Value, 1e-9);
|
||||
continue;
|
||||
}
|
||||
|
||||
e1 = Math.FusedMultiplyAdd(e1, decay, alpha * val);
|
||||
e2 = Math.FusedMultiplyAdd(e2, decay, alpha * e1);
|
||||
e3 = Math.FusedMultiplyAdd(e3, decay, alpha * e2);
|
||||
double tema1 = 3.0 * e1 - 3.0 * e2 + e3;
|
||||
|
||||
e4 = Math.FusedMultiplyAdd(e4, decay, alpha * tema1);
|
||||
e5 = Math.FusedMultiplyAdd(e5, decay, alpha * e4);
|
||||
e6 = Math.FusedMultiplyAdd(e6, decay, alpha * e5);
|
||||
double tema2 = 3.0 * e4 - 3.0 * e5 + e6;
|
||||
|
||||
double expected = 2.0 * tema1 - tema2;
|
||||
Assert.Equal(expected, mVal.Value, 1e-9);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void StaticCalculate_Matches_ObjectUpdate()
|
||||
{
|
||||
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));
|
||||
}
|
||||
|
||||
var mcnmaSeries = Mcnma.Batch(source, period);
|
||||
var mcnmaObj = new Mcnma(period);
|
||||
|
||||
for (int i = 0; i < source.Count; i++)
|
||||
{
|
||||
var val = mcnmaObj.Update(source[i]);
|
||||
Assert.Equal(val.Value, mcnmaSeries[i].Value, 1e-9);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ZeroAllocCalculate_Matches_ObjectUpdate()
|
||||
{
|
||||
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;
|
||||
}
|
||||
|
||||
Mcnma.Batch(source, output, period);
|
||||
var mcnmaObj = new Mcnma(period);
|
||||
|
||||
for (int i = 0; i < count; i++)
|
||||
{
|
||||
var val = mcnmaObj.Update(new TValue(DateTime.UtcNow, source[i]));
|
||||
Assert.Equal(val.Value, output[i], 1e-9);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Alpha_Constructor_Matches_Period_Constructor()
|
||||
{
|
||||
const int period = 10;
|
||||
double alpha = 2.0 / (period + 1);
|
||||
var mcnmaPeriod = new Mcnma(period);
|
||||
var mcnmaAlpha = new Mcnma(alpha);
|
||||
|
||||
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);
|
||||
var tVal = new TValue(bar.Time, bar.Close);
|
||||
|
||||
var pVal = mcnmaPeriod.Update(tVal);
|
||||
var aVal = mcnmaAlpha.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 mcnma = new Mcnma(alpha);
|
||||
Assert.Equal(period, mcnma.WarmupPeriod);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void StaticCalculate_Alpha_Matches_ObjectUpdate()
|
||||
{
|
||||
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));
|
||||
}
|
||||
|
||||
var mcnmaSeries = Mcnma.Batch(source, alpha);
|
||||
var mcnmaObj = new Mcnma(alpha);
|
||||
|
||||
for (int i = 0; i < source.Count; i++)
|
||||
{
|
||||
var val = mcnmaObj.Update(source[i]);
|
||||
Assert.Equal(val.Value, mcnmaSeries[i].Value, 1e-9);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ZeroAllocCalculate_Alpha_Matches_ObjectUpdate()
|
||||
{
|
||||
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;
|
||||
}
|
||||
|
||||
Mcnma.Batch(source, output, alpha);
|
||||
var mcnmaObj = new Mcnma(alpha);
|
||||
|
||||
for (int i = 0; i < count; i++)
|
||||
{
|
||||
var val = mcnmaObj.Update(new TValue(DateTime.UtcNow, source[i]));
|
||||
Assert.Equal(val.Value, output[i], 1e-9);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Mcnma_Constructor_ValidatesInput()
|
||||
{
|
||||
Assert.Throws<ArgumentException>(() => new Mcnma(0));
|
||||
Assert.Throws<ArgumentException>(() => new Mcnma(-1));
|
||||
Assert.Throws<ArgumentException>(() => new Mcnma(0.0));
|
||||
Assert.Throws<ArgumentException>(() => new Mcnma(1.1));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Mcnma_Calc_IsNew_AcceptsParameter()
|
||||
{
|
||||
var mcnma = new Mcnma(10);
|
||||
mcnma.Update(new TValue(DateTime.UtcNow, 100), isNew: true);
|
||||
Assert.Equal(100, mcnma.Last.Value);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Mcnma_Reset_ClearsState()
|
||||
{
|
||||
var mcnma = new Mcnma(10);
|
||||
mcnma.Update(new TValue(DateTime.UtcNow, 100));
|
||||
mcnma.Update(new TValue(DateTime.UtcNow, 110));
|
||||
|
||||
mcnma.Reset();
|
||||
|
||||
Assert.Equal(0, mcnma.Last.Value);
|
||||
Assert.False(mcnma.IsHot);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Mcnma_IterativeCorrections_RestoreToOriginalState()
|
||||
{
|
||||
var mcnma = new Mcnma(10);
|
||||
var gbm = new GBM(startPrice: 100.0, mu: 0.02, sigma: 0.1);
|
||||
|
||||
TValue tenthInput = default;
|
||||
for (int i = 0; i < 10; i++)
|
||||
{
|
||||
var bar = gbm.Next(isNew: true);
|
||||
tenthInput = new TValue(bar.Time, bar.Close);
|
||||
mcnma.Update(tenthInput, isNew: true);
|
||||
}
|
||||
|
||||
double valueAfterTen = mcnma.Last.Value;
|
||||
|
||||
for (int i = 0; i < 9; i++)
|
||||
{
|
||||
var bar = gbm.Next(isNew: false);
|
||||
mcnma.Update(new TValue(bar.Time, bar.Close), isNew: false);
|
||||
}
|
||||
|
||||
TValue finalValue = mcnma.Update(tenthInput, isNew: false);
|
||||
|
||||
Assert.Equal(valueAfterTen, finalValue.Value, 1e-9);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Mcnma_NaN_Input_UsesLastValidValue()
|
||||
{
|
||||
var mcnma = new Mcnma(10);
|
||||
mcnma.Update(new TValue(DateTime.UtcNow, 100));
|
||||
mcnma.Update(new TValue(DateTime.UtcNow, 110));
|
||||
|
||||
var resultAfterNaN = mcnma.Update(new TValue(DateTime.UtcNow, double.NaN));
|
||||
|
||||
Assert.True(double.IsFinite(resultAfterNaN.Value));
|
||||
Assert.NotEqual(0, resultAfterNaN.Value);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Mcnma_SpanCalc_ValidatesInput()
|
||||
{
|
||||
double[] source = [1, 2, 3, 4, 5];
|
||||
double[] output = new double[5];
|
||||
double[] wrongSizeOutput = new double[3];
|
||||
|
||||
Assert.Throws<ArgumentException>(() => Mcnma.Batch(source.AsSpan(), output.AsSpan(), 0));
|
||||
Assert.Throws<ArgumentException>(() => Mcnma.Batch(source.AsSpan(), wrongSizeOutput.AsSpan(), 3));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Mcnma_SpanCalc_HandlesNaN()
|
||||
{
|
||||
double[] source = [100, 110, double.NaN, 120, 130];
|
||||
double[] output = new double[5];
|
||||
|
||||
Mcnma.Batch(source.AsSpan(), output.AsSpan(), 3);
|
||||
|
||||
foreach (var val in output)
|
||||
{
|
||||
Assert.True(double.IsFinite(val));
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Mcnma_AllModes_ProduceSameResult()
|
||||
{
|
||||
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 = Mcnma.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];
|
||||
Mcnma.Batch(spanInput, spanOutput, period);
|
||||
double spanResult = spanOutput[^1];
|
||||
|
||||
// 3. Streaming Mode
|
||||
var streamingInd = new Mcnma(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 Mcnma(pubSource, period);
|
||||
for (int i = 0; i < series.Count; i++)
|
||||
{
|
||||
pubSource.Add(series[i]);
|
||||
}
|
||||
double eventingResult = eventingInd.Last.Value;
|
||||
|
||||
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];
|
||||
|
||||
Mcnma.Batch(source, output, 3);
|
||||
|
||||
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]}");
|
||||
|
||||
Assert.Equal(10.0, output[2], 1e-9);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,223 @@
|
||||
using Xunit.Abstractions;
|
||||
|
||||
namespace QuanTAlib.Tests;
|
||||
|
||||
public sealed class McnmaValidationTests : IDisposable
|
||||
{
|
||||
private readonly ValidationTestData _testData;
|
||||
private readonly ITestOutputHelper _output;
|
||||
private bool _disposed;
|
||||
|
||||
public McnmaValidationTests(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_ManualTemaComposition_Batch()
|
||||
{
|
||||
// Manual 6-EMA with first-value seeding (matches Pine exactly)
|
||||
int[] periods = { 5, 10, 14, 20, 50 };
|
||||
|
||||
foreach (var period in periods)
|
||||
{
|
||||
var mcnma = new Mcnma(period);
|
||||
var qResult = mcnma.Update(_testData.Data);
|
||||
|
||||
double alpha = 2.0 / (period + 1);
|
||||
double decay = 1.0 - alpha;
|
||||
double e1 = 0, e2 = 0, e3 = 0, e4 = 0, e5 = 0, e6 = 0;
|
||||
bool init = false;
|
||||
var manualResults = new List<double>();
|
||||
|
||||
for (int i = 0; i < _testData.Data.Count; i++)
|
||||
{
|
||||
double val = _testData.Data[i].Value;
|
||||
if (!init)
|
||||
{
|
||||
e1 = e2 = e3 = e4 = e5 = e6 = val;
|
||||
init = true;
|
||||
manualResults.Add(val);
|
||||
continue;
|
||||
}
|
||||
|
||||
e1 = Math.FusedMultiplyAdd(e1, decay, alpha * val);
|
||||
e2 = Math.FusedMultiplyAdd(e2, decay, alpha * e1);
|
||||
e3 = Math.FusedMultiplyAdd(e3, decay, alpha * e2);
|
||||
double tema1 = 3.0 * e1 - 3.0 * e2 + e3;
|
||||
|
||||
e4 = Math.FusedMultiplyAdd(e4, decay, alpha * tema1);
|
||||
e5 = Math.FusedMultiplyAdd(e5, decay, alpha * e4);
|
||||
e6 = Math.FusedMultiplyAdd(e6, decay, alpha * e5);
|
||||
double tema2 = 3.0 * e4 - 3.0 * e5 + e6;
|
||||
|
||||
manualResults.Add(2.0 * tema1 - tema2);
|
||||
}
|
||||
|
||||
for (int i = 0; i < qResult.Count; i++)
|
||||
{
|
||||
Assert.Equal(manualResults[i], qResult[i].Value, 1e-9);
|
||||
}
|
||||
}
|
||||
_output.WriteLine("MCNMA Batch(TSeries) validated successfully against manual 6-EMA composition");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Validate_StreamingVsBatch_Consistency()
|
||||
{
|
||||
int[] periods = { 5, 10, 14, 20 };
|
||||
|
||||
foreach (var period in periods)
|
||||
{
|
||||
var batchResult = Mcnma.Batch(_testData.Data, period);
|
||||
|
||||
var streaming = new Mcnma(period);
|
||||
for (int i = 0; i < _testData.Data.Count; i++)
|
||||
{
|
||||
streaming.Update(_testData.Data[i]);
|
||||
}
|
||||
|
||||
int start = Math.Max(0, _testData.Data.Count - 100);
|
||||
for (int i = start; i < _testData.Data.Count; i++)
|
||||
{
|
||||
Assert.Equal(batchResult[i].Value, batchResult[i].Value, 1e-9);
|
||||
}
|
||||
}
|
||||
_output.WriteLine("MCNMA Streaming vs Batch validated successfully");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Validate_SpanVsStreaming_Consistency()
|
||||
{
|
||||
int[] periods = { 5, 10, 14, 20 };
|
||||
double[] sourceData = _testData.RawData.ToArray();
|
||||
|
||||
foreach (var period in periods)
|
||||
{
|
||||
double[] spanOutput = new double[sourceData.Length];
|
||||
Mcnma.Batch(sourceData.AsSpan(), spanOutput.AsSpan(), period);
|
||||
|
||||
var streaming = new Mcnma(period);
|
||||
for (int i = 0; i < sourceData.Length; i++)
|
||||
{
|
||||
var val = streaming.Update(new TValue(DateTime.UtcNow, sourceData[i]));
|
||||
Assert.Equal(val.Value, spanOutput[i], 1e-9);
|
||||
}
|
||||
}
|
||||
_output.WriteLine("MCNMA Span vs Streaming validated successfully");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Validate_ConstantInput_ConvergesToInput()
|
||||
{
|
||||
// With constant input, all EMAs converge to the constant.
|
||||
// TEMA(const) = 3*const - 3*const + const = const
|
||||
// MCNMA = 2*const - const = const
|
||||
const double constantValue = 42.0;
|
||||
const int period = 10;
|
||||
|
||||
var mcnma = new Mcnma(period);
|
||||
double lastResult = 0;
|
||||
|
||||
for (int i = 0; i < 200; i++)
|
||||
{
|
||||
var result = mcnma.Update(new TValue(DateTime.UtcNow, constantValue));
|
||||
lastResult = result.Value;
|
||||
}
|
||||
|
||||
Assert.Equal(constantValue, lastResult, 1e-6);
|
||||
_output.WriteLine("MCNMA constant input convergence validated successfully");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Validate_Against_ManualFormula()
|
||||
{
|
||||
// Manual 6-EMA with first-value seeding (matches Pine exactly)
|
||||
int[] periods = { 5, 10, 14, 20 };
|
||||
|
||||
foreach (var period in periods)
|
||||
{
|
||||
var mcnma = new Mcnma(period);
|
||||
double alpha = 2.0 / (period + 1);
|
||||
double decay = 1.0 - alpha;
|
||||
double e1 = 0, e2 = 0, e3 = 0, e4 = 0, e5 = 0, e6 = 0;
|
||||
bool init = false;
|
||||
|
||||
for (int i = 0; i < _testData.Data.Count; i++)
|
||||
{
|
||||
var item = _testData.Data[i];
|
||||
var qVal = mcnma.Update(item);
|
||||
double val = item.Value;
|
||||
|
||||
if (!init)
|
||||
{
|
||||
e1 = e2 = e3 = e4 = e5 = e6 = val;
|
||||
init = true;
|
||||
Assert.Equal(val, qVal.Value, ValidationHelper.DefaultTolerance);
|
||||
continue;
|
||||
}
|
||||
|
||||
e1 = Math.FusedMultiplyAdd(e1, decay, alpha * val);
|
||||
e2 = Math.FusedMultiplyAdd(e2, decay, alpha * e1);
|
||||
e3 = Math.FusedMultiplyAdd(e3, decay, alpha * e2);
|
||||
double tema1 = 3.0 * e1 - 3.0 * e2 + e3;
|
||||
|
||||
e4 = Math.FusedMultiplyAdd(e4, decay, alpha * tema1);
|
||||
e5 = Math.FusedMultiplyAdd(e5, decay, alpha * e4);
|
||||
e6 = Math.FusedMultiplyAdd(e6, decay, alpha * e5);
|
||||
double tema2 = 3.0 * e4 - 3.0 * e5 + e6;
|
||||
|
||||
double manualVal = 2.0 * tema1 - tema2;
|
||||
Assert.Equal(manualVal, qVal.Value, ValidationHelper.DefaultTolerance);
|
||||
}
|
||||
}
|
||||
_output.WriteLine("MCNMA validated successfully against manual 6-EMA formula");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Validate_NaN_Robustness()
|
||||
{
|
||||
const int period = 10;
|
||||
var mcnma = new Mcnma(period);
|
||||
|
||||
for (int i = 0; i < 20; i++)
|
||||
{
|
||||
mcnma.Update(new TValue(DateTime.UtcNow, 100.0 + i));
|
||||
}
|
||||
|
||||
var nanResult = mcnma.Update(new TValue(DateTime.UtcNow, double.NaN));
|
||||
Assert.True(double.IsFinite(nanResult.Value), "MCNMA should handle NaN with last-valid substitution");
|
||||
|
||||
var infResult = mcnma.Update(new TValue(DateTime.UtcNow, double.PositiveInfinity));
|
||||
Assert.True(double.IsFinite(infResult.Value), "MCNMA should handle Infinity with last-valid substitution");
|
||||
|
||||
var negInfResult = mcnma.Update(new TValue(DateTime.UtcNow, double.NegativeInfinity));
|
||||
Assert.True(double.IsFinite(negInfResult.Value), "MCNMA should handle -Infinity with last-valid substitution");
|
||||
|
||||
var resumeResult = mcnma.Update(new TValue(DateTime.UtcNow, 125.0));
|
||||
Assert.True(double.IsFinite(resumeResult.Value), "MCNMA should resume cleanly after invalid inputs");
|
||||
|
||||
_output.WriteLine("MCNMA NaN/Infinity robustness validated successfully");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user