2024-10-05 15:20:13 -07:00
namespace QuanTAlib ;
2024-09-22 17:31:24 -07:00
2024-10-05 15:20:13 -07:00
/// <summary>
/// Represents a minimum value calculator with optional decay over a specified period.
/// This class calculates the minimum value within a given period, with the ability to
/// apply a decay factor to give more weight to recent values.
/// </summary>
/// <remarks>
/// The Min class uses a circular buffer to store values and calculates the minimum
/// efficiently. It also implements a decay mechanism to adjust the minimum value over
/// time, allowing for a more responsive indicator in changing market conditions.
2024-10-11 18:02:09 -07:00
///
/// The decay factor allows the indicator to "forget" old minimum values gradually,
/// which can be useful in adapting to new price trends or market regimes.
2024-10-05 15:20:13 -07:00
/// </remarks>
2024-10-06 06:59:26 +00:00
public class Min : AbstractBase
{
2024-10-11 18:02:09 -07:00
/// <summary>
/// The number of data points to consider for the minimum calculation.
/// </summary>
2024-10-05 15:20:13 -07:00
private readonly int Period ;
2024-10-11 18:02:09 -07:00
/// <summary>
/// Circular buffer to store the most recent data points.
/// </summary>
2024-10-05 15:20:13 -07:00
private readonly CircularBuffer _buffer ;
2024-10-11 18:02:09 -07:00
/// <summary>
/// The half-life decay factor used to gradually forget old minimums.
/// </summary>
2024-10-05 15:20:13 -07:00
private readonly double _halfLife ;
2024-10-11 18:02:09 -07:00
/// <summary>
/// The current minimum value.
/// </summary>
private double _currentMin ;
/// <summary>
/// The previous minimum value.
/// </summary>
private double _p_currentMin ;
/// <summary>
/// The number of periods since a new minimum was set.
/// </summary>
private int _timeSinceNewMin ;
/// <summary>
/// The previous value of _timeSinceNewMin.
/// </summary>
private int _p_timeSinceNewMin ;
2024-09-22 17:31:24 -07:00
2024-10-05 15:20:13 -07:00
/// <summary>
/// Initializes a new instance of the Min class with the specified period and decay.
/// </summary>
/// <param name="period">The period over which to calculate the minimum value.</param>
2024-10-11 18:02:09 -07:00
/// <param name="decay">The decay factor to apply to older values. Higher values cause faster forgetting of old minimums. Default is 0 (no decay).</param>
2024-10-05 15:20:13 -07:00
/// <exception cref="ArgumentOutOfRangeException">
/// Thrown when period is less than 1 or decay is negative.
/// </exception>
2024-10-06 14:44:43 -07:00
public Min ( int period , double decay = 0 )
2024-10-06 06:59:26 +00:00
{
if ( period < 1 )
{
2024-10-05 15:20:13 -07:00
throw new ArgumentOutOfRangeException ( nameof ( period ), "Period must be greater than or equal to 1." );
2024-09-22 17:31:24 -07:00
}
2024-10-06 06:59:26 +00:00
if ( decay < 0 )
{
2024-10-05 15:20:13 -07:00
throw new ArgumentOutOfRangeException ( nameof ( decay ), "Half-life must be non-negative." );
2024-09-22 17:31:24 -07:00
}
2024-10-05 15:20:13 -07:00
Period = period ;
WarmupPeriod = 0 ;
_buffer = new CircularBuffer ( period );
_halfLife = decay * 0.1 ;
Name = $"Min(period={period}, halfLife={decay:F2})" ;
Init ();
}
2024-09-22 17:31:24 -07:00
2024-10-05 15:20:13 -07:00
/// <summary>
/// Initializes a new instance of the Min class with the specified source, period, and decay.
/// </summary>
/// <param name="source">The source object to subscribe to for value updates.</param>
/// <param name="period">The period over which to calculate the minimum value.</param>
2024-10-11 18:02:09 -07:00
/// <param name="decay">The decay factor to apply to older values. Higher values cause faster forgetting of old minimums. Default is 0 (no decay).</param>
2024-10-06 06:59:26 +00:00
public Min ( object source , int period , double decay = 0 ) : this ( period , decay )
{
2024-10-05 15:20:13 -07:00
var pubEvent = source . GetType (). GetEvent ( "Pub" );
pubEvent ?. AddEventHandler ( source , new ValueSignal ( Sub ));
}
/// <summary>
/// Initializes the Min instance by setting initial values.
/// </summary>
2024-10-06 06:59:26 +00:00
public override void Init ()
{
2024-10-05 15:20:13 -07:00
base . Init ();
_currentMin = double . MaxValue ;
_timeSinceNewMin = 0 ;
}
/// <summary>
/// Manages the state of the Min instance based on whether a new value is being processed.
/// </summary>
/// <param name="isNew">Indicates whether the current input is a new value.</param>
2024-10-06 06:59:26 +00:00
protected override void ManageState ( bool isNew )
{
if ( isNew )
{
2024-10-05 15:20:13 -07:00
_p_currentMin = _currentMin ;
_lastValidValue = Input . Value ;
_index ++;
_timeSinceNewMin ++;
_p_timeSinceNewMin = _timeSinceNewMin ;
2024-10-06 06:59:26 +00:00
}
else
{
2024-10-05 15:20:13 -07:00
_currentMin = _p_currentMin ;
_timeSinceNewMin = _p_timeSinceNewMin ;
}
}
/// <summary>
/// Performs the minimum value calculation with decay.
/// </summary>
/// <returns>The calculated minimum value for the current period.</returns>
/// <remarks>
/// This method updates the current minimum value based on the input, applies the decay
/// factor, and ensures the result is not lower than the actual minimum in the buffer.
/// The decay rate is calculated using an exponential function based on the time since
/// the last new minimum and the specified half-life.
/// </remarks>
2024-10-06 06:59:26 +00:00
protected override double Calculation ()
{
2024-10-05 15:20:13 -07:00
ManageState ( Input . IsNew );
_buffer . Add ( Input . Value , Input . IsNew );
2024-10-06 06:59:26 +00:00
if ( Input . Value <= _currentMin )
{
2024-10-05 15:20:13 -07:00
_currentMin = Input . Value ;
2024-09-22 17:31:24 -07:00
_timeSinceNewMin = 0 ;
}
2024-10-05 15:20:13 -07:00
double decayRate = 1 - Math . Exp (- _halfLife * _timeSinceNewMin / Period );
2024-10-06 14:44:43 -07:00
_currentMin += decayRate * ( _buffer . Average () - _currentMin );
2024-10-05 15:20:13 -07:00
_currentMin = Math . Max ( _currentMin , _buffer . Min ());
2024-09-22 17:31:24 -07:00
2024-10-05 15:20:13 -07:00
IsHot = true ;
return _currentMin ;
2024-09-22 17:31:24 -07:00
}
2024-10-05 15:20:13 -07:00
}