mirror of
https://github.com/mihakralj/QuanTAlib.git
synced 2026-08-24 21:48:03 +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,36 @@
|
||||
using TradingPlatform.BusinessLayer;
|
||||
using QuanTAlib;
|
||||
|
||||
namespace QuanTAlib.Tests;
|
||||
|
||||
public sealed class KriIndicatorTests
|
||||
{
|
||||
[Fact] public void KriIndicator_Constructor_SetsDefaults() { var i = new KriIndicator(); Assert.Equal(14, i.Period); Assert.Equal(SourceType.Close, i.Source); Assert.True(i.ShowColdValues); Assert.Equal("KRI - Kairi Relative Index", i.Name); Assert.True(i.SeparateWindow); }
|
||||
[Fact] public void KriIndicator_MinHistoryDepths_EqualsZero() { Assert.Equal(0, KriIndicator.MinHistoryDepths); IWatchlistIndicator w = new KriIndicator(); Assert.Equal(0, w.MinHistoryDepths); }
|
||||
[Fact] public void KriIndicator_ShortName_IncludesParameters() { var i = new KriIndicator { Period = 20 }; i.Initialize(); Assert.Contains("KRI", i.ShortName, StringComparison.Ordinal); Assert.Contains("20", i.ShortName, StringComparison.Ordinal); }
|
||||
[Fact] public void KriIndicator_SourceCodeLink_IsValid() { var i = new KriIndicator(); Assert.Contains("github.com", i.SourceCodeLink, StringComparison.Ordinal); Assert.Contains("Kri.Quantower.cs", i.SourceCodeLink, StringComparison.Ordinal); }
|
||||
[Fact] public void KriIndicator_Initialize_CreatesInternalKri() { var i = new KriIndicator { Period = 10 }; i.Initialize(); Assert.Single(i.LinesSeries); }
|
||||
[Fact]
|
||||
public void KriIndicator_ProcessUpdate_HistoricalBar_ComputesValue()
|
||||
{
|
||||
var indicator = new KriIndicator { Period = 5 }; indicator.Initialize();
|
||||
var now = DateTime.UtcNow;
|
||||
for (int i = 0; i < 20; i++) { indicator.HistoricalData.AddBar(now.AddMinutes(i), 100 + i, 110 + i, 90 + i, 105 + i); indicator.ProcessUpdate(new UpdateArgs(UpdateReason.HistoricalBar)); }
|
||||
Assert.True(double.IsFinite(indicator.LinesSeries[0].GetValue(0)));
|
||||
}
|
||||
[Fact]
|
||||
public void KriIndicator_ProcessUpdate_NewBar_ComputesValue()
|
||||
{
|
||||
var indicator = new KriIndicator { Period = 5 }; indicator.Initialize();
|
||||
var now = DateTime.UtcNow;
|
||||
for (int i = 0; i < 20; i++)
|
||||
{
|
||||
indicator.HistoricalData.AddBar(now.AddMinutes(i), 100 + i, 110 + i, 90 + i, 105 + i);
|
||||
}
|
||||
indicator.ProcessUpdate(new UpdateArgs(UpdateReason.HistoricalBar));
|
||||
indicator.HistoricalData.AddBar(now.AddMinutes(20), 120, 130, 110, 125);
|
||||
indicator.ProcessUpdate(new UpdateArgs(UpdateReason.NewBar));
|
||||
Assert.Single(indicator.LinesSeries);
|
||||
}
|
||||
[Fact] public void KriIndicator_Parameters_CanBeChanged() { var i = new KriIndicator { Period = 20 }; i.Initialize(); Assert.Equal(20, i.Period); }
|
||||
}
|
||||
@@ -0,0 +1,205 @@
|
||||
using Xunit;
|
||||
|
||||
namespace QuanTAlib.Tests;
|
||||
|
||||
public sealed class KriTests
|
||||
{
|
||||
private const double Tolerance = 1e-9;
|
||||
|
||||
// ───── A) Constructor validation ─────
|
||||
[Fact] public void Constructor_DefaultPeriod_IsValid() { var k = new Kri(); Assert.Equal(14, k.Period); Assert.Equal("Kri(14)", k.Name); }
|
||||
[Fact] public void Constructor_InvalidPeriod_Throws() { var ex = Assert.Throws<ArgumentException>(() => new Kri(period: 0)); Assert.Equal("period", ex.ParamName); }
|
||||
[Fact] public void Constructor_NegativePeriod_Throws() { var ex = Assert.Throws<ArgumentException>(() => new Kri(period: -5)); Assert.Equal("period", ex.ParamName); }
|
||||
[Fact] public void Constructor_CustomPeriod_SetsCorrectly() { var k = new Kri(period: 20); Assert.Equal(20, k.Period); Assert.Equal("Kri(20)", k.Name); }
|
||||
|
||||
// ───── B) Basic calculation ─────
|
||||
[Fact] public void Update_ReturnsTValue() { var k = new Kri(5); Assert.IsType<TValue>(k.Update(new TValue(DateTime.UtcNow, 100.0))); }
|
||||
[Fact] public void Update_Last_IsAccessible() { var k = new Kri(5); k.Update(new TValue(DateTime.UtcNow, 100.0)); Assert.True(double.IsFinite(k.Last.Value)); }
|
||||
[Fact]
|
||||
public void Update_PriceAboveSMA_PositiveKRI()
|
||||
{
|
||||
var k = new Kri(5);
|
||||
for (int i = 0; i < 20; i++)
|
||||
{
|
||||
k.Update(new TValue(DateTime.UtcNow, 100.0 + i * 2));
|
||||
}
|
||||
Assert.True(k.Last.Value > 0, "Price above SMA should produce positive KRI");
|
||||
}
|
||||
[Fact]
|
||||
public void Update_PriceBelowSMA_NegativeKRI()
|
||||
{
|
||||
var k = new Kri(5);
|
||||
for (int i = 0; i < 20; i++)
|
||||
{
|
||||
k.Update(new TValue(DateTime.UtcNow, 200.0 - i * 2));
|
||||
}
|
||||
Assert.True(k.Last.Value < 0, "Price below SMA should produce negative KRI");
|
||||
}
|
||||
|
||||
// ───── C) State + bar correction ─────
|
||||
[Fact]
|
||||
public void Update_IsNew_False_RollsBack()
|
||||
{
|
||||
var k = new Kri(5);
|
||||
for (int i = 0; i < 12; i++)
|
||||
{
|
||||
k.Update(new TValue(DateTime.UtcNow, 100.0 + i), isNew: true);
|
||||
}
|
||||
k.Update(new TValue(DateTime.UtcNow, 105.0), isNew: false);
|
||||
var c1 = k.Last;
|
||||
k.Update(new TValue(DateTime.UtcNow, 105.0), isNew: false);
|
||||
Assert.Equal(c1.Value, k.Last.Value, Tolerance);
|
||||
}
|
||||
[Fact]
|
||||
public void Update_IterativeCorrections_Restore()
|
||||
{
|
||||
var k = new Kri(5);
|
||||
double[] data = new double[15];
|
||||
for (int i = 0; i < data.Length; i++)
|
||||
{
|
||||
data[i] = 100 + i * 2;
|
||||
}
|
||||
for (int i = 0; i < data.Length; i++)
|
||||
{
|
||||
k.Update(new TValue(DateTime.UtcNow, data[i]), isNew: true);
|
||||
}
|
||||
var baseline = k.Last.Value;
|
||||
k.Update(new TValue(DateTime.UtcNow, 999.0), isNew: false);
|
||||
k.Update(new TValue(DateTime.UtcNow, 888.0), isNew: false);
|
||||
k.Update(new TValue(DateTime.UtcNow, data[^1]), isNew: false);
|
||||
Assert.Equal(baseline, k.Last.Value, Tolerance);
|
||||
}
|
||||
[Fact]
|
||||
public void Reset_ClearsState()
|
||||
{
|
||||
var k = new Kri(5);
|
||||
for (int i = 0; i < 10; i++)
|
||||
{
|
||||
k.Update(new TValue(DateTime.UtcNow, 100.0 + i));
|
||||
}
|
||||
k.Reset();
|
||||
Assert.False(k.IsHot);
|
||||
Assert.Equal(0.0, k.Last.Value);
|
||||
}
|
||||
|
||||
// ───── D) Warmup/convergence ─────
|
||||
[Fact]
|
||||
public void IsHot_FlipsAfterPeriod()
|
||||
{
|
||||
int period = 10;
|
||||
var k = new Kri(period);
|
||||
for (int i = 0; i < period - 1; i++)
|
||||
{
|
||||
k.Update(new TValue(DateTime.UtcNow, 100.0 + i));
|
||||
Assert.False(k.IsHot);
|
||||
}
|
||||
k.Update(new TValue(DateTime.UtcNow, 110.0));
|
||||
Assert.True(k.IsHot);
|
||||
}
|
||||
[Fact] public void WarmupPeriod_MatchesPeriod() { Assert.Equal(14, new Kri(14).WarmupPeriod); }
|
||||
|
||||
// ───── E) Robustness ─────
|
||||
[Fact]
|
||||
public void Update_NaN_UsesLastValid()
|
||||
{
|
||||
var k = new Kri(5);
|
||||
for (int i = 0; i < 10; i++)
|
||||
{
|
||||
k.Update(new TValue(DateTime.UtcNow, 100.0 + i));
|
||||
}
|
||||
k.Update(new TValue(DateTime.UtcNow, double.NaN));
|
||||
Assert.True(double.IsFinite(k.Last.Value));
|
||||
}
|
||||
[Fact]
|
||||
public void Update_Infinity_UsesLastValid()
|
||||
{
|
||||
var k = new Kri(5);
|
||||
for (int i = 0; i < 10; i++)
|
||||
{
|
||||
k.Update(new TValue(DateTime.UtcNow, 100.0 + i));
|
||||
}
|
||||
k.Update(new TValue(DateTime.UtcNow, double.PositiveInfinity));
|
||||
Assert.True(double.IsFinite(k.Last.Value));
|
||||
}
|
||||
[Fact]
|
||||
public void Update_BatchNaN_RemainsFinite()
|
||||
{
|
||||
var k = new Kri(5);
|
||||
for (int i = 0; i < 3; i++)
|
||||
{
|
||||
k.Update(new TValue(DateTime.UtcNow, double.NaN));
|
||||
}
|
||||
Assert.True(double.IsFinite(k.Last.Value));
|
||||
}
|
||||
|
||||
// ───── F) Consistency (4 modes match) ─────
|
||||
[Fact]
|
||||
public void AllModes_ProduceSameResults()
|
||||
{
|
||||
int period = 10;
|
||||
var gbm = new GBM(startPrice: 100.0, mu: 0.02, sigma: 0.1, seed: 42);
|
||||
var bars = gbm.Fetch(500, DateTime.UtcNow.Ticks, TimeSpan.FromMinutes(1));
|
||||
TSeries source = bars.Close;
|
||||
|
||||
var streaming = new Kri(period);
|
||||
var streamResults = new double[source.Count];
|
||||
for (int i = 0; i < source.Count; i++)
|
||||
{
|
||||
streamResults[i] = streaming.Update(source[i]).Value;
|
||||
}
|
||||
|
||||
TSeries batchSeries = Kri.Batch(source, period);
|
||||
var spanOutput = new double[source.Count];
|
||||
Kri.Batch(source.Values, spanOutput, period);
|
||||
|
||||
var eventSource = new TSeries();
|
||||
var eventIndicator = new Kri(eventSource, period);
|
||||
var eventResults = new double[source.Count];
|
||||
for (int i = 0; i < source.Count; i++)
|
||||
{
|
||||
eventSource.Add(source[i]);
|
||||
eventResults[i] = eventIndicator.Last.Value;
|
||||
}
|
||||
|
||||
for (int i = 0; i < source.Count; i++)
|
||||
{
|
||||
Assert.Equal(streamResults[i], batchSeries.Values[i], Tolerance);
|
||||
Assert.Equal(streamResults[i], spanOutput[i], Tolerance);
|
||||
Assert.Equal(streamResults[i], eventResults[i], Tolerance);
|
||||
}
|
||||
}
|
||||
|
||||
// ───── G) Span API tests ─────
|
||||
[Fact] public void Batch_Span_MismatchedLength_Throws() { var ex = Assert.Throws<ArgumentException>(() => Kri.Batch(new double[10], new double[5], 5)); Assert.Equal("output", ex.ParamName); }
|
||||
[Fact] public void Batch_Span_InvalidPeriod_Throws() { var ex = Assert.Throws<ArgumentException>(() => Kri.Batch(new double[10], new double[10], 0)); Assert.Equal("period", ex.ParamName); }
|
||||
[Fact] public void Batch_Span_Empty_NoException() { Kri.Batch(ReadOnlySpan<double>.Empty, Span<double>.Empty, 5); Assert.True(true); }
|
||||
[Fact]
|
||||
public void Batch_Span_NaN_Handled()
|
||||
{
|
||||
double[] src = [100, double.NaN, 102, 103, 104, 105, 106, 107, 108, 109];
|
||||
var output = new double[src.Length];
|
||||
Kri.Batch(src, output, 5);
|
||||
for (int i = 0; i < output.Length; i++)
|
||||
{
|
||||
Assert.True(double.IsFinite(output[i]));
|
||||
}
|
||||
}
|
||||
|
||||
// ───── H) Chainability ─────
|
||||
[Fact]
|
||||
public void Pub_Fires_OnUpdate()
|
||||
{
|
||||
var k = new Kri(5); int f = 0;
|
||||
k.Pub += (object? _, in TValueEventArgs _) => f++;
|
||||
k.Update(new TValue(DateTime.UtcNow, 100.0));
|
||||
Assert.Equal(1, f);
|
||||
}
|
||||
[Fact]
|
||||
public void EventBased_Chaining_Works()
|
||||
{
|
||||
var source = new TSeries();
|
||||
var k = new Kri(source, 5);
|
||||
source.Add(new TValue(DateTime.UtcNow, 100.0));
|
||||
Assert.True(double.IsFinite(k.Last.Value));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,196 @@
|
||||
using System.Runtime.CompilerServices;
|
||||
using Xunit;
|
||||
using Xunit.Abstractions;
|
||||
|
||||
namespace QuanTAlib.Tests;
|
||||
|
||||
/// <summary>
|
||||
/// Self-consistency validation for KRI (Kairi Relative Index).
|
||||
/// KRI is not implemented by TA-Lib, Skender, Tulip, or Ooples,
|
||||
/// so validation uses streaming == batch == span mode consistency
|
||||
/// plus mathematical identity checks against the SMA-deviation formula:
|
||||
/// KRI = 100 × (price − SMA) / SMA.
|
||||
/// </summary>
|
||||
public sealed class KriValidationTests(ITestOutputHelper output)
|
||||
{
|
||||
private readonly ITestOutputHelper _output = output;
|
||||
private const double Tolerance = 1e-12;
|
||||
|
||||
// ── A) Streaming == Batch(Span) ───────────────────────────────────────────
|
||||
[Fact]
|
||||
[SkipLocalsInit]
|
||||
public void Validate_Streaming_Equals_Batch_Period14()
|
||||
{
|
||||
const int N = 200;
|
||||
const int period = 14;
|
||||
|
||||
var gbm = new GBM(100.0, 0.05, 0.2, seed: 1001);
|
||||
var prices = new double[N];
|
||||
for (int i = 0; i < N; i++) { prices[i] = gbm.Next(isNew: true).Close; }
|
||||
|
||||
// Streaming
|
||||
var kri = new Kri(period);
|
||||
for (int i = 0; i < N; i++)
|
||||
{
|
||||
kri.Update(new TValue(DateTime.UtcNow.AddSeconds(i), prices[i]), isNew: true);
|
||||
}
|
||||
double streamVal = kri.Last.Value;
|
||||
|
||||
// Batch span
|
||||
var batchOut = new double[N];
|
||||
Kri.Batch(prices.AsSpan(), batchOut.AsSpan(), period);
|
||||
|
||||
_output.WriteLine($"Streaming KRI={streamVal:F10}, Batch KRI={batchOut[N - 1]:F10}");
|
||||
Assert.Equal(streamVal, batchOut[N - 1], Tolerance);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
[SkipLocalsInit]
|
||||
public void Validate_Streaming_Equals_Batch_Period20()
|
||||
{
|
||||
const int N = 300;
|
||||
const int period = 20;
|
||||
|
||||
var gbm = new GBM(100.0, 0.05, 0.3, seed: 2002);
|
||||
var prices = new double[N];
|
||||
for (int i = 0; i < N; i++) { prices[i] = gbm.Next(isNew: true).Close; }
|
||||
|
||||
var kri = new Kri(period);
|
||||
for (int i = 0; i < N; i++)
|
||||
{
|
||||
kri.Update(new TValue(DateTime.UtcNow.AddSeconds(i), prices[i]), isNew: true);
|
||||
}
|
||||
|
||||
var batchOut = new double[N];
|
||||
Kri.Batch(prices.AsSpan(), batchOut.AsSpan(), period);
|
||||
|
||||
Assert.Equal(kri.Last.Value, batchOut[N - 1], Tolerance);
|
||||
}
|
||||
|
||||
// ── B) Batch(TSeries) == Calculate ────────────────────────────────────────
|
||||
[Fact]
|
||||
public void Validate_Batch_Equals_Calculate()
|
||||
{
|
||||
const int period = 14;
|
||||
var gbm = new GBM(100.0, 0.05, 0.2, seed: 77);
|
||||
var t0 = DateTime.UtcNow;
|
||||
var times = new System.Collections.Generic.List<long>(200);
|
||||
var vals = new System.Collections.Generic.List<double>(200);
|
||||
for (int i = 0; i < 200; i++)
|
||||
{
|
||||
times.Add(t0.AddSeconds(i).Ticks);
|
||||
vals.Add(gbm.Next(isNew: true).Close);
|
||||
}
|
||||
var series = new TSeries(times, vals);
|
||||
|
||||
var batchResult = Kri.Batch(series, period);
|
||||
var (calcResult, _) = Kri.Calculate(series, period);
|
||||
|
||||
for (int i = 0; i < series.Count; i++)
|
||||
{
|
||||
Assert.Equal(batchResult.Values[i], calcResult.Values[i], 1e-9);
|
||||
}
|
||||
_output.WriteLine("KRI Batch == Calculate: PASSED");
|
||||
}
|
||||
|
||||
// ── C) Price above SMA → KRI > 0 (bullish) ────────────────────────────────
|
||||
[Fact]
|
||||
public void Validate_PriceAboveSma_KriPositive()
|
||||
{
|
||||
// Rising prices: each bar is above the rolling SMA
|
||||
const int N = 100;
|
||||
const int period = 5;
|
||||
double[] prices = new double[N];
|
||||
for (int i = 0; i < N; i++) { prices[i] = 100.0 + i * 2.0; }
|
||||
|
||||
var batchOut = new double[N];
|
||||
Kri.Batch(prices.AsSpan(), batchOut.AsSpan(), period);
|
||||
|
||||
int warmup = period;
|
||||
for (int i = warmup; i < N; i++)
|
||||
{
|
||||
Assert.True(batchOut[i] > 0,
|
||||
$"KRI should be positive (price above SMA) at index {i}, got {batchOut[i]}");
|
||||
}
|
||||
_output.WriteLine("KRI price above SMA → KRI > 0: PASSED");
|
||||
}
|
||||
|
||||
// ── D) Price below SMA → KRI < 0 (bearish) ───────────────────────────────
|
||||
[Fact]
|
||||
public void Validate_PriceBelowSma_KriNegative()
|
||||
{
|
||||
// Falling prices: each bar is below the rolling SMA
|
||||
const int N = 100;
|
||||
const int period = 5;
|
||||
double[] prices = new double[N];
|
||||
for (int i = 0; i < N; i++) { prices[i] = 200.0 - i * 2.0; }
|
||||
|
||||
var batchOut = new double[N];
|
||||
Kri.Batch(prices.AsSpan(), batchOut.AsSpan(), period);
|
||||
|
||||
int warmup = period;
|
||||
for (int i = warmup; i < N; i++)
|
||||
{
|
||||
Assert.True(batchOut[i] < 0,
|
||||
$"KRI should be negative (price below SMA) at index {i}, got {batchOut[i]}");
|
||||
}
|
||||
_output.WriteLine("KRI price below SMA → KRI < 0: PASSED");
|
||||
}
|
||||
|
||||
// ── E) Constant price → KRI = 0 ───────────────────────────────────────────
|
||||
[Fact]
|
||||
public void Validate_ConstantPrice_KriIsZero()
|
||||
{
|
||||
const int N = 50;
|
||||
const int period = 10;
|
||||
double[] prices = new double[N];
|
||||
Array.Fill(prices, 100.0);
|
||||
|
||||
var batchOut = new double[N];
|
||||
Kri.Batch(prices.AsSpan(), batchOut.AsSpan(), period);
|
||||
|
||||
int warmup = period;
|
||||
for (int i = warmup; i < N; i++)
|
||||
{
|
||||
Assert.Equal(0.0, batchOut[i], 1e-10);
|
||||
}
|
||||
_output.WriteLine("KRI constant price → KRI = 0: PASSED");
|
||||
}
|
||||
|
||||
// ── F) Mathematical formula verification ──────────────────────────────────
|
||||
[Fact]
|
||||
public void Validate_Formula_Manual()
|
||||
{
|
||||
// Hand-crafted 5-bar SMA: prices = [10, 12, 14, 16, 18] → SMA = 14
|
||||
// KRI = 100 * (18 - 14) / 14 = 28.571...
|
||||
const int period = 5;
|
||||
double[] prices = [10.0, 12.0, 14.0, 16.0, 18.0];
|
||||
double expectedSma = (10.0 + 12.0 + 14.0 + 16.0 + 18.0) / 5.0;
|
||||
double expectedKri = 100.0 * (18.0 - expectedSma) / expectedSma;
|
||||
|
||||
var batchOut = new double[prices.Length];
|
||||
Kri.Batch(prices.AsSpan(), batchOut.AsSpan(), period);
|
||||
|
||||
Assert.Equal(expectedKri, batchOut[prices.Length - 1], 1e-9);
|
||||
_output.WriteLine($"KRI formula check: expected={expectedKri:F6}, actual={batchOut[^1]:F6}: PASSED");
|
||||
}
|
||||
|
||||
// ── G) Determinism ────────────────────────────────────────────────────────
|
||||
[Fact]
|
||||
public void Validate_Deterministic()
|
||||
{
|
||||
const int N = 200;
|
||||
const int period = 14;
|
||||
var gbm = new GBM(100.0, 0.05, 0.2, seed: 99);
|
||||
double[] prices = new double[N];
|
||||
for (int i = 0; i < N; i++) { prices[i] = gbm.Next(isNew: true).Close; }
|
||||
|
||||
var out1 = new double[N];
|
||||
var out2 = new double[N];
|
||||
Kri.Batch(prices.AsSpan(), out1.AsSpan(), period);
|
||||
Kri.Batch(prices.AsSpan(), out2.AsSpan(), period);
|
||||
|
||||
for (int i = 0; i < N; i++) { Assert.Equal(out1[i], out2[i], 15); }
|
||||
_output.WriteLine("KRI determinism: PASSED");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user