mirror of
https://github.com/mihakralj/QuanTAlib.git
synced 2026-08-23 21:18: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:
+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];
|
||||
|
||||
Reference in New Issue
Block a user