mirror of
https://github.com/mihakralj/QuanTAlib.git
synced 2026-08-23 21:18:04 +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,129 @@
|
||||
using TradingPlatform.BusinessLayer;
|
||||
using QuanTAlib;
|
||||
|
||||
namespace QuanTAlib.Tests;
|
||||
|
||||
public sealed class PgoIndicatorTests
|
||||
{
|
||||
[Fact]
|
||||
public void PgoIndicator_Constructor_SetsDefaults()
|
||||
{
|
||||
var indicator = new PgoIndicator();
|
||||
|
||||
Assert.Equal(14, indicator.Period);
|
||||
Assert.True(indicator.ShowColdValues);
|
||||
Assert.Equal("PGO - Pretty Good Oscillator", indicator.Name);
|
||||
Assert.True(indicator.SeparateWindow);
|
||||
Assert.True(indicator.OnBackGround);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void PgoIndicator_MinHistoryDepths_EqualsZero()
|
||||
{
|
||||
var indicator = new PgoIndicator { Period = 14 };
|
||||
|
||||
Assert.Equal(0, PgoIndicator.MinHistoryDepths);
|
||||
IWatchlistIndicator watchlistIndicator = indicator;
|
||||
Assert.Equal(0, watchlistIndicator.MinHistoryDepths);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void PgoIndicator_ShortName_IncludesParameters()
|
||||
{
|
||||
var indicator = new PgoIndicator { Period = 20 };
|
||||
indicator.Initialize();
|
||||
|
||||
Assert.Contains("PGO", indicator.ShortName, StringComparison.Ordinal);
|
||||
Assert.Contains("20", indicator.ShortName, StringComparison.Ordinal);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void PgoIndicator_SourceCodeLink_IsValid()
|
||||
{
|
||||
var indicator = new PgoIndicator();
|
||||
|
||||
Assert.Contains("github.com", indicator.SourceCodeLink, StringComparison.Ordinal);
|
||||
Assert.Contains("Pgo.Quantower.cs", indicator.SourceCodeLink, StringComparison.Ordinal);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void PgoIndicator_Initialize_CreatesInternalPgo()
|
||||
{
|
||||
var indicator = new PgoIndicator { Period = 10 };
|
||||
|
||||
indicator.Initialize();
|
||||
|
||||
Assert.Equal(4, indicator.LinesSeries.Count);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void PgoIndicator_ProcessUpdate_HistoricalBar_ComputesValue()
|
||||
{
|
||||
var indicator = new PgoIndicator { 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);
|
||||
|
||||
var args = new UpdateArgs(UpdateReason.HistoricalBar);
|
||||
indicator.ProcessUpdate(args);
|
||||
}
|
||||
|
||||
double value = indicator.LinesSeries[0].GetValue(0);
|
||||
Assert.True(double.IsFinite(value));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void PgoIndicator_ProcessUpdate_NewBar_ComputesValue()
|
||||
{
|
||||
var indicator = new PgoIndicator { 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.Equal(2, indicator.LinesSeries[0].Count);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void PgoIndicator_Parameters_CanBeChanged()
|
||||
{
|
||||
var indicator = new PgoIndicator { Period = 14 };
|
||||
|
||||
indicator.Period = 20;
|
||||
|
||||
Assert.Equal(20, indicator.Period);
|
||||
Assert.Equal(0, PgoIndicator.MinHistoryDepths);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void PgoIndicator_ReferenceLines_SetCorrectly()
|
||||
{
|
||||
var indicator = new PgoIndicator { Period = 5 };
|
||||
indicator.Initialize();
|
||||
|
||||
var now = DateTime.UtcNow;
|
||||
for (int i = 0; i < 10; i++)
|
||||
{
|
||||
indicator.HistoricalData.AddBar(now.AddMinutes(i), 100 + i, 110 + i, 90 + i, 105 + i);
|
||||
indicator.ProcessUpdate(new UpdateArgs(UpdateReason.HistoricalBar));
|
||||
}
|
||||
|
||||
// Zero line should be 0
|
||||
Assert.Equal(0.0, indicator.LinesSeries[1].GetValue(0));
|
||||
// Overbought line should be 3
|
||||
Assert.Equal(3.0, indicator.LinesSeries[2].GetValue(0));
|
||||
// Oversold line should be -3
|
||||
Assert.Equal(-3.0, indicator.LinesSeries[3].GetValue(0));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,408 @@
|
||||
using Xunit;
|
||||
|
||||
namespace QuanTAlib.Tests;
|
||||
|
||||
public sealed class PgoTests
|
||||
{
|
||||
private const int DefaultPeriod = 14;
|
||||
private const double Tolerance = 1e-10;
|
||||
|
||||
// ───── A) Constructor validation ─────
|
||||
|
||||
[Fact]
|
||||
public void Constructor_PeriodZero_ThrowsArgumentException()
|
||||
{
|
||||
var ex = Assert.Throws<ArgumentException>(() => new Pgo(period: 0));
|
||||
Assert.Equal("period", ex.ParamName);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Constructor_NegativePeriod_ThrowsArgumentException()
|
||||
{
|
||||
var ex = Assert.Throws<ArgumentException>(() => new Pgo(period: -1));
|
||||
Assert.Equal("period", ex.ParamName);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Constructor_ValidPeriod_SetsProperties()
|
||||
{
|
||||
var pgo = new Pgo(period: 10);
|
||||
Assert.Equal(10, pgo.Period);
|
||||
Assert.Equal("Pgo(10)", pgo.Name);
|
||||
Assert.Equal(10, pgo.WarmupPeriod);
|
||||
}
|
||||
|
||||
// ───── B) Basic calculation ─────
|
||||
|
||||
[Fact]
|
||||
public void Update_ReturnsTValue()
|
||||
{
|
||||
var pgo = new Pgo(DefaultPeriod);
|
||||
var bar = new TBar(DateTime.UtcNow, 100, 105, 95, 102, 1000);
|
||||
var result = pgo.Update(bar);
|
||||
Assert.IsType<TValue>(result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Update_Last_IsAccessible()
|
||||
{
|
||||
var pgo = new Pgo(DefaultPeriod);
|
||||
var bar = new TBar(DateTime.UtcNow, 100, 105, 95, 102, 1000);
|
||||
pgo.Update(bar);
|
||||
Assert.NotEqual(default, pgo.Last);
|
||||
Assert.False(pgo.IsHot);
|
||||
Assert.Equal($"Pgo({DefaultPeriod})", pgo.Name);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Update_ConstantBars_ZeroPgo()
|
||||
{
|
||||
var pgo = new Pgo(period: 5);
|
||||
for (int i = 0; i < 10; i++)
|
||||
{
|
||||
pgo.Update(new TBar(DateTime.UtcNow, 50, 50, 50, 50, 100));
|
||||
}
|
||||
// Constant bars have TR=0, SMA=close => PGO = 0/0 => 0.0 (guard)
|
||||
Assert.Equal(0.0, pgo.Last.Value, Tolerance);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Update_RisingClose_PositivePgo()
|
||||
{
|
||||
var pgo = new Pgo(period: 5);
|
||||
for (int i = 0; i < 10; i++)
|
||||
{
|
||||
double c = 100.0 + i;
|
||||
pgo.Update(new TBar(DateTime.UtcNow, c - 1, c + 2, c - 2, c, 100));
|
||||
}
|
||||
// Rising close above SMA => positive PGO
|
||||
Assert.True(pgo.Last.Value > 0);
|
||||
}
|
||||
|
||||
// ───── C) State + bar correction ─────
|
||||
|
||||
[Fact]
|
||||
public void Update_IsNew_True_AdvancesState()
|
||||
{
|
||||
var pgo = new Pgo(DefaultPeriod);
|
||||
pgo.Update(new TBar(DateTime.UtcNow, 100, 105, 95, 102, 1000), isNew: true);
|
||||
pgo.Update(new TBar(DateTime.UtcNow, 102, 110, 98, 108, 1000), isNew: true);
|
||||
|
||||
var last = pgo.Last;
|
||||
Assert.NotEqual(default, last);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Update_IsNew_False_RollsBack()
|
||||
{
|
||||
var pgo = new Pgo(period: 5);
|
||||
for (int i = 0; i < 6; i++)
|
||||
{
|
||||
double c = 100.0 + i;
|
||||
pgo.Update(new TBar(DateTime.UtcNow, c - 1, c + 2, c - 2, c, 100), isNew: true);
|
||||
}
|
||||
|
||||
// Bar correction: rewrite last bar
|
||||
pgo.Update(new TBar(DateTime.UtcNow, 104, 107, 103, 105, 100), isNew: false);
|
||||
var corrected = pgo.Last;
|
||||
|
||||
// Repeat same correction — should produce identical result
|
||||
pgo.Update(new TBar(DateTime.UtcNow, 104, 107, 103, 105, 100), isNew: false);
|
||||
var corrected2 = pgo.Last;
|
||||
|
||||
Assert.Equal(corrected.Value, corrected2.Value, Tolerance);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Update_IterativeCorrections_Restore()
|
||||
{
|
||||
var pgo = new Pgo(period: 5);
|
||||
TBar[] bars =
|
||||
[
|
||||
new(DateTime.UtcNow, 99, 102, 98, 100, 100),
|
||||
new(DateTime.UtcNow, 101, 104, 100, 102, 100),
|
||||
new(DateTime.UtcNow, 103, 106, 102, 104, 100),
|
||||
new(DateTime.UtcNow, 105, 108, 104, 106, 100),
|
||||
new(DateTime.UtcNow, 107, 110, 106, 108, 100),
|
||||
new(DateTime.UtcNow, 109, 112, 108, 110, 100),
|
||||
];
|
||||
|
||||
for (int i = 0; i < bars.Length; i++)
|
||||
{
|
||||
pgo.Update(bars[i], isNew: true);
|
||||
}
|
||||
|
||||
double baseline = pgo.Last.Value;
|
||||
|
||||
// Correct last bar 3 times, then restore original
|
||||
pgo.Update(new TBar(DateTime.UtcNow, 120, 130, 110, 999, 100), isNew: false);
|
||||
pgo.Update(new TBar(DateTime.UtcNow, 120, 130, 110, 888, 100), isNew: false);
|
||||
pgo.Update(bars[^1], isNew: false);
|
||||
|
||||
Assert.Equal(baseline, pgo.Last.Value, Tolerance);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Reset_ClearsState()
|
||||
{
|
||||
var pgo = new Pgo(DefaultPeriod);
|
||||
for (int i = 0; i < 20; i++)
|
||||
{
|
||||
double c = 100.0 + i;
|
||||
pgo.Update(new TBar(DateTime.UtcNow, c - 1, c + 2, c - 2, c, 100));
|
||||
}
|
||||
Assert.True(pgo.IsHot);
|
||||
|
||||
pgo.Reset();
|
||||
Assert.False(pgo.IsHot);
|
||||
Assert.Equal(default, pgo.Last);
|
||||
}
|
||||
|
||||
// ───── D) Warmup / convergence ─────
|
||||
|
||||
[Fact]
|
||||
public void IsHot_FlipsWhenBufferFull()
|
||||
{
|
||||
var pgo = new Pgo(period: 5);
|
||||
for (int i = 0; i < 4; i++)
|
||||
{
|
||||
double c = 100.0 + i;
|
||||
pgo.Update(new TBar(DateTime.UtcNow, c - 1, c + 2, c - 2, c, 100));
|
||||
Assert.False(pgo.IsHot);
|
||||
}
|
||||
pgo.Update(new TBar(DateTime.UtcNow, 103, 106, 102, 104, 100));
|
||||
Assert.True(pgo.IsHot);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void WarmupPeriod_MatchesPeriod()
|
||||
{
|
||||
var pgo = new Pgo(period: 20);
|
||||
Assert.Equal(20, pgo.WarmupPeriod);
|
||||
}
|
||||
|
||||
// ───── E) Robustness ─────
|
||||
|
||||
[Fact]
|
||||
public void Update_NaN_UsesLastValid()
|
||||
{
|
||||
var pgo = new Pgo(period: 5);
|
||||
for (int i = 0; i < 6; i++)
|
||||
{
|
||||
double c = 100.0 + i;
|
||||
pgo.Update(new TBar(DateTime.UtcNow, c - 1, c + 2, c - 2, c, 100));
|
||||
}
|
||||
|
||||
pgo.Update(new TBar(DateTime.UtcNow, double.NaN, double.NaN, double.NaN, double.NaN, 100));
|
||||
Assert.True(double.IsFinite(pgo.Last.Value));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Update_Infinity_UsesLastValid()
|
||||
{
|
||||
var pgo = new Pgo(period: 5);
|
||||
for (int i = 0; i < 6; i++)
|
||||
{
|
||||
double c = 100.0 + i;
|
||||
pgo.Update(new TBar(DateTime.UtcNow, c - 1, c + 2, c - 2, c, 100));
|
||||
}
|
||||
|
||||
pgo.Update(new TBar(DateTime.UtcNow, double.PositiveInfinity, double.PositiveInfinity,
|
||||
double.PositiveInfinity, double.PositiveInfinity, 100));
|
||||
Assert.True(double.IsFinite(pgo.Last.Value));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Update_BatchNaN_Safe()
|
||||
{
|
||||
var pgo = new Pgo(period: 5);
|
||||
for (int i = 0; i < 3; i++)
|
||||
{
|
||||
pgo.Update(new TBar(DateTime.UtcNow, double.NaN, double.NaN, double.NaN, double.NaN, 0));
|
||||
}
|
||||
Assert.True(double.IsFinite(pgo.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));
|
||||
|
||||
// 1. Streaming (TBar)
|
||||
var streaming = new Pgo(period);
|
||||
var streamResults = new double[bars.Count];
|
||||
for (int i = 0; i < bars.Count; i++)
|
||||
{
|
||||
streamResults[i] = streaming.Update(bars[i]).Value;
|
||||
}
|
||||
|
||||
// 2. Batch TBarSeries
|
||||
TSeries batchSeries = Pgo.Batch(bars, period);
|
||||
|
||||
// 3. Batch Span
|
||||
var spanOutput = new double[bars.Count];
|
||||
Pgo.Batch(bars.High.Values, bars.Low.Values, bars.Close.Values, spanOutput, period);
|
||||
|
||||
// Compare all modes
|
||||
for (int i = 0; i < bars.Count; i++)
|
||||
{
|
||||
Assert.Equal(streamResults[i], batchSeries.Values[i], Tolerance);
|
||||
Assert.Equal(streamResults[i], spanOutput[i], Tolerance);
|
||||
}
|
||||
}
|
||||
|
||||
// ───── G) Span API tests ─────
|
||||
|
||||
[Fact]
|
||||
public void Batch_Span_MismatchedLength_ThrowsArgumentException()
|
||||
{
|
||||
var high = new double[10];
|
||||
var low = new double[10];
|
||||
var close = new double[10];
|
||||
var output = new double[5];
|
||||
var ex = Assert.Throws<ArgumentException>(() =>
|
||||
Pgo.Batch(high.AsSpan(), low.AsSpan(), close.AsSpan(), output.AsSpan(), DefaultPeriod));
|
||||
Assert.Equal("destination", ex.ParamName);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Batch_Span_ZeroPeriod_ThrowsArgumentException()
|
||||
{
|
||||
var high = new double[10];
|
||||
var low = new double[10];
|
||||
var close = new double[10];
|
||||
var output = new double[10];
|
||||
var ex = Assert.Throws<ArgumentException>(() =>
|
||||
Pgo.Batch(high.AsSpan(), low.AsSpan(), close.AsSpan(), output.AsSpan(), 0));
|
||||
Assert.Equal("period", ex.ParamName);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Batch_Span_Empty_NoException()
|
||||
{
|
||||
double[] high = [];
|
||||
double[] low = [];
|
||||
double[] close = [];
|
||||
double[] output = [];
|
||||
var ex = Record.Exception(() =>
|
||||
Pgo.Batch(high.AsSpan(), low.AsSpan(), close.AsSpan(), output.AsSpan(), DefaultPeriod));
|
||||
Assert.Null(ex);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Batch_Span_MatchesTBarSeries()
|
||||
{
|
||||
var gbm = new GBM(startPrice: 100.0, mu: 0.02, sigma: 0.1, seed: 7);
|
||||
var bars = gbm.Fetch(200, DateTime.UtcNow.Ticks, TimeSpan.FromMinutes(1));
|
||||
int period = 10;
|
||||
|
||||
TSeries batchTs = Pgo.Batch(bars, period);
|
||||
var spanOutput = new double[bars.Count];
|
||||
Pgo.Batch(bars.High.Values, bars.Low.Values, bars.Close.Values, spanOutput, period);
|
||||
|
||||
for (int i = 0; i < bars.Count; i++)
|
||||
{
|
||||
Assert.Equal(batchTs.Values[i], spanOutput[i], Tolerance);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Batch_Span_NaN_Handled()
|
||||
{
|
||||
double[] high = [102, 104, double.NaN, 108, 110, 112, 114, 116, 118, 120];
|
||||
double[] low = [98, 100, double.NaN, 104, 106, 108, 110, 112, 114, 116];
|
||||
double[] close = [100, 102, double.NaN, 106, 108, 110, 112, 114, 116, 118];
|
||||
var output = new double[close.Length];
|
||||
var ex = Record.Exception(() =>
|
||||
Pgo.Batch(high.AsSpan(), low.AsSpan(), close.AsSpan(), output.AsSpan(), 5));
|
||||
Assert.Null(ex);
|
||||
}
|
||||
|
||||
// ───── H) Chainability ─────
|
||||
|
||||
[Fact]
|
||||
public void PubEvent_FiresOnUpdate()
|
||||
{
|
||||
var pgo = new Pgo(DefaultPeriod);
|
||||
int firedCount = 0;
|
||||
pgo.Pub += (object? _, in TValueEventArgs _) => firedCount++;
|
||||
|
||||
pgo.Update(new TBar(DateTime.UtcNow, 100, 105, 95, 102, 1000));
|
||||
Assert.Equal(1, firedCount);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void EventChaining_Works()
|
||||
{
|
||||
var pgo = new Pgo(period: 5);
|
||||
var downstream = new TSeries();
|
||||
pgo.Pub += (object? _, in TValueEventArgs e) => downstream.Add(e.Value);
|
||||
|
||||
for (int i = 0; i < 10; i++)
|
||||
{
|
||||
double c = 100.0 + i;
|
||||
pgo.Update(new TBar(DateTime.UtcNow, c - 1, c + 2, c - 2, c, 100));
|
||||
}
|
||||
|
||||
Assert.Equal(10, downstream.Count);
|
||||
}
|
||||
|
||||
// ───── Calculate ─────
|
||||
|
||||
[Fact]
|
||||
public void Calculate_ReturnsResultsAndHotIndicator()
|
||||
{
|
||||
var gbm = new GBM(startPrice: 100.0, mu: 0.02, sigma: 0.1, seed: 42);
|
||||
var bars = gbm.Fetch(50, DateTime.UtcNow.Ticks, TimeSpan.FromMinutes(1));
|
||||
|
||||
var (results, indicator) = Pgo.Calculate(bars, period: 5);
|
||||
|
||||
Assert.Equal(bars.Count, results.Count);
|
||||
Assert.True(indicator.IsHot);
|
||||
}
|
||||
|
||||
// ───── Update(TBarSeries) ─────
|
||||
|
||||
[Fact]
|
||||
public void UpdateTBarSeries_MatchesStreaming()
|
||||
{
|
||||
var gbm = new GBM(startPrice: 100.0, mu: 0.02, sigma: 0.1, seed: 42);
|
||||
var bars = gbm.Fetch(200, DateTime.UtcNow.Ticks, TimeSpan.FromMinutes(1));
|
||||
int period = 10;
|
||||
|
||||
var streaming = new Pgo(period);
|
||||
var streamResults = new double[bars.Count];
|
||||
for (int i = 0; i < bars.Count; i++)
|
||||
{
|
||||
streamResults[i] = streaming.Update(bars[i]).Value;
|
||||
}
|
||||
|
||||
var batch = new Pgo(period);
|
||||
TSeries batchResults = batch.Update(bars);
|
||||
|
||||
for (int i = 0; i < bars.Count; i++)
|
||||
{
|
||||
Assert.Equal(streamResults[i], batchResults.Values[i], Tolerance);
|
||||
}
|
||||
}
|
||||
|
||||
// ───── TValue overload ─────
|
||||
|
||||
[Fact]
|
||||
public void Update_TValue_ReturnsResult()
|
||||
{
|
||||
var pgo = new Pgo(period: 5);
|
||||
for (int i = 0; i < 10; i++)
|
||||
{
|
||||
pgo.Update(new TValue(DateTime.UtcNow, 100.0 + i));
|
||||
}
|
||||
// TValue creates synthetic bars (O=H=L=C=val). TR = |val - prevClose| > 0
|
||||
// when values change, so ATR > 0 and PGO is nonzero for rising prices.
|
||||
Assert.True(double.IsFinite(pgo.Last.Value));
|
||||
Assert.True(pgo.Last.Value > 0, "Rising TValue inputs should produce positive PGO");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,245 @@
|
||||
using Xunit.Abstractions;
|
||||
|
||||
using OoplesFinance.StockIndicators;
|
||||
using OoplesFinance.StockIndicators.Models;
|
||||
|
||||
namespace QuanTAlib.Tests;
|
||||
|
||||
public sealed class PgoValidationTests
|
||||
{
|
||||
private readonly TBarSeries _bars;
|
||||
private readonly ITestOutputHelper _output;
|
||||
|
||||
public PgoValidationTests(ITestOutputHelper output)
|
||||
{
|
||||
_output = output;
|
||||
var gbm = new GBM(startPrice: 100.0, mu: 0.02, sigma: 0.1, seed: 42);
|
||||
_bars = gbm.Fetch(500, DateTime.UtcNow.Ticks, TimeSpan.FromMinutes(1));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Validate_Streaming_Batch_Span_Agree()
|
||||
{
|
||||
int period = 14;
|
||||
|
||||
// Streaming
|
||||
var streaming = new Pgo(period);
|
||||
var streamValues = new List<double>(_bars.Count);
|
||||
for (int i = 0; i < _bars.Count; i++)
|
||||
{
|
||||
streamValues.Add(streaming.Update(_bars[i]).Value);
|
||||
}
|
||||
|
||||
// Batch (TBarSeries)
|
||||
TSeries batchSeries = Pgo.Batch(_bars, period);
|
||||
|
||||
// Span
|
||||
var spanOutput = new double[_bars.Count];
|
||||
Pgo.Batch(_bars.High.Values, _bars.Low.Values, _bars.Close.Values, spanOutput, period);
|
||||
|
||||
// Batch vs span should match exactly (same code path).
|
||||
// Streaming vs batch should agree closely.
|
||||
for (int i = 0; i < _bars.Count; i++)
|
||||
{
|
||||
Assert.Equal(batchSeries[i].Value, spanOutput[i], 12); // batch=span (same path)
|
||||
Assert.Equal(batchSeries[i].Value, streamValues[i], 10); // streaming matches batch
|
||||
}
|
||||
|
||||
_output.WriteLine("PGO validation: streaming, batch, and span outputs agree within tolerance.");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Validate_KnownValues_ConstantPrice()
|
||||
{
|
||||
// Constant OHLC bars: close=SMA, TR=0, ATR=0 → PGO = 0
|
||||
int period = 5;
|
||||
var pgo = new Pgo(period);
|
||||
|
||||
for (int i = 0; i < 20; i++)
|
||||
{
|
||||
pgo.Update(new TBar(DateTime.UtcNow, 50, 50, 50, 50, 100));
|
||||
}
|
||||
|
||||
Assert.Equal(0.0, pgo.Last.Value, 10);
|
||||
_output.WriteLine("PGO known-values: constant bars produce PGO=0.");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Validate_KnownValues_PriceAboveSma()
|
||||
{
|
||||
// When close > SMA and ATR > 0, PGO should be positive
|
||||
int period = 5;
|
||||
var pgo = new Pgo(period);
|
||||
|
||||
// Feed gradually rising prices
|
||||
for (int i = 0; i < 10; i++)
|
||||
{
|
||||
double c = 100.0 + i * 2;
|
||||
pgo.Update(new TBar(DateTime.UtcNow, c - 1, c + 3, c - 3, c, 100));
|
||||
}
|
||||
|
||||
Assert.True(pgo.Last.Value > 0, $"Expected positive PGO for rising prices, got {pgo.Last.Value}");
|
||||
_output.WriteLine($"PGO known-values: rising prices produce positive PGO = {pgo.Last.Value:F6}.");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Validate_KnownValues_PriceBelowSma()
|
||||
{
|
||||
// When close < SMA and ATR > 0, PGO should be negative
|
||||
int period = 5;
|
||||
var pgo = new Pgo(period);
|
||||
|
||||
// Feed rising prices first, then drop
|
||||
for (int i = 0; i < 7; i++)
|
||||
{
|
||||
double c = 100.0 + i * 5;
|
||||
pgo.Update(new TBar(DateTime.UtcNow, c - 1, c + 3, c - 3, c, 100));
|
||||
}
|
||||
// Now drop sharply
|
||||
for (int i = 0; i < 5; i++)
|
||||
{
|
||||
double c = 80.0 - i * 5;
|
||||
pgo.Update(new TBar(DateTime.UtcNow, c - 1, c + 3, c - 3, c, 100));
|
||||
}
|
||||
|
||||
Assert.True(pgo.Last.Value < 0, $"Expected negative PGO for dropped prices, got {pgo.Last.Value}");
|
||||
_output.WriteLine($"PGO known-values: dropped prices produce negative PGO = {pgo.Last.Value:F6}.");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Validate_MultiPeriod_Consistency()
|
||||
{
|
||||
// Different periods should produce different results
|
||||
int[] periods = [5, 14, 50];
|
||||
var results = new List<TSeries>();
|
||||
|
||||
foreach (int period in periods)
|
||||
{
|
||||
results.Add(Pgo.Batch(_bars, period));
|
||||
}
|
||||
|
||||
// After all warmups, values should differ for different periods
|
||||
int checkIdx = 100;
|
||||
for (int i = 0; i < results.Count - 1; i++)
|
||||
{
|
||||
Assert.NotEqual(results[i][checkIdx].Value, results[i + 1][checkIdx].Value);
|
||||
}
|
||||
|
||||
_output.WriteLine("PGO multi-period: different periods produce different results.");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Validate_Component_SmaAtr_Identity()
|
||||
{
|
||||
// Manually verify PGO = (close - SMA) / ATR
|
||||
// by computing SMA and ATR independently and comparing
|
||||
int period = 10;
|
||||
var pgo = new Pgo(period);
|
||||
|
||||
// Manual SMA/ATR tracking
|
||||
var smaBuffer = new RingBuffer(period);
|
||||
double smaSum = 0.0;
|
||||
double ema = 0.0;
|
||||
double e = 1.0;
|
||||
double alpha = 1.0 / period;
|
||||
double decay = 1.0 - alpha;
|
||||
double atr = 0.0;
|
||||
bool warmup = true;
|
||||
double prevClose = 0.0;
|
||||
bool hasPrev = false;
|
||||
|
||||
int validCount = 0;
|
||||
|
||||
for (int i = 0; i < _bars.Count; i++)
|
||||
{
|
||||
var bar = _bars[i];
|
||||
double close = bar.Close;
|
||||
double pc = hasPrev ? prevClose : close;
|
||||
|
||||
// SMA
|
||||
if (smaBuffer.Count == smaBuffer.Capacity)
|
||||
{
|
||||
smaSum -= smaBuffer.Oldest;
|
||||
}
|
||||
smaSum += close;
|
||||
smaBuffer.Add(close);
|
||||
double sma = smaSum / smaBuffer.Count;
|
||||
|
||||
// TR
|
||||
double tr = Math.Max(bar.High - bar.Low,
|
||||
Math.Max(Math.Abs(bar.High - pc), Math.Abs(bar.Low - pc)));
|
||||
|
||||
// EMA of TR
|
||||
ema = Math.FusedMultiplyAdd(alpha, tr - ema, ema);
|
||||
if (warmup)
|
||||
{
|
||||
e *= decay;
|
||||
double c = 1.0 / (1.0 - e);
|
||||
atr = c * ema;
|
||||
warmup = e > 1e-10;
|
||||
}
|
||||
else
|
||||
{
|
||||
atr = ema;
|
||||
}
|
||||
|
||||
prevClose = close;
|
||||
hasPrev = true;
|
||||
|
||||
// PGO
|
||||
var result = pgo.Update(bar);
|
||||
double expectedPgo = atr > 0 ? (close - sma) / atr : 0.0;
|
||||
|
||||
if (smaBuffer.IsFull)
|
||||
{
|
||||
Assert.Equal(expectedPgo, result.Value, 10);
|
||||
validCount++;
|
||||
}
|
||||
}
|
||||
|
||||
Assert.True(validCount > 0, "No valid comparison points");
|
||||
_output.WriteLine($"PGO component identity: validated {validCount} points.");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Validate_Determinism()
|
||||
{
|
||||
// Run twice with same data — results must be identical
|
||||
int period = 14;
|
||||
var results1 = new double[_bars.Count];
|
||||
var results2 = new double[_bars.Count];
|
||||
|
||||
var pgo1 = new Pgo(period);
|
||||
var pgo2 = new Pgo(period);
|
||||
|
||||
for (int i = 0; i < _bars.Count; i++)
|
||||
{
|
||||
results1[i] = pgo1.Update(_bars[i]).Value;
|
||||
results2[i] = pgo2.Update(_bars[i]).Value;
|
||||
}
|
||||
|
||||
for (int i = 0; i < _bars.Count; i++)
|
||||
{
|
||||
Assert.Equal(results1[i], results2[i], 15);
|
||||
}
|
||||
|
||||
_output.WriteLine("PGO determinism: two runs produce identical results.");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Pgo_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).CalculatePrettyGoodOscillator();
|
||||
var values = result.CustomValuesList;
|
||||
int finiteCount = values.Count(v => double.IsFinite(v));
|
||||
Assert.True(finiteCount > 100, $"Expected >100 finite values, got {finiteCount}");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user