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:
Miha Kralj
2026-03-12 12:34:16 -07:00
parent 8937b0c0fa
commit 060649192f
1149 changed files with 1780 additions and 3316 deletions
@@ -0,0 +1,171 @@
using TradingPlatform.BusinessLayer;
namespace QuanTAlib.Tests;
public class JmaIndicatorTests
{
[Fact]
public void JmaIndicator_Constructor_SetsDefaults()
{
var indicator = new JmaIndicator();
Assert.Equal(10, indicator.Period);
Assert.Equal(0, indicator.Phase);
Assert.Equal(SourceType.Close, indicator.Source);
Assert.True(indicator.ShowColdValues);
Assert.Equal("JMA - Jurik Moving Average", indicator.Name);
Assert.False(indicator.SeparateWindow);
Assert.True(indicator.OnBackGround);
}
[Fact]
public void JmaIndicator_MinHistoryDepths_EqualsPeriod()
{
var indicator = new JmaIndicator { Period = 20 };
Assert.Equal(0, JmaIndicator.MinHistoryDepths);
Assert.Equal(0, ((IWatchlistIndicator)indicator).MinHistoryDepths);
}
[Fact]
public void JmaIndicator_ShortName_IncludesParameters()
{
var indicator = new JmaIndicator { Period = 15, Phase = 50 };
Assert.Contains("JMA", indicator.ShortName, StringComparison.Ordinal);
Assert.Contains("15", indicator.ShortName, StringComparison.Ordinal);
Assert.Contains("50", indicator.ShortName, StringComparison.Ordinal);
}
[Fact]
public void JmaIndicator_SourceCodeLink_IsValid()
{
var indicator = new JmaIndicator();
Assert.Contains("github.com", indicator.SourceCodeLink, StringComparison.Ordinal);
Assert.Contains("Jma.Quantower.cs", indicator.SourceCodeLink, StringComparison.Ordinal);
}
[Fact]
public void JmaIndicator_Initialize_CreatesInternalJma()
{
var indicator = new JmaIndicator { Period = 10 };
// Initialize should not throw
indicator.Initialize();
// After init, line series should exist
Assert.Single(indicator.LinesSeries);
}
[Fact]
public void JmaIndicator_ProcessUpdate_HistoricalBar_ComputesValue()
{
var indicator = new JmaIndicator { 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 JmaIndicator_ProcessUpdate_NewBar_ComputesValue()
{
var indicator = new JmaIndicator { 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 JmaIndicator_ProcessUpdate_NewTick_ProcessesWithoutError()
{
var indicator = new JmaIndicator { 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 JmaIndicator_MultipleUpdates_ProducesCorrectSequence()
{
var indicator = new JmaIndicator { 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 JmaIndicator_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 JmaIndicator { 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 JmaIndicator_Parameters_CanBeChanged()
{
var indicator = new JmaIndicator { Period = 5, Phase = 10 };
Assert.Equal(5, indicator.Period);
Assert.Equal(10, indicator.Phase);
indicator.Period = 20;
indicator.Phase = -10;
Assert.Equal(20, indicator.Period);
Assert.Equal(-10, indicator.Phase);
Assert.Equal(0, JmaIndicator.MinHistoryDepths);
}
}
+369
View File
@@ -0,0 +1,369 @@
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);
}
}
@@ -0,0 +1,72 @@
using OoplesFinance.StockIndicators;
using OoplesFinance.StockIndicators.Models;
namespace QuanTAlib.Tests;
public class JmaValidationTests
{
[Fact]
public void Jma_FollowsPriceTrend()
{
// JMA should generally follow the price.
// If price goes up, JMA should eventually go up.
var jma = new Jma(10);
double previousJma = 0;
// Uptrend
for (int i = 0; i < 100; i++)
{
var result = jma.Update(new TValue(DateTime.UtcNow, i));
if (i > 20) // Allow warmup
{
Assert.True(result.Value > previousJma, $"JMA should be increasing in uptrend at step {i}");
}
previousJma = result.Value;
}
}
[Fact]
public void Jma_WithinBounds()
{
// JMA should stay within the range of recent prices (roughly)
// It's a moving average, so it shouldn't overshoot wildly unless phase is negative and high volatility?
// With default phase 0, it should be well behaved.
var jma = new Jma(10);
var gbm = new GBM(startPrice: 100, mu: 0, sigma: 0.5);
for (int i = 0; i < 1000; i++)
{
var bar = gbm.Next(isNew: true);
var result = jma.Update(new TValue(bar.Time, bar.Close));
if (i > 20)
{
// Update bounds of recent price history (simplified)
// This is a loose check.
// Just check it's finite and positive for this GBM
Assert.True(double.IsFinite(result.Value));
Assert.True(result.Value > 0);
}
}
}
[Fact]
public void Jma_MatchesOoples_Structural()
{
var gbm = new GBM(startPrice: 100.0, mu: 0.02, sigma: 0.15, seed: 42);
var bars = gbm.Fetch(500, DateTime.UtcNow.Ticks, TimeSpan.FromMinutes(1));
var ooplesData = bars.Select(b => new TickerData
{
Date = new DateTime(b.Time, DateTimeKind.Utc),
Open = b.Open, High = b.High, Low = b.Low,
Close = b.Close, Volume = b.Volume
}).ToList();
var result = new StockData(ooplesData).CalculateJurikMovingAverage();
var values = result.CustomValuesList;
int finiteCount = values.Count(v => double.IsFinite(v));
Assert.True(finiteCount > 100, $"Expected >100 finite values, got {finiteCount}");
}
}
@@ -0,0 +1,42 @@
namespace QuanTAlib.Tests;
public class JmaZeroDivTests
{
[Fact]
public void Period1_DoesNotProduceInfinityOrNaN()
{
// Arrange
var jma = new Jma(period: 1);
double[] values = { 100, 101, 102, 101, 100 };
// Act & Assert
foreach (var v in values)
{
var result = jma.Update(new TValue(DateTime.UtcNow, v));
Assert.False(double.IsNaN(result.Value), $"JMA(1) produced NaN for input {v}");
Assert.False(double.IsInfinity(result.Value), $"JMA(1) produced Infinity for input {v}");
// For period 1, JMA should ideally track price very closely
Assert.Equal(v, result.Value, precision: 1);
}
}
[Fact]
public void Period1_LogValuesAreFinite()
{
// This test inspects private fields via reflection or just checks behavior
// Since we can't easily access private fields, we'll rely on the calculation logic check
// If the fix is applied, we shouldn't see -Infinity in internal calculations if we could see them.
// But we can check if the output is exactly the input, which implies adapt=0 (if logic holds).
var jma = new Jma(period: 1);
var result = jma.Update(new TValue(DateTime.UtcNow, 100));
Assert.Equal(100, result.Value);
result = jma.Update(new TValue(DateTime.UtcNow, 200));
// If adapt is 0 (due to -Infinity log), bands snap to price.
// If JMA(1) is identity, result should be 200.
// With clamping, adapt is slightly non-zero (approx 1e-12), so result is very close to 200.
Assert.Equal(200, result.Value, precision: 7);
}
}