コンテンツにスキップ

cBotとインジケーターをアルゴを使用して管理する方法

別のアルゴリズムを管理するアルゴリズム機能により、トレーダーはコードを使用してチャートにcBotとインジケーターを追加できます。 この機能により、効果的な取引戦略を計画・開発し、動的な調整を行い、複数の戦略を実行し、自動化されたリスク管理を適用できます。

この記事と対応するビデオでは、他のアルゴリズムを管理するcBotを作成し、操作する方法を紹介します。

cBotを使用してインジケーターを追加する

Algoアプリで、cBotsタブを開きます。 移動平均を使用するSample Trend cBotサンプルを検索して選択します。

2つのインジケーターを定義します。

1
2
ChartIndicator _indicator1;
ChartIndicator _indicator2;

チャートに2つのインジケーターを追加します。

1
2
_indicator1 = Chart.Indicators.Add("Simple Moving Average", SourceSeries, FastPeriods, MAType);
_indicator2 = Chart.Indicators.Add("Simple Moving Average", SourceSeries, SlowPeriods, MAType);

インジケーターの外観は、出力ラインの設定を通じてカスタマイズできます。 カスタマイズ可能なオプションには、色、太さ、ラインスタイルが含まれます。

最初のインジケーターのラインを赤色で太くします。

1
2
_indicator1.Lines[0].Color = Color.Red;
_indicator1.Lines[0].Thickness = 3;

同じ操作を行って、いつでもチャートからインジケーターを削除できます。 バーが変化したときに変更を有効にします。

1
2
3
4
5
protected override void OnBarClosed()
{
    Chart.Indicators.Remove(_indicator1);
    Chart.Indicators.Remove(_indicator2);
}

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

 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
using cAlgo.API;
using cAlgo.API.Indicators;

namespace cAlgo
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None, AddIndicators = true)]
    public class SampleTrendcBot : Robot
    {
        [Parameter("Quantity (Lots)", Group = "Volume", DefaultValue = 1, MinValue = 0.01, Step = 0.01)]
        public double Quantity { get; set; }

        [Parameter("MA Type", Group = "Moving Average")]
        public MovingAverageType MAType { get; set; }

        [Parameter("Source", Group = "Moving Average")]
        public DataSeries SourceSeries { get; set; }

        [Parameter("Slow Periods", Group = "Moving Average", DefaultValue = 10)]
        public int SlowPeriods { get; set; }

        [Parameter("Fast Periods", Group = "Moving Average", DefaultValue = 5)]
        public int FastPeriods { get; set; }

        private MovingAverage slowMa;
        private MovingAverage fastMa;
        private const string label = "Sample Trend cBot";   

        ChartIndicator _indicator1;
        ChartIndicator _indicator2;

        protected override void OnStart()
        {
            fastMa = Indicators.MovingAverage(SourceSeries, FastPeriods, MAType);
            slowMa = Indicators.MovingAverage(SourceSeries, SlowPeriods, MAType);

            _indicator1 = Chart.Indicators.Add("Simple Moving Average", SourceSeries, FastPeriods, MAType);
            _indicator2 = Chart.Indicators.Add("Simple Moving Average", SourceSeries, SlowPeriods, MAType);

            _indicator1.Lines[0].Color = Color.Red;
            _indicator1.Lines[0].Thickness = 3;
        }

        protected override void OnBarClosed()
        {
            Chart.Indicators.Remove(_indicator1);
            Chart.Indicators.Remove(_indicator2);
        }

        protected override void OnTick()
        {
            var longPosition = Positions.Find(label, SymbolName, TradeType.Buy);
            var shortPosition = Positions.Find(label, SymbolName, TradeType.Sell);

            var currentSlowMa = slowMa.Result.Last(0);
            var currentFastMa = fastMa.Result.Last(0);
            var previousSlowMa = slowMa.Result.Last(1);
            var previousFastMa = fastMa.Result.Last(1);

            if (previousSlowMa > previousFastMa && currentSlowMa <= currentFastMa && longPosition == null)
            {
                if (shortPosition != null)
                    ClosePosition(shortPosition);

                ExecuteMarketOrder(TradeType.Buy, SymbolName, VolumeInUnits, label);
            }
            else if (previousSlowMa < previousFastMa && currentSlowMa >= currentFastMa && shortPosition == null)
            {
                if (longPosition != null)
                    ClosePosition(longPosition);

                ExecuteMarketOrder(TradeType.Sell, SymbolName, VolumeInUnits, label);
            }
        }

        private double VolumeInUnits
        {
            get { return Symbol.QuantityToVolumeInUnits(Quantity); }
        }
    }
}

cBotをビルドするには、Ctrl+Bホットキーを使用するか、ビルドをクリックします。

Tradeアプリに移動します。 EURUSDチャートを選択し、cBotアイコンをクリックし、Sample Trend cBotを検索して選択します。

インスタンスを追加ウィンドウが表示されたら、適用をクリックしてcBotを起動します。

取引に使用される2つの移動平均がチャートに追加されたのが確認できるはずです。

別のcBotを使用してcBotを起動する

別のcBotを通じてcBotを管理する方法をデモンストレーションします。 今回は、新しく空のcBotをゼロから作成します。

Algoアプリに移動し、cBotsタブの下にある新規ボタンをクリックします。 空白オプションを選択し、Add cBotsなどの名前を入力してから作成をクリックします。

まず、2つのチャートロボットオブジェクトを定義します。

1
2
ChartRobot _robot1;
ChartRobot _robot2;

次に、それらのロボットをOnStart()メソッド内でチャートに追加します。

1
2
_robot1 = Chart.Robots.Add("Sample Trend cBot", 0.01, MovingAverageType.Simple, Bars.ClosePrices, 10, 5);
_robot2 = Chart.Robots.Add("Sample Trend cBot", 0.01, MovingAverageType.Simple, Bars.ClosePrices, 12, 7);

また、cBotが起動するたびにメッセージを表示するイベントハンドラーを追加することもできます。

1
2
3
4
5
6
Chart.Robots.RobotStarted += ChartRobots_RobotStarted;

private void ChartRobots_RobotStarted(ChartRobotStartedEventArgs obj)
{
    Print ("Robot Started");
}

OnBarClosed()メソッド内にロジックを記述して、バーが変化したときに最初のロボットを起動し、停止してから次のバーで2番目のロボットを起動します。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
protected override void OnBarClosed()
{
    if (_robot1.State == RobotState.Stopped)
    {
        _robot1.Start();
        _robot2.Stop();
            }
    else if (_robot1.State == RobotState.Running)
    {
        _robot1.Stop();
        _robot2.Start();
    }
}

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

 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
using System;
using cAlgo.API;
using cAlgo.API.Collections;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;

namespace cAlgo.Robots
{
    [Robot(AccessRights = AccessRights.None, AddIndicators = true)]
    public class AddcBots : Robot
    {

        ChartRobot _robot1;
        ChartRobot _robot2;

        protected override void OnStart()
        {
            _robot1 = Chart.Robots.Add("Sample Trend cBot", 0.01, MovingAverageType.Simple, Bars.ClosePrices, 10, 5);
            _robot2 = Chart.Robots.Add("Sample Trend cBot", 0.01, MovingAverageType.Simple, Bars.ClosePrices, 12, 7);

            Chart.Robots.RobotStarted += ChartRobots_RobotStarted;
        }

        private void ChartRobots_RobotStarted(ChartRobotStartedEventArgs obj)
        {
            Print ("Robot Started");

        }

        protected override void OnBarClosed()
        {
            if (_robot1.State == RobotState.Stopped)
            {
                _robot1.Start();
                _robot2.Stop();
            }

            else if (_robot1.State == RobotState.Running)
            {
                _robot2.Start();
                _robot1.Stop();
            }
        }

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

cBotをビルドした後、Tradeアプリに戻り、Add cBotsを検索して選択し、cBotを起動します。

権限のリクエストダイアログが表示されたら、許可をクリックします。

Sample Trend cBotの2つのインスタンスがチャートに表示されるはずです。

最初のバーが完了するのを待つと、Sample Trend cBotの最初のインスタンスが自動的に起動するのが見えるはずです。

次のバーでは、Sample Trend cBotの2番目のインスタンスが自動的に起動するのが見えるはずです。

cBotが私たちのロジックを実行し、変化する条件に基づいて他の2つのcBotを管理する様子を観察できます。

実行中にcBotのパラメーターを変更する

実行中のcBotのパラメーターを変更する必要があるかもしれません。 例えば、重要な金融ニュースや更新を受けた後、すぐにコードを更新することを決定した場合です。

cBotを停止して再起動するのではなく、最初のcBotのSlowPeriodsパラメーターをすぐに変更しましょう。

1
2
3
4
5
6
else if(_robot1.State == RobotState.Running)
{
_robot1.Stop();
_robot1.Parameters["SlowPeriods"].Value = (int)_robot2.Parameters["SlowPeriods"].Value + 1;
_robot1.Start();
}

次に、cBotをリビルドします。

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

 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
using System;
using cAlgo.API;
using cAlgo.API.Collections;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;

namespace cAlgo.Robots
{
    [Robot(AccessRights = AccessRights.None, AddIndicators = true)]
    public class AddcBots : Robot
    {

        ChartRobot _robot1;
        ChartRobot _robot2;

        protected override void OnStart()
        {
            _robot1 = Chart.Robots.Add("Sample Trend cBot", 0.01, MovingAverageType.Simple, Bars.ClosePrices, 10, 5);
            _robot2 = Chart.Robots.Add("Sample Trend cBot", 0.01, MovingAverageType.Simple, Bars.ClosePrices, 12, 7);

            Chart.Robots.RobotStarted += ChartRobots_RobotStarted;
        }

        private void ChartRobots_RobotStarted(ChartRobotStartedEventArgs obj)
        {
            Print ("Robot Started");

        }

        protected override void OnBarClosed()
        {
            if (_robot1.State == RobotState.Stopped)
            {
                _robot1.Start();
                _robot2.Stop();
            }

            else if (_robot1.State == RobotState.Running)
            {
                _robot1.Stop();
                _robot1.Parameters["SlowPeriods"].Value = (int)_robot2.Parameters["SlowPeriods"].Value + 1;
                _robot1.Start();
            }
        }

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

Tradeアプリに移動し、cBotを起動して、各バーでインジケーターの値がどのように変化するかを確認します。

サマリー

この記事が、アルゴリズムを使用して他のアルゴリズムを起動、制御、管理する方法を理解するのに役立ったことを願っています。