diff --git a/.sonarlint/QuanTAlib.json b/.sonarlint/QuanTAlib.json index c44cece..f75a1bc 100644 --- a/.sonarlint/QuanTAlib.json +++ b/.sonarlint/QuanTAlib.json @@ -1,4 +1,4 @@ { - "sonarCloudOrganization": "mihakralj", + "sonarCloudOrganization": "mihakralj-quantalib", "projectKey": "mihakralj_QuanTAlib" } \ No newline at end of file diff --git a/Tests/test_skender.stock.cs b/Tests/test_skender.stock.cs index 38a87f2..ef9e74c 100644 --- a/Tests/test_skender.stock.cs +++ b/Tests/test_skender.stock.cs @@ -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); } diff --git a/lib/averages/Convolution.cs b/lib/averages/Convolution.cs index d0dd9f1..0a67857 100644 --- a/lib/averages/Convolution.cs +++ b/lib/averages/Convolution.cs @@ -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++) diff --git a/lib/averages/Dwma.cs b/lib/averages/Dwma.cs index 28a067e..864d468 100644 --- a/lib/averages/Dwma.cs +++ b/lib/averages/Dwma.cs @@ -25,7 +25,6 @@ public class Dwma : AbstractBase { private readonly Wma _innerWma; private readonly Wma _outerWma; - private readonly int _period; public Dwma(int period) { @@ -33,7 +32,6 @@ public Dwma(int period) { 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"; diff --git a/lib/averages/Frama.cs b/lib/averages/Frama.cs index af57ba5..10db169 100644 --- a/lib/averages/Frama.cs +++ b/lib/averages/Frama.cs @@ -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); diff --git a/lib/averages/Htit.cs b/lib/averages/Htit.cs index 13120bd..6e04311 100644 --- a/lib/averages/Htit.cs +++ b/lib/averages/Htit.cs @@ -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); diff --git a/lib/averages/Jma.cs b/lib/averages/Jma.cs index 8df7903..413f25b 100644 --- a/lib/averages/Jma.cs +++ b/lib/averages/Jma.cs @@ -27,14 +27,12 @@ namespace QuanTAlib; /// 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; @@ -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; @@ -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); @@ -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() @@ -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; diff --git a/lib/averages/Kama.cs b/lib/averages/Kama.cs index 8d8fc37..58f4011 100644 --- a/lib/averages/Kama.cs +++ b/lib/averages/Kama.cs @@ -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; @@ -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); @@ -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)] diff --git a/lib/averages/Maaf.cs b/lib/averages/Maaf.cs index c30d5b2..b5f6c5f 100644 --- a/lib/averages/Maaf.cs +++ b/lib/averages/Maaf.cs @@ -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; diff --git a/lib/averages/Mgdi.cs b/lib/averages/Mgdi.cs index f702ef8..d02c05d 100644 --- a/lib/averages/Mgdi.cs +++ b/lib/averages/Mgdi.cs @@ -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; @@ -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; @@ -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)] diff --git a/lib/averages/Tema.cs b/lib/averages/Tema.cs index 09d0d76..a1238ee 100644 --- a/lib/averages/Tema.cs +++ b/lib/averages/Tema.cs @@ -27,7 +27,6 @@ namespace QuanTAlib; /// public class Tema : AbstractBase { - private readonly int _period; private readonly double _k; private readonly double _oneMinusK; private readonly double _epsilon = 1e-10; @@ -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 diff --git a/lib/averages/Trima.cs b/lib/averages/Trima.cs index 3589239..650e254 100644 --- a/lib/averages/Trima.cs +++ b/lib/averages/Trima.cs @@ -28,7 +28,6 @@ namespace QuanTAlib; public class Trima : AbstractBase { private readonly Convolution _convolution; - private readonly double[] _kernel; /// The number of data points used in the TRIMA calculation. /// Thrown when period is less than 1. @@ -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; diff --git a/lib/averages/Wma.cs b/lib/averages/Wma.cs index 26fcaa5..04c2eff 100644 --- a/lib/averages/Wma.cs +++ b/lib/averages/Wma.cs @@ -28,9 +28,7 @@ namespace QuanTAlib; /// public class Wma : AbstractBase { - private readonly int _period; private readonly Convolution _convolution; - private readonly double[] _kernel; /// The number of data points used in the WMA calculation. /// Thrown when period is less than 1. @@ -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(); } diff --git a/lib/core/abstractBase.cs b/lib/core/abstractBase.cs index 2deb5fe..26ff9c0 100644 --- a/lib/core/abstractBase.cs +++ b/lib/core/abstractBase.cs @@ -44,7 +44,7 @@ protected static bool IsValidValue(double value) /// Creates a new TValue with the current state. /// [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); } diff --git a/lib/errors/Mapd.cs b/lib/errors/Mapd.cs index 874a551..a4b32b7 100644 --- a/lib/errors/Mapd.cs +++ b/lib/errors/Mapd.cs @@ -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)] diff --git a/lib/errors/Mape.cs b/lib/errors/Mape.cs index 14ae2d2..4030739 100644 --- a/lib/errors/Mape.cs +++ b/lib/errors/Mape.cs @@ -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)] diff --git a/lib/errors/Mpe.cs b/lib/errors/Mpe.cs index 1e33b67..d9d2171 100644 --- a/lib/errors/Mpe.cs +++ b/lib/errors/Mpe.cs @@ -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)] diff --git a/lib/errors/Rsquared.cs b/lib/errors/Rsquared.cs index 21a07d0..5712c38 100644 --- a/lib/errors/Rsquared.cs +++ b/lib/errors/Rsquared.cs @@ -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; diff --git a/lib/momentum/Adx.cs b/lib/momentum/Adx.cs index 2b1d57a..8cab3ce 100644 --- a/lib/momentum/Adx.cs +++ b/lib/momentum/Adx.cs @@ -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); diff --git a/lib/momentum/Adxr.cs b/lib/momentum/Adxr.cs index 4dc2932..75969c5 100644 --- a/lib/momentum/Adxr.cs +++ b/lib/momentum/Adxr.cs @@ -3,52 +3,45 @@ namespace QuanTAlib; /// /// ADXR: Average Directional Movement Index Rating -/// A momentum indicator that measures trend strength by comparing the current ADX -/// value with a historical ADX value. ADXR helps identify potential trend -/// reversals earlier than standard ADX. +/// A momentum indicator that measures the strength of a trend by comparing +/// the current ADX value with its value from a specified number of periods ago. /// /// /// The ADXR calculation process: -/// 1. Calculate current period ADX -/// 2. Calculate historical period ADX (shifted back by period) -/// 3. Average the current and historical ADX values +/// 1. Calculate current ADX +/// 2. Get ADX value from n periods ago +/// 3. Average the two values /// /// Key characteristics: /// - Oscillates between 0 and 100 /// - Values above 25 indicate strong trend /// - Values below 20 indicate weak or no trend -/// - Faster at identifying trend changes than ADX -/// - Does not indicate trend direction, only strength +/// - Can be used to confirm trend strength +/// - Helps identify potential trend reversals /// /// Formula: -/// ADXR = (Current ADX + Historical ADX) / 2 -/// where: -/// Historical ADX = ADX value from 'period' bars ago +/// ADXR = (Current ADX + ADX n periods ago) / 2 /// /// Sources: /// J. Welles Wilder Jr. - "New Concepts in Technical Trading Systems" (1978) /// https://www.investopedia.com/terms/a/adxr.asp -/// -/// Note: Default period of 14 was recommended by Wilder /// -[SkipLocalsInit] public sealed class Adxr : AbstractBarBase { private readonly Adx _currentAdx; - private readonly CircularBuffer _historicalAdx; - private const int DefaultPeriod = 14; + private readonly CircularBuffer _adxHistory; + private readonly int _period; /// The number of periods used in the ADXR calculation (default 14). /// Thrown when period is less than 1. [MethodImpl(MethodImplOptions.AggressiveInlining)] - public Adxr(int period = DefaultPeriod) + public Adxr(int period = 14) { - if (period < 1) - throw new ArgumentOutOfRangeException(nameof(period)); + ArgumentOutOfRangeException.ThrowIfLessThan(period, 1); _currentAdx = new(period); - _historicalAdx = new(period); - _index = 0; - WarmupPeriod = period * 3; // Need extra periods for historical ADX + _adxHistory = new(period); + _period = period; + WarmupPeriod = period * 3; // Need extra periods for ADX calculation and history Name = $"ADXR({period})"; } @@ -65,24 +58,25 @@ public Adxr(object source, int period) : this(period) protected override void ManageState(bool isNew) { if (isNew) + { _index++; + } } - [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] + [MethodImpl(MethodImplOptions.AggressiveInlining)] protected override double Calculation() { ManageState(Input.IsNew); // Calculate current ADX - double currentAdx = _currentAdx.Value; - _currentAdx.Calc(Input); - - // Store ADX value in historical buffer - _historicalAdx.Add(currentAdx, Input.IsNew); + double currentAdx = _currentAdx.Calc(Input); + _adxHistory.Add(currentAdx, Input.IsNew); - // Calculate ADXR once we have enough historical data - if (_index > _historicalAdx.Capacity) - return (currentAdx + _historicalAdx.Oldest()) / 2.0; + // Calculate ADXR once we have enough history + if (_index > _period) + { + return (currentAdx + _adxHistory[^_period]) * 0.5; + } return currentAdx; } diff --git a/lib/momentum/Apo.cs b/lib/momentum/Apo.cs index 31da989..cf23537 100644 --- a/lib/momentum/Apo.cs +++ b/lib/momentum/Apo.cs @@ -3,65 +3,34 @@ namespace QuanTAlib; /// /// APO: Absolute Price Oscillator -/// A momentum indicator that measures the absolute difference between two moving -/// averages of different periods. APO helps identify trend direction and potential -/// reversals by showing the momentum of price movement. +/// A momentum indicator that measures the difference between two moving averages +/// of different periods. Similar to PPO but shows absolute difference instead of percentage. /// -/// -/// The APO calculation process: -/// 1. Calculate fast period moving average -/// 2. Calculate slow period moving average -/// 3. Calculate absolute difference between the two averages -/// -/// Key characteristics: -/// - Oscillates above and below zero -/// - Positive values indicate upward price momentum -/// - Negative values indicate downward price momentum -/// - Zero line crossovers signal potential trend changes -/// - Similar to MACD but uses simple moving averages -/// -/// Formula: -/// APO = Fast MA - Slow MA -/// where: -/// Fast MA = Moving average of shorter period -/// Slow MA = Moving average of longer period -/// -/// Sources: -/// https://www.investopedia.com/terms/p/ppo.asp -/// https://school.stockcharts.com/doku.php?id=technical_indicators:price_oscillators_ppo -/// -/// Note: Default periods are 12 and 26, similar to MACD -/// -[SkipLocalsInit] public sealed class Apo : AbstractBase { - private readonly Sma _fastMa; - private readonly Sma _slowMa; - private const int DefaultFastPeriod = 12; - private const int DefaultSlowPeriod = 26; + private readonly AbstractBase _fastMa, _slowMa; - /// The number of periods for the fast moving average (default 12). - /// The number of periods for the slow moving average (default 26). - /// Thrown when either period is less than 1. + /// The period for the faster moving average. + /// The period for the slower moving average. + /// + /// Thrown when fastPeriod or slowPeriod is less than 1, or when fastPeriod is greater than or equal to slowPeriod. + /// [MethodImpl(MethodImplOptions.AggressiveInlining)] - public Apo(int fastPeriod = DefaultFastPeriod, int slowPeriod = DefaultSlowPeriod) + public Apo(int fastPeriod = 12, int slowPeriod = 26) { - if (fastPeriod < 1) - throw new ArgumentOutOfRangeException(nameof(fastPeriod)); - if (slowPeriod < 1) - throw new ArgumentOutOfRangeException(nameof(slowPeriod)); - if (fastPeriod >= slowPeriod) - throw new ArgumentException("Fast period must be less than slow period"); + ArgumentOutOfRangeException.ThrowIfLessThan(fastPeriod, 1); + ArgumentOutOfRangeException.ThrowIfLessThan(slowPeriod, 1); + ArgumentOutOfRangeException.ThrowIfGreaterThanOrEqual(fastPeriod, slowPeriod); - _fastMa = new(fastPeriod); - _slowMa = new(slowPeriod); + _fastMa = new Ema(fastPeriod); + _slowMa = new Ema(slowPeriod); WarmupPeriod = slowPeriod; Name = $"APO({fastPeriod},{slowPeriod})"; } /// The data source object that publishes updates. - /// The number of periods for the fast moving average. - /// The number of periods for the slow moving average. + /// The period for the faster moving average. + /// The period for the slower moving average. [MethodImpl(MethodImplOptions.AggressiveInlining)] public Apo(object source, int fastPeriod, int slowPeriod) : this(fastPeriod, slowPeriod) { @@ -73,19 +42,18 @@ public Apo(object source, int fastPeriod, int slowPeriod) : this(fastPeriod, slo protected override void ManageState(bool isNew) { if (isNew) + { _index++; + _lastValidValue = Input.Value; + } } - [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] + [MethodImpl(MethodImplOptions.AggressiveInlining)] protected override double Calculation() { ManageState(Input.IsNew); - - // Calculate both moving averages - double fastMa = _fastMa.Calc(Input.Value, Input.IsNew); - double slowMa = _slowMa.Calc(Input.Value, Input.IsNew); - - // Calculate absolute difference - return fastMa - slowMa; + _fastMa.Calc(Input); + _slowMa.Calc(Input); + return _fastMa.Value - _slowMa.Value; } }