Files
QuanTAlib/quantower/Volatility/AtrIndicator.cs
T

42 lines
1.1 KiB
C#
Raw Normal View History

2024-10-12 20:36:37 -07:00
using System.Drawing;
2024-09-30 06:46:07 -07:00
using TradingPlatform.BusinessLayer;
2024-10-12 20:36:37 -07:00
2024-09-30 06:46:07 -07:00
namespace QuanTAlib;
2024-10-12 20:36:37 -07:00
public class AtrIndicator : Indicator, IWatchlistIndicator
2024-09-30 06:46:07 -07:00
{
2024-10-12 20:36:37 -07:00
[InputParameter("Periods", sortIndex: 1, 1, 2000, 1, 0)]
public int Periods { get; set; } = 20;
2024-09-30 06:46:07 -07:00
private Atr? atr;
2024-10-12 20:36:37 -07:00
protected LineSeries? AtrSeries;
2024-10-13 11:19:27 -07:00
public static int MinHistoryDepths => 2;
2024-10-12 20:36:37 -07:00
int IWatchlistIndicator.MinHistoryDepths => MinHistoryDepths;
2024-09-30 06:46:07 -07:00
public AtrIndicator()
{
Name = "ATR - Average True Range";
2024-10-11 18:02:09 -07:00
Description = "Measures market volatility by calculating the average range between high and low prices.";
2024-09-30 06:46:07 -07:00
SeparateWindow = true;
2024-10-12 20:36:37 -07:00
AtrSeries = new("ATR", Color.Blue, 2, LineStyle.Solid);
AddLineSeries(AtrSeries);
2024-09-30 06:46:07 -07:00
}
2024-10-12 20:36:37 -07:00
protected override void OnInit()
2024-09-30 06:46:07 -07:00
{
2024-10-12 20:36:37 -07:00
atr = new Atr(Periods);
base.OnInit();
2024-09-30 06:46:07 -07:00
}
2024-10-12 20:36:37 -07:00
protected override void OnUpdate(UpdateArgs args)
{
TBar input = IndicatorExtensions.GetInputBar(this, args);
TValue result = atr!.Calc(input);
AtrSeries!.SetValue(result.Value);
}
public override string ShortName => $"ATR ({Periods})";
2024-10-11 18:02:09 -07:00
}