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,251 @@
|
||||
namespace QuanTAlib.Tests;
|
||||
|
||||
public class SlopeTests
|
||||
{
|
||||
[Fact]
|
||||
public void Properties_Accessible()
|
||||
{
|
||||
var slope = new Slope();
|
||||
Assert.Equal(0, slope.Last.Value);
|
||||
Assert.False(slope.IsHot);
|
||||
Assert.Contains("Slope", slope.Name, StringComparison.Ordinal);
|
||||
Assert.Equal(2, slope.WarmupPeriod);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Calc_IsNew_False_UpdatesValue()
|
||||
{
|
||||
var slope = new Slope();
|
||||
slope.Update(new TValue(DateTime.UtcNow, 10));
|
||||
slope.Update(new TValue(DateTime.UtcNow, 20));
|
||||
|
||||
double valueBefore = slope.Last.Value;
|
||||
|
||||
// Update with isNew=false should change the result
|
||||
slope.Update(new TValue(DateTime.UtcNow, 100), isNew: false);
|
||||
double valueAfter = slope.Last.Value;
|
||||
|
||||
Assert.NotEqual(valueBefore, valueAfter);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void NaN_Input_UsesLastValidValue()
|
||||
{
|
||||
var slope = new Slope();
|
||||
slope.Update(new TValue(DateTime.UtcNow, 10));
|
||||
slope.Update(new TValue(DateTime.UtcNow, 20));
|
||||
|
||||
var result = slope.Update(new TValue(DateTime.UtcNow, double.NaN));
|
||||
|
||||
Assert.True(double.IsFinite(result.Value));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Infinity_Input_UsesLastValidValue()
|
||||
{
|
||||
var slope = new Slope();
|
||||
slope.Update(new TValue(DateTime.UtcNow, 10));
|
||||
slope.Update(new TValue(DateTime.UtcNow, 20));
|
||||
|
||||
var resultPosInf = slope.Update(new TValue(DateTime.UtcNow, double.PositiveInfinity));
|
||||
Assert.True(double.IsFinite(resultPosInf.Value));
|
||||
|
||||
var resultNegInf = slope.Update(new TValue(DateTime.UtcNow, double.NegativeInfinity));
|
||||
Assert.True(double.IsFinite(resultNegInf.Value));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void IterativeCorrections_RestoreToOriginalState()
|
||||
{
|
||||
var slope = new Slope();
|
||||
var gbm = new GBM(startPrice: 100.0, mu: 0.02, sigma: 0.1, seed: 42);
|
||||
|
||||
// Feed 10 new values
|
||||
TValue tenthInput = default;
|
||||
for (int i = 0; i < 10; i++)
|
||||
{
|
||||
var bar = gbm.Next(isNew: true);
|
||||
tenthInput = new TValue(bar.Time, bar.Close);
|
||||
slope.Update(tenthInput, isNew: true);
|
||||
}
|
||||
|
||||
// Remember state after 10 values
|
||||
double stateAfterTen = slope.Last.Value;
|
||||
|
||||
// Generate 9 corrections with isNew=false (different values)
|
||||
for (int i = 0; i < 9; i++)
|
||||
{
|
||||
var bar = gbm.Next(isNew: false);
|
||||
slope.Update(new TValue(bar.Time, bar.Close), isNew: false);
|
||||
}
|
||||
|
||||
// Feed the remembered 10th input again with isNew=false
|
||||
TValue finalResult = slope.Update(tenthInput, isNew: false);
|
||||
|
||||
// State should match the original state after 10 values
|
||||
Assert.Equal(stateAfterTen, finalResult.Value, 1e-9);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SpanBatch_ValidatesInput()
|
||||
{
|
||||
double[] source = [1, 2, 3, 4, 5];
|
||||
double[] wrongSizeOutput = new double[3];
|
||||
|
||||
// Output must be same length as source
|
||||
Assert.Throws<ArgumentException>(() =>
|
||||
Slope.Batch(source.AsSpan(), wrongSizeOutput.AsSpan()));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void AllModes_ProduceSameResult()
|
||||
{
|
||||
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 (static span)
|
||||
var tValues = series.Values.ToArray();
|
||||
var batchOutput = new double[tValues.Length];
|
||||
Slope.Batch(tValues, batchOutput);
|
||||
double expected = batchOutput[^1];
|
||||
|
||||
// 2. Streaming Mode
|
||||
var streamingInd = new Slope();
|
||||
for (int i = 0; i < series.Count; i++)
|
||||
{
|
||||
streamingInd.Update(series[i]);
|
||||
}
|
||||
double streamingResult = streamingInd.Last.Value;
|
||||
|
||||
// 3. TSeries Batch Mode
|
||||
var batchSeriesResult = Slope.Batch(series);
|
||||
double tseriesResult = batchSeriesResult.Last.Value;
|
||||
|
||||
Assert.Equal(expected, streamingResult, precision: 9);
|
||||
Assert.Equal(expected, tseriesResult, precision: 9);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Calculation_KnownValues()
|
||||
{
|
||||
// slope[i] = source[i] - source[i-1]
|
||||
// Data: 10, 20, 25, 30, 28
|
||||
// Slopes: 0, 10, 5, 5, -2
|
||||
|
||||
double[] data = [10, 20, 25, 30, 28];
|
||||
double[] expected = [0, 10, 5, 5, -2];
|
||||
|
||||
var slope = new Slope();
|
||||
for (int i = 0; i < data.Length; i++)
|
||||
{
|
||||
var result = slope.Update(new TValue(DateTime.UtcNow, data[i]));
|
||||
Assert.Equal(expected[i], result.Value, precision: 9);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void IsHot_BecomesTrueAfterWarmup()
|
||||
{
|
||||
var slope = new Slope();
|
||||
|
||||
Assert.False(slope.IsHot);
|
||||
slope.Update(new TValue(DateTime.UtcNow, 10));
|
||||
Assert.False(slope.IsHot);
|
||||
slope.Update(new TValue(DateTime.UtcNow, 20));
|
||||
Assert.True(slope.IsHot);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Reset_ClearsState()
|
||||
{
|
||||
var slope = new Slope();
|
||||
for (int i = 0; i < 10; i++)
|
||||
{
|
||||
slope.Update(new TValue(DateTime.UtcNow, i));
|
||||
}
|
||||
Assert.True(slope.IsHot);
|
||||
|
||||
slope.Reset();
|
||||
Assert.False(slope.IsHot);
|
||||
Assert.Equal(0, slope.Last.Value);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Batch_Matches_Iterative()
|
||||
{
|
||||
int count = 1000;
|
||||
var data = new double[count];
|
||||
var gbm = new GBM(startPrice: 100, mu: 0.05, sigma: 0.2, seed: 123);
|
||||
|
||||
for (int i = 0; i < count; i++)
|
||||
{
|
||||
data[i] = gbm.Next().Close;
|
||||
}
|
||||
|
||||
// Iterative
|
||||
var slope = new Slope();
|
||||
var iterativeResults = new double[count];
|
||||
for (int i = 0; i < count; i++)
|
||||
{
|
||||
slope.Update(new TValue(DateTime.UtcNow, data[i]));
|
||||
iterativeResults[i] = slope.Last.Value;
|
||||
}
|
||||
|
||||
// Batch
|
||||
var batchResults = new double[count];
|
||||
Slope.Batch(data, batchResults);
|
||||
|
||||
// Compare
|
||||
for (int i = 0; i < count; i++)
|
||||
{
|
||||
Assert.Equal(iterativeResults[i], batchResults[i], precision: 9);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Update_TSeries_Matches_Iterative()
|
||||
{
|
||||
int count = 1000;
|
||||
var data = new TSeries();
|
||||
var gbm = new GBM(startPrice: 100, mu: 0.05, sigma: 0.2, seed: 123);
|
||||
|
||||
for (int i = 0; i < count; i++)
|
||||
{
|
||||
var bar = gbm.Next();
|
||||
data.Add(new TValue(bar.Time, bar.Close));
|
||||
}
|
||||
|
||||
// Iterative
|
||||
var slope = new Slope();
|
||||
var iterativeResults = new double[count];
|
||||
for (int i = 0; i < count; i++)
|
||||
{
|
||||
slope.Update(data[i]);
|
||||
iterativeResults[i] = slope.Last.Value;
|
||||
}
|
||||
|
||||
// TSeries Batch
|
||||
var slopeBatch = new Slope();
|
||||
var batchSeries = slopeBatch.Update(data);
|
||||
|
||||
// Compare
|
||||
for (int i = 0; i < count; i++)
|
||||
{
|
||||
Assert.Equal(iterativeResults[i], batchSeries[i].Value, precision: 9);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void EventSubscription_Works()
|
||||
{
|
||||
var source = new TSeries();
|
||||
var slope = new Slope(source);
|
||||
|
||||
source.Add(new TValue(DateTime.UtcNow, 10));
|
||||
source.Add(new TValue(DateTime.UtcNow, 20));
|
||||
|
||||
Assert.True(slope.IsHot);
|
||||
Assert.Equal(10, slope.Last.Value);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,173 @@
|
||||
using Skender.Stock.Indicators;
|
||||
|
||||
namespace QuanTAlib.Tests;
|
||||
|
||||
/// <summary>
|
||||
/// Validation tests for Slope using synthetic data with known mathematical results.
|
||||
/// </summary>
|
||||
public class SlopeValidationTests
|
||||
{
|
||||
[Fact]
|
||||
public void LinearSequence_ProducesConstantSlope()
|
||||
{
|
||||
// Linear sequence: 0, 2, 4, 6, 8, 10 (slope = 2)
|
||||
double[] data = [0, 2, 4, 6, 8, 10];
|
||||
double[] expected = [0, 2, 2, 2, 2, 2]; // First is 0 (no history), rest are 2
|
||||
|
||||
var slope = new Slope();
|
||||
for (int i = 0; i < data.Length; i++)
|
||||
{
|
||||
var result = slope.Update(new TValue(DateTime.UtcNow, data[i]));
|
||||
Assert.Equal(expected[i], result.Value, precision: 9);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ConstantSequence_ProducesZeroSlope()
|
||||
{
|
||||
// Constant sequence: 5, 5, 5, 5, 5 (slope = 0)
|
||||
double[] data = [5, 5, 5, 5, 5];
|
||||
double[] expected = [0, 0, 0, 0, 0];
|
||||
|
||||
var slope = new Slope();
|
||||
for (int i = 0; i < data.Length; i++)
|
||||
{
|
||||
var result = slope.Update(new TValue(DateTime.UtcNow, data[i]));
|
||||
Assert.Equal(expected[i], result.Value, precision: 9);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void DecreasingSequence_ProducesNegativeSlope()
|
||||
{
|
||||
// Decreasing sequence: 10, 7, 4, 1, -2 (slope = -3)
|
||||
double[] data = [10, 7, 4, 1, -2];
|
||||
double[] expected = [0, -3, -3, -3, -3];
|
||||
|
||||
var slope = new Slope();
|
||||
for (int i = 0; i < data.Length; i++)
|
||||
{
|
||||
var result = slope.Update(new TValue(DateTime.UtcNow, data[i]));
|
||||
Assert.Equal(expected[i], result.Value, precision: 9);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void QuadraticSequence_ProducesLinearSlope()
|
||||
{
|
||||
// Quadratic sequence: 0, 1, 4, 9, 16, 25 (x^2)
|
||||
// Slope: n^2 - (n-1)^2 = 2n - 1 → 1, 3, 5, 7, 9
|
||||
double[] data = [0, 1, 4, 9, 16, 25];
|
||||
double[] expected = [0, 1, 3, 5, 7, 9];
|
||||
|
||||
var slope = new Slope();
|
||||
for (int i = 0; i < data.Length; i++)
|
||||
{
|
||||
var result = slope.Update(new TValue(DateTime.UtcNow, data[i]));
|
||||
Assert.Equal(expected[i], result.Value, precision: 9);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void AlternatingSequence_ProducesAlternatingSlope()
|
||||
{
|
||||
// Alternating: 0, 10, 0, 10, 0
|
||||
double[] data = [0, 10, 0, 10, 0];
|
||||
double[] expected = [0, 10, -10, 10, -10];
|
||||
|
||||
var slope = new Slope();
|
||||
for (int i = 0; i < data.Length; i++)
|
||||
{
|
||||
var result = slope.Update(new TValue(DateTime.UtcNow, data[i]));
|
||||
Assert.Equal(expected[i], result.Value, precision: 9);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void FibonacciSequence_ProducesCorrectSlope()
|
||||
{
|
||||
// Fibonacci: 1, 1, 2, 3, 5, 8, 13
|
||||
// Slope: 0, 1, 1, 2, 3, 5
|
||||
double[] data = [1, 1, 2, 3, 5, 8, 13];
|
||||
double[] expected = [0, 0, 1, 1, 2, 3, 5];
|
||||
|
||||
var slope = new Slope();
|
||||
for (int i = 0; i < data.Length; i++)
|
||||
{
|
||||
var result = slope.Update(new TValue(DateTime.UtcNow, data[i]));
|
||||
Assert.Equal(expected[i], result.Value, precision: 9);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void BatchCalculation_MatchesSyntheticData()
|
||||
{
|
||||
double[] data = [0, 2, 4, 6, 8, 10];
|
||||
double[] expected = [0, 2, 2, 2, 2, 2];
|
||||
double[] output = new double[data.Length];
|
||||
|
||||
Slope.Batch(data, output);
|
||||
|
||||
for (int i = 0; i < data.Length; i++)
|
||||
{
|
||||
Assert.Equal(expected[i], output[i], precision: 9);
|
||||
}
|
||||
}
|
||||
|
||||
// === Skender Cross-Validation ===
|
||||
|
||||
/// <summary>
|
||||
/// Structural validation against Skender <c>GetSlope</c>.
|
||||
/// Skender Slope computes linear regression slope over a lookback window,
|
||||
/// while QuanTAlib Slope computes simple first difference (current - previous).
|
||||
/// Different formulas mean numeric equality is not expected.
|
||||
/// Both must produce finite output and agree on trend direction for simple linear data.
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public void Validate_Skender_Slope_Structural()
|
||||
{
|
||||
using var data = new ValidationTestData();
|
||||
const int period = 14;
|
||||
|
||||
// QuanTAlib Slope (streaming, simple difference)
|
||||
var slope = new Slope();
|
||||
var qResults = new List<double>();
|
||||
foreach (var tv in data.Data)
|
||||
{
|
||||
qResults.Add(slope.Update(tv).Value);
|
||||
}
|
||||
|
||||
// Skender Slope (linear regression slope)
|
||||
var sResult = data.SkenderQuotes.GetSlope(period).ToList();
|
||||
|
||||
// Structural: both produce finite output after warmup
|
||||
Assert.True(double.IsFinite(slope.Last.Value), "QuanTAlib Slope last must be finite");
|
||||
|
||||
int finiteCount = sResult.Count(r => r.Slope is not null && double.IsFinite(r.Slope.Value));
|
||||
Assert.True(finiteCount > 100, $"Skender Slope should produce >100 finite values, got {finiteCount}");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void LargeLinearSequence_ProducesConstantSlope()
|
||||
{
|
||||
// Generate 1000 points with slope = 0.5
|
||||
int count = 1000;
|
||||
double[] data = new double[count];
|
||||
for (int i = 0; i < count; i++)
|
||||
{
|
||||
data[i] = 100.0 + i * 0.5;
|
||||
}
|
||||
|
||||
var slope = new Slope();
|
||||
// First element - no previous value, slope = 0
|
||||
slope.Update(new TValue(DateTime.UtcNow, data[0]));
|
||||
Assert.Equal(0.0, slope.Last.Value, precision: 9);
|
||||
|
||||
// Rest should have constant slope of 0.5
|
||||
for (int i = 1; i < count; i++)
|
||||
{
|
||||
slope.Update(new TValue(DateTime.UtcNow, data[i]));
|
||||
Assert.Equal(0.5, slope.Last.Value, precision: 9);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user