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

ATR(Average True Range)は、資産のボラティリティを測定するテクニカルインジケータです。価格の動向を示すのではなく、価格のボラティリティの程度を示します。ATR は、通常、14 期間に基づいて、日中、日、週、または月単位で計算できます。ボラティリティが高い株は ATR が高くなる一方、ボラティリティが低い株は ATR が低くなります。

また、FinancialChart では、実行時に GetValues() メソッドを使用して、計算された ATR 値を取得できます。これにより、アプリケーションでアラートを作成したり、動的データを使用する際にログを取ることができます。

次のコードスニペットは、ATR クラスのインスタンスを作成して、Average True Indicator を使用します。また、このサンプルはクラス DataService を使用して、株価チャートのデータを取得します。

Public Class DataService

    Public Class DataService
        Private _companies As New List(Of Company)()

        Public 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)
            Dim path As String = String.Format("FinancialChartExplorer.
            Resources.{0}.json", symbol)
            Dim stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(path)
            Dim ser = New System.Runtime.Serialization.Json
            .DataContractJsonSerializer(GetType(Quote()))
            Dim data = DirectCast(ser.ReadObject(stream), Quote())
            Return data.ToList()
        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
            Get
                Return m_date
            End Get
            Set
                m_date = Value
            End Set
        End Property
        Private m_date As String
        Public Property high() As Double
            Get
                Return m_high
            End Get
            Set
                m_high = Value
            End Set
        End Property
        Private m_high As Double
        Public Property low() As Double
            Get
                Return m_low
            End Get
            Set
                m_low = Value
            End Set
        End Property
        Private m_low As Double
        Public Property open() As Double
            Get
                Return m_open
            End Get
            Set
                m_open = Value
            End Set
        End Property
        Private m_open As Double
        Public Property close() As Double
            Get
                Return m_close
            End Get
            Set
                m_close = Value
            End Set
        End Property
        Private m_close As Double
        Public Property volume() As Double
            Get
                Return m_volume
            End Get
            Set
                m_volume = Value
            End Set
        End Property
        Private m_volume As Double
    End Class

    Public Class Company
        Public Property Symbol() As String
            Get
                Return m_Symbol
            End Get
            Set
                m_Symbol = Value
            End Set
        End Property
        Private m_Symbol As String
        Public Property Name() As String
            Get
                Return m_Name
            End Get
            Set
                m_Name = Value
            End Set
        End Property
        Private m_Name As String
    End Class

End Class
public class DataService
{
    List<Company> _companies = new List<Company>();

    public 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)
    {
        string path = string.Format("FinancialChartExplorer.Resources.{0}.json", symbol);
        var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(path);
        var ser = new System.Runtime.Serialization.Json.DataContractJsonSerializer(typeof(Quote[]));
        var data = (Quote[])ser.ReadObject(stream);
        return data.ToList();
    }

    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; }
}
Dim atr As New ATR() With
{
    .Name = "ATR"
}

Dim dataService__1 = DataService.GetService()
Dim data = dataService__1.GetSymbolData("box")

FinancialChart1.BeginUpdate()
FinancialChart1.BindingX = "date"
FinancialChart1.Binding = "close"
FinancialChart1.Series.Add(New FinancialSeries())
FinancialChart1.ChartType = C1.Chart.Finance.FinancialChartType.Line
FinancialChart1.DataSource = data
FinancialChart1.Rendered += Function(s, a)
                    financialChart2.AxisX.Min = FinancialChart1.AxisX.ActualMin
                    financialChart2.AxisX.Max = FinancialChart1.AxisX.ActualMax

                    End Function
FinancialChart1.EndUpdate()

financialChart2.BeginUpdate()
financialChart2.ChartType = C1.Chart.Finance.FinancialChartType.Line
financialChart2.BindingX = "date"
financialChart2.Binding = "high,low,close"
financialChart2.Series.Add(atr)
financialChart2.Legend.Position = C1.Chart.Position.Bottom
financialChart2.DataSource = data
financialChart2.EndUpdate()
period.Value = atr.Period
ATR atr = new ATR() { Name = "ATR" };

var dataService = DataService.GetService();
var data = dataService.GetSymbolData("box");

financialChart1.BeginUpdate();
financialChart1.BindingX = "date";
financialChart1.Binding = "close";
financialChart1.Series.Add(new FinancialSeries());
financialChart1.ChartType = C1.Chart.Finance.FinancialChartType.Line;
financialChart1.DataSource = data;
financialChart1.Rendered += (s, a) =>
{
    financialChart2.AxisX.Min = financialChart1.AxisX.ActualMin;
    financialChart2.AxisX.Max = financialChart1.AxisX.ActualMax;
};
financialChart1.EndUpdate();

financialChart2.BeginUpdate();
financialChart2.ChartType = C1.Chart.Finance.FinancialChartType.Line;
financialChart2.BindingX = "date";
financialChart2.Binding = "high,low,close";
financialChart2.Series.Add(atr);
financialChart2.Legend.Position = C1.Chart.Position.Bottom;
financialChart2.DataSource = data;
financialChart2.EndUpdate();
period.Value = atr.Period;