mirror of
https://github.com/mihakralj/QuanTAlib.git
synced 2026-08-25 22:08:05 +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,84 @@
|
||||
using TradingPlatform.BusinessLayer;
|
||||
using QuanTAlib;
|
||||
|
||||
namespace QuanTAlib.Tests;
|
||||
|
||||
public class BlmaIndicatorTests
|
||||
{
|
||||
[Fact]
|
||||
public void BlmaIndicator_Constructor_SetsDefaults()
|
||||
{
|
||||
var indicator = new BlmaIndicator();
|
||||
|
||||
Assert.Equal(14, indicator.Period);
|
||||
Assert.True(indicator.ShowColdValues);
|
||||
Assert.Equal("BLMA - Blackman Window Moving Average", indicator.Name);
|
||||
Assert.False(indicator.SeparateWindow);
|
||||
Assert.Equal(SourceType.Close, indicator.Source);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void BlmaIndicator_MinHistoryDepths_EqualsPeriod()
|
||||
{
|
||||
var indicator = new BlmaIndicator { Period = 20 };
|
||||
|
||||
Assert.Equal(0, BlmaIndicator.MinHistoryDepths);
|
||||
IWatchlistIndicator watchlistIndicator = indicator;
|
||||
Assert.Equal(0, watchlistIndicator.MinHistoryDepths);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void BlmaIndicator_ShortName_IncludesParameters()
|
||||
{
|
||||
var indicator = new BlmaIndicator { Period = 20 };
|
||||
indicator.Initialize();
|
||||
|
||||
Assert.Contains("BLMA", indicator.ShortName, StringComparison.Ordinal);
|
||||
Assert.Contains("20", indicator.ShortName, StringComparison.Ordinal);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void BlmaIndicator_SourceCodeLink_IsValid()
|
||||
{
|
||||
var indicator = new BlmaIndicator();
|
||||
|
||||
Assert.Contains("github.com", indicator.SourceCodeLink, StringComparison.Ordinal);
|
||||
Assert.Contains("Blma.Quantower.cs", indicator.SourceCodeLink, StringComparison.Ordinal);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void BlmaIndicator_Initialize_CreatesInternalBlma()
|
||||
{
|
||||
var indicator = new BlmaIndicator { Period = 14 };
|
||||
|
||||
// Initialize should not throw
|
||||
indicator.Initialize();
|
||||
|
||||
// After init, line series should exist
|
||||
Assert.Single(indicator.LinesSeries);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void BlmaIndicator_ProcessUpdate_HistoricalBar_ComputesValue()
|
||||
{
|
||||
var indicator = new BlmaIndicator { Period = 5 };
|
||||
indicator.Initialize();
|
||||
|
||||
// Add historical data
|
||||
var now = DateTime.UtcNow;
|
||||
// Need enough bars for Period
|
||||
for (int i = 0; i < 20; i++)
|
||||
{
|
||||
indicator.HistoricalData.AddBar(now.AddMinutes(i), 100 + i, 110 + i, 90 + i, 105 + i);
|
||||
|
||||
// Process update for each bar to simulate history loading
|
||||
var args = new UpdateArgs(UpdateReason.HistoricalBar);
|
||||
indicator.ProcessUpdate(args);
|
||||
}
|
||||
|
||||
// Line series should have a value
|
||||
double blma = indicator.LinesSeries[0].GetValue(0);
|
||||
|
||||
Assert.True(double.IsFinite(blma));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,173 @@
|
||||
|
||||
namespace QuanTAlib.Tests;
|
||||
|
||||
public class BlmaTests
|
||||
{
|
||||
private readonly GBM _gbm;
|
||||
|
||||
public BlmaTests()
|
||||
{
|
||||
_gbm = new GBM();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Constructor_ValidatesInput()
|
||||
{
|
||||
Assert.Throws<ArgumentOutOfRangeException>(() => new Blma(0));
|
||||
Assert.Throws<ArgumentOutOfRangeException>(() => new Blma(-1));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Constructor_ValidatesSource()
|
||||
{
|
||||
Assert.Throws<ArgumentNullException>(() => new Blma(null!, 10));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void BasicCalculation_MatchesManual()
|
||||
{
|
||||
var blma = new Blma(3);
|
||||
var input = new[] { 10.0, 20.0, 30.0 };
|
||||
|
||||
// Bar 1: Count=1. Weights for n=1: [1]. Result = 10.
|
||||
var r1 = blma.Update(new TValue(DateTime.UtcNow, input[0]));
|
||||
Assert.Equal(10.0, r1.Value);
|
||||
|
||||
// Bar 2: Count=2. Weights for n=2 sum to 0. Fallback to average: (10+20)/2 = 15.
|
||||
var r2 = blma.Update(new TValue(DateTime.UtcNow, input[1]));
|
||||
Assert.Equal(15.0, r2.Value);
|
||||
|
||||
// Bar 3: Count=3. Weights [0, 1, 0]. Sum=1. Result=20.
|
||||
var r3 = blma.Update(new TValue(DateTime.UtcNow, input[2]));
|
||||
Assert.Equal(20.0, r3.Value, 1e-6);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void AllModes_ProduceSameResult()
|
||||
{
|
||||
const int period = 10;
|
||||
var bars = _gbm.Fetch(100, DateTime.UtcNow.Ticks, TimeSpan.FromMinutes(1));
|
||||
var series = bars.Close;
|
||||
|
||||
// 1. Batch Mode
|
||||
var batchSeries = new Blma(period).Update(series);
|
||||
double expected = batchSeries.Last.Value;
|
||||
|
||||
// 2. Span Mode
|
||||
var tValues = series.Values.ToArray();
|
||||
var spanInput = new ReadOnlySpan<double>(tValues);
|
||||
var spanOutput = new double[tValues.Length];
|
||||
Blma.Batch(spanInput, spanOutput, period);
|
||||
double spanResult = spanOutput[^1];
|
||||
|
||||
// 3. Streaming Mode
|
||||
var streamingInd = new Blma(period);
|
||||
for (int i = 0; i < series.Count; i++)
|
||||
{
|
||||
streamingInd.Update(series[i]);
|
||||
}
|
||||
double streamingResult = streamingInd.Last.Value;
|
||||
|
||||
// Assert
|
||||
Assert.Equal(expected, spanResult, 1e-9);
|
||||
Assert.Equal(expected, streamingResult, 1e-9);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void NaN_Handling()
|
||||
{
|
||||
var blma = new Blma(5);
|
||||
|
||||
blma.Update(new TValue(DateTime.UtcNow, 10));
|
||||
blma.Update(new TValue(DateTime.UtcNow, 20));
|
||||
// For N=2, weights sum to 0. Fallback to average: (10+20)/2 = 15.
|
||||
|
||||
var result = blma.Update(new TValue(DateTime.UtcNow, double.NaN));
|
||||
|
||||
Assert.Equal(15.0, result.Value); // Should return last valid value
|
||||
Assert.Equal(15.0, blma.Last.Value); // Should retain last valid value
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void IsNew_Behavior()
|
||||
{
|
||||
var blma = new Blma(3);
|
||||
|
||||
// Bar 1
|
||||
blma.Update(new TValue(DateTime.UtcNow, 10), isNew: true);
|
||||
|
||||
// Bar 2
|
||||
blma.Update(new TValue(DateTime.UtcNow, 20), isNew: true);
|
||||
|
||||
// Bar 3 (Update)
|
||||
blma.Update(new TValue(DateTime.UtcNow, 30), isNew: true);
|
||||
var val1 = blma.Last.Value;
|
||||
|
||||
// Bar 3 (Correction)
|
||||
blma.Update(new TValue(DateTime.UtcNow, 40), isNew: false);
|
||||
var val2 = blma.Last.Value;
|
||||
|
||||
// For Blackman window, the newest value (index N-1) has weight 0.
|
||||
// So changing the newest value does NOT change the current result.
|
||||
Assert.Equal(val1, val2);
|
||||
|
||||
// However, the internal buffer MUST be updated.
|
||||
// Case A: Bar 3 = 40 (current state)
|
||||
blma.Update(new TValue(DateTime.UtcNow, 100), isNew: true);
|
||||
var valWith40 = blma.Last.Value;
|
||||
|
||||
// Case B: Reconstruct scenario with Bar 3 = 30
|
||||
var blma2 = new Blma(3);
|
||||
blma2.Update(new TValue(DateTime.UtcNow, 10), isNew: true);
|
||||
blma2.Update(new TValue(DateTime.UtcNow, 20), isNew: true);
|
||||
blma2.Update(new TValue(DateTime.UtcNow, 30), isNew: true);
|
||||
blma2.Update(new TValue(DateTime.UtcNow, 100), isNew: true);
|
||||
var valWith30 = blma2.Last.Value;
|
||||
|
||||
Assert.NotEqual(valWith30, valWith40);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Prime_PreservesTimestamps()
|
||||
{
|
||||
var blma = new Blma(5);
|
||||
double[] input = [1, 2, 3, 4, 5];
|
||||
var timestamps = new List<DateTime>();
|
||||
|
||||
blma.Pub += (object? sender, in TValueEventArgs args) => timestamps.Add(args.Value.AsDateTime);
|
||||
|
||||
|
||||
blma.Prime(input);
|
||||
|
||||
Assert.Equal(input.Length, timestamps.Count);
|
||||
// Verify timestamps are unique and increasing
|
||||
for (int i = 1; i < timestamps.Count; i++)
|
||||
{
|
||||
Assert.True(timestamps[i] > timestamps[i - 1], $"Timestamp at {i} ({timestamps[i].Ticks}) should be greater than {i - 1} ({timestamps[i - 1].Ticks})");
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Prime_Overload_UsesProvidedTimestamps()
|
||||
{
|
||||
var blma = new Blma(5);
|
||||
var now = DateTime.UtcNow;
|
||||
TValue[] input =
|
||||
[
|
||||
new(now, 1),
|
||||
new(now.AddMinutes(1), 2),
|
||||
new(now.AddMinutes(2), 3)
|
||||
];
|
||||
var timestamps = new List<DateTime>();
|
||||
|
||||
blma.Pub += (object? sender, in TValueEventArgs args) => timestamps.Add(args.Value.AsDateTime);
|
||||
|
||||
|
||||
blma.Prime(input);
|
||||
|
||||
Assert.Equal(input.Length, timestamps.Count);
|
||||
Assert.Equal(input[0].AsDateTime, timestamps[0]);
|
||||
Assert.Equal(input[1].AsDateTime, timestamps[1]);
|
||||
Assert.Equal(input[2].AsDateTime, timestamps[2]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,130 @@
|
||||
|
||||
namespace QuanTAlib.Tests;
|
||||
|
||||
public class BlmaValidationTests
|
||||
{
|
||||
private readonly GBM _gbm;
|
||||
|
||||
public BlmaValidationTests()
|
||||
{
|
||||
_gbm = new GBM();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ValidateAgainstReferenceImplementation()
|
||||
{
|
||||
// Generate test data
|
||||
var bars = _gbm.Fetch(1000, DateTime.UtcNow.Ticks, TimeSpan.FromMinutes(1));
|
||||
var series = bars.Close;
|
||||
const int period = 14;
|
||||
|
||||
// 1. QuanTAlib Implementation
|
||||
var blma = new Blma(period);
|
||||
var quantalibResult = new List<double>();
|
||||
foreach (var item in series)
|
||||
{
|
||||
quantalibResult.Add(blma.Update(item).Value);
|
||||
}
|
||||
|
||||
// 2. Reference Implementation (PineScript logic)
|
||||
var referenceResult = CalculateReference(series, period);
|
||||
|
||||
// Compare
|
||||
Assert.Equal(quantalibResult.Count, referenceResult.Count);
|
||||
for (int i = 0; i < quantalibResult.Count; i++)
|
||||
{
|
||||
// Allow small difference due to float precision
|
||||
Assert.Equal(referenceResult[i], quantalibResult[i], 1e-9);
|
||||
}
|
||||
}
|
||||
|
||||
private static List<double> CalculateReference(TSeries source, int period)
|
||||
{
|
||||
var result = new List<double>();
|
||||
var buffer = new List<double>();
|
||||
|
||||
for (int i = 0; i < source.Count; i++)
|
||||
{
|
||||
buffer.Add(source[i].Value);
|
||||
|
||||
// PineScript logic:
|
||||
// int p = math.min(bar_index + 1, period)
|
||||
int p = Math.Min(buffer.Count, period);
|
||||
|
||||
// Calculate weights
|
||||
var weights = new double[p];
|
||||
double totalWeight = 0;
|
||||
|
||||
if (p == 1)
|
||||
{
|
||||
weights[0] = 1.0;
|
||||
totalWeight = 1.0;
|
||||
}
|
||||
else
|
||||
{
|
||||
double invPMinus1 = 1.0 / (p - 1);
|
||||
double pi2 = 2.0 * Math.PI;
|
||||
double pi4 = 4.0 * Math.PI;
|
||||
double a0 = 0.42;
|
||||
double a1 = 0.5;
|
||||
double a2 = 0.08;
|
||||
|
||||
for (int j = 0; j < p; j++)
|
||||
{
|
||||
double ratio = j * invPMinus1;
|
||||
double w = a0 - (a1 * Math.Cos(pi2 * ratio)) + (a2 * Math.Cos(pi4 * ratio));
|
||||
weights[j] = w;
|
||||
totalWeight += w;
|
||||
}
|
||||
}
|
||||
|
||||
// Calculate weighted sum
|
||||
double sum = 0;
|
||||
// PineScript: for i = 0 to p - 1
|
||||
// float price = source[i] (where source[0] is newest)
|
||||
// float w = array.get(weights, i)
|
||||
// So weights[0] * newest, weights[1] * 2nd newest...
|
||||
|
||||
// My C# buffer is chronological (0 is oldest).
|
||||
// So buffer[buffer.Count - 1] is newest.
|
||||
// buffer[buffer.Count - 1 - j] is j-th lag.
|
||||
|
||||
// Wait, in Blma.cs I implemented:
|
||||
// sum += buffer[i] * weights[i] (where buffer[0] is oldest)
|
||||
// So weights[0] * oldest.
|
||||
|
||||
// PineScript: weights[0] * newest.
|
||||
// Since Blackman window is symmetric, weights[0] == weights[p-1].
|
||||
// So weights[0] * newest == weights[p-1] * newest (if symmetric).
|
||||
// But weights[0] is 0. weights[p-1] is 0.
|
||||
// weights[p/2] is peak.
|
||||
// So symmetric window applied forward or backward is the same.
|
||||
// Let's verify symmetry.
|
||||
// w(j) vs w(p-1-j).
|
||||
// ratio(j) = j/(p-1).
|
||||
// ratio(p-1-j) = (p-1-j)/(p-1) = 1 - j/(p-1) = 1 - ratio(j).
|
||||
// cos(2pi * (1-r)) = cos(2pi - 2pi*r) = cos(-2pi*r) = cos(2pi*r).
|
||||
// cos(4pi * (1-r)) = cos(4pi - 4pi*r) = cos(4pi*r).
|
||||
// So yes, w(j) == w(p-1-j).
|
||||
// So applying weights[0] to newest or oldest doesn't matter for the sum.
|
||||
|
||||
// However, I should match my implementation in Blma.cs.
|
||||
// In Blma.cs: sum += buffer[i] * weights[i] (buffer[0] is oldest).
|
||||
// So weights[0] * oldest.
|
||||
|
||||
// In this reference implementation, let's do the same.
|
||||
// Use the last p elements of buffer.
|
||||
int start = buffer.Count - p;
|
||||
for (int j = 0; j < p; j++)
|
||||
{
|
||||
// buffer[start + j] is the value.
|
||||
// weights[j] is the weight.
|
||||
sum += buffer[start + j] * weights[j];
|
||||
}
|
||||
|
||||
result.Add(sum / totalWeight);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user