Files

54 lines
1.7 KiB
C#
Raw Permalink 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
2024-10-21 16:06:47 -07:00
[InputParameter("Show cold values", sortIndex: 21)]
public bool ShowColdValues { get; set; } = true;
2024-09-30 06:46:07 -07:00
private Atr? atr;
2024-10-12 20:36:37 -07:00
protected LineSeries? AtrSeries;
2024-10-21 16:06:47 -07:00
public int MinHistoryDepths => Math.Max(5, Periods * 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
2024-10-21 16:06:47 -07:00
AtrSeries = new($"ATR {Periods}", Color.Blue, 2, LineStyle.Solid);
2024-10-12 20:36:37 -07:00
AddLineSeries(AtrSeries);
}
protected override void OnInit()
{
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)
2024-09-30 06:46:07 -07:00
{
2024-10-12 20:36:37 -07:00
TBar input = IndicatorExtensions.GetInputBar(this, args);
TValue result = atr!.Calc(input);
AtrSeries!.SetValue(result.Value);
2024-10-21 16:06:47 -07:00
AtrSeries!.SetMarker(0, Color.Transparent); //OnPaintChart draws the line, hidden here
2024-09-30 06:46:07 -07:00
}
2024-10-22 05:46:48 -07:00
#pragma warning disable CA1416 // Validate platform compatibility
2024-10-12 20:36:37 -07:00
public override string ShortName => $"ATR ({Periods})";
2024-10-21 16:06:47 -07:00
public override void OnPaintChart(PaintChartEventArgs args)
{
base.OnPaintChart(args);
2024-11-06 20:56:32 -08:00
this.PaintHLine(args, 0.05, new Pen(color: IndicatorExtensions.Volatility, width: 2));
2024-10-21 16:06:47 -07:00
this.PaintSmoothCurve(args, AtrSeries!, atr!.WarmupPeriod, showColdValues: ShowColdValues, tension: 0.2);
}
2024-10-11 18:02:09 -07:00
}