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

FinancialChart の MACD(移動平均収束発散法)インジケータは、トレンドフォロー型のモメンタムインジケータです。資産価格の強さ、方向、期間、およびモメンタムの変化を明らかにします。このインジケータを使用すると、短期的な価格モメンタムを効率よく見極めることができます。

MACD モメンタムオシレータは、26 日間の指数移動平均と 12 日間の指数移動平均の関係を表示します。2 つの移動平均が収束、交差、発散するに伴って、MACD オシレータはゼロラインの上下に変動します。オシレータに重ねて「シグナルライン」がプロットされます。このラインは MACD の 9 日間の指数移動平均を示し、買いシグナルと売りシグナルのトリガとして機能します。MACD がゼロラインより下になると、売りシグナルの発生です。

MACD ヒストグラムは、短期 MACD ラインとシグナルラインの差を示すオシレータです。MACD インジケータと同様に、ヒストグラムもゼロラインの上下に変動します。正のヒストグラムは、MACD がシグナルラインより上にあることを示し、MACD がシグナルラインより下になると、負のヒストグラムになります。負の MACD ヒストグラムは売りシグナルです。

FinancialChart に MACD インジケータと MACD ヒストグラムを追加するには、FinancialChart コントロールをアプリケーションに追加し、コントロールに適切なデータソースを連結するか、Quote Collection でコントロールにデータを追加します。FinancialChart にデータを連結または追加するには、ItemsSource オブジェクトを使用します。MacdBase クラスは、FastPeriodSlowPeriod、および SmoothingPeriod プロパティを公開します。これらのプロパティの値に基づいて、MACD インジケータとヒストグラムが計算され、FinancialChart にプロットされます。系列の外観は、MacdLineStyle および SignalLineStyle プロパティで操作できます。

また、FinancialChart では、アプリケーションでアラートを作成したり、動的データの使用中にログを取るために、計算された Macd 値Macd x 値シグナル値シグナル x 値を実行時に取得できます。

MACD-Indicator

次の例は、上の図で示すように、企業 Box Inc. の一定期間の株価を考察し、ボリュームチャートとは別に MACD インジケータと MACD ヒストグラムをプロットします。この例では、json ファイルのデータを使用します。この json ファイルにアクセスするために、DataService.cs クラスを作成します。

json ファイルの[ビルドアクション]プロパティが[埋め込まれたリソース]に設定されていることを確認します。
PriceChart.xaml
コードのコピー
<Window xmlns:c1="http://schemas.componentone.com/winfx/2006/xaml"  
        x:Class="MACDInd.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:MACDInd"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525"
        DataContext="{Binding RelativeSource={RelativeSource Mode=Self}}">
<Grid>
    <c1:C1FinancialChart x:Name="financialChart"
                         ItemsSource="{Binding Data}"
                         BindingX="Date"
                         ChartType="Line"
                         ToolTipContent="{}{seriesName}&#x000A;{Date} {y}"
                         Margin="0,0,0,146"
                         Rendered="OnFinancialChartRendered">
        
        <c1:FinancialSeries Binding="High,Low,Open,Close" SeriesName="Box Inc." />            
        <c1:C1FinancialChart.AxisX>
            <c1:Axis LabelAngle="45" MajorUnit="3"/>
        </c1:C1FinancialChart.AxisX>
    </c1:C1FinancialChart>

 

IndicatorChart.xaml
コードのコピー
    <c1:C1FinancialChart x:Name="indicatorChart"
                         BindingX="Date"
                         Binding="High,Low,Close"
                         LegendPosition="Bottom"
                         ItemsSource="{Binding Data}"                             
                         Background="White"
                         ToolTipContent="{}{seriesName}&#x000A;Date: {Date}&#x000A;Y: 
                         {y:n2}&#x000A;Volume: {Volume:n0}"
                         Margin="0,178,0,0">

        <c1:Macd x:Name="Macd" SeriesName="MACD,Signal">
            <c1:Macd.MacdLineStyle>
                <c1:ChartStyle Stroke="OrangeRed" />
            </c1:Macd.MacdLineStyle>
        </c1:Macd>
        
        <c1:MacdHistogram x:Name="MACDHistogram" 
                          SeriesName= "MACD Histogram" 
                          FastPeriod="12" 
                          SlowPeriod="26" 
                          SmoothingPeriod="9" />
        
        <c1:C1FinancialChart.AxisX>
        <c1:Axis LabelAngle="45" MajorUnit="3"/>
    </c1:C1FinancialChart.AxisX>        
    </c1:C1FinancialChart>
</Grid>

  

DataService.cs に次の参照を追加します。

Public Class DataService
    Public Function GetData() As List(Of Quote)
        Dim path As String = "Indicator.Resources.box.json"
        'Indicatorをアプリケーション名で置き換えます。
        Dim stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(path)
        Dim ser = New 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 DataService
{
    public List<Quote> GetData()
    {
        string path = "MACDInd.Resources.box.json";
        //MACDIndをアプリケーション名で置き換えます。 
        var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(path);
        var ser = new 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;
    }
}

Json Data

[
    {"date":"15/01/23","open":20.2,"high":24.73,"low":20.16,"close":23.23,"volume":42593223},
    {"date":"15/01/26","open":23.67,"high":24.39,"low":22.5,"close":22.6,"volume":8677164},
    {"date":"15/01/27","open":22,"high":22.47,"low":21.17,"close":21.3,"volume":3272512},
    {"date":"15/01/28","open":21.62,"high":21.84,"low":19.6,"close":19.78,"volume":5047364},
    {"date":"15/01/29","open":19.9,"high":19.95,"low":18.51,"close":18.8,"volume":3419482},
    {"date":"15/01/30","open":18.47,"high":19.48,"low":18.22,"close":18.81,"volume":2266439},
    {"date":"15/02/02","open":19.18,"high":19.3,"low":18.01,"close":18.02,"volume":2071168},
    {"date":"15/02/03","open":18.22,"high":18.64,"low":18.12,"close":18.24,"volume":1587435},
    {"date":"15/02/04","open":18.2,"high":18.35,"low":17,"close":17.1,"volume":2912224},
    {"date":"15/02/05","open":17.3,"high":17.31,"low":16.41,"close":16.66,"volume":2682187},
    {"date":"15/02/06","open":17.39,"high":18.88,"low":17.21,"close":18.12,"volume":3929164},
    {"date":"15/02/09","open":18.86,"high":19.95,"low":18.45,"close":19.6,"volume":3226650},
    {"date":"15/02/10","open":20.5,"high":21,"low":19.63,"close":20.99,"volume":2804409},
    {"date":"15/02/11","open":20.89,"high":21,"low":20.2,"close":20.96,"volume":1698365},
    {"date":"15/02/12","open":20.66,"high":20.85,"low":19.75,"close":20.17,"volume":1370320},
    {"date":"15/02/13","open":20.19,"high":20.68,"low":20,"close":20.18,"volume":711951},
    {"date":"15/02/17","open":19.5,"high":20.1,"low":18.8,"close":19.05,"volume":2093602},
    {"date":"15/02/18","open":18.31,"high":18.5,"low":17.96,"close":18,"volume":1849490},
    {"date":"15/02/19","open":18.33,"high":19.25,"low":17.91,"close":18.96,"volume":1311518},
    {"date":"15/02/20","open":18.68,"high":19.3,"low":18.65,"close":18.85,"volume":1001692},
    {"date":"15/02/23","open":18.8,"high":18.89,"low":18.11,"close":18.21,"volume":670087},
    {"date":"15/02/24","open":18.46,"high":19,"low":18.27,"close":18.83,"volume":759263},
    {"date":"15/02/25","open":18.83,"high":19.48,"low":18.47,"close":18.67,"volume":915580},
    {"date":"15/02/26","open":18.64,"high":19.2,"low":18.64,"close":18.94,"volume":461283},
    {"date":"15/02/27","open":18.8,"high":19.12,"low":18.55,"close":18.66,"volume":617199},
    {"date":"15/03/02","open":18.66,"high":19.09,"low":18.65,"close":18.79,"volume":519605},
    {"date":"15/03/03","open":18.79,"high":19.21,"low":18.45,"close":18.59,"volume":832415},
    {"date":"15/03/04","open":18.64,"high":19.05,"low":18.32,"close":19,"volume":539688},
    {"date":"15/03/05","open":19.2,"high":19.2,"low":18.8,"close":19.14,"volume":486149},
    {"date":"15/03/06","open":19.03,"high":19.1,"low":18.7,"close":18.91,"volume":685659},
    {"date":"15/03/09","open":18.98,"high":20.15,"low":18.96,"close":19.4,"volume":1321363},
    {"date":"15/03/10","open":19.3,"high":19.8,"low":18.85,"close":19.64,"volume":615743},
    {"date":"15/03/11","open":20.08,"high":20.65,"low":19.24,"close":20.53,"volume":2167167},
    {"date":"15/03/12","open":17.17,"high":18.2,"low":16.76,"close":18.2,"volume":6837638},
    {"date":"15/03/13","open":18.05,"high":18.05,"low":17.3,"close":17.88,"volume":1715629},
    {"date":"15/03/16","open":17.91,"high":18,"low":17.01,"close":17.13,"volume":1321313},
    {"date":"15/03/17","open":17.28,"high":17.37,"low":16.6,"close":17.12,"volume":1272242},
    {"date":"15/03/18","open":17.1,"high":17.27,"low":16.91,"close":17.01,"volume":530063},
    {"date":"15/03/19","open":17,"high":17.28,"low":17,"close":17.06,"volume":536427},
    {"date":"15/03/20","open":17.13,"high":17.24,"low":16.88,"close":17.21,"volume":1320237},
    {"date":"15/03/23","open":17.21,"high":17.23,"low":17.01,"close":17.11,"volume":509798},
    {"date":"15/03/24","open":17.02,"high":17.18,"low":16.82,"close":17,"volume":962149},
    {"date":"15/03/25","open":16.92,"high":16.99,"low":16.82,"close":16.97,"volume":565673},
    {"date":"15/03/26","open":16.83,"high":17.56,"low":16.83,"close":17.54,"volume":884523},
    {"date":"15/03/27","open":17.58,"high":18.3,"low":17.11,"close":18.3,"volume":705626},
    {"date":"15/03/30","open":18.5,"high":19.4,"low":18.4,"close":19.05,"volume":1151620},
    {"date":"15/03/31","open":19.08,"high":20.58,"low":18.4,"close":19.75,"volume":2020679},
    {"date":"15/04/01","open":19.69,"high":19.69,"low":18.55,"close":18.65,"volume":961078},
    {"date":"15/04/02","open":18.56,"high":18.66,"low":17.85,"close":17.9,"volume":884233},
    {"date":"15/04/06","open":17.78,"high":17.94,"low":17.51,"close":17.66,"volume":605252},
    {"date":"15/04/07","open":17.62,"high":17.9,"low":17.53,"close":17.61,"volume":591988},
    {"date":"15/04/08","open":17.64,"high":17.85,"low":17.32,"close":17.36,"volume":618855},
    {"date":"15/04/09","open":17.33,"high":17.54,"low":17.1,"close":17.1,"volume":761855},
    {"date":"15/04/10","open":17.08,"high":17.36,"low":17,"close":17.05,"volume":568373},
    {"date":"15/04/13","open":17.24,"high":17.26,"low":16.81,"close":17.1,"volume":667142},
    {"date":"15/04/14","open":17.1,"high":17.89,"low":17.02,"close":17.52,"volume":870138},
    {"date":"15/04/15","open":17.6,"high":17.99,"low":17.5,"close":17.69,"volume":530456},
    {"date":"15/04/16","open":17.95,"high":18,"low":17.6,"close":17.82,"volume":548730},
    {"date":"15/04/17","open":17.75,"high":17.79,"low":17.5,"close":17.79,"volume":446373},
    {"date":"15/04/20","open":17.63,"high":17.98,"low":17.52,"close":17.93,"volume":487017},
    {"date":"15/04/21","open":17.96,"high":17.98,"low":17.71,"close":17.92,"volume":320302},
    {"date":"15/04/22","open":17.88,"high":18.33,"low":17.57,"close":18.29,"volume":644812},
    {"date":"15/04/23","open":18.29,"high":18.61,"low":18.18,"close":18.28,"volume":563879},
    {"date":"15/04/24","open":18.5,"high":18.5,"low":17.61,"close":17.75,"volume":650762},
    {"date":"15/04/27","open":17.97,"high":18.05,"low":17.45,"close":17.57,"volume":437294},
    {"date":"15/04/28","open":17.65,"high":17.79,"low":17.39,"close":17.5,"volume":224519},
    {"date":"15/04/29","open":17.68,"high":17.68,"low":17.1,"close":17.21,"volume":495706},
    {"date":"15/04/30","open":17.22,"high":17.3,"low":17,"close":17.11,"volume":391040},
    {"date":"15/05/01","open":17.11,"high":17.55,"low":16.85,"close":17.5,"volume":563075},
    {"date":"15/05/02","open":17.56,"high":17.85,"low":17.3,"close":17.4,"volume":253138},
    {"date":"15/05/05","open":17.68,"high":17.68,"low":17.09,"close":17.43,"volume":290935},
    {"date":"15/05/06","open":17.48,"high":17.48,"low":17,"close":17.04,"volume":313662},
    {"date":"15/05/07","open":17.05,"high":17.19,"low":16.92,"close":17.04,"volume":360284},
    {"date":"15/05/08","open":17.13,"high":17.21,"low":16.91,"close":17.1,"volume":297653},
    {"date":"15/05/11","open":17.16,"high":17.44,"low":17.13,"close":17.31,"volume":268504},
    {"date":"15/05/12","open":17.28,"high":17.44,"low":16.99,"close":17.24,"volume":376961},
    {"date":"15/05/13","open":17.24,"high":17.3,"low":17.06,"close":17.2,"volume":244617},
    {"date":"15/05/14","open":17.24,"high":17.25,"low":17.02,"close":17.08,"volume":252526},
    {"date":"15/05/15","open":17.06,"high":17.16,"low":16.95,"close":16.95,"volume":274783},
    {"date":"15/05/18","open":16.95,"high":17.01,"low":16.76,"close":16.87,"volume":418513},
    {"date":"15/05/19","open":16.93,"high":16.94,"low":16.6,"close":16.83,"volume":367660},
    {"date":"15/05/20","open":16.8,"high":16.9,"low":16.65,"close":16.86,"volume":297914},
    {"date":"15/05/21","open":16.9,"high":17.08,"low":16.79,"close":16.88,"volume":229346},
    {"date":"15/05/22","open":16.9,"high":17.05,"low":16.85,"close":17,"volume":253279},
    {"date":"15/05/26","open":17.03,"high":17.08,"low":16.86,"close":17.01,"volume":212640},
    {"date":"15/05/27","open":17.01,"high":17.99,"low":16.87,"close":17.75,"volume":857109},
    {"date":"15/05/28","open":17.77,"high":17.77,"low":17.44,"close":17.62,"volume":338482}
]

コードビューで次の参照を追加します。

<DataContract>
Public Class Quote
    <DataMember(Name:="date")>
    Public Property [Date]() As String
        Get
            Return m_Date
        End Get
        Set
            m_Date = Value
        End Set
    End Property
    Private m_Date As String

    <DataMember(Name:="high")>
    Public Property High() As Double
        Get
            Return m_High
        End Get
        Set
            m_High = Value
        End Set
    End Property
    Private m_High As Double

    <DataMember(Name:="low")>
    Public Property Low() As Double
        Get
            Return m_Low
        End Get
        Set
            m_Low = Value
        End Set
    End Property
    Private m_Low As Double

    <DataMember(Name:="open")>
    Public Property Open() As Double
        Get
            Return m_Open
        End Get
        Set
            m_Open = Value
        End Set
    End Property
    Private m_Open As Double

    <DataMember(Name:="close")>
    Public Property Close() As Double
        Get
            Return m_Close
        End Get
        Set
            m_Close = Value
        End Set
    End Property
    Private m_Close As Double

    <DataMember(Name:="volume")>
    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
''' Macd.xamlの相互作用ロジック
Partial Public Class Macd
    Inherits Window
    Private dataService As DataService = dataService.GetService()
    Public Sub New()
        InitializeComponent()
    End Sub
    Public ReadOnly Property Data() As List(Of Quote)
        Get
            Return dataService.GetData()
        End Get
    End Property

    Private Sub OnFinancialChartRendered(sender As Object, e As C1.WPF.Chart.RenderEventArgs)
        indicatorChart.AxisX.Min = DirectCast(financialChart.AxisX, IAxis).GetMin()
        indicatorChart.AxisX.Max = DirectCast(financialChart.AxisX, IAxis).GetMax()
    End Sub
End Class
[DataContract]
public class Quote
{
    [DataMember(Name = "date")]
    public string Date { get; set; }

    [DataMember(Name = "high")]
    public double High { get; set; }

    [DataMember(Name = "low")]
    public double Low { get; set; }

    [DataMember(Name = "open")]
    public double Open { get; set; }

    [DataMember(Name = "close")]
    public double Close { get; set; }

    [DataMember(Name = "volume")]
    public double Volume { get; set; }
}
/// MainWindow.xamlの相互作用ロジック
public partial class MainWindow : Window
{
    DataService dataService = DataService.GetService();
    public MainWindow()
    {
        InitializeComponent();
    }
    public List<Quote> Data
    {
        get
        {
            return dataService.GetData();
        }
    }

    private void OnFinancialChartRendered(object sender, C1.WPF.Chart.RenderEventArgs e)
    {
        indicatorChart.AxisX.Min = ((IAxis)financialChart.AxisX).GetMin();
        indicatorChart.AxisX.Max = ((IAxis)financialChart.AxisX).GetMax();
    }
}

先頭に移動

関連トピック