mirror of
https://github.com/mihakralj/QuanTAlib.git
synced 2026-08-16 17:48:05 +00:00
Chop & Cog
This commit is contained in:
@@ -7,7 +7,7 @@ Done: 15, Todo: 2
|
||||
✔️ *DMI - Directional Movement Index (DI+, DI-)
|
||||
✔️ DMX - Jurik Directional Movement Index
|
||||
✔️ DPO - Detrended Price Oscillator
|
||||
*MACD - Moving Average Convergence/Divergence (MACD, Signal, Histogram)
|
||||
✔️ *MACD - Moving Average Convergence/Divergence (MACD, Signal, Histogram)
|
||||
✔️ MOM - Momentum
|
||||
✔️ PMO - Price Momentum Oscillator
|
||||
✔️ PO - Price Oscillator
|
||||
|
||||
@@ -0,0 +1,111 @@
|
||||
using System.Runtime.CompilerServices;
|
||||
namespace QuanTAlib;
|
||||
|
||||
/// <summary>
|
||||
/// CHOP: Choppiness Index
|
||||
/// A technical indicator that measures the market's trendiness versus choppiness.
|
||||
/// It helps determine if the market is trending or moving sideways by comparing
|
||||
/// the total movement to the net directional movement over a period.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// The CHOP calculation process:
|
||||
/// 1. Calculate ATR sum over period
|
||||
/// 2. Calculate total price range over period
|
||||
/// 3. Scale result to oscillate between 0 and 100
|
||||
///
|
||||
/// Key characteristics:
|
||||
/// - Oscillates between 0 and 100
|
||||
/// - Values above 61.8 indicate choppy market
|
||||
/// - Values below 38.2 indicate trending market
|
||||
/// - Based on ATR and price range
|
||||
/// - Higher values = more choppy/sideways
|
||||
/// - Lower values = more trending
|
||||
///
|
||||
/// Formula:
|
||||
/// CHOP = 100 * LOG10(SUM(ATR,n)/(HIGH(n)-LOW(n))) / LOG10(n)
|
||||
/// where:
|
||||
/// n = period
|
||||
/// ATR = Average True Range
|
||||
/// HIGH(n) = Highest high over period n
|
||||
/// LOW(n) = Lowest low over period n
|
||||
///
|
||||
/// Sources:
|
||||
/// E.W. Dreiss
|
||||
/// https://www.tradingview.com/support/solutions/43000501980-choppiness-index/
|
||||
///
|
||||
/// Note: Default period is 14
|
||||
/// </remarks>
|
||||
|
||||
[SkipLocalsInit]
|
||||
public sealed class Chop : AbstractBase
|
||||
{
|
||||
private readonly Atr _atr;
|
||||
private readonly CircularBuffer _highs;
|
||||
private readonly CircularBuffer _lows;
|
||||
private readonly CircularBuffer _atrValues;
|
||||
private readonly double _logPeriod;
|
||||
private const int DefaultPeriod = 14;
|
||||
private const double ScalingFactor = 100.0;
|
||||
|
||||
/// <param name="period">The number of periods used in the CHOP calculation (default 14).</param>
|
||||
/// <exception cref="ArgumentOutOfRangeException">Thrown when period is less than 1.</exception>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public Chop(int period = DefaultPeriod)
|
||||
{
|
||||
if (period < 1)
|
||||
throw new ArgumentOutOfRangeException(nameof(period));
|
||||
|
||||
_atr = new(period);
|
||||
_highs = new(period);
|
||||
_lows = new(period);
|
||||
_atrValues = new(period);
|
||||
_logPeriod = Math.Log10(period);
|
||||
WarmupPeriod = period;
|
||||
Name = $"CHOP({period})";
|
||||
}
|
||||
|
||||
/// <param name="source">The data source object that publishes updates.</param>
|
||||
/// <param name="period">The number of periods used in the CHOP calculation.</param>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public Chop(object source, int period = DefaultPeriod) : this(period)
|
||||
{
|
||||
var pubEvent = source.GetType().GetEvent("Pub");
|
||||
pubEvent?.AddEventHandler(source, new BarSignal(Sub));
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
protected override void ManageState(bool isNew)
|
||||
{
|
||||
if (isNew)
|
||||
_index++;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
|
||||
protected override double Calculation()
|
||||
{
|
||||
ManageState(BarInput.IsNew);
|
||||
|
||||
// Calculate ATR and store it
|
||||
double atr = _atr.Calc(BarInput);
|
||||
_atrValues.Add(atr, BarInput.IsNew);
|
||||
|
||||
// Store high and low prices
|
||||
_highs.Add(BarInput.High, BarInput.IsNew);
|
||||
_lows.Add(BarInput.Low, BarInput.IsNew);
|
||||
|
||||
// Calculate highest high and lowest low over period
|
||||
double highestHigh = _highs.Max();
|
||||
double lowestLow = _lows.Min();
|
||||
double range = highestHigh - lowestLow;
|
||||
|
||||
// Calculate sum of ATR values
|
||||
double atrSum = _atrValues.Sum();
|
||||
|
||||
// Avoid division by zero
|
||||
if (range < double.Epsilon || _logPeriod < double.Epsilon)
|
||||
return 0.0;
|
||||
|
||||
// Calculate CHOP
|
||||
return ScalingFactor * Math.Log10(atrSum / range) / _logPeriod;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,101 @@
|
||||
using System.Runtime.CompilerServices;
|
||||
namespace QuanTAlib;
|
||||
|
||||
/// <summary>
|
||||
/// COG: Ehler's Center of Gravity Oscillator
|
||||
/// A momentum oscillator that uses the concept of center of gravity from physics
|
||||
/// to measure price momentum. It calculates a weighted sum where more recent
|
||||
/// prices have higher weights.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// The COG calculation process:
|
||||
/// 1. Calculate weighted sum of prices (numerator)
|
||||
/// 2. Calculate sum of weights (denominator)
|
||||
/// 3. Divide to get center of gravity
|
||||
/// 4. Invert and normalize result
|
||||
///
|
||||
/// Key characteristics:
|
||||
/// - Oscillates around zero
|
||||
/// - Leading indicator (less lag than traditional momentum)
|
||||
/// - Positive values indicate upward momentum
|
||||
/// - Negative values indicate downward momentum
|
||||
/// - Zero line crossovers signal trend changes
|
||||
///
|
||||
/// Formula:
|
||||
/// COG = -((Σ(Price(i) * i)) / (Σ(Price(i))) - (period + 1)/2)
|
||||
/// where:
|
||||
/// i = position in period (1 to period)
|
||||
/// Price(i) = price at position i
|
||||
///
|
||||
/// Sources:
|
||||
/// John F. Ehlers - "Cybernetic Analysis for Stocks and Futures"
|
||||
/// https://www.mesasoftware.com/papers/CenterOfGravity.pdf
|
||||
///
|
||||
/// Note: Default period is 10
|
||||
/// </remarks>
|
||||
|
||||
[SkipLocalsInit]
|
||||
public sealed class Cog : AbstractBase
|
||||
{
|
||||
private readonly CircularBuffer _prices;
|
||||
private readonly int _period;
|
||||
private const int DefaultPeriod = 10;
|
||||
|
||||
/// <param name="period">The number of periods used in the COG calculation (default 10).</param>
|
||||
/// <exception cref="ArgumentOutOfRangeException">Thrown when period is less than 1.</exception>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public Cog(int period = DefaultPeriod)
|
||||
{
|
||||
if (period < 1)
|
||||
throw new ArgumentOutOfRangeException(nameof(period));
|
||||
|
||||
_period = period;
|
||||
_prices = new(period);
|
||||
WarmupPeriod = period;
|
||||
Name = $"COG({period})";
|
||||
}
|
||||
|
||||
/// <param name="source">The data source object that publishes updates.</param>
|
||||
/// <param name="period">The number of periods used in the COG calculation.</param>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public Cog(object source, int period = DefaultPeriod) : 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)
|
||||
_index++;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
|
||||
protected override double Calculation()
|
||||
{
|
||||
ManageState(Input.IsNew);
|
||||
|
||||
// Add new price to buffer
|
||||
_prices.Add(Input.Value, Input.IsNew);
|
||||
|
||||
double numerator = 0.0;
|
||||
double denominator = 0.0;
|
||||
|
||||
// Calculate weighted sums
|
||||
for (int i = 0; i < _prices.Count; i++)
|
||||
{
|
||||
double price = _prices[i];
|
||||
double weight = i + 1;
|
||||
numerator += price * weight;
|
||||
denominator += price;
|
||||
}
|
||||
|
||||
// Avoid division by zero
|
||||
if (Math.Abs(denominator) < double.Epsilon)
|
||||
return 0.0;
|
||||
|
||||
// Calculate center of gravity and normalize
|
||||
return -((numerator / denominator) - (_period + 1.0) / 2.0);
|
||||
}
|
||||
}
|
||||
@@ -1,15 +1,15 @@
|
||||
# Oscillators indicators
|
||||
Done: 6, Todo: 23
|
||||
Done: 11, Todo: 18
|
||||
|
||||
✔️ AC - Acceleration Oscillator
|
||||
✔️ AO - Awesome Oscillator
|
||||
✔️ *AROON - Aroon oscillator (Up, Down)
|
||||
,
|
||||
CCI - Commodity Channel Index
|
||||
CFO - Chande Forcast Oscillator
|
||||
BOP - Balance of Power
|
||||
✔️ CCI - Commodity Channel Index
|
||||
✔️ CFO - Chande Forcast Oscillator
|
||||
✔️ CMO - Chande Momentum Oscillator
|
||||
CHOP - Choppiness Index
|
||||
COG - Ehler's Center of Gravity
|
||||
✔️ CHOP - Choppiness Index
|
||||
✔️ COG - Ehler's Center of Gravity
|
||||
COPPOCK - Coppock Curve
|
||||
CRSI - Connor RSI
|
||||
CTI - Ehler's Correlation Trend Indicator
|
||||
|
||||
Reference in New Issue
Block a user