mirror of
https://github.com/mihakralj/QuanTAlib.git
synced 2026-08-25 13:58:04 +00:00
fixes
This commit is contained in:
+1
-2
@@ -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);
|
||||
|
||||
+25
-31
@@ -3,52 +3,45 @@ namespace QuanTAlib;
|
||||
|
||||
/// <summary>
|
||||
/// 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.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// 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
|
||||
/// </remarks>
|
||||
[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;
|
||||
|
||||
/// <param name="period">The number of periods used in the ADXR calculation (default 14).</param>
|
||||
/// <exception cref="ArgumentOutOfRangeException">Thrown when period is less than 1.</exception>
|
||||
[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 sealed class Adxr : AbstractBarBase
|
||||
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);
|
||||
double currentAdx = _currentAdx.Calc(Input);
|
||||
_adxHistory.Add(currentAdx, Input.IsNew);
|
||||
|
||||
// Store ADX value in historical buffer
|
||||
_historicalAdx.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;
|
||||
}
|
||||
|
||||
+23
-55
@@ -3,65 +3,34 @@ namespace QuanTAlib;
|
||||
|
||||
/// <summary>
|
||||
/// 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.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// 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
|
||||
/// </remarks>
|
||||
[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;
|
||||
|
||||
/// <param name="fastPeriod">The number of periods for the fast moving average (default 12).</param>
|
||||
/// <param name="slowPeriod">The number of periods for the slow moving average (default 26).</param>
|
||||
/// <exception cref="ArgumentOutOfRangeException">Thrown when either period is less than 1.</exception>
|
||||
/// <param name="fastPeriod">The period for the faster moving average.</param>
|
||||
/// <param name="slowPeriod">The period for the slower moving average.</param>
|
||||
/// <exception cref="ArgumentOutOfRangeException">
|
||||
/// Thrown when fastPeriod or slowPeriod is less than 1, or when fastPeriod is greater than or equal to slowPeriod.
|
||||
/// </exception>
|
||||
[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})";
|
||||
}
|
||||
|
||||
/// <param name="source">The data source object that publishes updates.</param>
|
||||
/// <param name="fastPeriod">The number of periods for the fast moving average.</param>
|
||||
/// <param name="slowPeriod">The number of periods for the slow moving average.</param>
|
||||
/// <param name="fastPeriod">The period for the faster moving average.</param>
|
||||
/// <param name="slowPeriod">The period for the slower moving average.</param>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public Apo(object source, int fastPeriod, int slowPeriod) : this(fastPeriod, slowPeriod)
|
||||
{
|
||||
@@ -73,19 +42,18 @@ public sealed class Apo : AbstractBase
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user