コンテンツにスキップ

BacktestingInPlugins Sample

概要

BacktestingInPlugins Sampleプラグインは、以下の主要な機能を使用してcTraderプラットフォームのインターフェースから直接、過去の戦略テストを実行することを可能にします:

  • ドロップダウンメニュー、アクションボタン、結果表示フィールドを含む構造化されたパネルレイアウトを作成します。
  • 簡単に選択できるよう、インストールされているすべてのcBotをドロップダウンにリストします。
  • トリガーされると、設定された通貨ペアで過去のデータを使用してバックテストを実行します。
  • バックテスト結果からROI(投資利益率)と純利益を表示します。

このプラグインはアクティブ通貨ペアパネル内のカスタムパネルで実行され、cTrader WindowsまたはMacが開いている間はアクティブのままです。 プラットフォームが閉じられるか再起動されると自動的に停止します。

プラグインの作成

ステップバイステップのガイドで、テンプレートまたはゼロからプラグインを作成、編集、ビルドする方法をご覧ください。

BacktestingInPlugins SampleプラグインのコードはGitHubで見つけることができます。または、以下にコピーしてください。

サンプルコード
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 backtesting is finished
            _cBotsComboBox.IsEnabled = true;
            _startBacktestingButton.IsEnabled = true;
        }

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

カスタマイズオプション

このプラグインは、バックテストを開始し、基本的な結果を表示するための直接的なインターフェースを提供します。 以下の表は、その主要コンポーネントとその機能を概説しています:

パラメータ 説明 可能な値
_grid プラグインインターフェースのレイアウトを定義します。 (3, 1), (2, 2), など。
_startBacktestingButton 開始ボタンの外観とラベルをカスタマイズします。 Start Backtesting, Color.Green, new CornerRadius(5)
_cBotsComboBox.AddItem レジストリからcBotをComboBoxに入力します。 robottype.name
_resultsTextBlock.Text バックテストサイクル中にメッセージを表示します。 Select a cBot..., Backtesting in progress..., Backtesting results summary, など。
CBotsComboBox_SelectedItemChanged テスト用に選択されたcBotを更新します。 obj.selecteditem
StartTimeUtc バックテストの開始日時(UTC)。 new datetime(2023, 6, 1)
EndTimeUtc バックテストの終了日時(UTC)。 datetime.utcnow
DataMode 各データポイントの移動が何を表すか(1分、1時間、または1日)を定義します。 backtestingdatamode.m1
Balance 仮想口座の開始残高。 10000, 20000, など。
Backtesting.Start バックテストを実行する通貨ペアと時間枠を定義します。 eurusd, timeframe.hour
Backtesting_Completed テスト結果を抽出してUIに表示します。 resultsnode[main][roi], resultsnode[main][netprofit], など。

注意

ドロップダウンが正しく入力されるように、少なくとも1つのcBotがcTraderにインストールされていることを確認してください。

ユースケース

このプラグインは、アクティブ通貨ペアパネルで過去の戦略テストを実行する方法を提供します。 cBotの選択とテストを簡素化し、戦略評価に理想的です。 以下は、プラグインが実際の取引シナリオにどのように適用できるかを示す実用的な使用例です。

ユースケース シナリオ 価値
戦略の比較 同一のテスト条件を使用して複数のcBotでバックテストを実行します。 単一のセッション内で直接的なパフォーマンス比較を可能にします。
クイックパフォーマンスレビュー 標準的な市場設定で任意のインストール済みcBotをテストします。 ライブ口座で実行する前にcBotのパフォーマンスを評価するのに役立ちます。
固定ルールのバックテスト 事前定義されたバックテストを起動してアルゴリズムの変更を検証します。 戦略のデバッグやバージョンテスト時にテスト条件の一貫性を確保します。
クイックインサイトツール インストールされたcBotをテストした直後にROIと利益のフィードバックを表示します。 個別のテストセッションを起動するよりも時間を節約できます。

概要

このプラグインは、cBotを選択し、事前定義された設定を使用して過去のバックテストを実行するための合理化されたインターフェースを提供します。 選択された通貨ペアと時間枠でテストを実行し、ROIや純利益などの主要なパフォーマンス指標を表示します。 この構造は、迅速な戦略検証をサポートし、さまざまな取引シナリオに適応可能です。

開発の詳細については、プラグインのドキュメントを参照してください。