ATR(Average True Range)は、資産のボラティリティを測定するテクニカルインジケータです。価格の動向を示すのではなく、価格のボラティリティの程度を示します。ATR は、通常、14 期間に基づいて、日中、日、週、または月単位で計算できます。ボラティリティが高い株は ATR が高くなる一方、ボラティリティが低い株は ATR が低くなります。
また、FinancialChart では、実行時に GetValues() メソッドを使用して、計算された ATR 値を取得できます。これにより、アプリケーションでアラートを作成したり、動的データを使用する際にログを取ることができます。
次のコードスニペットは、ATR クラスのインスタンスを作成して、Average True Indicator を使用します。また、このサンプルはクラス DataService を使用して、株価チャートのデータを取得します。
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(); } } }