Skip to content

Commit

Permalink
Merge branch 'dev'
Browse files Browse the repository at this point in the history
  • Loading branch information
mihakralj committed Nov 5, 2024
2 parents 82fc868 + fc3e9f7 commit dbe9f81
Show file tree
Hide file tree
Showing 21 changed files with 82 additions and 134 deletions.
2 changes: 1 addition & 1 deletion .sonarlint/QuanTAlib.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"sonarCloudOrganization": "mihakralj",
"sonarCloudOrganization": "mihakralj-quantalib",
"projectKey": "mihakralj_QuanTAlib"
}
2 changes: 1 addition & 1 deletion Tests/test_skender.stock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ public void MAMA()
.GetMama(fastLimit: 0.5, slowLimit: 0.05)
.Select(i => i.Mama.Null2NaN()!);
Assert.Equal(QL.Length, SK.Count());
for (int i = QL.Length - 1; i > 100; i--)
for (int i = QL.Length - 1; i > 500; i--)
{
Assert.InRange(SK.ElementAt(i) - QL[i].Value, -range, range);
}
Expand Down
2 changes: 1 addition & 1 deletion lib/averages/Convolution.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ private void NormalizeKernel()
}

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

for (int i = 0; i < _activeLength; i++)
Expand Down
2 changes: 0 additions & 2 deletions lib/averages/Dwma.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,13 @@ public class Dwma : AbstractBase
{
private readonly Wma _innerWma;
private readonly Wma _outerWma;
private readonly int _period;

public Dwma(int period)
{
if (period < 1)
{
throw new System.ArgumentException("Period must be greater than or equal to 1.", nameof(period));
}
_period = period;
_innerWma = new Wma(period);
_outerWma = new Wma(period);
Name = "Dwma";
Expand Down
4 changes: 2 additions & 2 deletions lib/averages/Frama.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,14 +82,14 @@ protected override void ManageState(bool isNew)
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
private void UpdateMinMax(double price, ref double high, ref double low)
private static void UpdateMinMax(double price, ref double high, ref double low)
{
high = System.Math.Max(high, price);
low = System.Math.Min(low, price);
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
private double CalculateAlpha(double dimension)
private static double CalculateAlpha(double dimension)
{
double alpha = System.Math.Exp(-4.6 * (dimension - 1));
return System.Math.Clamp(alpha, 0.01, 1.0);
Expand Down
2 changes: 1 addition & 1 deletion lib/averages/Htit.cs
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ protected override double Calculation()
_imBuffer.Add(im, Input.IsNew);

// Calculate period
double pd = (im != 0 && re != 0) ? TWO_PI / System.Math.Atan(im / re) : 0;
double pd = (im >= double.Epsilon && re >= double.Epsilon) ? TWO_PI / System.Math.Atan(im / re) : 0;
pd = ClampPeriod(pd, _lastPd);
pd = (ALPHA * pd) + (BETA * _lastPd);
_pdBuffer.Add(pd, Input.IsNew);
Expand Down
23 changes: 10 additions & 13 deletions lib/averages/Jma.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,12 @@ namespace QuanTAlib;
/// </remarks>
public class Jma : AbstractBase
{
private readonly double _period;
private readonly double _phase;
private readonly CircularBuffer _vsumBuff;
private readonly CircularBuffer _avoltyBuff;
private readonly double _beta;
private readonly double _len1;
private readonly double _pow1;
private readonly double _oneMinusAlpha;
private readonly double _oneMinusAlphaSquared;
private readonly double _alphaSquared;

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

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

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

// Precalculate constants for alpha-based calculations
double alpha = System.Math.Pow(_beta, _pow1);
_oneMinusAlpha = 1.0 - alpha;
double alpha = Math.Pow(_beta, _pow1);
double _oneMinusAlpha = 1.0 - alpha;
_oneMinusAlphaSquared = _oneMinusAlpha * _oneMinusAlpha;
_alphaSquared = alpha * alpha;

Expand Down Expand Up @@ -120,7 +117,7 @@ protected override void ManageState(bool isNew)
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private double CalculateVolatility(double price, double del1, double del2)
{
double volty = System.Math.Max(System.Math.Abs(del1), System.Math.Abs(del2));
double volty = Math.Max(Math.Abs(del1), Math.Abs(del2));
_vsumBuff.Add(volty, Input.IsNew);
_vSum += (_vsumBuff[^1] - _vsumBuff[0]) / _vsumBuff.Count;
_avoltyBuff.Add(_vSum, Input.IsNew);
Expand All @@ -131,7 +128,7 @@ private double CalculateVolatility(double price, double del1, double del2)
private double CalculateRelativeVolatility(double volty, double avgVolty)
{
double rvolty = (avgVolty > 0) ? volty / avgVolty : 1;
return System.Math.Min(System.Math.Max(rvolty, 1.0), System.Math.Pow(_len1, 1.0 / _pow1));
return Math.Min(Math.Max(rvolty, 1.0), Math.Pow(_len1, 1.0 / _pow1));
}

protected override double Calculation()
Expand All @@ -152,13 +149,13 @@ protected override double Calculation()
double avgVolty = _avoltyBuff.Average();

double rvolty = CalculateRelativeVolatility(volty, avgVolty);
double pow2 = System.Math.Pow(rvolty, _pow1);
double Kv = System.Math.Pow(_beta, System.Math.Sqrt(pow2));
double pow2 = Math.Pow(rvolty, _pow1);
double Kv = Math.Pow(_beta, Math.Sqrt(pow2));

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

double alpha = System.Math.Pow(_beta, pow2);
double alpha = Math.Pow(_beta, pow2);
double ma1 = price + (alpha * (_prevMa1 - price));
_prevMa1 = ma1;

Expand Down
8 changes: 4 additions & 4 deletions lib/averages/Kama.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ namespace QuanTAlib;
public class Kama : AbstractBase
{
private readonly int _period;
private readonly double _scFast, _scSlow;
private readonly double _scSlow;
private readonly double _scDiff; // Precalculated (_scFast - _scSlow)
private readonly CircularBuffer _buffer;
private double _lastKama, _p_lastKama;
Expand All @@ -43,7 +43,7 @@ public Kama(int period, int fast = 2, int slow = 30)
throw new System.ArgumentException("Period must be greater than or equal to 1.", nameof(period));
}
_period = period;
_scFast = 2.0 / (((period < fast) ? period : fast) + 1);
double _scFast = 2.0 / (((period < fast) ? period : fast) + 1);
_scSlow = 2.0 / (slow + 1);
_scDiff = _scFast - _scSlow;
_buffer = new CircularBuffer(_period + 1);
Expand Down Expand Up @@ -97,9 +97,9 @@ private double CalculateVolatility()
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
private double CalculateEfficiencyRatio(double change, double volatility)
private static double CalculateEfficiencyRatio(double change, double volatility)
{
return volatility != 0 ? change / volatility : 0;
return volatility >= double.Epsilon ? change / volatility : 0;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
Expand Down
6 changes: 3 additions & 3 deletions lib/averages/Maaf.cs
Original file line number Diff line number Diff line change
Expand Up @@ -142,15 +142,15 @@ protected override double Calculation()
double value1 = GetMedian(length);
value2 = (alpha * (smooth - _prevValue2)) + _prevValue2;

if (value1 != 0)
if (value1 >= double.Epsilon)
{
value3 = System.Math.Abs(value1 - value2) / value1;
value3 = Math.Abs(value1 - value2) / value1;
}

length -= 2;
}

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

Expand Down
4 changes: 1 addition & 3 deletions lib/averages/Mgdi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ namespace QuanTAlib;
public class Mgdi : AbstractBase
{
private readonly int _period;
private readonly double _kFactor;
private readonly double _kFactorPeriod; // Precalculated k * period
private double _prevMd, _p_prevMd;

Expand All @@ -45,7 +44,6 @@ public Mgdi(int period, double kFactor = 0.6)
throw new System.ArgumentOutOfRangeException(nameof(kFactor), "K-Factor must be greater than 0.");
}
_period = period;
_kFactor = kFactor;
_kFactorPeriod = kFactor * period;
Name = "Mgdi";
WarmupPeriod = period;
Expand Down Expand Up @@ -85,7 +83,7 @@ protected override void ManageState(bool isNew)
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private double CalculateRatio(double value)
{
return _prevMd != 0 ? value / _prevMd : 1;
return _prevMd >= double.Epsilon ? value / _prevMd : 1;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
Expand Down
4 changes: 1 addition & 3 deletions lib/averages/Tema.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ namespace QuanTAlib;
/// </remarks>
public class Tema : AbstractBase
{
private readonly int _period;
private readonly double _k;
private readonly double _oneMinusK;
private readonly double _epsilon = 1e-10;
Expand All @@ -44,8 +43,7 @@ public Tema(int period)
{
throw new System.ArgumentOutOfRangeException(nameof(period), "Period must be greater than or equal to 1.");
}
_period = period;
_k = 2.0 / (_period + 1);
_k = 2.0 / (period + 1);
_oneMinusK = 1.0 - _k;
Name = "Tema";
double percentile = 0.85; //targeting 85th percentile of correctness of converging EMA
Expand Down
3 changes: 1 addition & 2 deletions lib/averages/Trima.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ namespace QuanTAlib;
public class Trima : AbstractBase
{
private readonly Convolution _convolution;
private readonly double[] _kernel;

/// <param name="period">The number of data points used in the TRIMA calculation.</param>
/// <exception cref="ArgumentException">Thrown when period is less than 1.</exception>
Expand All @@ -38,7 +37,7 @@ public Trima(int period)
{
throw new System.ArgumentException("Period must be greater than or equal to 1.", nameof(period));
}
_kernel = GenerateKernel(period);
double[] _kernel = GenerateKernel(period);
_convolution = new Convolution(_kernel);
Name = "Trima";
WarmupPeriod = period;
Expand Down
7 changes: 2 additions & 5 deletions lib/averages/Wma.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,7 @@ namespace QuanTAlib;
/// </remarks>
public class Wma : AbstractBase
{
private readonly int _period;
private readonly Convolution _convolution;
private readonly double[] _kernel;

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

Expand Down
2 changes: 1 addition & 1 deletion lib/core/abstractBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ protected static bool IsValidValue(double value)
/// Creates a new TValue with the current state.
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
protected TValue CreateTValue(System.DateTime time, double value, bool isNew, bool isHot = false)
protected static TValue CreateTValue(System.DateTime time, double value, bool isNew, bool isHot = false)
{
return new TValue(time, value, isNew, isHot);
}
Expand Down
2 changes: 1 addition & 1 deletion lib/errors/Mapd.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ protected override void ManageState(bool isNew)
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
private static double CalculatePercentageDeviation(double actual, double predicted)
{
return actual != 0 ? Math.Abs((actual - predicted) / actual) : 0;
return actual >= double.Epsilon ? Math.Abs((actual - predicted) / actual) : 0;
}

[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
Expand Down
2 changes: 1 addition & 1 deletion lib/errors/Mape.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ protected override void ManageState(bool isNew)
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
private static double CalculatePercentageError(double actual, double predicted)
{
return actual != 0 ? Math.Abs((actual - predicted) / actual) : 0;
return actual >= double.Epsilon ? Math.Abs((actual - predicted) / actual) : 0;
}

[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
Expand Down
2 changes: 1 addition & 1 deletion lib/errors/Mpe.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ protected override void ManageState(bool isNew)
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
private static double CalculatePercentageError(double actual, double predicted)
{
return actual != 0 ? (actual - predicted) / actual : 0;
return actual >= double.Epsilon ? (actual - predicted) / actual : 0;
}

[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
Expand Down
2 changes: 1 addition & 1 deletion lib/errors/Rsquared.cs
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ protected override double Calculation()
sumSquaredTotal += squaredTotal;
}

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

IsHot = _index >= WarmupPeriod;
Expand Down
3 changes: 1 addition & 2 deletions lib/momentum/Adx.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,7 @@ public sealed class Adx : AbstractBarBase
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public Adx(int period = DefaultPeriod)
{
if (period < 1)
throw new ArgumentOutOfRangeException(nameof(period));
ArgumentOutOfRangeException.ThrowIfLessThan(period, 1);
_smoothedTr = new(period, useSma: true);
_smoothedPlusDm = new(period, useSma: true);
_smoothedMinusDm = new(period, useSma: true);
Expand Down
Loading

0 comments on commit dbe9f81

Please sign in to comment.