コンテンツにスキップ

トレードウォッチ用プラグインの作成方法

トレードウォッチに新しいタブを追加できる機能により、cTraderのUIはさまざまな取引ニーズに合わせてカスタマイズ可能で適応性があります。

この記事と対応する動画では、プラグインを使用してトレードウォッチパネルにオブジェクトを追加する方法をデモンストレーションします。

プラグインを作成する

最初にウェブサイトプラグインを作成しますが、最終的にはM1タイムフレームとUSDJPY通貨ペアの最後の既知のバー価格に関する情報を表示する2×2のグリッドプラグインになります。

Algoアプリに移動し、プラグインタブに移動して開始できます。 新規 ボタンをクリックして新しいプラグインを作成します。 空白 オプションを選択します。 プラグインに「Previous Bar Info」などの名前を付け、作成ボタンをクリックします。

トレードウォッチパネルに新しいタブを追加し、Previous Bar Infoと名前を付けます。

1
2
var tradeWatchTab = TradeWatch.AddTab("Previous Bar Info");
tradeWatchTab.IsSelected = true;

シンプルなWebViewコンポーネントを追加します。

1
2
3
4
var webView = new WebView();                        
tradeWatchTab.Child = webView;

webView.NavigateAsync("https://ctrader.com/");

以下の完全なコードをコピーできます:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
using System;
using cAlgo.API;
using cAlgo.API.Collections;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;

namespace cAlgo.Plugins
{
    [Plugin(AccessRights = AccessRights.None)]
    public class PreviousBarInfo : Plugin
    {
        protected override void OnStart()
        {
            var tradeWatchTab = TradeWatch.AddTab("Previous Bar Info");
            tradeWatchTab.IsSelected = true;

            var webView = new WebView();                        
            tradeWatchTab.Child = webView;

            webView.NavigateAsync("https://ctrader.com/");

        }

        protected override void OnStop()
        {
            // Handle Plugin stop here
        }
    }        
}

プラグインをビルドするには、ビルドボタンをクリックするか、Ctrl+Bのホットキーを使用します。

Tradeアプリに移動して、プラグインが何を表示しているかを確認します。

WebViewコンポーネントを使用すると、プラグインコードでウェブサイトのURLを設定することで、プラグイン内に任意のウェブサイトを表示できます。 さらに、日常の取引で使用するウェブサイト用に異なるプラグインを作成し、cTraderの設定でそれらをオンまたはオフにすることができます。

プラグインにグリッドとテキストボックスを追加する

Algo アプリに戻り、プラグインコードを編集します。

WebViewコンポーネントをグリッドオブジェクトに置き換え、各グリッドをトレードウォッチプラグインの子にする必要があります。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
var grid = new Grid(2, 2) 
{
    HorizontalAlignment = HorizontalAlignment.Center,
    VerticalAlignment = VerticalAlignment.Center,
    ShowGridLines = true,
    Height = 150,
    Width = 150,
};

tradeWatchTab.Child = grid;

グリッドをプラグインの中央に配置し、グリッドラインを表示します。

以下の完全なコードをコピーできます:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
using System;
using cAlgo.API;
using cAlgo.API.Collections;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;

namespace cAlgo.Plugins
{
    [Plugin(AccessRights = AccessRights.None)]
    public class PreviousBarInfo : Plugin
    {
        protected override void OnStart()
        {
            var tradeWatchTab = TradeWatch.AddTab("Previous Bar Info");
            tradeWatchTab.IsSelected = true;

            var grid = new Grid(2, 2) 
            {
                HorizontalAlignment = HorizontalAlignment.Center,
                VerticalAlignment = VerticalAlignment.Center,
                ShowGridLines = true,
                Height = 150,
                Width = 150,
            };

            tradeWatchTab.Child = grid;

        }

        protected override void OnStop()
        {
            // Handle Plugin stop here
        }
    }        
}

プラグインをビルドし、Tradeアプリに移動して結果を確認します。

Algoアプリでプラグインコードを引き続き改善します。

4つの重要なテキストボックス(オープン、ハイ、ロー、クローズの各値用)とバー変数を宣言します。

1
2
3
4
5
TextBlock _lowBlock;
TextBlock _highBlock;
TextBlock _closeBlock;
TextBlock _openBlock;
Bars _bars;

USDJPY通貨ペアの分足バーデータを取得するバー変数を追加します。

1
_bars = MarketData.GetBars(TimeFrame.Minute, "USDJPY");

テキストボックスを初期化し、セルの中央に配置します。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
_lowBlock = new TextBlock 
{
    Text = "Low:" + _bars.LowPrices.LastValue,
    HorizontalAlignment = HorizontalAlignment.Center,
    VerticalAlignment = VerticalAlignment.Center,
};

_highBlock = new TextBlock 
{
    Text = "High:" + _bars.HighPrices.LastValue,
    HorizontalAlignment = HorizontalAlignment.Center,
    VerticalAlignment = VerticalAlignment.Center,
};

_closeBlock = new TextBlock 
{
    Text = "Close:" +_bars.ClosePrices.LastValue,
    HorizontalAlignment = HorizontalAlignment.Center,
    VerticalAlignment = VerticalAlignment.Center,
};

_openBlock = new TextBlock 
{
    Text = "Open:" + _bars.OpenPrices.LastValue,
    HorizontalAlignment = HorizontalAlignment.Center,
    VerticalAlignment = VerticalAlignment.Center,
};

テキストボックスを初期化した後、それらをグリッドセルに追加できます。

1
2
3
4
grid.AddChild(_lowBlock, 0, 0);
grid.AddChild(_highBlock, 0, 1);
grid.AddChild(_openBlock, 1, 0);
grid.AddChild(_closeBlock, 1, 1);

以下の完全なコードをコピーできます:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
using System;
using cAlgo.API;
using cAlgo.API.Collections;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;

namespace cAlgo.Plugins
{
    [Plugin(AccessRights = AccessRights.None)]
    public class PreviousBarInfo : Plugin
    {

        TextBlock _lowBlock;
        TextBlock _highBlock;
        TextBlock _closeBlock;
        TextBlock _openBlock;
        Bars _bars;

        protected override void OnStart()
        {
            var tradeWatchTab = TradeWatch.AddTab("Previous Bar Info");
            tradeWatchTab.IsSelected = true;

            var grid = new Grid(2, 2) 
            {
                HorizontalAlignment = HorizontalAlignment.Center,
                VerticalAlignment = VerticalAlignment.Center,
                ShowGridLines = true,
                Height = 150,
                Width = 150,
            };

            tradeWatchTab.Child = grid;

            _bars = MarketData.GetBars(TimeFrame.Minute, "USDJPY");

            _lowBlock = new TextBlock 
            {
                Text = "Low:" + _bars.LowPrices.LastValue,
                HorizontalAlignment = HorizontalAlignment.Center,
                VerticalAlignment = VerticalAlignment.Center,
            };

            _highBlock = new TextBlock 
            {
                Text = "High:" + _bars.HighPrices.LastValue,
                HorizontalAlignment = HorizontalAlignment.Center,
                VerticalAlignment = VerticalAlignment.Center,
            };

            _closeBlock = new TextBlock 
            {
                Text = "Close:" +_bars.ClosePrices.LastValue,
                HorizontalAlignment = HorizontalAlignment.Center,
                VerticalAlignment = VerticalAlignment.Center,
            };

            _openBlock = new TextBlock 
            {
                Text = "Open:" + _bars.OpenPrices.LastValue,
                HorizontalAlignment = HorizontalAlignment.Center,
                VerticalAlignment = VerticalAlignment.Center,
            };

            grid.AddChild(_lowBlock, 0, 0);
            grid.AddChild(_highBlock, 0, 1);
            grid.AddChild(_openBlock, 1, 0);
            grid.AddChild(_closeBlock, 1, 1);

        }

        protected override void OnStop()
        {
            // Handle Plugin stop here
        }
    }        
}

プラグインをビルドし、Tradeアプリに移動して変更を確認します。

イベントをサブスクライブする

Algoアプリのプラグインソースコードに戻ります。 ティックイベントをサブスクライブし、各ティックで値が更新されるようにするために、以下のコード行を追加します:

1
2
3
4
5
6
7
8
9
_bars.Tick += _bars_Tick;

private void _bars_Tick(BarsTickEventArgs obj)
{
    _lowBlock.Text = "Low: " +_bars.LowPrices.LastValue.ToString();
    _highBlock.Text = "High: " +_bars.HighPrices.LastValue.ToString();
    _openBlock.Text = "Open: " +_bars.HighPrices.LastValue.ToString();
    _closeBlock.Text = "Close: " +_bars.HighPrices.LastValue.ToString();
}

以下の完全なコードをコピーできます:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
using System;
using cAlgo.API;
using cAlgo.API.Collections;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;

namespace cAlgo.Plugins
{
    [Plugin(AccessRights = AccessRights.None)]
    public class PreviousBarInfo : Plugin
    {
        TextBlock _lowBlock;
        TextBlock _highBlock;
        TextBlock _closeBlock;
        TextBlock _openBlock;
        Bars _bars;

        protected override void OnStart()
        {
            var tradeWatchTab = TradeWatch.AddTab("Previous Bar Info");
            tradeWatchTab.IsSelected = true;

            var grid = new Grid(2, 2) 
            {
                HorizontalAlignment = HorizontalAlignment.Center,
                VerticalAlignment = VerticalAlignment.Center,
                ShowGridLines = true,
                Height = 150,
                Width = 150,
            };

            tradeWatchTab.Child = grid;

            _bars = MarketData.GetBars(TimeFrame.Minute, "USDJPY");

            _lowBlock = new TextBlock 
            {
                Text = "Low:" + _bars.LowPrices.LastValue,
                HorizontalAlignment = HorizontalAlignment.Center,
                VerticalAlignment = VerticalAlignment.Center,
            };

            _highBlock = new TextBlock 
            {
                Text = "High:" + _bars.HighPrices.LastValue,
                HorizontalAlignment = HorizontalAlignment.Center,
                VerticalAlignment = VerticalAlignment.Center,
            };

            _closeBlock = new TextBlock 
            {
                Text = "Close:" +_bars.ClosePrices.LastValue,
                HorizontalAlignment = HorizontalAlignment.Center,
                VerticalAlignment = VerticalAlignment.Center,
            };

            _openBlock = new TextBlock 
            {
                Text = "Open:" + _bars.OpenPrices.LastValue,
                HorizontalAlignment = HorizontalAlignment.Center,
                VerticalAlignment = VerticalAlignment.Center,
            };

            grid.AddChild(_lowBlock, 0, 0);
            grid.AddChild(_highBlock, 0, 1);
            grid.AddChild(_openBlock, 1, 0);
            grid.AddChild(_closeBlock, 1, 1);

            _bars.Tick += _bars_Tick;            

        }

        private void _bars_Tick(BarsTickEventArgs obj)
        {
            _lowBlock.Text = "Low: " +_bars.LowPrices.LastValue.ToString();
            _highBlock.Text = "High: " +_bars.HighPrices.LastValue.ToString();
            _openBlock.Text = "Open: " +_bars.HighPrices.LastValue.ToString();
            _closeBlock.Text = "Close: " +_bars.HighPrices.LastValue.ToString();
        }

        protected override void OnStop()
        {
            // Handle Plugin stop here
        }
    }        
}

プラグインをビルドし、Tradeアプリに移動します。

概要

この記事を読んだ後、トレードウォッチパネルにウェブサイト、グリッド、テキストボックス、その他の有用なオブジェクトを追加できるようになったと信じています。