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:
Miha Kralj
2026-01-27 23:48:33 -08:00
parent 8ac15f1efa
commit a9e72dae0d
29 changed files with 464 additions and 114 deletions
+22 -2
View File
@@ -31,8 +31,8 @@ public class VwapsdIndicator : Indicator, IWatchlistIndicator
Description = "Volume weighted average price with configurable standard deviation bands";
VwapSeries = new("VWAP", Color.Blue, 2, LineStyle.Solid);
UpperSeries = new($"Upper (+{NumDevs}σ)", Color.Red, 1, LineStyle.Solid);
LowerSeries = new($"Lower (-{NumDevs}σ)", Color.Green, 1, LineStyle.Solid);
UpperSeries = new("Upper", Color.Red, 1, LineStyle.Solid);
LowerSeries = new("Lower", Color.Green, 1, LineStyle.Solid);
WidthSeries = new("Width", Color.Gray, 1, LineStyle.Dot);
AddLineSeries(VwapSeries);
@@ -47,9 +47,29 @@ public class VwapsdIndicator : Indicator, IWatchlistIndicator
protected override void OnInit()
{
vwapsd = new(NumDevs);
if (UpperSeries != null)
{
UpperSeries.Name = $"Upper (+{NumDevs:F1}σ)";
}
if (LowerSeries != null)
{
LowerSeries.Name = $"Lower (-{NumDevs:F1}σ)";
}
base.OnInit();
}
private void UpdateSeriesNames()
{
if (UpperSeries != null)
{
UpperSeries.Name = $"Upper (+{NumDevs:F1}σ)";
}
if (LowerSeries != null)
{
LowerSeries.Name = $"Lower (-{NumDevs:F1}σ)";
}
}
protected override void OnUpdate(UpdateArgs args)
{
var item = HistoricalData[0, SeekOriginHistory.End];
+6 -4
View File
@@ -467,22 +467,23 @@ public class VwapsdTests
double[] upper = new double[5];
double[] lower = new double[5];
double[] vwap = new double[5];
double[] stdDev = new double[5];
double[] wrongSize = new double[3];
// NumDevs must be >= MinNumDevs
Assert.Throws<ArgumentOutOfRangeException>(() =>
Vwapsd.Calculate(price.AsSpan(), volume.AsSpan(),
upper.AsSpan(), lower.AsSpan(), vwap.AsSpan(), 0));
upper.AsSpan(), lower.AsSpan(), vwap.AsSpan(), stdDev.AsSpan(), 0));
// NumDevs must be <= MaxNumDevs
Assert.Throws<ArgumentOutOfRangeException>(() =>
Vwapsd.Calculate(price.AsSpan(), volume.AsSpan(),
upper.AsSpan(), lower.AsSpan(), vwap.AsSpan(), 6.0));
upper.AsSpan(), lower.AsSpan(), vwap.AsSpan(), stdDev.AsSpan(), 6.0));
// All arrays must be same length
Assert.Throws<ArgumentException>(() =>
Vwapsd.Calculate(price.AsSpan(), volume.AsSpan(),
wrongSize.AsSpan(), lower.AsSpan(), vwap.AsSpan(), 1.0));
wrongSize.AsSpan(), lower.AsSpan(), vwap.AsSpan(), stdDev.AsSpan(), 1.0));
}
[Fact]
@@ -493,9 +494,10 @@ public class VwapsdTests
double[] upper = new double[5];
double[] lower = new double[5];
double[] vwap = new double[5];
double[] stdDev = new double[5];
Vwapsd.Calculate(price.AsSpan(), volume.AsSpan(),
upper.AsSpan(), lower.AsSpan(), vwap.AsSpan(), 1.0);
upper.AsSpan(), lower.AsSpan(), vwap.AsSpan(), stdDev.AsSpan(), 1.0);
foreach (var val in vwap)
{
@@ -118,10 +118,11 @@ public sealed class VwapsdValidationTests : IDisposable
double[] spanVwap = new double[bars.Count];
double[] spanUpper = new double[bars.Count];
double[] spanLower = new double[bars.Count];
double[] spanStdDev = new double[bars.Count];
Vwapsd.Calculate(price.AsSpan(), volume.AsSpan(),
spanUpper.AsSpan(), spanLower.AsSpan(),
spanVwap.AsSpan(), numDevs);
spanVwap.AsSpan(), spanStdDev.AsSpan(), numDevs);
// Compare last 100 values
int compareCount = Math.Min(100, bars.Count - 2);
+10 -1
View File
@@ -337,16 +337,24 @@ public sealed class Vwapsd : AbstractBase
/// <summary>
/// Calculates VWAP SD Bands using span arrays.
/// </summary>
/// <param name="price">Source price values (typically HLC3)</param>
/// <param name="volume">Volume values</param>
/// <param name="upper">Output span for upper band</param>
/// <param name="lower">Output span for lower band</param>
/// <param name="vwap">Output span for VWAP values</param>
/// <param name="stdDev">Output span for standard deviation values</param>
/// <param name="numDevs">Number of standard deviations for bands (default 2.0)</param>
public static void Calculate(
ReadOnlySpan<double> price,
ReadOnlySpan<double> volume,
Span<double> upper,
Span<double> lower,
Span<double> vwap,
Span<double> stdDev,
double numDevs = DefaultNumDevs)
{
int len = price.Length;
if (len != volume.Length || len != upper.Length || len != lower.Length || len != vwap.Length)
if (len != volume.Length || len != upper.Length || len != lower.Length || len != vwap.Length || len != stdDev.Length)
{
throw new ArgumentException("All spans must have the same length.", nameof(price));
}
@@ -396,6 +404,7 @@ public sealed class Vwapsd : AbstractBase
double stdev = Math.Sqrt(variance);
vwap[i] = vwapVal;
stdDev[i] = stdev;
upper[i] = vwapVal + numDevs * stdev;
lower[i] = vwapVal - numDevs * stdev;
}