mirror of
https://github.com/mihakralj/QuanTAlib.git
synced 2026-08-22 20:48:04 +00:00
Refactor and optimize various components of QuanTAlib
- Removed WmaVector class to streamline weighted moving average calculations. - Simplified RingBuffer implementation by removing unnecessary comments and improving clarity. - Enhanced SIMD extensions for better performance and readability. - Updated TBar and TBarSeries classes to improve property calculations and reduce overhead. - Cleaned up TValue struct by removing redundant comments. - Added comprehensive unit tests for IndicatorExtensions and TrimaIndicator to ensure functionality and correctness.
This commit is contained in:
@@ -21,7 +21,8 @@ This notebook demonstrates:
|
||||
1. **Manual Data Processing**: Understanding Batch vs. Streaming modes.
|
||||
2. **Streaming with `isNew`**: Handling intra-bar updates.
|
||||
3. **Large Dataset Processing**: Using Geometric Brownian Motion (GBM) generated data.
|
||||
4. **Vectorized Operations**: Calculating multiple SMAs simultaneously.
|
||||
4. **Handling Invalid Values**: Last-value substitution for NaN/Infinity.
|
||||
5. **SMA vs EMA**: Comparing Simple and Exponential Moving Averages.
|
||||
|
||||
#!csharp
|
||||
|
||||
@@ -189,63 +190,9 @@ Console.WriteLine($"Match: {Math.Abs(batchLargeResult.Last().Value - lastStreamV
|
||||
|
||||
#!markdown
|
||||
|
||||
## 4. Vectorized SMA (Multiple Periods)
|
||||
## 4. Handling Invalid Values (NaN/Infinity)
|
||||
|
||||
`SmaVector` allows calculating multiple SMAs (e.g., 5, 10, 20) simultaneously. This is useful for comparing different timeframes.
|
||||
|
||||
### Vectorized Batch
|
||||
|
||||
#!csharp
|
||||
|
||||
int[] periods = { 5, 10, 20 };
|
||||
Console.WriteLine($"\n--- Vectorized Batch SMA (Periods: {string.Join(", ", periods)}) ---");
|
||||
|
||||
var smaVectorBatch = new SmaVector(periods);
|
||||
var vectorBatchResults = smaVectorBatch.Calculate(closeSeries);
|
||||
|
||||
for (int i = 0; i < periods.Length; i++)
|
||||
{
|
||||
Console.WriteLine($"SMA({periods[i]}) Last Value: {vectorBatchResults[i].Last().Value:F2}");
|
||||
}
|
||||
|
||||
#!markdown
|
||||
|
||||
### Vectorized Streaming
|
||||
|
||||
#!csharp
|
||||
|
||||
Console.WriteLine($"\n--- Vectorized Streaming SMA (Periods: {string.Join(", ", periods)}) ---");
|
||||
|
||||
var smaVectorStream = new SmaVector(periods);
|
||||
TValue[] lastVectorVal = null;
|
||||
|
||||
foreach(var item in closeSeries)
|
||||
{
|
||||
lastVectorVal = smaVectorStream.Update(item);
|
||||
}
|
||||
|
||||
for (int i = 0; i < periods.Length; i++)
|
||||
{
|
||||
Console.WriteLine($"SMA({periods[i]}) Last Value: {lastVectorVal[i].Value:F2}");
|
||||
}
|
||||
|
||||
// Verification
|
||||
bool allMatch = true;
|
||||
for (int i = 0; i < periods.Length; i++)
|
||||
{
|
||||
if (Math.Abs(vectorBatchResults[i].Last().Value - lastVectorVal[i].Value) > 1e-10)
|
||||
{
|
||||
allMatch = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
Console.WriteLine($"\nAll Vectorized Stream/Batch values match: {allMatch}");
|
||||
|
||||
#!markdown
|
||||
|
||||
## 5. Handling Invalid Values (NaN/Infinity)
|
||||
|
||||
Both `Sma` and `SmaVector` use **last-value substitution** for invalid inputs. When a non-finite value (NaN, PositiveInfinity, NegativeInfinity) is encountered, it is replaced with the last valid value. This provides output continuity instead of propagating invalid values through the calculation.
|
||||
`Sma` uses **last-value substitution** for invalid inputs. When a non-finite value (NaN, PositiveInfinity, NegativeInfinity) is encountered, it is replaced with the last valid value. This provides output continuity instead of propagating invalid values through the calculation.
|
||||
|
||||
#!csharp
|
||||
|
||||
@@ -296,30 +243,9 @@ for (int i = 0; i < seriesWithNaN.Count; i++)
|
||||
Console.WriteLine($" {inputStr,-10} → {output:F2} (IsFinite: {double.IsFinite(output)})");
|
||||
}
|
||||
|
||||
#!csharp
|
||||
|
||||
Console.WriteLine("\n--- Vectorized SMA with Invalid Values ---");
|
||||
|
||||
int[] periodsNaN = { 5, 10 };
|
||||
var smaVectorNaN = new SmaVector(periodsNaN);
|
||||
|
||||
// Feed values including invalid ones
|
||||
var inputsNaN = new double[] { 100, 110, double.NaN, 120, double.PositiveInfinity, 130 };
|
||||
var time = DateTime.Now;
|
||||
|
||||
foreach (var val in inputsNaN)
|
||||
{
|
||||
var results = smaVectorNaN.Update(new TValue(time, val));
|
||||
var inputStr = double.IsFinite(val) ? val.ToString("F2") : val.ToString();
|
||||
Console.WriteLine($"Input: {inputStr,-10} → SMA(5): {results[0].Value:F2}, SMA(10): {results[1].Value:F2}");
|
||||
time = time.AddMinutes(1);
|
||||
}
|
||||
|
||||
Console.WriteLine("\nAll outputs are finite - invalid inputs were substituted with last valid values.");
|
||||
|
||||
#!markdown
|
||||
|
||||
## 6. SMA vs EMA Comparison
|
||||
## 5. SMA vs EMA Comparison
|
||||
|
||||
The SMA and EMA are both trend-following indicators, but they weight data differently:
|
||||
|
||||
|
||||
+7
-93
@@ -38,15 +38,13 @@ public sealed class Sma
|
||||
private readonly int _period;
|
||||
private readonly RingBuffer _buffer;
|
||||
|
||||
// Running sum maintained separately for O(1) bar correction
|
||||
private double _sum;
|
||||
private double _p_sum; // Sum AFTER last isNew=true (for correction restore)
|
||||
private double _p_lastInput; // Input that was added on last isNew=true
|
||||
private double _p_sum;
|
||||
private double _p_lastInput;
|
||||
private double _lastValidValue;
|
||||
private double _p_lastValidValue;
|
||||
private int _tickCount; // Counter for periodic sum resync
|
||||
private int _tickCount;
|
||||
|
||||
// Resync interval: recalculate sum from buffer every N ticks to prevent drift
|
||||
private const int ResyncInterval = 1000;
|
||||
|
||||
/// <summary>
|
||||
@@ -93,23 +91,15 @@ public sealed class Sma
|
||||
return _lastValidValue;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Updates internal state with a new value.
|
||||
/// Shared logic for both streaming and batch-reconstruction.
|
||||
/// </summary>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
private void UpdateState(double val)
|
||||
{
|
||||
// Calculate what to remove from sum (oldest value if buffer full)
|
||||
double removedValue = _buffer.Count == _buffer.Capacity ? _buffer.Oldest : 0.0;
|
||||
|
||||
// Update sum: remove oldest, add newest
|
||||
_sum = _sum - removedValue + val;
|
||||
|
||||
// Update buffer
|
||||
_buffer.Add(val);
|
||||
|
||||
// Periodic resync: recalculate sum from scratch to eliminate floating-point drift
|
||||
_tickCount++;
|
||||
if (_buffer.IsFull && _tickCount >= ResyncInterval)
|
||||
{
|
||||
@@ -118,43 +108,27 @@ public sealed class Sma
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Updates SMA with the given value.
|
||||
/// O(1) for both isNew=true and isNew=false.
|
||||
/// </summary>
|
||||
/// <param name="input">Input value</param>
|
||||
/// <param name="isNew">True for new bar, false for update to current bar (default: true)</param>
|
||||
/// <returns>Current SMA value</returns>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public TValue Update(TValue input, bool isNew = true)
|
||||
{
|
||||
if (isNew)
|
||||
{
|
||||
// Get valid value (this may update _lastValidValue)
|
||||
double val = GetValidValue(input.Value);
|
||||
|
||||
UpdateState(val);
|
||||
|
||||
// Save state AFTER this update for potential future corrections
|
||||
_p_sum = _sum;
|
||||
_p_lastInput = val;
|
||||
_p_lastValidValue = _lastValidValue;
|
||||
}
|
||||
else
|
||||
{
|
||||
// Bar correction: restore to state AFTER last isNew=true, then swap last value
|
||||
// Restore _lastValidValue BEFORE calling GetValidValue
|
||||
_lastValidValue = _p_lastValidValue;
|
||||
|
||||
// Get valid value (this may update _lastValidValue)
|
||||
double val = GetValidValue(input.Value);
|
||||
|
||||
// _p_sum is the sum AFTER the last isNew=true completed
|
||||
// _p_lastInput is the value that was added on last isNew=true
|
||||
// We want: new_sum = _p_sum - _p_lastInput + val
|
||||
_sum = _p_sum - _p_lastInput + val;
|
||||
|
||||
// Update buffer's newest value
|
||||
_buffer.UpdateNewest(val);
|
||||
}
|
||||
|
||||
@@ -163,11 +137,6 @@ public sealed class Sma
|
||||
return Value;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Updates SMA with the entire series.
|
||||
/// </summary>
|
||||
/// <param name="source">Input series</param>
|
||||
/// <returns>SMA series</returns>
|
||||
public TSeries Update(TSeries source)
|
||||
{
|
||||
if (source.Count == 0) return new TSeries(new List<long>(), new List<double>());
|
||||
@@ -183,25 +152,15 @@ public sealed class Sma
|
||||
var sourceValues = source.Values;
|
||||
var sourceTimes = source.Times;
|
||||
|
||||
// 1. Fast Batch Calculation (SIMD optimized)
|
||||
Calculate(sourceValues, vSpan, _period);
|
||||
|
||||
// 2. Copy Times
|
||||
sourceTimes.CopyTo(tSpan);
|
||||
|
||||
// 3. Reconstruct State for subsequent updates
|
||||
// We need to restore _buffer, _sum, and _lastValidValue to what they would be
|
||||
// if we had processed the series sequentially.
|
||||
|
||||
// Find the last valid value before the reconstruction window
|
||||
// The reconstruction window is the last 'period' elements (or less if len < period)
|
||||
int windowSize = Math.Min(len, _period);
|
||||
int startIndex = len - windowSize;
|
||||
|
||||
// Restore _lastValidValue from before the window
|
||||
if (startIndex > 0)
|
||||
{
|
||||
// Scan backwards to find last valid value
|
||||
for (int i = startIndex - 1; i >= 0; i--)
|
||||
{
|
||||
if (double.IsFinite(sourceValues[i]))
|
||||
@@ -213,10 +172,9 @@ public sealed class Sma
|
||||
}
|
||||
else
|
||||
{
|
||||
_lastValidValue = 0; // Reset if starting from 0
|
||||
_lastValidValue = 0;
|
||||
}
|
||||
|
||||
// Rebuild buffer and sum from last 'period' values using shared logic
|
||||
_buffer.Clear();
|
||||
_sum = 0;
|
||||
_tickCount = 0;
|
||||
@@ -227,7 +185,6 @@ public sealed class Sma
|
||||
UpdateState(val);
|
||||
}
|
||||
|
||||
// Save state for potential future corrections
|
||||
_p_sum = _sum;
|
||||
_p_lastInput = sourceValues[len - 1];
|
||||
_p_lastValidValue = _lastValidValue;
|
||||
@@ -281,17 +238,11 @@ public sealed class Sma
|
||||
CalculateScalarCore(source, output, period);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Scalar implementation with NaN handling via last-value substitution.
|
||||
/// Uses circular buffer for sliding window calculation.
|
||||
/// Optimized with split loops and periodic resync.
|
||||
/// </summary>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
private static void CalculateScalarCore(ReadOnlySpan<double> source, Span<double> output, int period)
|
||||
{
|
||||
int len = source.Length;
|
||||
|
||||
// Use stackalloc for small periods, otherwise fall back to heap allocation
|
||||
const int StackAllocThreshold = 256;
|
||||
Span<double> buffer = period <= StackAllocThreshold
|
||||
? stackalloc double[period]
|
||||
@@ -302,8 +253,6 @@ public sealed class Sma
|
||||
int bufferIndex = 0;
|
||||
int i = 0;
|
||||
|
||||
// Phase 1: Warmup (0 to period-1)
|
||||
// No need to remove oldest value, just accumulate
|
||||
int warmupEnd = Math.Min(period, len);
|
||||
for (; i < warmupEnd; i++)
|
||||
{
|
||||
@@ -318,9 +267,6 @@ public sealed class Sma
|
||||
output[i] = sum / (i + 1);
|
||||
}
|
||||
|
||||
// Phase 2: Hot loop (period to len)
|
||||
// Buffer is full, remove oldest, add newest
|
||||
// Optimized buffer indexing (no modulo)
|
||||
int tickCount = 0;
|
||||
for (; i < len; i++)
|
||||
{
|
||||
@@ -330,23 +276,19 @@ public sealed class Sma
|
||||
else
|
||||
val = lastValid;
|
||||
|
||||
// Remove oldest, add newest
|
||||
sum = sum - buffer[bufferIndex] + val;
|
||||
buffer[bufferIndex] = val;
|
||||
|
||||
// Increment buffer index with wrap-around check (faster than modulo)
|
||||
bufferIndex++;
|
||||
if (bufferIndex >= period)
|
||||
bufferIndex = 0;
|
||||
|
||||
output[i] = sum / period;
|
||||
|
||||
// Periodic resync every 1000 ticks
|
||||
tickCount++;
|
||||
if (tickCount >= ResyncInterval)
|
||||
{
|
||||
tickCount = 0;
|
||||
// Recalculate sum from buffer to prevent drift
|
||||
double recalcSum = 0;
|
||||
for (int k = 0; k < period; k++)
|
||||
{
|
||||
@@ -357,29 +299,17 @@ public sealed class Sma
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// SIMD-optimized implementation for SMA calculation.
|
||||
/// Processes 4 consecutive values per iteration using AVX2 (Vector256<double>).
|
||||
/// Assumes input contains no NaN/Infinity values.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// Key insight: For consecutive positions i, i+1, i+2, i+3:
|
||||
/// - sum[i+1] = sum[i] - src[i-period+1] + src[i+1]
|
||||
/// - We can vectorize the load of 4 "leaving" values and 4 "entering" values
|
||||
/// - Then use prefix-sum style to compute the 4 sums from one base sum
|
||||
/// </remarks>
|
||||
[MethodImpl(MethodImplOptions.AggressiveOptimization)]
|
||||
private static unsafe void CalculateSimdCore(ReadOnlySpan<double> source, Span<double> output, int period)
|
||||
{
|
||||
int len = source.Length;
|
||||
const int VectorWidth = 4; // Vector256<double> holds 4 doubles
|
||||
const int VectorWidth = 4;
|
||||
|
||||
fixed (double* srcPtr = source)
|
||||
fixed (double* outPtr = output)
|
||||
{
|
||||
double invPeriod = 1.0 / period;
|
||||
|
||||
// Phase 1: Warmup - scalar processing until buffer is full
|
||||
int warmupEnd = Math.Min(period, len);
|
||||
double sum = 0;
|
||||
for (int i = 0; i < warmupEnd; i++)
|
||||
@@ -391,8 +321,6 @@ public sealed class Sma
|
||||
if (len <= period)
|
||||
return;
|
||||
|
||||
// Phase 2: SIMD hot loop
|
||||
// Uses prefix-sum approach to break dependency chain
|
||||
var vInvPeriod = Vector256.Create(invPeriod);
|
||||
var vZero = Vector256<double>.Zero;
|
||||
int simdEnd = period + ((len - period) / VectorWidth) * VectorWidth;
|
||||
@@ -400,44 +328,31 @@ public sealed class Sma
|
||||
|
||||
for (int i = period; i < simdEnd; i += VectorWidth)
|
||||
{
|
||||
// Load 4 entering values and 4 leaving values
|
||||
var vNew = Avx.LoadVector256(srcPtr + i);
|
||||
var vOld = Avx.LoadVector256(srcPtr + i - period);
|
||||
|
||||
// Delta = New - Old
|
||||
var vDelta = Avx.Subtract(vNew, vOld);
|
||||
|
||||
// Prefix sum of Deltas
|
||||
// Step 1: Shift right by 1 element (insert 0)
|
||||
// [D0, D1, D2, D3] -> [0, D0, D1, D2]
|
||||
var vShift1 = Avx2.Permute4x64(vDelta.AsUInt64(), 0b_10_01_00_00).AsDouble();
|
||||
vShift1 = Avx.Blend(vZero, vShift1, 0b_1110);
|
||||
var vP1 = Avx.Add(vDelta, vShift1); // [D0, D0+D1, D1+D2, D2+D3]
|
||||
var vP1 = Avx.Add(vDelta, vShift1);
|
||||
|
||||
// Step 2: Shift right by 2 elements (insert 0)
|
||||
// [D0, D0+D1, D1+D2, D2+D3] -> [0, 0, D0, D0+D1]
|
||||
var vShift2 = Avx2.Permute4x64(vP1.AsUInt64(), 0b_01_00_00_00).AsDouble();
|
||||
vShift2 = Avx.Blend(vZero, vShift2, 0b_1100);
|
||||
var vP2 = Avx.Add(vP1, vShift2); // [D0, D0+D1, D0+D1+D2, D0+D1+D2+D3]
|
||||
var vP2 = Avx.Add(vP1, vShift2);
|
||||
|
||||
// Add previous sum to all
|
||||
var vSumPrev = Vector256.Create(sum);
|
||||
var vSums = Avx.Add(vSumPrev, vP2);
|
||||
|
||||
// Store result
|
||||
var vResult = Avx.Multiply(vSums, vInvPeriod);
|
||||
Avx.Store(outPtr + i, vResult);
|
||||
|
||||
// Update sum for next iteration (last element of vSums)
|
||||
sum = vSums.GetElement(3);
|
||||
|
||||
// Periodic resync every 1000 ticks
|
||||
tickCount += VectorWidth;
|
||||
if (tickCount >= ResyncInterval)
|
||||
{
|
||||
tickCount = 0;
|
||||
// Recalculate sum from scratch using the window ending at i + VectorWidth - 1
|
||||
// Window: [i + VectorWidth - period ... i + VectorWidth - 1]
|
||||
int lastIdx = i + VectorWidth - 1;
|
||||
double recalcSum = 0;
|
||||
for (int k = 0; k < period; k++)
|
||||
@@ -448,7 +363,6 @@ public sealed class Sma
|
||||
}
|
||||
}
|
||||
|
||||
// Phase 3: Scalar tail
|
||||
for (int i = simdEnd; i < len; i++)
|
||||
{
|
||||
sum = sum - srcPtr[i - period] + srcPtr[i];
|
||||
|
||||
+2
-26
@@ -99,33 +99,9 @@ Console.WriteLine($"Last SMA: {smaOutput[^1]}");
|
||||
* **2-3x faster** than TSeries API for large datasets
|
||||
* **Compatible** with `ArrayPool<T>` for buffer management
|
||||
|
||||
### Multi-Period SMA (`SmaVector`)
|
||||
|
||||
The `SmaVector` class calculates multiple SMAs with different periods on the same input series simultaneously.
|
||||
|
||||
```csharp
|
||||
using QuanTAlib;
|
||||
|
||||
// Initialize with multiple periods
|
||||
int[] periods = { 5, 10, 20 };
|
||||
var smaVector = new SmaVector(periods);
|
||||
|
||||
// Streaming update
|
||||
TValue[] results = smaVector.Update(new TValue(time, price));
|
||||
|
||||
// Access values
|
||||
Console.WriteLine($"SMA(5): {results[0].Value}");
|
||||
Console.WriteLine($"SMA(10): {results[1].Value}");
|
||||
Console.WriteLine($"SMA(20): {results[2].Value}");
|
||||
|
||||
// Batch calculation
|
||||
TSeries source = ...;
|
||||
TSeries[] seriesResults = smaVector.Calculate(source);
|
||||
```
|
||||
|
||||
### Bar Correction (isNew Parameter)
|
||||
|
||||
Both `Sma` and `SmaVector` support intra-bar updates for real-time trading systems:
|
||||
`Sma` supports intra-bar updates for real-time trading systems:
|
||||
|
||||
```csharp
|
||||
var sma = new Sma(10);
|
||||
@@ -151,7 +127,7 @@ sma.Update(new TValue(time + 1, 101.2), isNew: true);
|
||||
|
||||
### Handling Invalid Values (NaN/Infinity)
|
||||
|
||||
Both `Sma` and `SmaVector` use **last-value substitution** for handling invalid inputs:
|
||||
`Sma` uses **last-value substitution** for handling invalid inputs:
|
||||
|
||||
```csharp
|
||||
var sma = new Sma(10);
|
||||
|
||||
@@ -1,371 +0,0 @@
|
||||
|
||||
namespace QuanTAlib.Tests;
|
||||
|
||||
public class SmaVectorTests
|
||||
{
|
||||
[Fact]
|
||||
public void Initialization_WithPeriods_Works()
|
||||
{
|
||||
int[] periods = { 5, 10, 20 };
|
||||
var smaVector = new SmaVector(periods);
|
||||
|
||||
var res = smaVector.Update(new TValue(DateTime.UtcNow, 100.0));
|
||||
|
||||
Assert.Equal(3, res.Length);
|
||||
Assert.Equal(100.0, res[0].Value, 1e-9);
|
||||
Assert.Equal(100.0, res[1].Value, 1e-9);
|
||||
Assert.Equal(100.0, res[2].Value, 1e-9);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Initialization_WithZeroPeriod_ThrowsArgumentException()
|
||||
{
|
||||
int[] periods = { 10, 0, 20 };
|
||||
|
||||
Assert.Throws<ArgumentOutOfRangeException>(() => new SmaVector(periods));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Initialization_WithNegativePeriod_ThrowsArgumentException()
|
||||
{
|
||||
int[] periods = { 10, -5, 20 };
|
||||
|
||||
Assert.Throws<ArgumentOutOfRangeException>(() => new SmaVector(periods));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Calc_Streaming_MatchesSingleSma()
|
||||
{
|
||||
int[] periods = { 5, 10, 20 };
|
||||
var smaVector = new SmaVector(periods);
|
||||
var smaSingles = periods.Select(p => new Sma(p)).ToArray();
|
||||
|
||||
var values = new double[] { 10, 20, 30, 40, 50, 40, 30, 20, 10 };
|
||||
var time = DateTime.UtcNow;
|
||||
|
||||
foreach (var val in values)
|
||||
{
|
||||
var tVal = new TValue(time, val);
|
||||
var multiRes = smaVector.Update(tVal);
|
||||
|
||||
for (int i = 0; i < periods.Length; i++)
|
||||
{
|
||||
var singleRes = smaSingles[i].Update(tVal);
|
||||
Assert.Equal(singleRes.Value, multiRes[i].Value, 1e-9);
|
||||
Assert.Equal(singleRes.Time, multiRes[i].Time);
|
||||
}
|
||||
|
||||
time = time.AddMinutes(1);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Calc_Series_MatchesSingleSma()
|
||||
{
|
||||
int[] periods = { 5, 10, 20 };
|
||||
var smaVector = new SmaVector(periods);
|
||||
|
||||
int len = 100;
|
||||
var t = new System.Collections.Generic.List<long>(len);
|
||||
var v = new System.Collections.Generic.List<double>(len);
|
||||
var now = DateTime.UtcNow;
|
||||
|
||||
for (int i = 0; i < len; i++)
|
||||
{
|
||||
t.Add(now.AddMinutes(i).Ticks);
|
||||
v.Add(Math.Sin(i * 0.1) * 100);
|
||||
}
|
||||
|
||||
var series = new TSeries(t, v);
|
||||
|
||||
var multiRes = smaVector.Calculate(series);
|
||||
|
||||
// Reset and recalculate for comparison
|
||||
var smaSingles = periods.Select(p => new Sma(p)).ToArray();
|
||||
for (int j = 0; j < len; j++)
|
||||
{
|
||||
var tVal = new TValue(new DateTime(t[j], DateTimeKind.Utc), v[j]);
|
||||
for (int i = 0; i < periods.Length; i++)
|
||||
{
|
||||
var singleRes = smaSingles[i].Update(tVal);
|
||||
Assert.Equal(singleRes.Value, multiRes[i].Values[j], 1e-8);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Calc_Series_MatchesStreaming()
|
||||
{
|
||||
int[] periods = { 5, 10, 20 };
|
||||
var smaVectorBatch = new SmaVector(periods);
|
||||
var smaVectorStream = new SmaVector(periods);
|
||||
|
||||
int len = 100;
|
||||
var t = new System.Collections.Generic.List<long>(len);
|
||||
var v = new System.Collections.Generic.List<double>(len);
|
||||
var now = DateTime.UtcNow;
|
||||
|
||||
for (int i = 0; i < len; i++)
|
||||
{
|
||||
t.Add(now.AddMinutes(i).Ticks);
|
||||
v.Add(Math.Sin(i * 0.1) * 100);
|
||||
}
|
||||
|
||||
var series = new TSeries(t, v);
|
||||
|
||||
var batchRes = smaVectorBatch.Calculate(series);
|
||||
|
||||
for (int i = 0; i < len; i++)
|
||||
{
|
||||
var tVal = new TValue(new DateTime(t[i], DateTimeKind.Utc), v[i]);
|
||||
var streamRes = smaVectorStream.Update(tVal);
|
||||
|
||||
for (int j = 0; j < periods.Length; j++)
|
||||
{
|
||||
Assert.Equal(batchRes[j].Values[i], streamRes[j].Value, 1e-9);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Calculate_Static_MatchesInstanceMethod()
|
||||
{
|
||||
int[] periods = { 5, 10, 20 };
|
||||
|
||||
int len = 50;
|
||||
var t = new System.Collections.Generic.List<long>(len);
|
||||
var v = new System.Collections.Generic.List<double>(len);
|
||||
var now = DateTime.UtcNow;
|
||||
|
||||
for (int i = 0; i < len; i++)
|
||||
{
|
||||
t.Add(now.AddMinutes(i).Ticks);
|
||||
v.Add(Math.Sin(i * 0.1) * 100);
|
||||
}
|
||||
|
||||
var series = new TSeries(t, v);
|
||||
|
||||
var instanceSma = new SmaVector(periods);
|
||||
var instanceRes = instanceSma.Calculate(series);
|
||||
|
||||
var staticRes = SmaVector.Calculate(series, periods);
|
||||
|
||||
for (int i = 0; i < periods.Length; i++)
|
||||
{
|
||||
Assert.Equal(instanceRes[i].Count, staticRes[i].Count);
|
||||
for (int j = 0; j < len; j++)
|
||||
{
|
||||
Assert.Equal(instanceRes[i].Values[j], staticRes[i].Values[j], 1e-9);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Reset_ClearsState()
|
||||
{
|
||||
int[] periods = { 10 };
|
||||
var smaVector = new SmaVector(periods);
|
||||
|
||||
smaVector.Update(new TValue(DateTime.UtcNow, 100.0));
|
||||
smaVector.Update(new TValue(DateTime.UtcNow, 200.0));
|
||||
smaVector.Reset();
|
||||
|
||||
var res = smaVector.Update(new TValue(DateTime.UtcNow, 50.0));
|
||||
|
||||
Assert.Equal(50.0, res[0].Value, 1e-9);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Update_NaN_Input_UsesLastValidValue()
|
||||
{
|
||||
int[] periods = { 10, 20 };
|
||||
var smaVector = new SmaVector(periods);
|
||||
|
||||
smaVector.Update(new TValue(DateTime.UtcNow, 100.0));
|
||||
smaVector.Update(new TValue(DateTime.UtcNow, 110.0));
|
||||
|
||||
var resultAfterNaN = smaVector.Update(new TValue(DateTime.UtcNow, double.NaN));
|
||||
|
||||
foreach (var result in resultAfterNaN)
|
||||
{
|
||||
Assert.True(double.IsFinite(result.Value), $"Expected finite value but got {result.Value}");
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Update_Infinity_Input_UsesLastValidValue()
|
||||
{
|
||||
int[] periods = { 10, 20 };
|
||||
var smaVector = new SmaVector(periods);
|
||||
|
||||
smaVector.Update(new TValue(DateTime.UtcNow, 100.0));
|
||||
smaVector.Update(new TValue(DateTime.UtcNow, 110.0));
|
||||
|
||||
var resultAfterPosInf = smaVector.Update(new TValue(DateTime.UtcNow, double.PositiveInfinity));
|
||||
foreach (var result in resultAfterPosInf)
|
||||
{
|
||||
Assert.True(double.IsFinite(result.Value));
|
||||
}
|
||||
|
||||
var resultAfterNegInf = smaVector.Update(new TValue(DateTime.UtcNow, double.NegativeInfinity));
|
||||
foreach (var result in resultAfterNegInf)
|
||||
{
|
||||
Assert.True(double.IsFinite(result.Value));
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Update_MultipleNaN_ContinuesWithLastValid()
|
||||
{
|
||||
int[] periods = { 5, 10 };
|
||||
var smaVector = new SmaVector(periods);
|
||||
|
||||
smaVector.Update(new TValue(DateTime.UtcNow, 100.0));
|
||||
smaVector.Update(new TValue(DateTime.UtcNow, 110.0));
|
||||
smaVector.Update(new TValue(DateTime.UtcNow, 120.0));
|
||||
|
||||
var r1 = smaVector.Update(new TValue(DateTime.UtcNow, double.NaN));
|
||||
var r2 = smaVector.Update(new TValue(DateTime.UtcNow, double.NaN));
|
||||
var r3 = smaVector.Update(new TValue(DateTime.UtcNow, double.NaN));
|
||||
|
||||
foreach (var result in r1) Assert.True(double.IsFinite(result.Value));
|
||||
foreach (var result in r2) Assert.True(double.IsFinite(result.Value));
|
||||
foreach (var result in r3) Assert.True(double.IsFinite(result.Value));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Calculate_Series_HandlesNaN()
|
||||
{
|
||||
int[] periods = { 5, 10 };
|
||||
var smaVector = new SmaVector(periods);
|
||||
|
||||
var t = new System.Collections.Generic.List<long>();
|
||||
var v = new System.Collections.Generic.List<double>();
|
||||
var now = DateTime.UtcNow;
|
||||
|
||||
t.Add(now.Ticks); v.Add(100.0);
|
||||
t.Add(now.AddMinutes(1).Ticks); v.Add(110.0);
|
||||
t.Add(now.AddMinutes(2).Ticks); v.Add(double.NaN);
|
||||
t.Add(now.AddMinutes(3).Ticks); v.Add(120.0);
|
||||
t.Add(now.AddMinutes(4).Ticks); v.Add(double.PositiveInfinity);
|
||||
t.Add(now.AddMinutes(5).Ticks); v.Add(130.0);
|
||||
|
||||
var series = new TSeries(t, v);
|
||||
var results = smaVector.Calculate(series);
|
||||
|
||||
foreach (var periodResults in results)
|
||||
{
|
||||
foreach (var val in periodResults.Values)
|
||||
{
|
||||
Assert.True(double.IsFinite(val), $"Expected finite value but got {val}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Reset_ClearsLastValidValue()
|
||||
{
|
||||
int[] periods = { 10 };
|
||||
var smaVector = new SmaVector(periods);
|
||||
|
||||
smaVector.Update(new TValue(DateTime.UtcNow, 100.0));
|
||||
smaVector.Update(new TValue(DateTime.UtcNow, double.NaN));
|
||||
|
||||
smaVector.Reset();
|
||||
|
||||
var result = smaVector.Update(new TValue(DateTime.UtcNow, 50.0));
|
||||
Assert.Equal(50.0, result[0].Value, 1e-9);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void NaN_Handling_MatchesSingleSma()
|
||||
{
|
||||
int[] periods = { 5, 10, 20 };
|
||||
var smaVector = new SmaVector(periods);
|
||||
var smaSingles = periods.Select(p => new Sma(p)).ToArray();
|
||||
|
||||
var values = new double[] { 10, 20, double.NaN, 40, double.PositiveInfinity, 60, 70 };
|
||||
var time = DateTime.UtcNow;
|
||||
|
||||
foreach (var val in values)
|
||||
{
|
||||
var tVal = new TValue(time, val);
|
||||
var multiRes = smaVector.Update(tVal);
|
||||
|
||||
for (int i = 0; i < periods.Length; i++)
|
||||
{
|
||||
var singleRes = smaSingles[i].Update(tVal);
|
||||
Assert.Equal(singleRes.Value, multiRes[i].Value, 1e-9);
|
||||
}
|
||||
|
||||
time = time.AddMinutes(1);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Values_Property_UpdatesAfterUpdate()
|
||||
{
|
||||
int[] periods = { 5, 10 };
|
||||
var smaVector = new SmaVector(periods);
|
||||
|
||||
var result = smaVector.Update(new TValue(DateTime.UtcNow, 100.0));
|
||||
|
||||
Assert.Equal(result[0].Value, smaVector.Values[0].Value);
|
||||
Assert.Equal(result[1].Value, smaVector.Values[1].Value);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Values_Property_UpdatesAfterCalculate()
|
||||
{
|
||||
int[] periods = { 5, 10 };
|
||||
var smaVector = new SmaVector(periods);
|
||||
|
||||
var t = new System.Collections.Generic.List<long> { 100, 200, 300 };
|
||||
var v = new System.Collections.Generic.List<double> { 10.0, 20.0, 30.0 };
|
||||
var series = new TSeries(t, v);
|
||||
|
||||
var results = smaVector.Calculate(series);
|
||||
|
||||
Assert.Equal(results[0].Last.Value, smaVector.Values[0].Value, 1e-9);
|
||||
Assert.Equal(results[1].Last.Value, smaVector.Values[1].Value, 1e-9);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Update_BarCorrection_WorksCorrectly()
|
||||
{
|
||||
int[] periods = { 3 };
|
||||
var smaVector = new SmaVector(periods);
|
||||
|
||||
smaVector.Update(new TValue(DateTime.UtcNow, 10.0), isNew: true);
|
||||
smaVector.Update(new TValue(DateTime.UtcNow, 20.0), isNew: true);
|
||||
smaVector.Update(new TValue(DateTime.UtcNow, 30.0), isNew: true);
|
||||
|
||||
var res1 = smaVector.Values[0].Value;
|
||||
Assert.Equal(20.0, res1, 1e-9); // (10+20+30)/3 = 20
|
||||
|
||||
// Correct the last bar
|
||||
var res2 = smaVector.Update(new TValue(DateTime.UtcNow, 60.0), isNew: false);
|
||||
|
||||
Assert.Equal(30.0, res2[0].Value, 1e-9); // (10+20+60)/3 = 30
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SMA_MatchesExpectedValues()
|
||||
{
|
||||
int[] periods = { 3 };
|
||||
var smaVector = new SmaVector(periods);
|
||||
|
||||
// Test sequence: 10, 20, 30, 40, 50
|
||||
// Expected SMA(3): 10, 15, 20, 30, 40
|
||||
var expected = new double[] { 10, 15, 20, 30, 40 };
|
||||
var values = new double[] { 10, 20, 30, 40, 50 };
|
||||
var time = DateTime.UtcNow;
|
||||
|
||||
for (int i = 0; i < values.Length; i++)
|
||||
{
|
||||
var res = smaVector.Update(new TValue(time, values[i]));
|
||||
Assert.Equal(expected[i], res[0].Value, 1e-9);
|
||||
time = time.AddMinutes(1);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,185 +0,0 @@
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace QuanTAlib;
|
||||
|
||||
/// <summary>
|
||||
/// Multi-Period Simple Moving Average (SMA) - SIMD optimized.
|
||||
/// Calculates multiple SMAs with different periods for the same input series in parallel.
|
||||
/// Uses last-value substitution for invalid inputs (NaN/Infinity).
|
||||
/// </summary>
|
||||
[SkipLocalsInit]
|
||||
public class SmaVector
|
||||
{
|
||||
private readonly RingBuffer[] _buffers;
|
||||
private readonly RingBuffer[] _p_buffers; // Previous state for bar correction
|
||||
private readonly int _count;
|
||||
private double _lastValidValue;
|
||||
|
||||
/// <summary>
|
||||
/// Current SMA values for all periods.
|
||||
/// </summary>
|
||||
public ReadOnlySpan<TValue> Values => _values;
|
||||
|
||||
private readonly TValue[] _values;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes SmaVector with specified periods.
|
||||
/// </summary>
|
||||
/// <param name="periods">Array of periods (each must be > 0)</param>
|
||||
public SmaVector(int[] periods)
|
||||
{
|
||||
_count = periods.Length;
|
||||
_buffers = new RingBuffer[_count];
|
||||
_p_buffers = new RingBuffer[_count];
|
||||
_values = new TValue[_count];
|
||||
|
||||
for (int i = 0; i < _count; i++)
|
||||
{
|
||||
ArgumentOutOfRangeException.ThrowIfLessThanOrEqual(periods[i], 0);
|
||||
_buffers[i] = new RingBuffer(periods[i]);
|
||||
_p_buffers[i] = new RingBuffer(periods[i]);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets a valid input value, using last-value substitution for non-finite inputs.
|
||||
/// </summary>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
private double GetValidValue(double input)
|
||||
{
|
||||
if (double.IsFinite(input))
|
||||
{
|
||||
_lastValidValue = input;
|
||||
return input;
|
||||
}
|
||||
return _lastValidValue;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Resets all SMA states.
|
||||
/// </summary>
|
||||
public void Reset()
|
||||
{
|
||||
for (int i = 0; i < _count; i++)
|
||||
{
|
||||
_buffers[i].Clear();
|
||||
_p_buffers[i].Clear();
|
||||
}
|
||||
_lastValidValue = 0;
|
||||
Array.Clear(_values);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Updates SMAs with the given value.
|
||||
/// Uses last-value substitution: invalid inputs (NaN/Infinity) are replaced with
|
||||
/// the last known good value, providing continuity in the output series.
|
||||
/// </summary>
|
||||
/// <param name="input">Input value</param>
|
||||
/// <param name="isNew">True for new bar, false for update to current bar (default: true)</param>
|
||||
/// <returns>Array of SMA values</returns>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public TValue[] Update(TValue input, bool isNew = true)
|
||||
{
|
||||
if (isNew)
|
||||
{
|
||||
// Save current state for potential bar correction
|
||||
for (int i = 0; i < _count; i++)
|
||||
{
|
||||
_p_buffers[i].CopyFrom(_buffers[i]);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// Restore previous state for bar correction
|
||||
for (int i = 0; i < _count; i++)
|
||||
{
|
||||
_buffers[i].CopyFrom(_p_buffers[i]);
|
||||
}
|
||||
}
|
||||
|
||||
// Last-value substitution: replace non-finite inputs with last valid value
|
||||
double val = GetValidValue(input.Value);
|
||||
|
||||
// Update each buffer and calculate SMA
|
||||
for (int i = 0; i < _count; i++)
|
||||
{
|
||||
_buffers[i].Add(val);
|
||||
_values[i] = new TValue(input.Time, _buffers[i].Average);
|
||||
}
|
||||
|
||||
return _values;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Calculates SMAs for the entire series.
|
||||
/// </summary>
|
||||
/// <param name="source">Input series</param>
|
||||
/// <returns>Array of SMA series</returns>
|
||||
public TSeries[] Calculate(TSeries source)
|
||||
{
|
||||
int len = source.Count;
|
||||
var resultSeries = new TSeries[_count];
|
||||
|
||||
// Reset state for fresh calculation
|
||||
for (int i = 0; i < _count; i++)
|
||||
{
|
||||
_buffers[i].Clear();
|
||||
}
|
||||
_lastValidValue = 0;
|
||||
|
||||
// Pre-allocate lists
|
||||
var tLists = new List<long>[_count];
|
||||
var vLists = new List<double>[_count];
|
||||
|
||||
for (int i = 0; i < _count; i++)
|
||||
{
|
||||
tLists[i] = new List<long>(len);
|
||||
vLists[i] = new List<double>(len);
|
||||
CollectionsMarshal.SetCount(tLists[i], len);
|
||||
CollectionsMarshal.SetCount(vLists[i], len);
|
||||
}
|
||||
|
||||
var sourceValues = source.Values;
|
||||
var sourceTimes = source.Times;
|
||||
|
||||
for (int t = 0; t < len; t++)
|
||||
{
|
||||
double val = sourceValues[t];
|
||||
long time = sourceTimes[t];
|
||||
|
||||
// Last-value substitution: replace non-finite inputs with last valid value
|
||||
val = GetValidValue(val);
|
||||
|
||||
for (int i = 0; i < _count; i++)
|
||||
{
|
||||
_buffers[i].Add(val);
|
||||
CollectionsMarshal.AsSpan(tLists[i])[t] = time;
|
||||
CollectionsMarshal.AsSpan(vLists[i])[t] = _buffers[i].Average;
|
||||
}
|
||||
}
|
||||
|
||||
// Create TSeries and update Values
|
||||
for (int i = 0; i < _count; i++)
|
||||
{
|
||||
resultSeries[i] = new TSeries(tLists[i], vLists[i]);
|
||||
var lastT = CollectionsMarshal.AsSpan(tLists[i])[len - 1];
|
||||
var lastV = CollectionsMarshal.AsSpan(vLists[i])[len - 1];
|
||||
_values[i] = new TValue(lastT, lastV);
|
||||
}
|
||||
|
||||
return resultSeries;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Calculates SMAs for the entire series using specified periods.
|
||||
/// </summary>
|
||||
/// <param name="source">Input series</param>
|
||||
/// <param name="periods">Array of periods</param>
|
||||
/// <returns>Array of SMA series</returns>
|
||||
public static TSeries[] Calculate(TSeries source, int[] periods)
|
||||
{
|
||||
var smaVector = new SmaVector(periods);
|
||||
return smaVector.Calculate(source);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user