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
+9 -8
View File
@@ -309,17 +309,18 @@ public class MdapeTests
}
[Fact]
public void Calculate_ZeroActual_ReturnsZeroError()
public void Calculate_ZeroActual_UsesSubstituteValue()
{
// When actual is zero or near-zero, should return 0 error (epsilon protection)
// When actual is zero or near-zero, implementation substitutes 1.0 fallback
// to avoid division by zero (epsilon protection means substitute, not return 0)
var mdape = new Mdape(3);
mdape.Update(0.0, 10);
mdape.Update(0.0, 20);
mdape.Update(0.0, 30);
mdape.Update(0.0, 10); // actual=1.0 (substituted), pred=10 → |1-10|/1 * 100 = 900%
mdape.Update(0.0, 20); // actual=1.0 (substituted), pred=20 → |1-20|/1 * 100 = 1900%
mdape.Update(0.0, 30); // actual=1.0 (substituted), pred=30 → |1-30|/1 * 100 = 2900%
// With epsilon protection, all errors are 0
Assert.Equal(0.0, mdape.Last.Value, Precision);
// Median of [900, 1900, 2900] = 1900
Assert.Equal(1900.0, mdape.Last.Value, Precision);
}
[Fact]
@@ -361,4 +362,4 @@ public class MdapeTests
Assert.Equal(mdape1.Last.Value, mdape2.Last.Value, Precision);
}
}
}
+7 -4
View File
@@ -74,9 +74,12 @@ public sealed class Mdape : AbstractBase
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private TValue UpdateCore(DateTime time, double actualVal, double predictedVal, bool isNew)
{
if (!double.IsFinite(actualVal))
// Validate actual: must be finite AND have sufficient magnitude (matches Batch logic)
if (!double.IsFinite(actualVal) || Math.Abs(actualVal) < 1e-10)
{
actualVal = double.IsFinite(_state.LastValidActual) ? _state.LastValidActual : 1.0;
actualVal = double.IsFinite(_state.LastValidActual) && Math.Abs(_state.LastValidActual) >= 1e-10
? _state.LastValidActual
: 1.0;
}
else
{
@@ -92,10 +95,10 @@ public sealed class Mdape : AbstractBase
_state.LastValidPredicted = predictedVal;
}
// Calculate absolute percentage error
// Calculate absolute percentage error (absActual guaranteed >= 1e-10 by validation above)
double absActual = Math.Abs(actualVal);
double absError = Math.Abs(actualVal - predictedVal);
double percentageError = absActual > 1e-10 ? (absError / absActual) * 100.0 : 0.0;
double percentageError = (absError / absActual) * 100.0;
if (isNew)
{