Files
QuanTAlib/lib/volatility/Atr.cs
T

111 lines
3.4 KiB
C#
Raw Normal View History

2024-09-30 06:46:07 -07:00
namespace QuanTAlib;
2024-10-05 15:20:13 -07:00
/// <summary>
/// Represents an Average True Range (ATR) calculator, a measure of market volatility.
/// </summary>
/// <remarks>
2024-10-21 16:06:47 -07:00
/// The ATR class calculates the average true range using a Relative Moving Average (RMA)
2024-10-05 15:20:13 -07:00
/// of the true range. The true range is the greatest of: current high - current low,
/// absolute value of current high - previous close, or absolute value of current low - previous close.
/// </remarks>
2024-10-08 17:31:29 +00:00
public class Atr : AbstractBase
2024-10-06 06:59:26 +00:00
{
2024-10-21 16:06:47 -07:00
public double Tr { get; private set; }
private readonly Rma _ma;
2024-09-30 06:46:07 -07:00
private double _prevClose, _p_prevClose;
2024-10-05 15:20:13 -07:00
/// <summary>
/// Initializes a new instance of the Atr class with the specified period.
/// </summary>
/// <param name="period">The period over which to calculate the ATR.</param>
/// <exception cref="ArgumentOutOfRangeException">
/// Thrown when period is less than 1.
/// </exception>
2024-10-06 06:59:26 +00:00
public Atr(int period)
{
if (period < 1)
{
2024-09-30 06:46:07 -07:00
throw new ArgumentOutOfRangeException(nameof(period), "Period must be greater than or equal to 1.");
}
2024-10-21 16:06:47 -07:00
_ma = new(period, useSma: true);
2024-09-30 06:46:07 -07:00
WarmupPeriod = _ma.WarmupPeriod;
2024-09-30 07:30:27 -07:00
Name = $"ATR({period})";
2024-09-30 06:46:07 -07:00
}
2024-10-05 15:20:13 -07:00
/// <summary>
/// Initializes a new instance of the Atr class with the specified source and period.
/// </summary>
/// <param name="source">The source object to subscribe to for bar updates.</param>
/// <param name="period">The period over which to calculate the ATR.</param>
2024-10-06 06:59:26 +00:00
public Atr(object source, int period) : this(period)
{
2024-09-30 06:46:07 -07:00
var pubEvent = source.GetType().GetEvent("Pub");
pubEvent?.AddEventHandler(source, new BarSignal(Sub));
}
2024-10-05 15:20:13 -07:00
/// <summary>
/// Initializes the Atr instance by setting up the initial state.
/// </summary>
2024-10-06 06:59:26 +00:00
public override void Init()
{
2024-09-30 06:46:07 -07:00
base.Init();
_ma.Init();
_prevClose = double.NaN;
2024-10-21 16:06:47 -07:00
Tr = 0;
2024-09-30 06:46:07 -07:00
}
2024-10-05 15:20:13 -07:00
/// <summary>
/// Manages the state of the Atr instance based on whether a new bar is being processed.
/// </summary>
/// <param name="isNew">Indicates whether the current input is a new bar.</param>
2024-10-06 06:59:26 +00:00
protected override void ManageState(bool isNew)
{
if (isNew)
{
2024-09-30 06:46:07 -07:00
_index++;
_p_prevClose = _prevClose;
2024-10-06 06:59:26 +00:00
}
else
{
2024-09-30 06:46:07 -07:00
_prevClose = _p_prevClose;
}
}
2024-10-05 15:20:13 -07:00
/// <summary>
/// Performs the ATR calculation for the current bar.
/// </summary>
/// <returns>
/// The calculated ATR value for the current bar.
/// </returns>
/// <remarks>
2024-10-21 16:06:47 -07:00
/// This method calculates the true range for the current bar and then uses an RMA
2024-10-05 15:20:13 -07:00
/// to smooth the true range values. For the first bar, it uses the high-low range
/// as the true range.
/// </remarks>
2024-10-06 06:59:26 +00:00
protected override double Calculation()
{
2024-10-07 21:41:26 -07:00
ManageState(BarInput.IsNew);
2024-09-30 06:46:07 -07:00
2024-10-21 16:06:47 -07:00
if (_index == 1)
2024-10-06 06:59:26 +00:00
{
2024-10-21 16:06:47 -07:00
Tr = BarInput.High - BarInput.Low;
_prevClose = BarInput.Close;
2024-09-30 06:46:07 -07:00
}
2024-10-21 16:06:47 -07:00
else
{
Tr = Math.Max(
BarInput.High - BarInput.Low,
Math.Max(
Math.Abs(BarInput.High - _prevClose),
Math.Abs(BarInput.Low - _prevClose)
)
);
}
_ma.Calc(new TValue(Input.Time, Tr, BarInput.IsNew));
2024-09-30 06:46:07 -07:00
IsHot = _ma.IsHot;
2024-10-07 21:41:26 -07:00
_prevClose = BarInput.Close;
2024-10-21 16:06:47 -07:00
return _ma.Value;
2024-09-30 06:46:07 -07:00
}
}