Skip to content

Commit f582db2

Browse files
committed
fixes
1 parent 5b333bd commit f582db2

23 files changed

+85
-137
lines changed

.sonarlint/QuanTAlib.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
{
2-
"sonarCloudOrganization": "mihakralj",
2+
"sonarCloudOrganization": "mihakralj-quantalib",
33
"projectKey": "mihakralj_QuanTAlib"
44
}

Tests/test_skender.stock.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,7 @@ public void MAMA()
304304
.GetMama(fastLimit: 0.5, slowLimit: 0.05)
305305
.Select(i => i.Mama.Null2NaN()!);
306306
Assert.Equal(QL.Length, SK.Count());
307-
for (int i = QL.Length - 1; i > 100; i--)
307+
for (int i = QL.Length - 1; i > 500; i--)
308308
{
309309
Assert.InRange(SK.ElementAt(i) - QL[i].Value, -range, range);
310310
}

lib/averages/Convolution.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ private void NormalizeKernel()
9696
}
9797

9898
// Normalize the kernel or set equal weights if the sum is zero
99-
double normalizationFactor = (sum != 0) ? sum : _activeLength;
99+
double normalizationFactor = (sum >= double.Epsilon) ? sum : _activeLength;
100100
double invNormFactor = 1.0 / normalizationFactor;
101101

102102
for (int i = 0; i < _activeLength; i++)

lib/averages/Dwma.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,13 @@ public class Dwma : AbstractBase
2525
{
2626
private readonly Wma _innerWma;
2727
private readonly Wma _outerWma;
28-
private readonly int _period;
2928

3029
public Dwma(int period)
3130
{
3231
if (period < 1)
3332
{
3433
throw new System.ArgumentException("Period must be greater than or equal to 1.", nameof(period));
3534
}
36-
_period = period;
3735
_innerWma = new Wma(period);
3836
_outerWma = new Wma(period);
3937
Name = "Dwma";

lib/averages/Frama.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,14 +82,14 @@ protected override void ManageState(bool isNew)
8282
}
8383

8484
[MethodImpl(MethodImplOptions.AggressiveInlining)]
85-
private void UpdateMinMax(double price, ref double high, ref double low)
85+
private static void UpdateMinMax(double price, ref double high, ref double low)
8686
{
8787
high = System.Math.Max(high, price);
8888
low = System.Math.Min(low, price);
8989
}
9090

9191
[MethodImpl(MethodImplOptions.AggressiveInlining)]
92-
private double CalculateAlpha(double dimension)
92+
private static double CalculateAlpha(double dimension)
9393
{
9494
double alpha = System.Math.Exp(-4.6 * (dimension - 1));
9595
return System.Math.Clamp(alpha, 0.01, 1.0);

lib/averages/Htit.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ protected override double Calculation()
156156
_imBuffer.Add(im, Input.IsNew);
157157

158158
// Calculate period
159-
double pd = (im != 0 && re != 0) ? TWO_PI / System.Math.Atan(im / re) : 0;
159+
double pd = (im >= double.Epsilon && re >= double.Epsilon) ? TWO_PI / System.Math.Atan(im / re) : 0;
160160
pd = ClampPeriod(pd, _lastPd);
161161
pd = (ALPHA * pd) + (BETA * _lastPd);
162162
_pdBuffer.Add(pd, Input.IsNew);

lib/averages/Jma.cs

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,12 @@ namespace QuanTAlib;
2727
/// </remarks>
2828
public class Jma : AbstractBase
2929
{
30-
private readonly double _period;
3130
private readonly double _phase;
3231
private readonly CircularBuffer _vsumBuff;
3332
private readonly CircularBuffer _avoltyBuff;
3433
private readonly double _beta;
3534
private readonly double _len1;
3635
private readonly double _pow1;
37-
private readonly double _oneMinusAlpha;
3836
private readonly double _oneMinusAlphaSquared;
3937
private readonly double _alphaSquared;
4038

@@ -55,19 +53,18 @@ public Jma(int period, int phase = 0, double factor = 0.45, int buffer = 10)
5553
throw new System.ArgumentOutOfRangeException(nameof(period), "Period must be greater than or equal to 1.");
5654
}
5755
Factor = factor;
58-
_period = period;
59-
_phase = System.Math.Clamp((phase * 0.01) + 1.5, 0.5, 2.5);
56+
_phase = Math.Clamp((phase * 0.01) + 1.5, 0.5, 2.5);
6057

6158
_vsumBuff = new CircularBuffer(buffer);
6259
_avoltyBuff = new CircularBuffer(65);
6360
_beta = factor * (period - 1) / ((factor * (period - 1)) + 2);
6461

65-
_len1 = System.Math.Max((System.Math.Log(System.Math.Sqrt(period - 1)) / System.Math.Log(2.0)) + 2.0, 0);
66-
_pow1 = System.Math.Max(_len1 - 2.0, 0.5);
62+
_len1 = Math.Max((Math.Log(Math.Sqrt(period - 1)) / Math.Log(2.0)) + 2.0, 0);
63+
_pow1 = Math.Max(_len1 - 2.0, 0.5);
6764

6865
// Precalculate constants for alpha-based calculations
69-
double alpha = System.Math.Pow(_beta, _pow1);
70-
_oneMinusAlpha = 1.0 - alpha;
66+
double alpha = Math.Pow(_beta, _pow1);
67+
double _oneMinusAlpha = 1.0 - alpha;
7168
_oneMinusAlphaSquared = _oneMinusAlpha * _oneMinusAlpha;
7269
_alphaSquared = alpha * alpha;
7370

@@ -120,7 +117,7 @@ protected override void ManageState(bool isNew)
120117
[MethodImpl(MethodImplOptions.AggressiveInlining)]
121118
private double CalculateVolatility(double price, double del1, double del2)
122119
{
123-
double volty = System.Math.Max(System.Math.Abs(del1), System.Math.Abs(del2));
120+
double volty = Math.Max(Math.Abs(del1), Math.Abs(del2));
124121
_vsumBuff.Add(volty, Input.IsNew);
125122
_vSum += (_vsumBuff[^1] - _vsumBuff[0]) / _vsumBuff.Count;
126123
_avoltyBuff.Add(_vSum, Input.IsNew);
@@ -131,7 +128,7 @@ private double CalculateVolatility(double price, double del1, double del2)
131128
private double CalculateRelativeVolatility(double volty, double avgVolty)
132129
{
133130
double rvolty = (avgVolty > 0) ? volty / avgVolty : 1;
134-
return System.Math.Min(System.Math.Max(rvolty, 1.0), System.Math.Pow(_len1, 1.0 / _pow1));
131+
return Math.Min(Math.Max(rvolty, 1.0), Math.Pow(_len1, 1.0 / _pow1));
135132
}
136133

137134
protected override double Calculation()
@@ -152,13 +149,13 @@ protected override double Calculation()
152149
double avgVolty = _avoltyBuff.Average();
153150

154151
double rvolty = CalculateRelativeVolatility(volty, avgVolty);
155-
double pow2 = System.Math.Pow(rvolty, _pow1);
156-
double Kv = System.Math.Pow(_beta, System.Math.Sqrt(pow2));
152+
double pow2 = Math.Pow(rvolty, _pow1);
153+
double Kv = Math.Pow(_beta, Math.Sqrt(pow2));
157154

158155
_upperBand = (del1 >= 0) ? price : price - (Kv * del1);
159156
_lowerBand = (del2 <= 0) ? price : price - (Kv * del2);
160157

161-
double alpha = System.Math.Pow(_beta, pow2);
158+
double alpha = Math.Pow(_beta, pow2);
162159
double ma1 = price + (alpha * (_prevMa1 - price));
163160
_prevMa1 = ma1;
164161

lib/averages/Kama.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ namespace QuanTAlib;
2727
public class Kama : AbstractBase
2828
{
2929
private readonly int _period;
30-
private readonly double _scFast, _scSlow;
30+
private readonly double _scSlow;
3131
private readonly double _scDiff; // Precalculated (_scFast - _scSlow)
3232
private readonly CircularBuffer _buffer;
3333
private double _lastKama, _p_lastKama;
@@ -43,7 +43,7 @@ public Kama(int period, int fast = 2, int slow = 30)
4343
throw new System.ArgumentException("Period must be greater than or equal to 1.", nameof(period));
4444
}
4545
_period = period;
46-
_scFast = 2.0 / (((period < fast) ? period : fast) + 1);
46+
double _scFast = 2.0 / (((period < fast) ? period : fast) + 1);
4747
_scSlow = 2.0 / (slow + 1);
4848
_scDiff = _scFast - _scSlow;
4949
_buffer = new CircularBuffer(_period + 1);
@@ -97,9 +97,9 @@ private double CalculateVolatility()
9797
}
9898

9999
[MethodImpl(MethodImplOptions.AggressiveInlining)]
100-
private double CalculateEfficiencyRatio(double change, double volatility)
100+
private static double CalculateEfficiencyRatio(double change, double volatility)
101101
{
102-
return volatility != 0 ? change / volatility : 0;
102+
return volatility >= double.Epsilon ? change / volatility : 0;
103103
}
104104

105105
[MethodImpl(MethodImplOptions.AggressiveInlining)]

lib/averages/Maaf.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -142,15 +142,15 @@ protected override double Calculation()
142142
double value1 = GetMedian(length);
143143
value2 = (alpha * (smooth - _prevValue2)) + _prevValue2;
144144

145-
if (value1 != 0)
145+
if (value1 >= double.Epsilon)
146146
{
147-
value3 = System.Math.Abs(value1 - value2) / value1;
147+
value3 = Math.Abs(value1 - value2) / value1;
148148
}
149149

150150
length -= 2;
151151
}
152152

153-
length = System.Math.Max(length, 3);
153+
length = Math.Max(length, 3);
154154
double finalAlpha = CalculateAlpha(length);
155155
double filter = (finalAlpha * (smooth - _prevFilter)) + _prevFilter;
156156

lib/averages/Mama.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ private static double CalculateHilbertTransform(CircularBuffer buffer, double ad
111111
[MethodImpl(MethodImplOptions.AggressiveInlining)]
112112
private double CalculatePeriod(double im, double re)
113113
{
114-
if (im == 0 || re == 0) return _pd[^2];
114+
if (System.Math.Abs(im) <= double.Epsilon || System.Math.Abs(re) <= double.Epsilon) return _pd[^2];
115115
return _twoPi / System.Math.Atan(im / re);
116116
}
117117

@@ -167,7 +167,7 @@ protected override double Calculation()
167167
_pd[^1] = AdjustPeriod(_pd[^1]);
168168

169169
// Phase calculation
170-
double phase = _i1[^1] != 0 ? System.Math.Atan(_q1[^1] / _i1[^1]) * _radToDeg : _ph[^2];
170+
double phase = Math.Abs(_i1[^1]) >= double.Epsilon ? System.Math.Atan(_q1[^1] / _i1[^1]) * _radToDeg : _ph[^2];
171171
_ph.Add(phase, Input.IsNew);
172172

173173
// Adaptive alpha

lib/averages/Mgdi.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ namespace QuanTAlib;
2727
public class Mgdi : AbstractBase
2828
{
2929
private readonly int _period;
30-
private readonly double _kFactor;
3130
private readonly double _kFactorPeriod; // Precalculated k * period
3231
private double _prevMd, _p_prevMd;
3332

@@ -45,7 +44,6 @@ public Mgdi(int period, double kFactor = 0.6)
4544
throw new System.ArgumentOutOfRangeException(nameof(kFactor), "K-Factor must be greater than 0.");
4645
}
4746
_period = period;
48-
_kFactor = kFactor;
4947
_kFactorPeriod = kFactor * period;
5048
Name = "Mgdi";
5149
WarmupPeriod = period;
@@ -85,7 +83,7 @@ protected override void ManageState(bool isNew)
8583
[MethodImpl(MethodImplOptions.AggressiveInlining)]
8684
private double CalculateRatio(double value)
8785
{
88-
return _prevMd != 0 ? value / _prevMd : 1;
86+
return _prevMd >= double.Epsilon ? value / _prevMd : 1;
8987
}
9088

9189
[MethodImpl(MethodImplOptions.AggressiveInlining)]

lib/averages/Tema.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ namespace QuanTAlib;
2727
/// </remarks>
2828
public class Tema : AbstractBase
2929
{
30-
private readonly int _period;
3130
private readonly double _k;
3231
private readonly double _oneMinusK;
3332
private readonly double _epsilon = 1e-10;
@@ -44,8 +43,7 @@ public Tema(int period)
4443
{
4544
throw new System.ArgumentOutOfRangeException(nameof(period), "Period must be greater than or equal to 1.");
4645
}
47-
_period = period;
48-
_k = 2.0 / (_period + 1);
46+
_k = 2.0 / (period + 1);
4947
_oneMinusK = 1.0 - _k;
5048
Name = "Tema";
5149
double percentile = 0.85; //targeting 85th percentile of correctness of converging EMA

lib/averages/Trima.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ namespace QuanTAlib;
2828
public class Trima : AbstractBase
2929
{
3030
private readonly Convolution _convolution;
31-
private readonly double[] _kernel;
3231

3332
/// <param name="period">The number of data points used in the TRIMA calculation.</param>
3433
/// <exception cref="ArgumentException">Thrown when period is less than 1.</exception>
@@ -38,7 +37,7 @@ public Trima(int period)
3837
{
3938
throw new System.ArgumentException("Period must be greater than or equal to 1.", nameof(period));
4039
}
41-
_kernel = GenerateKernel(period);
40+
double[] _kernel = GenerateKernel(period);
4241
_convolution = new Convolution(_kernel);
4342
Name = "Trima";
4443
WarmupPeriod = period;

lib/averages/Wma.cs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,7 @@ namespace QuanTAlib;
2828
/// </remarks>
2929
public class Wma : AbstractBase
3030
{
31-
private readonly int _period;
3231
private readonly Convolution _convolution;
33-
private readonly double[] _kernel;
3432

3533
/// <param name="period">The number of data points used in the WMA calculation.</param>
3634
/// <exception cref="ArgumentException">Thrown when period is less than 1.</exception>
@@ -40,11 +38,10 @@ public Wma(int period)
4038
{
4139
throw new System.ArgumentException("Period must be greater than or equal to 1.", nameof(period));
4240
}
43-
_period = period;
44-
_kernel = GenerateWmaKernel(_period);
41+
double[] _kernel = GenerateWmaKernel(period);
4542
_convolution = new Convolution(_kernel);
4643
Name = "Wma";
47-
WarmupPeriod = _period;
44+
WarmupPeriod = period;
4845
Init();
4946
}
5047

lib/core/abstractBase.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ protected static bool IsValidValue(double value)
4444
/// Creates a new TValue with the current state.
4545
/// </summary>
4646
[MethodImpl(MethodImplOptions.AggressiveInlining)]
47-
protected TValue CreateTValue(System.DateTime time, double value, bool isNew, bool isHot = false)
47+
protected static TValue CreateTValue(System.DateTime time, double value, bool isNew, bool isHot = false)
4848
{
4949
return new TValue(time, value, isNew, isHot);
5050
}

lib/errors/Mapd.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ protected override void ManageState(bool isNew)
8181
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
8282
private static double CalculatePercentageDeviation(double actual, double predicted)
8383
{
84-
return actual != 0 ? Math.Abs((actual - predicted) / actual) : 0;
84+
return actual >= double.Epsilon ? Math.Abs((actual - predicted) / actual) : 0;
8585
}
8686

8787
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]

lib/errors/Mape.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ protected override void ManageState(bool isNew)
8181
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
8282
private static double CalculatePercentageError(double actual, double predicted)
8383
{
84-
return actual != 0 ? Math.Abs((actual - predicted) / actual) : 0;
84+
return actual >= double.Epsilon ? Math.Abs((actual - predicted) / actual) : 0;
8585
}
8686

8787
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]

lib/errors/Mase.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ private double CalculateMase()
120120
double sumAbsoluteError = CalculateSumAbsoluteError(actualValues, predictedValues);
121121
double naiveForecastError = CalculateNaiveForecastError(actualValues, naiveValues);
122122

123-
return naiveForecastError != 0 ? (sumAbsoluteError / _actualBuffer.Count) / naiveForecastError : double.PositiveInfinity;
123+
return naiveForecastError >= double.Epsilon ? (sumAbsoluteError / _actualBuffer.Count) / naiveForecastError : double.PositiveInfinity;
124124
}
125125

126126
/// <summary>

lib/errors/Mpe.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ protected override void ManageState(bool isNew)
8282
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
8383
private static double CalculatePercentageError(double actual, double predicted)
8484
{
85-
return actual != 0 ? (actual - predicted) / actual : 0;
85+
return actual >= double.Epsilon ? (actual - predicted) / actual : 0;
8686
}
8787

8888
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]

lib/errors/Rsquared.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ protected override double Calculation()
115115
sumSquaredTotal += squaredTotal;
116116
}
117117

118-
rsquared = sumSquaredTotal != 0 ? 1 - (sumSquaredResidual / sumSquaredTotal) : 0;
118+
rsquared = sumSquaredTotal >= double.Epsilon ? 1 - (sumSquaredResidual / sumSquaredTotal) : 0;
119119
}
120120

121121
IsHot = _index >= WarmupPeriod;

lib/momentum/Adx.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,7 @@ public sealed class Adx : AbstractBarBase
5656
[MethodImpl(MethodImplOptions.AggressiveInlining)]
5757
public Adx(int period = DefaultPeriod)
5858
{
59-
if (period < 1)
60-
throw new ArgumentOutOfRangeException(nameof(period));
59+
ArgumentOutOfRangeException.ThrowIfLessThan(period, 1);
6160
_smoothedTr = new(period, useSma: true);
6261
_smoothedPlusDm = new(period, useSma: true);
6362
_smoothedMinusDm = new(period, useSma: true);

0 commit comments

Comments
 (0)