FinancialChart for WPF
ATR
分析機能 > インジケータ > ATR

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 {
            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();
        }
    }
}