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,36 @@
using TradingPlatform.BusinessLayer;
using QuanTAlib;
namespace QuanTAlib.Tests;
public sealed class PslIndicatorTests
{
[Fact] public void PslIndicator_Constructor_SetsDefaults() { var i = new PslIndicator(); Assert.Equal(12, i.Period); Assert.Equal(SourceType.Close, i.Source); Assert.True(i.ShowColdValues); Assert.Equal("PSL - Psychological Line", i.Name); Assert.True(i.SeparateWindow); }
[Fact] public void PslIndicator_MinHistoryDepths_EqualsZero() { Assert.Equal(0, PslIndicator.MinHistoryDepths); IWatchlistIndicator w = new PslIndicator(); Assert.Equal(0, w.MinHistoryDepths); }
[Fact] public void PslIndicator_ShortName_IncludesParameters() { var i = new PslIndicator { Period = 20 }; i.Initialize(); Assert.Contains("PSL", i.ShortName, StringComparison.Ordinal); Assert.Contains("20", i.ShortName, StringComparison.Ordinal); }
[Fact] public void PslIndicator_SourceCodeLink_IsValid() { var i = new PslIndicator(); Assert.Contains("github.com", i.SourceCodeLink, StringComparison.Ordinal); Assert.Contains("Psl.Quantower.cs", i.SourceCodeLink, StringComparison.Ordinal); }
[Fact] public void PslIndicator_Initialize_CreatesInternalPsl() { var i = new PslIndicator { Period = 10 }; i.Initialize(); Assert.Single(i.LinesSeries); }
[Fact]
public void PslIndicator_ProcessUpdate_HistoricalBar_ComputesValue()
{
var indicator = new PslIndicator { 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 PslIndicator_ProcessUpdate_NewBar_ComputesValue()
{
var indicator = new PslIndicator { 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 PslIndicator_Parameters_CanBeChanged() { var i = new PslIndicator { Period = 20 }; i.Initialize(); Assert.Equal(20, i.Period); }
}
+215
View File
@@ -0,0 +1,215 @@
using Xunit;
namespace QuanTAlib.Tests;
public sealed class PslTests
{
private const double Tolerance = 1e-9;
// ───── A) Constructor validation ─────
[Fact] public void Constructor_DefaultPeriod_IsValid() { var p = new Psl(); Assert.Equal(12, p.Period); Assert.Equal("Psl(12)", p.Name); }
[Fact] public void Constructor_InvalidPeriod_Throws() { var ex = Assert.Throws<ArgumentException>(() => new Psl(period: 0)); Assert.Equal("period", ex.ParamName); }
[Fact] public void Constructor_NegativePeriod_Throws() { var ex = Assert.Throws<ArgumentException>(() => new Psl(period: -5)); Assert.Equal("period", ex.ParamName); }
[Fact] public void Constructor_CustomPeriod_SetsCorrectly() { var p = new Psl(period: 20); Assert.Equal(20, p.Period); Assert.Equal("Psl(20)", p.Name); }
// ───── B) Basic calculation ─────
[Fact] public void Update_ReturnsTValue() { var p = new Psl(5); Assert.IsType<TValue>(p.Update(new TValue(DateTime.UtcNow, 100.0))); }
[Fact] public void Update_Last_IsAccessible() { var p = new Psl(5); p.Update(new TValue(DateTime.UtcNow, 100.0)); Assert.True(double.IsFinite(p.Last.Value)); }
[Fact]
public void Update_RisingPrices_PslAbove50()
{
var p = new Psl(5);
for (int i = 0; i < 20; i++)
{
p.Update(new TValue(DateTime.UtcNow, 100.0 + i * 2));
}
Assert.True(p.Last.Value > 50, "Rising prices should produce PSL > 50");
}
[Fact]
public void Update_FallingPrices_PslBelow50()
{
var p = new Psl(5);
for (int i = 0; i < 20; i++)
{
p.Update(new TValue(DateTime.UtcNow, 200.0 - i * 2));
}
Assert.True(p.Last.Value < 50, "Falling prices should produce PSL < 50");
}
[Fact]
public void Update_OutputInRange()
{
var p = new Psl(5);
for (int i = 0; i < 20; i++)
{
p.Update(new TValue(DateTime.UtcNow, 100.0 + i));
Assert.InRange(p.Last.Value, 0.0, 100.0);
}
}
// ───── C) State + bar correction ─────
[Fact]
public void Update_IsNew_False_RollsBack()
{
var p = new Psl(5);
for (int i = 0; i < 12; i++)
{
p.Update(new TValue(DateTime.UtcNow, 100.0 + i), isNew: true);
}
p.Update(new TValue(DateTime.UtcNow, 105.0), isNew: false);
var c1 = p.Last;
p.Update(new TValue(DateTime.UtcNow, 105.0), isNew: false);
Assert.Equal(c1.Value, p.Last.Value, Tolerance);
}
[Fact]
public void Update_IterativeCorrections_Restore()
{
var p = new Psl(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++)
{
p.Update(new TValue(DateTime.UtcNow, data[i]), isNew: true);
}
var baseline = p.Last.Value;
p.Update(new TValue(DateTime.UtcNow, 999.0), isNew: false);
p.Update(new TValue(DateTime.UtcNow, 888.0), isNew: false);
p.Update(new TValue(DateTime.UtcNow, data[^1]), isNew: false);
Assert.Equal(baseline, p.Last.Value, Tolerance);
}
[Fact]
public void Reset_ClearsState()
{
var p = new Psl(5);
for (int i = 0; i < 10; i++)
{
p.Update(new TValue(DateTime.UtcNow, 100.0 + i));
}
p.Reset();
Assert.False(p.IsHot);
Assert.Equal(0.0, p.Last.Value);
}
// ───── D) Warmup/convergence ─────
[Fact]
public void IsHot_FlipsAfterPeriod()
{
int period = 10;
var p = new Psl(period);
for (int i = 0; i < period - 1; i++)
{
p.Update(new TValue(DateTime.UtcNow, 100.0 + i));
Assert.False(p.IsHot);
}
p.Update(new TValue(DateTime.UtcNow, 110.0));
Assert.True(p.IsHot);
}
[Fact] public void WarmupPeriod_MatchesPeriod() { Assert.Equal(12, new Psl(12).WarmupPeriod); }
// ───── E) Robustness ─────
[Fact]
public void Update_NaN_UsesLastValid()
{
var p = new Psl(5);
for (int i = 0; i < 10; i++)
{
p.Update(new TValue(DateTime.UtcNow, 100.0 + i));
}
p.Update(new TValue(DateTime.UtcNow, double.NaN));
Assert.True(double.IsFinite(p.Last.Value));
}
[Fact]
public void Update_Infinity_UsesLastValid()
{
var p = new Psl(5);
for (int i = 0; i < 10; i++)
{
p.Update(new TValue(DateTime.UtcNow, 100.0 + i));
}
p.Update(new TValue(DateTime.UtcNow, double.PositiveInfinity));
Assert.True(double.IsFinite(p.Last.Value));
}
[Fact]
public void Update_BatchNaN_RemainsFinite()
{
var p = new Psl(5);
for (int i = 0; i < 3; i++)
{
p.Update(new TValue(DateTime.UtcNow, double.NaN));
}
Assert.True(double.IsFinite(p.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 Psl(period);
var streamResults = new double[source.Count];
for (int i = 0; i < source.Count; i++)
{
streamResults[i] = streaming.Update(source[i]).Value;
}
TSeries batchSeries = Psl.Batch(source, period);
var spanOutput = new double[source.Count];
Psl.Batch(source.Values, spanOutput, period);
var eventSource = new TSeries();
var eventIndicator = new Psl(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>(() => Psl.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>(() => Psl.Batch(new double[10], new double[10], 0)); Assert.Equal("period", ex.ParamName); }
[Fact] public void Batch_Span_Empty_NoException() { Psl.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];
Psl.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 p = new Psl(5); int f = 0;
p.Pub += (object? _, in TValueEventArgs _) => f++;
p.Update(new TValue(DateTime.UtcNow, 100.0));
Assert.Equal(1, f);
}
[Fact]
public void EventBased_Chaining_Works()
{
var source = new TSeries();
var p = new Psl(source, 5);
source.Add(new TValue(DateTime.UtcNow, 100.0));
Assert.True(double.IsFinite(p.Last.Value));
}
}
@@ -0,0 +1,225 @@
using System.Runtime.CompilerServices;
using Xunit;
using Xunit.Abstractions;
namespace QuanTAlib.Tests;
/// <summary>
/// Self-consistency validation for PSL (Psychological Line).
/// PSL is not implemented by TA-Lib, Skender, Tulip, or Ooples,
/// so validation uses streaming == batch == span mode consistency
/// plus mathematical identity checks against the formula:
/// PSL = 100 × (count of up-bars in period) / period.
/// </summary>
public sealed class PslValidationTests(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_Period12()
{
const int N = 200;
const int period = 12;
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 psl = new Psl(period);
for (int i = 0; i < N; i++)
{
psl.Update(new TValue(DateTime.UtcNow.AddSeconds(i), prices[i]), isNew: true);
}
double streamVal = psl.Last.Value;
// Batch span
var batchOut = new double[N];
Psl.Batch(prices.AsSpan(), batchOut.AsSpan(), period);
_output.WriteLine($"Streaming PSL={streamVal:F10}, Batch PSL={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 psl = new Psl(period);
for (int i = 0; i < N; i++)
{
psl.Update(new TValue(DateTime.UtcNow.AddSeconds(i), prices[i]), isNew: true);
}
var batchOut = new double[N];
Psl.Batch(prices.AsSpan(), batchOut.AsSpan(), period);
Assert.Equal(psl.Last.Value, batchOut[N - 1], Tolerance);
}
// ── B) Batch(TSeries) == Calculate ────────────────────────────────────────
[Fact]
public void Validate_Batch_Equals_Calculate()
{
const int period = 12;
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 = Psl.Batch(series, period);
var (calcResult, _) = Psl.Calculate(series, period);
for (int i = 0; i < series.Count; i++)
{
Assert.Equal(batchResult.Values[i], calcResult.Values[i], 1e-9);
}
_output.WriteLine("PSL Batch == Calculate: PASSED");
}
// ── C) All up-bars → PSL = 100 ────────────────────────────────────────────
[Fact]
public void Validate_AllUpBars_PslIs100()
{
// Monotonically rising prices: every bar is an up-bar
const int N = 50;
const int period = 12;
double[] prices = new double[N];
for (int i = 0; i < N; i++) { prices[i] = 100.0 + i; }
var batchOut = new double[N];
Psl.Batch(prices.AsSpan(), batchOut.AsSpan(), period);
int warmup = period;
for (int i = warmup; i < N; i++)
{
Assert.Equal(100.0, batchOut[i], 1e-9);
}
_output.WriteLine("PSL all up-bars → PSL = 100: PASSED");
}
// ── D) All down-bars → PSL = 0 ────────────────────────────────────────────
[Fact]
public void Validate_AllDownBars_PslIsZero()
{
// Monotonically falling prices: every bar is a down-bar
const int N = 50;
const int period = 12;
double[] prices = new double[N];
for (int i = 0; i < N; i++) { prices[i] = 200.0 - i; }
var batchOut = new double[N];
Psl.Batch(prices.AsSpan(), batchOut.AsSpan(), period);
int warmup = period;
for (int i = warmup; i < N; i++)
{
Assert.Equal(0.0, batchOut[i], 1e-9);
}
_output.WriteLine("PSL all down-bars → PSL = 0: PASSED");
}
// ── E) Alternating bars → PSL = 50 (when period is even) ─────────────────
[Fact]
public void Validate_AlternatingBars_PslIs50()
{
// Alternating up/down after warmup (even period)
const int N = 100;
const int period = 10; // even period
double[] prices = new double[N];
prices[0] = 100.0;
for (int i = 1; i < N; i++)
{
prices[i] = prices[i - 1] + (i % 2 == 0 ? 1.0 : -1.0);
}
var batchOut = new double[N];
Psl.Batch(prices.AsSpan(), batchOut.AsSpan(), period);
// After warmup, alternating pattern → 5/10 = 50%
int checkStart = period + 10;
for (int i = checkStart; i < N; i++)
{
Assert.Equal(50.0, batchOut[i], 1e-9);
}
_output.WriteLine("PSL alternating bars (period=10) → PSL = 50: PASSED");
}
// ── F) Output range [0, 100] ──────────────────────────────────────────────
[Fact]
public void Validate_OutputRange_ZeroToHundred()
{
const int N = 300;
const int period = 12;
var gbm = new GBM(100.0, 0.5, 2.0, seed: 42);
double[] prices = new double[N];
for (int i = 0; i < N; i++) { prices[i] = gbm.Next(isNew: true).Close; }
var batchOut = new double[N];
Psl.Batch(prices.AsSpan(), batchOut.AsSpan(), period);
for (int i = 0; i < N; i++)
{
Assert.True(batchOut[i] >= 0.0 && batchOut[i] <= 100.0,
$"PSL out of [0,100] range at index {i}: {batchOut[i]}");
}
_output.WriteLine("PSL output range [0, 100]: PASSED");
}
// ── G) Formula verification — manual calculation ──────────────────────────
[Fact]
public void Validate_Formula_Manual()
{
// prices = [10, 11, 9, 12, 11, 13] → up-bars: {11>10, 12>9, 13>11} = 3 of 5
// After 6 bars with period=5: last 5 are [11, 9, 12, 11, 13]
// up-bars in that window: 9<11(down), 12>9(up), 11<12(down), 13>11(up) → 2/5?
// Actually includes first transition into the buffer, let's use known window:
// window [9, 12, 11, 13, ?] — use 6 bars so window is last 5
// prices[1..5] = [11,9,12,11,13]: diffs=[11-10=up, 9-11=down, 12-9=up, 11-12=down, 13-11=up] = 3/5 = 60
const int period = 5;
double[] prices = [10.0, 11.0, 9.0, 12.0, 11.0, 13.0];
var batchOut = new double[prices.Length];
Psl.Batch(prices.AsSpan(), batchOut.AsSpan(), period);
// At index 5: window = last 5 prices [11,9,12,11,13]
// up comparisons: 11>10=yes, 9>11=no, 12>9=yes, 11>12=no, 13>11=yes → 3/5 = 60
Assert.Equal(60.0, batchOut[prices.Length - 1], 1e-9);
_output.WriteLine($"PSL formula check: expected=60, actual={batchOut[^1]}: PASSED");
}
// ── H) Determinism ────────────────────────────────────────────────────────
[Fact]
public void Validate_Deterministic()
{
const int N = 200;
const int period = 12;
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];
Psl.Batch(prices.AsSpan(), out1.AsSpan(), period);
Psl.Batch(prices.AsSpan(), out2.AsSpan(), period);
for (int i = 0; i < N; i++) { Assert.Equal(out1[i], out2[i], 15); }
_output.WriteLine("PSL determinism: PASSED");
}
}