ComponentOne FinancialChart for UWP
Analytics / Indicators / Average True Range
In This Topic
    Average True Range
    In This Topic

    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.

    FinancialChart also enables you to fetch the calculated ATR values at run-time using GetValues() method. This can help in creating alerts in application or maintaining logs while working with dynamic data

    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 {
                .Symbol = "box",
                .Name = "Box Inc"
            })
            _companies.Add(New Company() With {
                .Symbol = "fb",
                .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.{0}.json", symbol)
                Dim asm As Assembly = Me.[GetType]().GetTypeInfo().Assembly
                Dim stream As Stream = asm.GetManifestResourceStream(path)
                Dim ser As New DataContractJsonSerializer(GetType(Quote()))
                Dim data As Quote() = CType(ser.ReadObject(stream), Quote())
                Dim dataList As List(Of Quote) = data.ToList()
                If IsWindowsPhoneDevice() Then
                    dataList = dataList.Take(20).ToList()
                End If
                _cache.Add(symbol, dataList)
            End If
    
            Return _cache(symbol)
        End Function
    
        Function IsWindowsPhoneDevice() As Boolean
            If Windows.Foundation.Metadata.ApiInformation.IsTypePresent("Windows.Phone.UI.Input.HardwareButtons") Then
                Return True
            End If
            Return False
        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 Quote
        Public Property [date]() As String
        Public Property high() As Double
        Public Property low() As Double
        Public Property open() As Double
        Public Property close() As Double
        Public Property volume() As Double
    End Class
    
    Public Class Company
        Public Property Symbol() As String
        Public Property Name() As String
    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);
                Assembly asm = this.GetType().GetTypeInfo().Assembly;
                var stream = asm.GetManifestResourceStream(path);
                var ser = new DataContractJsonSerializer(typeof(Quote[]));
                var data = (Quote[])ser.ReadObject(stream);
                var dataList = data.ToList();
                if (IsWindowsPhoneDevice())
                {
                    dataList = dataList.Take(20).ToList();
                }
                _cache.Add(symbol, dataList);
            }
    
            return _cache[symbol];
        }
    
        bool IsWindowsPhoneDevice()
        {
            if (Windows.Foundation.Metadata.ApiInformation.IsTypePresent("Windows.Phone.UI.Input.HardwareButtons"))
            {
                return true;
            }
            return false;
        }
    
        static DataService _ds;
        public static DataService GetService()
        {
            if (_ds == null)
                _ds = new DataService();
            return _ds;
        }
    }
    
    public class Quote
    {
        public string date { get; set; }
        public double high { get; set; }
        public double low { get; set; }
        public double open { get; set; }
        public double close { get; set; }
        public double volume { get; set; }
    }
    
    public class Company
    {
        public string Symbol { get; set; }
        public string Name { get; set; }
    }
    
    Partial Public Class Indicators
        Inherits Page
        Private dataService As DataService = dataService.GetService()
        Private atr As New ATR() With {
            .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
    
        Sub OnIndicatorsLoaded(sender As Object, e As RoutedEventArgs) Handles Me.Loaded
            cbIndicatorType.SelectedIndex = 0
            Me.cbPeriod.Value = atr.Period
        End Sub
    
        Sub OnIndicatorTypeSelectionChanged(sender As Object, e As SelectionChangedEventArgs)
            Dim ser As FinancialSeries = Nothing
            If cbIndicatorType.SelectedIndex = 0 Then
                ser = atr
    
                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
    
        Sub OnCbPeriodValueChanged(sender As Object, e As PropertyChangedEventArgs(Of Double))
            atr.Period = 14
        End Sub
    
        Sub OnFinancialChartRendered(sender As Object, e As RenderEventArgs)
            If indicatorChart IsNot Nothing Then
                indicatorChart.AxisX.Min = (CType(financialChart.AxisX, IAxis)).GetMin()
                indicatorChart.AxisX.Max = (CType(financialChart.AxisX, IAxis)).GetMax()
            End If
        End Sub
    End Class
    
    public partial class Indicators : Page
    {
        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 OnCbPeriodValueChanged(object sender, PropertyChangedEventArgs<double> e)
        {
            atr.Period = 14;
        }
    
        void OnFinancialChartRendered(object sender, RenderEventArgs e)
        {
            if (indicatorChart != null)
            {
                indicatorChart.AxisX.Min = ((IAxis)financialChart.AxisX).GetMin();
                indicatorChart.AxisX.Max = ((IAxis)financialChart.AxisX).GetMax();
            }
        }
    }