mirror of
https://github.com/mihakralj/QuanTAlib.git
synced 2026-08-25 05:48:06 +00:00
feat: Dpo, Tsi, Vortex, Bpp, Cci, Cfo, Tr, Ui, Vc, Vov, Vr, Vs, Mfi, Nvi, Obv, Pvi, Pvo, Pvol, Pvr, Pvt, Tvi
This commit is contained in:
@@ -0,0 +1,112 @@
|
||||
using System.Runtime.CompilerServices;
|
||||
namespace QuanTAlib;
|
||||
|
||||
/// <summary>
|
||||
/// DPO: Detrended Price Oscillator
|
||||
/// A momentum indicator that removes the trend from price by comparing the current price
|
||||
/// to a past moving average, helping to identify cycles in the price.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// The DPO calculation process:
|
||||
/// 1. Calculate the period shifted back by (period / 2 + 1) days
|
||||
/// 2. Calculate SMA for the shifted period
|
||||
/// 3. DPO = Price - SMA(Price, period) shifted back
|
||||
///
|
||||
/// Key characteristics:
|
||||
/// - Removes long-term trends
|
||||
/// - Helps identify cycles
|
||||
/// - Oscillates above and below zero
|
||||
/// - Default period is 20 days
|
||||
/// - Uses price displacement
|
||||
///
|
||||
/// Formula:
|
||||
/// DPO = Price - SMA(Price, period) shifted (period/2 + 1) bars back
|
||||
///
|
||||
/// Market Applications:
|
||||
/// - Cycle identification
|
||||
/// - Overbought/Oversold conditions
|
||||
/// - Price momentum
|
||||
/// - Trading signals
|
||||
/// - Market timing
|
||||
///
|
||||
/// Sources:
|
||||
/// Donald Dorsey - Original development
|
||||
/// https://www.investopedia.com/terms/d/detrended-price-oscillator-dpo.asp
|
||||
///
|
||||
/// Note: DPO helps identify cycles by removing the trend component from the price data
|
||||
/// </remarks>
|
||||
|
||||
[SkipLocalsInit]
|
||||
public sealed class Dpo : AbstractBase
|
||||
{
|
||||
private readonly int _shift;
|
||||
private readonly CircularBuffer _prices;
|
||||
private readonly CircularBuffer _sma;
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public Dpo(int period = 20)
|
||||
{
|
||||
_shift = period / 2 + 1;
|
||||
WarmupPeriod = period + _shift;
|
||||
Name = $"DPO({period})";
|
||||
_prices = new CircularBuffer(WarmupPeriod);
|
||||
_sma = new CircularBuffer(period);
|
||||
Init();
|
||||
}
|
||||
|
||||
/// <param name="source">The data source object that publishes updates.</param>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public Dpo(object source, int period = 20) : this(period)
|
||||
{
|
||||
var pubEvent = source.GetType().GetEvent("Pub");
|
||||
pubEvent?.AddEventHandler(source, new BarSignal(Sub));
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public override void Init()
|
||||
{
|
||||
base.Init();
|
||||
_prices.Clear();
|
||||
_sma.Clear();
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
protected override void ManageState(bool isNew)
|
||||
{
|
||||
if (isNew)
|
||||
{
|
||||
_lastValidValue = Value;
|
||||
_index++;
|
||||
}
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
|
||||
protected override double Calculation()
|
||||
{
|
||||
ManageState(BarInput.IsNew);
|
||||
|
||||
// Add current price to buffer
|
||||
_prices.Add(BarInput.Close);
|
||||
|
||||
// Need enough prices for the shifted SMA calculation
|
||||
if (_index <= _shift)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Add price from shift periods ago to SMA buffer
|
||||
_sma.Add(_prices[_shift]);
|
||||
|
||||
// Need enough prices for full calculation
|
||||
if (_index <= WarmupPeriod)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Calculate DPO
|
||||
double dpo = BarInput.Close - _sma.Average();
|
||||
|
||||
IsHot = _index >= WarmupPeriod;
|
||||
return dpo;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,151 @@
|
||||
using System.Runtime.CompilerServices;
|
||||
namespace QuanTAlib;
|
||||
|
||||
/// <summary>
|
||||
/// TSI: True Strength Index
|
||||
/// A momentum indicator that shows both trend direction and overbought/oversold conditions
|
||||
/// by using two smoothing steps on price changes.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// The TSI calculation process:
|
||||
/// 1. Calculate price change (PC):
|
||||
/// PC = Close - Previous Close
|
||||
/// 2. Calculate absolute price change (APC):
|
||||
/// APC = |PC|
|
||||
/// 3. Double smooth both PC and APC using EMA:
|
||||
/// First PC EMA = EMA(PC, firstPeriod)
|
||||
/// Second PC EMA = EMA(First PC EMA, secondPeriod)
|
||||
/// First APC EMA = EMA(APC, firstPeriod)
|
||||
/// Second APC EMA = EMA(First APC EMA, secondPeriod)
|
||||
/// 4. Calculate TSI:
|
||||
/// TSI = (Second PC EMA / Second APC EMA) * 100
|
||||
///
|
||||
/// Key characteristics:
|
||||
/// - Double smoothed momentum indicator
|
||||
/// - Oscillates between +100 and -100
|
||||
/// - Default periods are 25 and 13
|
||||
/// - Shows trend direction
|
||||
/// - Identifies overbought/oversold
|
||||
///
|
||||
/// Formula:
|
||||
/// TSI = (EMA(EMA(PC, r), s) / EMA(EMA(|PC|, r), s)) * 100
|
||||
/// where:
|
||||
/// PC = Close - Previous Close
|
||||
/// r = first period (default 25)
|
||||
/// s = second period (default 13)
|
||||
///
|
||||
/// Market Applications:
|
||||
/// - Trend direction
|
||||
/// - Overbought/Oversold levels
|
||||
/// - Centerline crossovers
|
||||
/// - Divergence analysis
|
||||
/// - Signal line crossovers
|
||||
///
|
||||
/// Sources:
|
||||
/// William Blau - Original development (1991)
|
||||
/// https://www.investopedia.com/terms/t/tsi.asp
|
||||
///
|
||||
/// Note: Values above +25 indicate overbought conditions, while values below -25 indicate oversold conditions
|
||||
/// </remarks>
|
||||
|
||||
[SkipLocalsInit]
|
||||
public sealed class Tsi : AbstractBase
|
||||
{
|
||||
private readonly int _firstPeriod;
|
||||
private double _prevClose;
|
||||
private double _pcFirstEma;
|
||||
private double _pcSecondEma;
|
||||
private double _apcFirstEma;
|
||||
private double _apcSecondEma;
|
||||
private readonly double _firstAlpha;
|
||||
private readonly double _secondAlpha;
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public Tsi(int firstPeriod = 25, int secondPeriod = 13)
|
||||
{
|
||||
_firstPeriod = firstPeriod;
|
||||
WarmupPeriod = firstPeriod + secondPeriod;
|
||||
Name = $"TSI({_firstPeriod},{secondPeriod})";
|
||||
_firstAlpha = 2.0 / (firstPeriod + 1);
|
||||
_secondAlpha = 2.0 / (secondPeriod + 1);
|
||||
Init();
|
||||
}
|
||||
|
||||
/// <param name="source">The data source object that publishes updates.</param>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public Tsi(object source, int firstPeriod = 25, int secondPeriod = 13) : this(firstPeriod, secondPeriod)
|
||||
{
|
||||
var pubEvent = source.GetType().GetEvent("Pub");
|
||||
pubEvent?.AddEventHandler(source, new BarSignal(Sub));
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public override void Init()
|
||||
{
|
||||
base.Init();
|
||||
_prevClose = 0;
|
||||
_pcFirstEma = 0;
|
||||
_pcSecondEma = 0;
|
||||
_apcFirstEma = 0;
|
||||
_apcSecondEma = 0;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
protected override void ManageState(bool isNew)
|
||||
{
|
||||
if (isNew)
|
||||
{
|
||||
_lastValidValue = Value;
|
||||
_index++;
|
||||
}
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
|
||||
protected override double Calculation()
|
||||
{
|
||||
ManageState(BarInput.IsNew);
|
||||
|
||||
// Skip first period to establish previous close
|
||||
if (_index == 1)
|
||||
{
|
||||
_prevClose = BarInput.Close;
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Calculate price changes
|
||||
double pc = BarInput.Close - _prevClose;
|
||||
double apc = Math.Abs(pc);
|
||||
|
||||
// Initialize or update EMAs
|
||||
if (_index <= _firstPeriod)
|
||||
{
|
||||
_pcFirstEma = pc;
|
||||
_apcFirstEma = apc;
|
||||
}
|
||||
else
|
||||
{
|
||||
_pcFirstEma = (_firstAlpha * pc) + ((1 - _firstAlpha) * _pcFirstEma);
|
||||
_apcFirstEma = (_firstAlpha * apc) + ((1 - _firstAlpha) * _apcFirstEma);
|
||||
}
|
||||
|
||||
if (_index <= WarmupPeriod)
|
||||
{
|
||||
_pcSecondEma = _pcFirstEma;
|
||||
_apcSecondEma = _apcFirstEma;
|
||||
}
|
||||
else
|
||||
{
|
||||
_pcSecondEma = (_secondAlpha * _pcFirstEma) + ((1 - _secondAlpha) * _pcSecondEma);
|
||||
_apcSecondEma = (_secondAlpha * _apcFirstEma) + ((1 - _secondAlpha) * _apcSecondEma);
|
||||
}
|
||||
|
||||
// Store current close for next calculation
|
||||
_prevClose = BarInput.Close;
|
||||
|
||||
// Calculate TSI
|
||||
double tsi = Math.Abs(_apcSecondEma) > double.Epsilon ? (_pcSecondEma / _apcSecondEma) * 100 : 0;
|
||||
|
||||
IsHot = _index >= WarmupPeriod;
|
||||
return tsi;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,162 @@
|
||||
using System.Runtime.CompilerServices;
|
||||
namespace QuanTAlib;
|
||||
|
||||
/// <summary>
|
||||
/// VORTEX: Vortex Indicator
|
||||
/// A technical indicator consisting of two oscillating lines that identify trend reversals
|
||||
/// and confirm current trends based on the highs and lows of the previous period.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// The Vortex calculation process:
|
||||
/// 1. Calculate True Range (TR):
|
||||
/// TR = max(High - Low, |High - Previous Close|, |Low - Previous Close|)
|
||||
/// 2. Calculate +VM (Positive Movement):
|
||||
/// +VM = |Current High - Previous Low|
|
||||
/// 3. Calculate -VM (Negative Movement):
|
||||
/// -VM = |Current Low - Previous High|
|
||||
/// 4. Calculate period sums:
|
||||
/// TR Period Sum = Sum(TR, period)
|
||||
/// +VM Period Sum = Sum(+VM, period)
|
||||
/// -VM Period Sum = Sum(-VM, period)
|
||||
/// 5. Calculate +VI and -VI:
|
||||
/// +VI = +VM Period Sum / TR Period Sum
|
||||
/// -VI = -VM Period Sum / TR Period Sum
|
||||
///
|
||||
/// Key characteristics:
|
||||
/// - Two oscillating lines (+VI and -VI)
|
||||
/// - No upper or lower bounds
|
||||
/// - Default period is 14 days
|
||||
/// - Crossovers signal trend changes
|
||||
/// - Uses true range normalization
|
||||
///
|
||||
/// Formula:
|
||||
/// +VI = Sum(+VM, period) / Sum(TR, period)
|
||||
/// -VI = Sum(-VM, period) / Sum(TR, period)
|
||||
///
|
||||
/// Market Applications:
|
||||
/// - Trend identification
|
||||
/// - Trend reversals
|
||||
/// - Trend confirmation
|
||||
/// - Trading signals
|
||||
/// - Market momentum
|
||||
///
|
||||
/// Sources:
|
||||
/// Etienne Botes and Douglas Siepman - Original development (2010)
|
||||
/// https://www.investopedia.com/terms/v/vortex-indicator-vi.asp
|
||||
///
|
||||
/// Note: When +VI crosses above -VI, it signals a potential uptrend, and vice versa
|
||||
/// </remarks>
|
||||
|
||||
[SkipLocalsInit]
|
||||
public sealed class Vortex : AbstractBase
|
||||
{
|
||||
private readonly CircularBuffer _tr;
|
||||
private readonly CircularBuffer _vmPlus;
|
||||
private readonly CircularBuffer _vmMinus;
|
||||
private double _prevHigh;
|
||||
private double _prevLow;
|
||||
private double _prevClose;
|
||||
public double _viPlus { get; set; }
|
||||
public double _viMinus { get; set; }
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public Vortex(int period = 14)
|
||||
{
|
||||
WarmupPeriod = period + 1; // Need one extra period for previous values
|
||||
Name = $"VORTEX({period})";
|
||||
_tr = new CircularBuffer(period);
|
||||
_vmPlus = new CircularBuffer(period);
|
||||
_vmMinus = new CircularBuffer(period);
|
||||
Init();
|
||||
}
|
||||
|
||||
/// <param name="source">The data source object that publishes updates.</param>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public Vortex(object source, int period = 14) : this(period)
|
||||
{
|
||||
var pubEvent = source.GetType().GetEvent("Pub");
|
||||
pubEvent?.AddEventHandler(source, new BarSignal(Sub));
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public override void Init()
|
||||
{
|
||||
base.Init();
|
||||
_prevHigh = 0;
|
||||
_prevLow = 0;
|
||||
_prevClose = 0;
|
||||
_viPlus = 0;
|
||||
_viMinus = 0;
|
||||
_tr.Clear();
|
||||
_vmPlus.Clear();
|
||||
_vmMinus.Clear();
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
protected override void ManageState(bool isNew)
|
||||
{
|
||||
if (isNew)
|
||||
{
|
||||
_lastValidValue = Value;
|
||||
_index++;
|
||||
}
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
|
||||
protected override double Calculation()
|
||||
{
|
||||
ManageState(BarInput.IsNew);
|
||||
|
||||
// Skip first period to establish previous values
|
||||
if (_index == 1)
|
||||
{
|
||||
_prevHigh = BarInput.High;
|
||||
_prevLow = BarInput.Low;
|
||||
_prevClose = BarInput.Close;
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Calculate True Range
|
||||
double tr = Math.Max(BarInput.High - BarInput.Low,
|
||||
Math.Max(Math.Abs(BarInput.High - _prevClose),
|
||||
Math.Abs(BarInput.Low - _prevClose)));
|
||||
|
||||
// Calculate VM+ and VM-
|
||||
double vmPlus = Math.Abs(BarInput.High - _prevLow);
|
||||
double vmMinus = Math.Abs(BarInput.Low - _prevHigh);
|
||||
|
||||
// Add values to buffers
|
||||
_tr.Add(tr);
|
||||
_vmPlus.Add(vmPlus);
|
||||
_vmMinus.Add(vmMinus);
|
||||
|
||||
// Calculate VI+ and VI-
|
||||
double trSum = _tr.Sum();
|
||||
if (Math.Abs(trSum) > double.Epsilon)
|
||||
{
|
||||
_viPlus = _vmPlus.Sum() / trSum;
|
||||
_viMinus = _vmMinus.Sum() / trSum;
|
||||
}
|
||||
|
||||
// Store current values for next calculation
|
||||
_prevHigh = BarInput.High;
|
||||
_prevLow = BarInput.Low;
|
||||
_prevClose = BarInput.Close;
|
||||
|
||||
// Return the difference between VI+ and VI-
|
||||
double vortex = _viPlus - _viMinus;
|
||||
|
||||
IsHot = _index >= WarmupPeriod;
|
||||
return vortex;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the positive Vortex line (VI+)
|
||||
/// </summary>
|
||||
public double ViPlus => _viPlus;
|
||||
|
||||
/// <summary>
|
||||
/// Gets the negative Vortex line (VI-)
|
||||
/// </summary>
|
||||
public double ViMinus => _viMinus;
|
||||
}
|
||||
@@ -1,20 +1,20 @@
|
||||
# Momentum indicators
|
||||
Done: 12, Todo: 5
|
||||
Done: 15, Todo: 2
|
||||
|
||||
✔️ ADX - Average Directional Movement Index
|
||||
✔️ ADXR - Average Directional Movement Index Rating
|
||||
✔️ APO - Absolute Price Oscillator
|
||||
✔️ DMI - Directional Movement Index
|
||||
✔️ *DMI - Directional Movement Index (DI+, DI-)
|
||||
✔️ DMX - Jurik Directional Movement Index
|
||||
DPO - Detrended Price Oscillator
|
||||
MACD - Moving Average Convergence/Divergence
|
||||
✔️ DPO - Detrended Price Oscillator
|
||||
*MACD - Moving Average Convergence/Divergence (MACD, Signal, Histogram)
|
||||
✔️ MOM - Momentum
|
||||
✔️ PMO - Price Momentum Oscillator
|
||||
✔️ PO - Price Oscillator
|
||||
✔️ PPO - Percentage Price Oscillator
|
||||
✔️ PRS - Price Relative Strength
|
||||
✔️ ROC - Rate of Change
|
||||
TSI - True Strength Index
|
||||
✔️ TSI - True Strength Index
|
||||
✔️ TRIX - 1-day ROC of TEMA
|
||||
✔️ VEL - Jurik Signal Velocity
|
||||
VORTEX - Vortex Indicator
|
||||
✔️ *VORTEX - Vortex Indicator (VI+, VI-)
|
||||
|
||||
Reference in New Issue
Block a user