Files
QuanTAlib/lib/volatility/Rvi.cs
T

119 lines
3.6 KiB
C#
Raw Normal View History

2024-10-27 09:38:53 -07:00
using System;
2024-10-05 15:20:13 -07:00
namespace QuanTAlib;
2024-10-04 21:31:25 -07:00
2024-10-05 15:20:13 -07:00
/// <summary>
2024-10-27 09:38:53 -07:00
/// RVI: Relative Volatility Index
/// A technical indicator developed by Donald Dorsey that measures the direction
/// of volatility by comparing upward and downward price movements. RVI helps
/// identify whether volatility is increasing more in up or down moves.
2024-10-05 15:20:13 -07:00
/// </summary>
/// <remarks>
2024-10-27 09:38:53 -07:00
/// The RVI calculation process:
/// 1. Separates price changes into up/down moves
/// 2. Calculates standard deviation for each
/// 3. Applies moving average smoothing
/// 4. Computes relative strength ratio
/// 5. Scales to percentage (0-100)
2024-10-05 15:20:13 -07:00
///
2024-10-27 09:38:53 -07:00
/// Key characteristics:
/// - Oscillator (0-100 range)
/// - Directional volatility measure
/// - Combines volatility and momentum
/// - Uses standard deviation
/// - Smoothed output
///
/// Formula:
/// RVI = 100 * SMA(StdDev(upMoves)) / (SMA(StdDev(upMoves)) + SMA(StdDev(downMoves)))
/// where:
/// upMove = max(close - prevClose, 0)
/// downMove = max(prevClose - close, 0)
///
/// Market Applications:
/// - Trend confirmation
/// - Divergence analysis
/// - Volatility breakouts
/// - Market reversals
/// - Overbought/oversold levels
///
/// Sources:
/// Donald Dorsey - "Technical Analysis of Stocks & Commodities" (1993)
/// https://www.investopedia.com/terms/r/relative_volatility_index.asp
///
/// Note: Similar concept to RSI but using volatility
2024-10-05 15:20:13 -07:00
/// </remarks>
2024-10-27 09:38:53 -07:00
2024-10-06 06:59:26 +00:00
public class Rvi : AbstractBase
{
2024-10-06 14:44:43 -07:00
private readonly Stddev _upStdDev, _downStdDev;
private readonly Sma _upSma, _downSma;
2024-10-05 15:20:13 -07:00
private double _previousClose;
2024-10-04 21:31:25 -07:00
2024-10-27 09:38:53 -07:00
/// <param name="period">The number of periods for RVI calculation.</param>
/// <exception cref="ArgumentOutOfRangeException">Thrown when period is less than 2.</exception>
2024-10-06 14:44:43 -07:00
public Rvi(int period)
2024-10-06 06:59:26 +00:00
{
if (period < 2)
{
2024-10-27 09:38:53 -07:00
throw new ArgumentOutOfRangeException(nameof(period),
"Period must be greater than or equal to 2.");
2024-10-04 21:31:25 -07:00
}
2024-10-08 13:59:33 -07:00
int Period = period;
2024-10-05 15:20:13 -07:00
WarmupPeriod = period;
Name = $"RVI(period={period})";
_upStdDev = new Stddev(Period);
_downStdDev = new Stddev(Period);
_upSma = new(Period);
_downSma = new(Period);
Init();
}
2024-10-04 21:31:25 -07:00
2024-10-27 09:38:53 -07:00
/// <param name="source">The data source object that publishes updates.</param>
/// <param name="period">The number of periods for RVI calculation.</param>
2024-10-06 06:59:26 +00:00
public Rvi(object source, int period) : this(period)
{
2024-10-05 15:20:13 -07:00
var pubEvent = source.GetType().GetEvent("Pub");
pubEvent?.AddEventHandler(source, new ValueSignal(Sub));
}
2024-10-04 21:31:25 -07:00
2024-10-06 06:59:26 +00:00
public override void Init()
{
2024-10-05 15:20:13 -07:00
base.Init();
_previousClose = 0;
}
2024-10-04 21:31:25 -07:00
2024-10-06 06:59:26 +00:00
protected override void ManageState(bool isNew)
{
if (isNew)
{
2024-10-05 15:20:13 -07:00
_lastValidValue = Value;
_index++;
2024-10-04 21:31:25 -07:00
}
}
2024-10-05 15:20:13 -07:00
2024-10-06 06:59:26 +00:00
protected override double Calculation()
{
2024-10-05 15:20:13 -07:00
ManageState(Input.IsNew);
double close = Input.Value;
double change = close - _previousClose;
2024-10-27 09:38:53 -07:00
// Separate into up and down moves
2024-10-05 15:20:13 -07:00
double upMove = Math.Max(change, 0);
double downMove = Math.Max(-change, 0);
2024-10-27 09:38:53 -07:00
// Calculate standard deviations and apply smoothing
2024-10-05 15:20:13 -07:00
_upSma.Calc(_upStdDev.Calc(new TValue(Input.Time, upMove, Input.IsNew)));
_downSma.Calc(_downStdDev.Calc(new TValue(Input.Time, downMove, Input.IsNew)));
2024-10-27 09:38:53 -07:00
// Calculate RVI ratio
2024-10-05 15:20:13 -07:00
double rvi;
2024-10-27 09:38:53 -07:00
rvi = (_upSma.Value + _downSma.Value != 0)
? 100 * _upSma.Value / (_upSma.Value + _downSma.Value)
: 0;
2024-10-05 15:20:13 -07:00
_previousClose = close;
IsHot = _index >= WarmupPeriod;
return rvi;
}
}