Analytics > Indicators > Average True Range |
Average True Range (ATR) is a technical indicator for measuring the volatility of an asset. It does not provide an indication of the price trend, but of the degree of the price volatility. It is typically based on 14 periods, and could be calculated intra daily, daily, weekly or monthly basis. Stocks having high volatility will have a higher ATR, while low volatility stocks will have a lower ATR.
The following code snippet creates an instance of the ATR class to use Average True Indicator. Also, the sample uses a class DataService.cs to get data for the financial chart.
Public Class DataService Private _companies As New List(Of Company)() Private _cache As New Dictionary(Of String, List(Of Quote))() Private Sub New() _companies.Add(New Company() With { Key.Symbol = "box", Key.Name = "Box Inc" }) _companies.Add(New Company() With { Key.Symbol = "fb", Key.Name = "Facebook" }) End Sub Public Function GetCompanies() As List(Of Company) Return _companies End Function Public Function GetSymbolData(symbol As String) As List(Of Quote) If Not _cache.Keys.Contains(symbol) Then Dim path As String = String.Format("FinancialChartExplorer.Resources.{0}.json", symbol) Dim stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(path) Dim ser = New DataContractJsonSerializer(GetType(Quote())) Dim data = DirectCast(ser.ReadObject(stream), Quote()) _cache.Add(symbol, data.ToList()) End If Return _cache(symbol) End Function Shared _ds As DataService Public Shared Function GetService() As DataService If _ds Is Nothing Then _ds = New DataService() End If Return _ds End Function End Class
public class DataService { List<Company> _companies = new List<Company>(); Dictionary<string, List<Quote>> _cache = new Dictionary<string, List<Quote>>(); private DataService() { _companies.Add(new Company() { Symbol = "box", Name = "Box Inc" }); _companies.Add(new Company() { Symbol = "fb", Name = "Facebook" }); } public List<Company> GetCompanies() { return _companies; } public List<Quote> GetSymbolData(string symbol) { if (!_cache.Keys.Contains(symbol)) { string path = string.Format("FinancialChartExplorer.Resources.{0}.json", symbol); var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(path); var ser = new DataContractJsonSerializer(typeof(Quote[])); var data = (Quote[])ser.ReadObject(stream); _cache.Add(symbol, data.ToList()); } return _cache[symbol]; } static DataService _ds; public static DataService GetService() { if (_ds == null) _ds = new DataService(); return _ds; } }
Partial Public Class Indicators Inherits UserControl Private dataService As DataService = DataService.GetService() Private atr As New ATR() With { Key.SeriesName = "ATR" } Public Sub New() InitializeComponent() End Sub Public ReadOnly Property Data() As List(Of Quote) Get Return dataService.GetSymbolData("box") End Get End Property Public ReadOnly Property IndicatorType() As List(Of String) Get Return New List(Of String)() From { "Average True Range" } End Get End Property Private Sub OnIndicatorTypeSelectionChanged(sender As Object, e As SelectionChangedEventArgs) Dim ser As FinancialSeries = Nothing If cbIndicatorType.SelectedIndex = 0 Then ser = atr End If If ser IsNot Nothing AndAlso Not indicatorChart.Series.Contains(ser) Then indicatorChart.BeginUpdate() indicatorChart.Series.Clear() indicatorChart.Series.Add(ser) indicatorChart.EndUpdate() End If End Sub Private Sub OnFinancialChartRendered(sender As Object, e As C1.WPF.Chart.RenderEventArgs) If indicatorChart IsNot Nothing Then indicatorChart.AxisX.Min = DirectCast(financialChart.AxisX, IAxis).GetMin() indicatorChart.AxisX.Max = DirectCast(financialChart.AxisX, IAxis).GetMax() End If End Sub End Class
public partial class Indicators : UserControl { DataService dataService = DataService.GetService(); ATR atr = new ATR() { SeriesName = "ATR" }; public Indicators() { InitializeComponent(); } public List<Quote> Data { get { return dataService.GetSymbolData("box"); } } public List<string> IndicatorType { get { return new List<string>() { "Average True Range", }; } } void OnIndicatorTypeSelectionChanged(object sender, SelectionChangedEventArgs e) { FinancialSeries ser = null; if (cbIndicatorType.SelectedIndex == 0) ser = atr; if (ser != null && !indicatorChart.Series.Contains(ser)) { indicatorChart.BeginUpdate(); indicatorChart.Series.Clear(); indicatorChart.Series.Add(ser); indicatorChart.EndUpdate(); } } void OnFinancialChartRendered(object sender, C1.WPF.Chart.RenderEventArgs e) { if (indicatorChart != null) { indicatorChart.AxisX.Min = ((IAxis)financialChart.AxisX).GetMin(); indicatorChart.AxisX.Max = ((IAxis)financialChart.AxisX).GetMax(); } } }