mirror of
https://github.com/mihakralj/QuanTAlib.git
synced 2026-08-26 06:18:05 +00:00
Refactor and enhance various channel indicators for improved performance and stability
- Updated Codacy instructions to streamline usage guidelines. - Refactored Bbands class to utilize ArrayPool for memory management, preventing stack overflow on large series. - Changed Fcb class to use long for monotonic deques to avoid truncation issues. - Enhanced Kchannel class to ensure safe defaults for non-finite values. - Improved Maenv class to prevent double-priming during calculations. - Modified Mmchannel class to ensure non-negative buffer indices and removed unnecessary state tracking. - Updated Pchannel class to correctly reference IsHot state. - Refined Regchannel class to avoid double-processing during calculations. - Enhanced Starchannel class to sanitize non-finite values during calculations. - Adjusted Stbands.Quantower.cs to allow finer control over multiplier precision. - Updated Ubands class to only update last valid values on new bars. - Modified Uchannel.Quantower.cs to allow for finer multiplier precision. - Enhanced Vwapbands classes to include standard deviation calculations and ensure consistent array lengths. - Refactored Vwapsd classes to include standard deviation outputs and ensure consistent array lengths. - Updated MonotonicDeque to use long for indices to prevent overflow. - Improved Mdape class to handle zero actual values with a substitute value for error calculation. - Enhanced Rae class to ensure correct state management during updates. - Refined Wmape class to simplify the logic for finding last valid actual and predicted values. - Updated Cmf.Quantower classes to ensure MinHistoryDepths reflects the current period.
This commit is contained in:
@@ -441,17 +441,18 @@ public class VwapbandsTests
|
||||
double[] upper2 = new double[5];
|
||||
double[] lower2 = new double[5];
|
||||
double[] vwap = new double[5];
|
||||
double[] stdDev = new double[5];
|
||||
double[] wrongSize = new double[3];
|
||||
|
||||
// Multiplier must be >= MinMultiplier
|
||||
Assert.Throws<ArgumentOutOfRangeException>(() =>
|
||||
Vwapbands.Calculate(price.AsSpan(), volume.AsSpan(),
|
||||
upper1.AsSpan(), lower1.AsSpan(), upper2.AsSpan(), lower2.AsSpan(), vwap.AsSpan(), 0));
|
||||
upper1.AsSpan(), lower1.AsSpan(), upper2.AsSpan(), lower2.AsSpan(), vwap.AsSpan(), stdDev.AsSpan(), 0));
|
||||
|
||||
// All arrays must be same length
|
||||
Assert.Throws<ArgumentException>(() =>
|
||||
Vwapbands.Calculate(price.AsSpan(), volume.AsSpan(),
|
||||
wrongSize.AsSpan(), lower1.AsSpan(), upper2.AsSpan(), lower2.AsSpan(), vwap.AsSpan(), 1.0));
|
||||
wrongSize.AsSpan(), lower1.AsSpan(), upper2.AsSpan(), lower2.AsSpan(), vwap.AsSpan(), stdDev.AsSpan(), 1.0));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
@@ -464,9 +465,10 @@ public class VwapbandsTests
|
||||
double[] upper2 = new double[5];
|
||||
double[] lower2 = new double[5];
|
||||
double[] vwap = new double[5];
|
||||
double[] stdDev = new double[5];
|
||||
|
||||
Vwapbands.Calculate(price.AsSpan(), volume.AsSpan(),
|
||||
upper1.AsSpan(), lower1.AsSpan(), upper2.AsSpan(), lower2.AsSpan(), vwap.AsSpan(), 1.0);
|
||||
upper1.AsSpan(), lower1.AsSpan(), upper2.AsSpan(), lower2.AsSpan(), vwap.AsSpan(), stdDev.AsSpan(), 1.0);
|
||||
|
||||
foreach (var val in vwap)
|
||||
{
|
||||
|
||||
@@ -124,11 +124,12 @@ public sealed class VwapbandsValidationTests : IDisposable
|
||||
double[] spanLower1 = new double[bars.Count];
|
||||
double[] spanUpper2 = new double[bars.Count];
|
||||
double[] spanLower2 = new double[bars.Count];
|
||||
double[] spanStdDev = new double[bars.Count];
|
||||
|
||||
Vwapbands.Calculate(price.AsSpan(), volume.AsSpan(),
|
||||
spanUpper1.AsSpan(), spanLower1.AsSpan(),
|
||||
spanUpper2.AsSpan(), spanLower2.AsSpan(),
|
||||
spanVwap.AsSpan(), multiplier);
|
||||
spanVwap.AsSpan(), spanStdDev.AsSpan(), multiplier);
|
||||
|
||||
// Compare last 100 values
|
||||
int compareCount = Math.Min(100, bars.Count - 2);
|
||||
|
||||
@@ -351,6 +351,15 @@ public sealed class Vwapbands : AbstractBase
|
||||
/// <summary>
|
||||
/// Calculates VWAP Bands using span arrays.
|
||||
/// </summary>
|
||||
/// <param name="price">Source price values (typically HLC3)</param>
|
||||
/// <param name="volume">Volume values</param>
|
||||
/// <param name="upper1">Output span for upper band at 1σ</param>
|
||||
/// <param name="lower1">Output span for lower band at 1σ</param>
|
||||
/// <param name="upper2">Output span for upper band at 2σ</param>
|
||||
/// <param name="lower2">Output span for lower band at 2σ</param>
|
||||
/// <param name="vwap">Output span for VWAP values</param>
|
||||
/// <param name="stdDev">Output span for standard deviation values</param>
|
||||
/// <param name="multiplier">Band multiplier (default 1.0)</param>
|
||||
public static void Calculate(
|
||||
ReadOnlySpan<double> price,
|
||||
ReadOnlySpan<double> volume,
|
||||
@@ -359,11 +368,12 @@ public sealed class Vwapbands : AbstractBase
|
||||
Span<double> upper2,
|
||||
Span<double> lower2,
|
||||
Span<double> vwap,
|
||||
Span<double> stdDev,
|
||||
double multiplier = DefaultMultiplier)
|
||||
{
|
||||
int len = price.Length;
|
||||
if (len != volume.Length || len != upper1.Length || len != lower1.Length ||
|
||||
len != upper2.Length || len != lower2.Length || len != vwap.Length)
|
||||
len != upper2.Length || len != lower2.Length || len != vwap.Length || len != stdDev.Length)
|
||||
{
|
||||
throw new ArgumentException("All spans must have the same length.", nameof(price));
|
||||
}
|
||||
@@ -408,6 +418,7 @@ public sealed class Vwapbands : AbstractBase
|
||||
double stdev = Math.Sqrt(variance);
|
||||
|
||||
vwap[i] = vwapVal;
|
||||
stdDev[i] = stdev;
|
||||
upper1[i] = vwapVal + multiplier * stdev;
|
||||
lower1[i] = vwapVal - multiplier * stdev;
|
||||
upper2[i] = vwapVal + 2.0 * multiplier * stdev;
|
||||
|
||||
Reference in New Issue
Block a user