Class optimization

This commit is contained in:
Miha
2024-10-27 16:11:08 -07:00
parent b2fcdda785
commit 6c67a0cf31
77 changed files with 2634 additions and 1455 deletions
+27 -21
View File
@@ -1,4 +1,4 @@
using System;
using System.Runtime.CompilerServices;
namespace QuanTAlib;
/// <summary>
@@ -34,14 +34,18 @@ namespace QuanTAlib;
/// Note: Similar to RSI but with different scaling and calculation method
/// </remarks>
public class Cmo : AbstractBase
[SkipLocalsInit]
public sealed class Cmo : AbstractBase
{
private readonly CircularBuffer _sumH;
private readonly CircularBuffer _sumL;
private double _prevValue, _p_prevValue;
private const double Epsilon = 1e-10;
private const double ScalingFactor = 100.0;
/// <param name="period">The number of periods used in the CMO calculation.</param>
/// <exception cref="ArgumentOutOfRangeException">Thrown when period is less than 1.</exception>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public Cmo(int period)
{
if (period < 1)
@@ -55,12 +59,14 @@ public class Cmo : AbstractBase
/// <param name="source">The data source object that publishes updates.</param>
/// <param name="period">The number of periods used in the CMO calculation.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public Cmo(object source, int period) : this(period)
{
var pubEvent = source.GetType().GetEvent("Pub");
pubEvent?.AddEventHandler(source, new ValueSignal(Sub));
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
protected override void ManageState(bool isNew)
{
if (isNew)
@@ -74,6 +80,20 @@ public class Cmo : AbstractBase
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
private static (double up, double down) CalculateMovements(double diff)
{
return diff > 0 ? (diff, 0) : (0, -diff);
}
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
private static double CalculateCmo(double sumH, double sumL)
{
double divisor = sumH + sumL;
return (Math.Abs(divisor) > Epsilon) ? ScalingFactor * ((sumH - sumL) / divisor) : 0.0;
}
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
protected override double Calculation()
{
ManageState(Input.IsNew);
@@ -88,25 +108,11 @@ public class Cmo : AbstractBase
_prevValue = Input.Value;
// Separate upward and downward movements
if (diff > 0)
{
_sumH.Add(diff, Input.IsNew);
_sumL.Add(0, Input.IsNew);
}
else
{
_sumH.Add(0, Input.IsNew);
_sumL.Add(-diff, Input.IsNew);
}
var (up, down) = CalculateMovements(diff);
_sumH.Add(up, Input.IsNew);
_sumL.Add(down, Input.IsNew);
// Calculate sums for the specified period
double sumH = _sumH.Sum();
double sumL = _sumL.Sum();
double divisor = sumH + sumL;
// Calculate CMO value
return (Math.Abs(divisor) > double.Epsilon) ?
100.0 * ((sumH - sumL) / divisor) :
0.0;
// Calculate sums and CMO value
return CalculateCmo(_sumH.Sum(), _sumL.Sum());
}
}
+26 -10
View File
@@ -1,4 +1,4 @@
using System;
using System.Runtime.CompilerServices;
namespace QuanTAlib;
/// <summary>
@@ -35,15 +35,19 @@ namespace QuanTAlib;
/// Note: Default period of 14 was recommended by Wilder
/// </remarks>
public class Rsi : AbstractBase
[SkipLocalsInit]
public sealed class Rsi : AbstractBase
{
private readonly Rma _avgGain;
private readonly Rma _avgLoss;
private double _prevValue, _p_prevValue;
private const double ScalingFactor = 100.0;
private const int DefaultPeriod = 14;
/// <param name="period">The number of periods used in the RSI calculation (default 14).</param>
/// <exception cref="ArgumentOutOfRangeException">Thrown when period is less than 1.</exception>
public Rsi(int period = 14)
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public Rsi(int period = DefaultPeriod)
{
if (period < 1)
throw new ArgumentOutOfRangeException(nameof(period));
@@ -56,12 +60,14 @@ public class Rsi : AbstractBase
/// <param name="source">The data source object that publishes updates.</param>
/// <param name="period">The number of periods used in the RSI calculation.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public Rsi(object source, int period) : this(period)
{
var pubEvent = source.GetType().GetEvent("Pub");
pubEvent?.AddEventHandler(source, new ValueSignal(Sub));
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
protected override void ManageState(bool isNew)
{
if (isNew)
@@ -75,6 +81,19 @@ public class Rsi : AbstractBase
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
private static (double gain, double loss) CalculateGainLoss(double change)
{
return (Math.Max(change, 0), Math.Max(-change, 0));
}
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
private static double CalculateRsi(double avgGain, double avgLoss)
{
return avgLoss > 0 ? ScalingFactor - (ScalingFactor / (1 + (avgGain / avgLoss))) : ScalingFactor;
}
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
protected override double Calculation()
{
ManageState(Input.IsNew);
@@ -86,17 +105,14 @@ public class Rsi : AbstractBase
// Calculate price change and separate gains/losses
double change = Input.Value - _prevValue;
double gain = Math.Max(change, 0);
double loss = Math.Max(-change, 0);
var (gain, loss) = CalculateGainLoss(change);
_prevValue = Input.Value;
// Calculate smoothed averages using Wilder's method
_avgGain.Calc(gain, IsNew: Input.IsNew);
_avgLoss.Calc(loss, IsNew: Input.IsNew);
_avgGain.Calc(gain, Input.IsNew);
_avgLoss.Calc(loss, Input.IsNew);
// Calculate RSI
double rsi = (_avgLoss.Value > 0) ? 100 - (100 / (1 + (_avgGain.Value / _avgLoss.Value))) : 100;
return rsi;
return CalculateRsi(_avgGain.Value, _avgLoss.Value);
}
}
+36 -12
View File
@@ -1,4 +1,4 @@
using System;
using System.Runtime.CompilerServices;
namespace QuanTAlib;
/// <summary>
@@ -34,24 +34,34 @@ namespace QuanTAlib;
/// Note: Proprietary enhancement of RSI using JMA technology
/// </remarks>
public class Rsx : AbstractBase
[SkipLocalsInit]
public sealed class Rsx : AbstractBase
{
private readonly Rma _avgGain;
private readonly Rma _avgLoss;
private readonly Jma _rsx;
private double _prevValue, _p_prevValue;
private const double ScalingFactor = 100.0;
private const int DefaultPeriod = 14;
private const int DefaultPhase = 0;
private const double DefaultFactor = 0.55;
private const int JmaPeriod = 8;
private const int JmaPower = 100;
private const double JmaPhase = 0.25;
private const int JmaExtra = 3;
/// <param name="period">The number of periods for RSI calculation (default 14).</param>
/// <param name="phase">The phase parameter for JMA smoothing (default 0).</param>
/// <param name="factor">The factor parameter for smoothing control (default 0.55).</param>
/// <exception cref="ArgumentOutOfRangeException">Thrown when period is less than 1.</exception>
public Rsx(int period = 14, int phase = 0, double factor = 0.55)
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public Rsx(int period = DefaultPeriod, int phase = DefaultPhase, double factor = DefaultFactor)
{
if (period < 1)
throw new ArgumentOutOfRangeException(nameof(period));
_avgGain = new(period);
_avgLoss = new(period);
_rsx = new(8, 100, 0.25, 3);
_rsx = new(JmaPeriod, JmaPower, JmaPhase, JmaExtra);
_index = 0;
WarmupPeriod = period + 1;
Name = $"RSX({period})";
@@ -61,12 +71,14 @@ public class Rsx : AbstractBase
/// <param name="period">The number of periods for RSI calculation.</param>
/// <param name="phase">The phase parameter for JMA smoothing.</param>
/// <param name="factor">The factor parameter for smoothing control.</param>
public Rsx(object source, int period, int phase = 0, double factor = 0.55) : this(period, phase, factor)
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public Rsx(object source, int period, int phase = DefaultPhase, double factor = DefaultFactor) : this(period, phase, factor)
{
var pubEvent = source.GetType().GetEvent("Pub");
pubEvent?.AddEventHandler(source, new ValueSignal(Sub));
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
protected override void ManageState(bool isNew)
{
if (isNew)
@@ -80,6 +92,19 @@ public class Rsx : AbstractBase
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
private static (double gain, double loss) CalculateGainLoss(double change)
{
return (Math.Max(change, 0), Math.Max(-change, 0));
}
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
private static double CalculateRsi(double avgGain, double avgLoss)
{
return avgLoss > 0 ? ScalingFactor - (ScalingFactor / (1 + (avgGain / avgLoss))) : ScalingFactor;
}
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
protected override double Calculation()
{
ManageState(Input.IsNew);
@@ -91,18 +116,17 @@ public class Rsx : AbstractBase
// Calculate RSI components
double change = Input.Value - _prevValue;
double gain = Math.Max(change, 0);
double loss = Math.Max(-change, 0);
var (gain, loss) = CalculateGainLoss(change);
_prevValue = Input.Value;
// Calculate RSI
_avgGain.Calc(gain, IsNew: Input.IsNew);
_avgLoss.Calc(loss, IsNew: Input.IsNew);
double rsi = (_avgLoss.Value > 0) ? 100 - (100 / (1 + (_avgGain.Value / _avgLoss.Value))) : 100;
_avgGain.Calc(gain, Input.IsNew);
_avgLoss.Calc(loss, Input.IsNew);
double rsi = CalculateRsi(_avgGain.Value, _avgLoss.Value);
// Apply JMA smoothing
double rsx = _rsx.Calc(rsi, Input.IsNew);
_rsx.Calc(rsi, Input.IsNew);
return rsx;
return _rsx.Value;
}
}