跳转至

插件中的回测

cTrader Algo 允许直接从插件中进行 cBot 回测,这为算法开发者提供了许多可能性。 阅读下面的一分钟摘要以了解更多信息。

一分钟掌握插件中的回测!

  • 编程方式启动回测或响应用户反馈,并将结果显示在 cTrader UI 中插件可以放置的任何合适位置。
  • 通过添加新的回测策略(如蒙特卡洛模拟)来扩展 cTrader 内置的回测功能。
  • 向回测结果添加自定义统计信息,并直接在 cTrader UI 中显示。
  • 创建超越标准遗传算法的复杂优化方法。

插件中的回测如何工作

Plugin 基类可以访问 Backtesting 接口,从中可以调用具有以下签名的 Start() 方法。

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

参数

parameterValues 数组中,cBot 参数必须按固定顺序传递(即它们在 cTrader UI 中指定的顺序)。 如果缺少某些参数,将自动插入默认值。

回测流程

当以编程方式启动回测时,您可以并行启动多个回测进程,这可能会为您节省大量时间。

此外,界面还包含两个事件,即 ProgressChangedCompletedCompleted 事件的参数 (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 backtesting is finished
            _cBotsComboBox.IsEnabled = true;
            _startBacktestingButton.IsEnabled = true;
        }

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

    }
}

插件会动态响应回测进程的状态。 一旦回测完成,插件会在 TextBlock 中显示结果。 _startBacktestingButton_cBotsComboBox 在回测期间被禁用。

总结

通过插件进行回测是一项强大的功能,允许您在 cTrader 提供的强大回测逻辑之上构建 UI 扩展。 结合其他 API 成员,如 AlgoRegistry,插件中的回测为销售和开发 cTrader 算法的用户提供了许多可能性。

Image title