Files
Miha Kralj 060649192f 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
2026-03-12 12:34:16 -07:00

370 lines
12 KiB
C#

namespace QuanTAlib.Tests;
public class JmaTests
{
[Fact]
public void Jma_Constructor_ValidatesInput()
{
// JMA doesn't explicitly throw on period currently, but let's check if it handles valid inputs
var jma = new Jma(10);
Assert.NotNull(jma);
}
[Fact]
public void Jma_Calc_ReturnsValue()
{
var jma = new Jma(10);
Assert.Equal(0, jma.Last.Value);
TValue result = jma.Update(new TValue(DateTime.UtcNow, 100));
Assert.True(result.Value > 0);
Assert.Equal(result.Value, jma.Last.Value);
}
[Fact]
public void Jma_SpanCalc_ValidatesInput()
{
double[] source = [1, 2, 3, 4, 5];
double[] output = new double[5];
double[] wrongSizeOutput = new double[3];
// Period must be > 0
Assert.Throws<ArgumentOutOfRangeException>(() => Jma.Batch(source.AsSpan(), output.AsSpan(), 0, 0));
Assert.Throws<ArgumentOutOfRangeException>(() => Jma.Batch(source.AsSpan(), output.AsSpan(), -1, 0));
// Output must be same length as source
Assert.Throws<ArgumentException>(() => Jma.Batch(source.AsSpan(), wrongSizeOutput.AsSpan(), 3, 0));
}
[Fact]
public void Jma_Calc_IsNew_False_UpdatesValue()
{
var jma = new Jma(10);
jma.Update(new TValue(DateTime.UtcNow, 100));
jma.Update(new TValue(DateTime.UtcNow, 110), isNew: true);
double beforeUpdate = jma.Last.Value;
jma.Update(new TValue(DateTime.UtcNow, 120), isNew: false);
double afterUpdate = jma.Last.Value;
// Update should change the value
Assert.NotEqual(beforeUpdate, afterUpdate);
}
[Fact]
public void Jma_Reset_ClearsState()
{
var jma = new Jma(10);
jma.Update(new TValue(DateTime.UtcNow, 100));
jma.Update(new TValue(DateTime.UtcNow, 105));
double valueBefore = jma.Last.Value;
jma.Reset();
Assert.Equal(0, jma.Last.Value);
// After reset, should accept new values
jma.Update(new TValue(DateTime.UtcNow, 50));
Assert.NotEqual(0, jma.Last.Value);
Assert.NotEqual(valueBefore, jma.Last.Value);
}
[Fact]
public void Jma_IsHot_BecomesTrueAfterWarmup()
{
var jma = new Jma(10);
Assert.False(jma.IsHot);
// Warmup for JMA(10) is approx 203 bars
// ceil(20 + 80 * 10^0.36) = 203
int warmup = (int)Math.Ceiling(20.0 + 80.0 * Math.Pow(10, 0.36));
for (int i = 1; i < warmup; i++)
{
jma.Update(new TValue(DateTime.UtcNow, i * 10));
Assert.False(jma.IsHot);
}
jma.Update(new TValue(DateTime.UtcNow, 100));
Assert.True(jma.IsHot);
}
[Fact]
public void Jma_IterativeCorrections_RestoreToOriginalState()
{
var jma = new Jma(10);
var gbm = new GBM(startPrice: 100.0, mu: 0.02, sigma: 0.1);
// Feed 20 new values (enough to fill buffer and stabilize)
TValue lastInput = default;
for (int i = 0; i < 20; i++)
{
var bar = gbm.Next(isNew: true);
lastInput = new TValue(bar.Time, bar.Close);
jma.Update(lastInput, isNew: true);
}
// Remember JMA state
double jmaAfter = jma.Last.Value;
// Generate 5 corrections with isNew=false (different values)
for (int i = 0; i < 5; i++)
{
var bar = gbm.Next(isNew: false);
jma.Update(new TValue(bar.Time, bar.Close), isNew: false);
}
// Feed the remembered last input again with isNew=false
TValue finalJma = jma.Update(lastInput, isNew: false);
// JMA should match the original state
Assert.Equal(jmaAfter, finalJma.Value, 1e-10);
}
[Fact]
public void Jma_NaN_Input_UsesLastValidValue()
{
var jma = new Jma(10);
// Feed some valid values
jma.Update(new TValue(DateTime.UtcNow, 100));
jma.Update(new TValue(DateTime.UtcNow, 110));
// Feed NaN - should use last valid value (110)
var resultAfterNaN = jma.Update(new TValue(DateTime.UtcNow, double.NaN));
// Result should be finite (not NaN)
Assert.True(double.IsFinite(resultAfterNaN.Value));
Assert.NotEqual(0, resultAfterNaN.Value);
}
[Fact]
public void Jma_SpanCalc_MatchesTSeriesCalc()
{
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 = Jma.Batch(series, 10);
// Calculate with Span API
Jma.Batch(source.AsSpan(), output.AsSpan(), 10);
// Compare results
for (int i = 0; i < 100; i++)
{
Assert.Equal(tseriesResult[i].Value, output[i], 1e-10);
}
}
[Fact]
public void Jma_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 = Jma.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];
Jma.Batch(spanInput, spanOutput, period);
double spanResult = spanOutput[^1];
// 3. Streaming Mode
var streamingInd = new Jma(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 Jma(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 Jma_Phase_AffectsResult()
{
var series = new TSeries();
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);
series.Add(bar.Time, bar.Close);
}
var jmaPhase0 = Jma.Batch(series, 10, phase: 0);
var jmaPhase100 = Jma.Batch(series, 10, phase: 100);
var jmaPhaseMinus100 = Jma.Batch(series, 10, phase: -100);
Assert.NotEqual(jmaPhase0.Last.Value, jmaPhase100.Last.Value);
Assert.NotEqual(jmaPhase0.Last.Value, jmaPhaseMinus100.Last.Value);
}
[Fact]
public void Jma_Power_RemovedFromApi()
{
// Power parameter was removed — it was never used in calculation.
// Verify the 2-parameter Batch still works correctly.
var series = new TSeries();
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);
series.Add(bar.Time, bar.Close);
}
var result = Jma.Batch(series, 10);
Assert.True(double.IsFinite(result.Last.Value));
}
[Fact]
public void Jma_Period_AffectsResult()
{
// Verify that different periods produce meaningfully different outputs
var series = new TSeries();
var gbm = new GBM(startPrice: 100.0, mu: 0.02, sigma: 0.1, seed: 42);
for (int i = 0; i < 500; i++)
{
var bar = gbm.Next(isNew: true);
series.Add(bar.Time, bar.Close);
}
// --- TSeries Batch mode ---
var jma7 = Jma.Batch(series, 7);
var jma14 = Jma.Batch(series, 14);
var jma50 = Jma.Batch(series, 50);
var jma100 = Jma.Batch(series, 100);
// Last values must all differ
Assert.NotEqual(jma7.Last.Value, jma14.Last.Value);
Assert.NotEqual(jma14.Last.Value, jma50.Last.Value);
Assert.NotEqual(jma50.Last.Value, jma100.Last.Value);
// Longer period = smoother = values closer to mean (less extreme)
// Verify at least some interior values differ (not just the last)
int midIdx = series.Count / 2;
Assert.NotEqual(jma7[midIdx].Value, jma50[midIdx].Value);
Assert.NotEqual(jma14[midIdx].Value, jma100[midIdx].Value);
// --- Span Batch mode ---
double[] source = series.Values.ToArray();
double[] out7 = new double[source.Length];
double[] out14 = new double[source.Length];
double[] out50 = new double[source.Length];
Jma.Batch(source.AsSpan(), out7.AsSpan(), 7);
Jma.Batch(source.AsSpan(), out14.AsSpan(), 14);
Jma.Batch(source.AsSpan(), out50.AsSpan(), 50);
Assert.NotEqual(out7[^1], out14[^1]);
Assert.NotEqual(out14[^1], out50[^1]);
// Span results must match TSeries results
Assert.Equal(jma7.Last.Value, out7[^1], 1e-10);
Assert.Equal(jma14.Last.Value, out14[^1], 1e-10);
Assert.Equal(jma50.Last.Value, out50[^1], 1e-10);
// --- Streaming mode ---
var stream7 = new Jma(7);
var stream50 = new Jma(50);
for (int i = 0; i < series.Count; i++)
{
stream7.Update(series[i]);
stream50.Update(series[i]);
}
Assert.NotEqual(stream7.Last.Value, stream50.Last.Value);
Assert.Equal(jma7.Last.Value, stream7.Last.Value, 1e-10);
Assert.Equal(jma50.Last.Value, stream50.Last.Value, 1e-10);
}
[Fact]
public void Jma_SpanCalc_HandlesNaN()
{
double[] source = [100, 110, double.NaN, 120, 130];
double[] output = new double[5];
Jma.Batch(source.AsSpan(), output.AsSpan(), 3);
foreach (var val in output)
{
Assert.True(double.IsFinite(val));
}
}
[Fact]
public void Jma_BatchUpdate_ThenStreamingUpdate_IsNewFalse_Works()
{
// This test verifies the fix for the state synchronization issue
// where _p_state and buffers weren't updated after batch Update(TSeries)
var jma = new Jma(10);
var gbm = new GBM(startPrice: 100.0, mu: 0.02, sigma: 0.1, seed: 42);
// Create a batch series with enough bars to reach warmup (203 for JMA(10))
int warmupBars = jma.WarmupPeriod + 50;
var series = new TSeries();
for (int i = 0; i < warmupBars; i++)
{
var bar = gbm.Next(isNew: true);
series.Add(bar.Time, bar.Close);
}
// Process batch - this should update _state, _p_state, and buffer snapshots
jma.Update(series);
// Now do a streaming update with isNew=true (new bar)
var bar51 = gbm.Next(isNew: true);
jma.Update(new TValue(bar51.Time, bar51.Close), isNew: true);
// Do several corrections with isNew=false
for (int i = 0; i < 3; i++)
{
var correction = gbm.Next(isNew: false);
jma.Update(new TValue(correction.Time, correction.Close), isNew: false);
}
// Feed the original bar51 value again with isNew=false
// It should restore to the state after bar51
var restoredResult = jma.Update(new TValue(bar51.Time, bar51.Close), isNew: false);
// The key test: After batch processing, we should be able to advance to a new bar
// and then do corrections without errors. Before the fix, this would fail because
// _p_state had stale data from before the batch processing.
Assert.True(double.IsFinite(restoredResult.Value));
Assert.True(jma.IsHot);
}
}