mirror of
https://github.com/mihakralj/QuanTAlib.git
synced 2026-08-25 05:48:06 +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,168 @@
|
||||
using TradingPlatform.BusinessLayer;
|
||||
|
||||
namespace QuanTAlib.Tests;
|
||||
|
||||
public class RmaIndicatorTests
|
||||
{
|
||||
[Fact]
|
||||
public void RmaIndicator_Constructor_SetsDefaults()
|
||||
{
|
||||
var indicator = new RmaIndicator();
|
||||
|
||||
Assert.Equal(14, indicator.Period);
|
||||
Assert.Equal(SourceType.Close, indicator.Source);
|
||||
Assert.True(indicator.ShowColdValues);
|
||||
Assert.Equal("RMA - Running Moving Average", indicator.Name);
|
||||
Assert.False(indicator.SeparateWindow);
|
||||
Assert.True(indicator.OnBackGround);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void RmaIndicator_MinHistoryDepths_EqualsPeriod()
|
||||
{
|
||||
var indicator = new RmaIndicator { Period = 20 };
|
||||
|
||||
Assert.Equal(0, RmaIndicator.MinHistoryDepths);
|
||||
Assert.Equal(0, ((IWatchlistIndicator)indicator).MinHistoryDepths);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void RmaIndicator_ShortName_IncludesPeriodAndSource()
|
||||
{
|
||||
var indicator = new RmaIndicator { Period = 15 };
|
||||
|
||||
Assert.Contains("RMA", indicator.ShortName, StringComparison.Ordinal);
|
||||
Assert.Contains("15", indicator.ShortName, StringComparison.Ordinal);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void RmaIndicator_Initialize_CreatesInternalRma()
|
||||
{
|
||||
var indicator = new RmaIndicator { Period = 10 };
|
||||
|
||||
// Initialize should not throw
|
||||
indicator.Initialize();
|
||||
|
||||
// After init, line series should exist
|
||||
Assert.Single(indicator.LinesSeries);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void RmaIndicator_ProcessUpdate_HistoricalBar_ComputesValue()
|
||||
{
|
||||
var indicator = new RmaIndicator { 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 RmaIndicator_ProcessUpdate_NewBar_ComputesValue()
|
||||
{
|
||||
var indicator = new RmaIndicator { Period = 3 };
|
||||
indicator.Initialize();
|
||||
|
||||
// Add historical data
|
||||
var now = DateTime.UtcNow;
|
||||
indicator.HistoricalData.AddBar(now, 100, 105, 95, 102);
|
||||
indicator.HistoricalData.AddBar(now.AddMinutes(1), 102, 108, 100, 106);
|
||||
|
||||
// Process first update
|
||||
indicator.ProcessUpdate(new UpdateArgs(UpdateReason.HistoricalBar));
|
||||
indicator.ProcessUpdate(new UpdateArgs(UpdateReason.NewBar));
|
||||
|
||||
// Line series should have values
|
||||
Assert.Equal(2, indicator.LinesSeries[0].Count);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void RmaIndicator_ProcessUpdate_NewTick_ProcessesWithoutError()
|
||||
{
|
||||
var indicator = new RmaIndicator { Period = 3 };
|
||||
indicator.Initialize();
|
||||
|
||||
// Add historical data
|
||||
var now = DateTime.UtcNow;
|
||||
indicator.HistoricalData.AddBar(now, 100, 105, 95, 102);
|
||||
|
||||
// Process historical bar first
|
||||
indicator.ProcessUpdate(new UpdateArgs(UpdateReason.HistoricalBar));
|
||||
double firstValue = indicator.LinesSeries[0].GetValue(0);
|
||||
|
||||
// Update with new tick (same bar data - simulates intrabar update)
|
||||
indicator.ProcessUpdate(new UpdateArgs(UpdateReason.NewTick));
|
||||
double secondValue = indicator.LinesSeries[0].GetValue(0);
|
||||
|
||||
// Both values should be finite
|
||||
Assert.True(double.IsFinite(firstValue));
|
||||
Assert.True(double.IsFinite(secondValue));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void RmaIndicator_MultipleUpdates_ProducesCorrectRmaSequence()
|
||||
{
|
||||
var indicator = new RmaIndicator { Period = 3 };
|
||||
indicator.Initialize();
|
||||
|
||||
var now = DateTime.UtcNow;
|
||||
double[] closes = { 100, 102, 104, 103, 105, 107, 106 };
|
||||
|
||||
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)));
|
||||
}
|
||||
|
||||
// RMA should be smoothing the values
|
||||
// Last RMA value should be between first and last close
|
||||
double lastRma = indicator.LinesSeries[0].GetValue(0);
|
||||
Assert.True(lastRma >= 100 && lastRma <= 110);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void RmaIndicator_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 RmaIndicator { 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 RmaIndicator_Period_CanBeChanged()
|
||||
{
|
||||
var indicator = new RmaIndicator { Period = 5 };
|
||||
Assert.Equal(5, indicator.Period);
|
||||
|
||||
indicator.Period = 20;
|
||||
Assert.Equal(20, indicator.Period);
|
||||
Assert.Equal(0, RmaIndicator.MinHistoryDepths);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,214 @@
|
||||
namespace QuanTAlib.Tests;
|
||||
|
||||
public class RmaTests
|
||||
{
|
||||
[Fact]
|
||||
public void Rma_Constructor_Period_ValidatesInput()
|
||||
{
|
||||
Assert.Throws<ArgumentException>(() => new Rma(0));
|
||||
Assert.Throws<ArgumentException>(() => new Rma(-1));
|
||||
|
||||
var rma = new Rma(10);
|
||||
Assert.NotNull(rma);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Rma_Calc_ReturnsValue()
|
||||
{
|
||||
var rma = new Rma(10);
|
||||
|
||||
Assert.Equal(0, rma.Last.Value);
|
||||
|
||||
TValue result = rma.Update(new TValue(DateTime.UtcNow, 100));
|
||||
|
||||
Assert.True(result.Value > 0);
|
||||
Assert.Equal(result.Value, rma.Last.Value);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Rma_Calc_IsNew_AcceptsParameter()
|
||||
{
|
||||
var rma = new Rma(10);
|
||||
|
||||
rma.Update(new TValue(DateTime.UtcNow, 100), isNew: true);
|
||||
double value1 = rma.Last.Value;
|
||||
|
||||
rma.Update(new TValue(DateTime.UtcNow, 105), isNew: true);
|
||||
double value2 = rma.Last.Value;
|
||||
|
||||
// Values should change with new bars
|
||||
Assert.NotEqual(value1, value2);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Rma_Calc_IsNew_False_UpdatesValue()
|
||||
{
|
||||
var rma = new Rma(10);
|
||||
|
||||
rma.Update(new TValue(DateTime.UtcNow, 100));
|
||||
rma.Update(new TValue(DateTime.UtcNow, 110), isNew: true);
|
||||
double beforeUpdate = rma.Last.Value;
|
||||
|
||||
rma.Update(new TValue(DateTime.UtcNow, 120), isNew: false);
|
||||
double afterUpdate = rma.Last.Value;
|
||||
|
||||
// Update should change the value
|
||||
Assert.NotEqual(beforeUpdate, afterUpdate);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Rma_Reset_ClearsState()
|
||||
{
|
||||
var rma = new Rma(10);
|
||||
|
||||
rma.Update(new TValue(DateTime.UtcNow, 100));
|
||||
rma.Update(new TValue(DateTime.UtcNow, 105));
|
||||
double valueBefore = rma.Last.Value;
|
||||
|
||||
rma.Reset();
|
||||
|
||||
Assert.Equal(0, rma.Last.Value);
|
||||
|
||||
// After reset, should accept new values
|
||||
rma.Update(new TValue(DateTime.UtcNow, 50));
|
||||
Assert.NotEqual(0, rma.Last.Value);
|
||||
Assert.NotEqual(valueBefore, rma.Last.Value);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Rma_IsHot_BecomesTrueAt95PercentCoverage()
|
||||
{
|
||||
var rma = new Rma(10);
|
||||
|
||||
// Initially IsHot should be false
|
||||
Assert.False(rma.IsHot);
|
||||
|
||||
// IsHot triggers at 95% coverage (E <= 0.05)
|
||||
// E = (1 - alpha)^N where alpha = 1 / period
|
||||
// For period 10: alpha = 0.1, (1-alpha) = 0.9
|
||||
// N = ln(0.05) / ln(0.9) ≈ 28.4, so ~29 bars
|
||||
|
||||
int steps = 0;
|
||||
while (!rma.IsHot && steps < 1000)
|
||||
{
|
||||
rma.Update(new TValue(DateTime.UtcNow, 100));
|
||||
steps++;
|
||||
}
|
||||
|
||||
Assert.True(rma.IsHot);
|
||||
Assert.True(steps > 0);
|
||||
// For period 10, should become hot around 29 bars
|
||||
Assert.InRange(steps, 28, 30);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Rma_EquivalentToEmaWithAlpha()
|
||||
{
|
||||
const int period = 10;
|
||||
double alpha = 1.0 / period;
|
||||
|
||||
var rma = new Rma(period);
|
||||
var ema = new Ema(alpha);
|
||||
|
||||
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);
|
||||
var rmaVal = rma.Update(new TValue(bar.Time, bar.Close));
|
||||
var emaVal = ema.Update(new TValue(bar.Time, bar.Close));
|
||||
|
||||
Assert.Equal(emaVal.Value, rmaVal.Value, 1e-10);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Rma_BatchCalc_MatchesIterativeCalc()
|
||||
{
|
||||
var rmaIterative = new Rma(10);
|
||||
var rmaBatch = new Rma(10);
|
||||
var gbm = new GBM(startPrice: 100.0, mu: 0.02, sigma: 0.1);
|
||||
|
||||
// Generate data
|
||||
var series = new TSeries();
|
||||
var inputList = new List<TValue>();
|
||||
for (int i = 0; i < 100; i++)
|
||||
{
|
||||
var bar = gbm.Next(isNew: true);
|
||||
series.Add(bar.Time, bar.Close);
|
||||
inputList.Add(new TValue(bar.Time, bar.Close));
|
||||
}
|
||||
|
||||
// Calculate iteratively
|
||||
var iterativeResults = new TSeries();
|
||||
foreach (var item in inputList)
|
||||
{
|
||||
iterativeResults.Add(rmaIterative.Update(item));
|
||||
}
|
||||
|
||||
// Calculate batch
|
||||
var batchResults = rmaBatch.Update(series);
|
||||
|
||||
// Compare
|
||||
Assert.Equal(series.Count, iterativeResults.Count);
|
||||
Assert.Equal(iterativeResults.Count, batchResults.Count);
|
||||
for (int i = 0; i < inputList.Count; i++)
|
||||
{
|
||||
Assert.Equal(iterativeResults[i].Value, batchResults[i].Value, 1e-10);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Rma_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 = Rma.Batch(series, 10);
|
||||
|
||||
// Calculate with Span API
|
||||
Rma.Batch(source.AsSpan(), output.AsSpan(), 10);
|
||||
|
||||
// Compare results
|
||||
for (int i = 0; i < 100; i++)
|
||||
{
|
||||
Assert.Equal(tseriesResult[i].Value, output[i], 1e-9);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Rma_NaN_Input_UsesLastValidValue()
|
||||
{
|
||||
var rma = new Rma(10);
|
||||
|
||||
// Feed some valid values
|
||||
rma.Update(new TValue(DateTime.UtcNow, 100));
|
||||
rma.Update(new TValue(DateTime.UtcNow, 110));
|
||||
|
||||
// Feed NaN - should use last valid value (110)
|
||||
var resultAfterNaN = rma.Update(new TValue(DateTime.UtcNow, double.NaN));
|
||||
|
||||
// Result should be finite (not NaN)
|
||||
Assert.True(double.IsFinite(resultAfterNaN.Value));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Chainability_Works()
|
||||
{
|
||||
var source = new TSeries();
|
||||
var rma = new Rma(source, 10);
|
||||
|
||||
source.Add(new TValue(DateTime.UtcNow, 100));
|
||||
Assert.Equal(100, rma.Last.Value, 1e-9);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,96 @@
|
||||
using OoplesFinance.StockIndicators;
|
||||
using OoplesFinance.StockIndicators.Models;
|
||||
using Skender.Stock.Indicators;
|
||||
|
||||
namespace QuanTAlib.Tests;
|
||||
|
||||
public sealed class RmaValidationTests : IDisposable
|
||||
{
|
||||
private readonly ValidationTestData _testData;
|
||||
private bool _disposed;
|
||||
|
||||
public RmaValidationTests()
|
||||
{
|
||||
_testData = new ValidationTestData(count: 10000, seed: 123);
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
Dispose(true);
|
||||
}
|
||||
|
||||
private void Dispose(bool disposing)
|
||||
{
|
||||
if (_disposed)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
_disposed = true;
|
||||
|
||||
if (disposing)
|
||||
{
|
||||
_testData?.Dispose();
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Rma_Matches_Skender_Smma()
|
||||
{
|
||||
// Arrange
|
||||
const int period = 14;
|
||||
|
||||
// QuanTAlib RMA
|
||||
var rma = new Rma(period);
|
||||
var quantalibResults = new TSeries();
|
||||
foreach (var item in _testData.Data)
|
||||
{
|
||||
quantalibResults.Add(rma.Update(item));
|
||||
}
|
||||
|
||||
// Skender SMMA
|
||||
var skenderResults = _testData.SkenderQuotes.GetSmma(period).ToList();
|
||||
|
||||
// Assert
|
||||
// Skip warmup period for comparison
|
||||
// Skender uses SMA initialization, QuanTAlib uses zero-lag compensator
|
||||
// They should converge after some periods
|
||||
int skip = period * 30;
|
||||
|
||||
int itemsToVerify = _testData.Data.Count - skip;
|
||||
ValidationHelper.VerifyData(quantalibResults, skenderResults, (s) => s.Smma, skip: itemsToVerify, tolerance: ValidationHelper.OoplesTolerance);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Validate_Against_Ooples()
|
||||
{
|
||||
// Arrange
|
||||
int period = 14;
|
||||
|
||||
// QuanTAlib RMA
|
||||
var rma = new Rma(period);
|
||||
var qResult = rma.Update(_testData.Data);
|
||||
|
||||
// Ooples WWMA (Welles Wilder Moving Average)
|
||||
var ooplesData = _testData.SkenderQuotes.Select(q => new TickerData
|
||||
{
|
||||
Date = q.Date,
|
||||
Close = (double)q.Close,
|
||||
High = (double)q.High,
|
||||
Low = (double)q.Low,
|
||||
Open = (double)q.Open,
|
||||
Volume = (double)q.Volume
|
||||
}).ToList();
|
||||
|
||||
var stockData = new StockData(ooplesData);
|
||||
var oResult = stockData.CalculateWellesWilderMovingAverage(length: period);
|
||||
var oValues = oResult.OutputValues["Wwma"];
|
||||
|
||||
// Assert
|
||||
// Skip warmup period for comparison
|
||||
int skip = period * 30;
|
||||
int itemsToVerify = _testData.Data.Count - skip;
|
||||
|
||||
ValidationHelper.VerifyData(qResult, oValues, (s) => s, skip: itemsToVerify, tolerance: ValidationHelper.OoplesTolerance);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user