プラグインでのバックテスト

cTrader Algo では、プラグインから直接 cBot のバックテストを行うことができるため、アルゴリズム開発者に多くの可能性を提供します。以下の1分間の要約で、さらに詳しく学びましょう。

プラグインでのバックテストを1分で理解!

  • プログラム的にバックテストを開始するか、ユーザーのフィードバックに応じてバックテストを実行し、プラグインを配置できるcTrader UI内の任意の場所に結果を出力します!
  • Monte Carloシミュレーションなど、新しいバックテスト戦略を追加して、cTraderの組み込みバックテスト機能を拡張します。
  • バックテスト結果にカスタム統計を追加し、それをcTrader UI内で直接表示します!
  • 標準の遺伝的アルゴリズムを超えた複雑な最適化方法を作成します。

プラグインでのバックテストの仕組み

Plugin 基底クラスは、Backtesting インターフェースにアクセスでき、以下のシグネチャを持つ Start() メソッドを呼び出すことができます。

1
BacktestingProcess Start(RobotType robotType, string symbolName, TimeFrame timeFrame, BacktestingSettings settings, params object[] parameterValues);

パラメータ

parameterValues 配列では、cBot パラメータは固定の順序で渡す必要があります(cTrader UIで指定されている順序)。一部のパラメータが欠けている場合、デフォルト値が自動的に挿入されます。

バックテストプロセス

プログラム的にバックテストを開始する際には、複数のバックテストプロセスを並行して実行できるため、時間を大幅に節約できる可能性があります。

さらに、インターフェースには ProgressChanged および Completed の二つのイベントも含まれています。Completed イベントの引数(BacktestingCompletedEventArgs)には、最終的なバックテスト結果の JSON オブジェクト(JsonReport)が含まれており、これを解釈して結果の統計を新しいユーザーに表示することができます。

例示プラグインの作成

以下のプラグインは、ASP に新しいブロックを表示します。ブロック内で、プラグインはユーザーに所有する任意の cBot を選択させ、それを EURUSD h1 でバックテストします。バックテストが終了すると、プラグインは最終的なROIとネット利益を表示します。

  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
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
using System;
using cAlgo.API;
using cAlgo.API.Collections;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;
using System.Linq;
using System.Text.Json;
using System.Text.Json.Nodes;
namespace cAlgo.Plugins
{
    [Plugin(AccessRights = AccessRights.None)]
    public class BacktestingInPluginsSample : Plugin
    {

        // Declaring the necessary UI elements
        // and the cBot (RobotType) selected in the ComboBox
        private Grid _grid;
        private ComboBox _cBotsComboBox;
        private Button _startBacktestingButton;
        private TextBlock _resultsTextBlock;
        private RobotType _selectedRobotType;

        protected override void OnStart()
        {
            // Initialising and structuring the UI elements
            _grid = new Grid(3, 1);
            _cBotsComboBox = new ComboBox();
            _startBacktestingButton = new Button
            {
                BackgroundColor = Color.Green,
                CornerRadius = new CornerRadius(5),
                Text = "Start Backtesting",
            };
            _resultsTextBlock = new TextBlock
            {
                HorizontalAlignment = HorizontalAlignment.Center,
                VerticalAlignment = VerticalAlignment.Center,
                Text = "Select a cBot...",
            };

            _grid.AddChild(_cBotsComboBox, 0, 0);
            _grid.AddChild(_startBacktestingButton, 1, 0);
            _grid.AddChild(_resultsTextBlock, 2, 0);


            var block = Asp.SymbolTab.AddBlock("Backtesting Plugin");

            block.Child = _grid;

             // Populating the ComboBox with existing cBots
            PopulateCBotsComboBox();

            // Assigning event handlers to the Button.Click,
            // ComboBox.SelectedItemChanged, and Backtesting.Completed events
            _startBacktestingButton.Click += StartBacktestingButton_Click;
            _cBotsComboBox.SelectedItemChanged += CBotsComboBox_SelectedItemChanged;
            Backtesting.Completed += Backtesting_Completed;

        }

        protected void StartBacktestingButton_Click(ButtonClickEventArgs obj)
        {

            // Initialising and configuring the backtesting settings
            var backtestingSettings = new BacktestingSettings 
            {
                DataMode = BacktestingDataMode.M1,
                StartTimeUtc = new DateTime(2023, 6, 1),
                EndTimeUtc = DateTime.UtcNow,
                Balance = 10000,
            };

            // Starting backtesting on EURUSD h1
            Backtesting.Start(_selectedRobotType, "EURUSD", TimeFrame.Hour, backtestingSettings);

            // Disabling other controls and changing
            // the text inside the TextBlock
            _cBotsComboBox.IsEnabled = false;
            _startBacktestingButton.IsEnabled = false;
            _resultsTextBlock.Text = "Backtesting in progress...";
        }

        protected void PopulateCBotsComboBox()
        {
            // Iterating over the AlgoRegistry and
            // getting the names of all installed cBots
            foreach (var robotType in AlgoRegistry.OfType<RobotType>())
            {
                _cBotsComboBox.AddItem(robotType.Name);
            }
        }

        protected void Backtesting_Completed(BacktestingCompletedEventArgs obj)
        {
            // Attaining the JSON results of backtesting
            string jsonResults = obj.JsonReport;

            // Converting the JSON string into a JsonNode
            JsonNode resultsNode = JsonNode.Parse(jsonResults);

            // Attaining the ROI and net profit from backtesting results
            _resultsTextBlock.Text = $"ROI: {resultsNode["main"]["roi"]}\nNet Profit: {resultsNode["main"]["netProfit"]}";

            // Re-enabling controls after backteting is finished
            _cBotsComboBox.IsEnabled = true;
            _startBacktestingButton.IsEnabled = true;
        }

        protected void CBotsComboBox_SelectedItemChanged(ComboBoxSelectedItemChangedEventArgs obj)
        {
            // Updading the variable to always contain
            // the cBto selected in the ComboBox
            _selectedRobotType = AlgoRegistry.Get(obj.SelectedItem) as RobotType;
        }

    }
}

プラグインはバックテストプロセスの状態に動的に反応します。バックテストが終了すると、プラグインは TextBlock に結果を表示します。バックテスト中は、_startBacktestingButton_cBotsComboBox は無効化されます。

まとめ

プラグインを通じたバックテストは、cTrader が提供する強力なバックテストロジックの上に UI 拡張機能を構築できる強力な機能です。AlgoRegistry などの他の API メンバーと組み合わせることで、プラグインでのバックテストは cTrader アルゴを販売および/または開発している誰にとっても多くの可能性を提供します。

目次

このページについて