cTrader Algo cho phép backtest cBot trực tiếp từ một plugin, mở ra nhiều khả năng cho các nhà phát triển thuật toán. Đọc tóm tắt một phút của chúng tôi bên dưới để tìm hiểu thêm.
Backtest trong plugin trong một phút!
Khởi chạy backtest theo chương trình hoặc để phản hồi phản hồi của người dùng và xuất kết quả ở bất kỳ vị trí phù hợp nào trong giao diện người dùng cTrader nơi có thể đặt plugin.
Mở rộng khả năng backtest tích hợp sẵn trong cTrader bằng cách thêm các chiến lược backtest mới như mô phỏng Monte Carlo.
Thêm thống kê tùy chỉnh vào kết quả backtest và hiển thị chúng trực tiếp trong giao diện người dùng cTrader.
Tạo các phương pháp tối ưu hóa phức tạp vượt xa thuật toán di truyền tiêu chuẩn.
Cách thức hoạt động của backtest trong plugin
Lớp cơ sở Plugin có quyền truy cập vào giao diện Backtesting từ đó có thể gọi phương thức Start() với chữ ký sau.
Trong mảng parameterValues, các tham số cBot phải được truyền theo thứ tự cố định (thứ tự được chỉ định trong giao diện người dùng cTrader). Nếu một số tham số bị thiếu, các giá trị mặc định sẽ được chèn tự động.
Quy trình backtest
Khi khởi chạy backtest theo chương trình, bạn có thể khởi chạy nhiều quy trình backtest song song, điều này có thể tiết kiệm cho bạn rất nhiều thời gian.
Ngoài ra, giao diện cũng chứa hai sự kiện, cụ thể là ProgressChanged và Completed. Các đối số cho sự kiện Completed (BacktestingCompletedEventArgs) chứa một đối tượng JSON của kết quả backtest cuối cùng (JsonReport), cho phép bạn diễn giải chúng theo nhu cầu và hiển thị thống kê kết quả cho người dùng mới.
Tạo một plugin ví dụ
Plugin sau đây hiển thị một khối mới trong Bảng ký hiệu đang hoạt động (ASP). Bên trong khối, plugin cho phép người dùng chọn bất kỳ cBot nào họ sở hữu và backtest nó trên EURUSD h1. Sau khi backtest kết thúc, plugin hiển thị ROI và lợi nhuận ròng cuối cùng.
usingSystem;usingcAlgo.API;usingcAlgo.API.Collections;usingcAlgo.API.Indicators;usingcAlgo.API.Internals;usingSystem.Linq;usingSystem.Text.Json;usingSystem.Text.Json.Nodes;namespacecAlgo.Plugins{[Plugin(AccessRights = AccessRights.None)]publicclassBacktestingInPluginsSample:Plugin{// Declaring the necessary UI elements// and the cBot (RobotType) selected in the ComboBoxprivateGrid_grid;privateComboBox_cBotsComboBox;privateButton_startBacktestingButton;privateTextBlock_resultsTextBlock;privateRobotType_selectedRobotType;protectedoverridevoidOnStart(){// Initialising and structuring the UI elements_grid=newGrid(3,1);_cBotsComboBox=newComboBox();_startBacktestingButton=newButton{BackgroundColor=Color.Green,CornerRadius=newCornerRadius(5),Text="Start Backtesting",};_resultsTextBlock=newTextBlock{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);varblock=Asp.SymbolTab.AddBlock("Backtesting Plugin");block.Child=_grid;// Populating the ComboBox with existing cBotsPopulateCBotsComboBox();// 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;}protectedvoidStartBacktestingButton_Click(ButtonClickEventArgsobj){// Initialising and configuring the backtesting settingsvarbacktestingSettings=newBacktestingSettings{DataMode=BacktestingDataMode.M1,StartTimeUtc=newDateTime(2023,6,1),EndTimeUtc=DateTime.UtcNow,Balance=10000,};// Starting backtesting on EURUSD h1Backtesting.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...";}protectedvoidPopulateCBotsComboBox(){// Iterating over the AlgoRegistry and// getting the names of all installed cBotsforeach(varrobotTypeinAlgoRegistry.OfType<RobotType>()){_cBotsComboBox.AddItem(robotType.Name);}}protectedvoidBacktesting_Completed(BacktestingCompletedEventArgsobj){// Attaining the JSON results of backtestingstringjsonResults=obj.JsonReport;// Converting the JSON string into a JsonNodeJsonNoderesultsNode=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;}protectedvoidCBotsComboBox_SelectedItemChanged(ComboBoxSelectedItemChangedEventArgsobj){// Updading the variable to always contain// the cBot selected in the ComboBox_selectedRobotType=AlgoRegistry.Get(obj.SelectedItem)asRobotType;}}}
Plugin phản ứng động với trạng thái của quá trình backtest. Ngay khi backtest hoàn tất, plugin hiển thị kết quả trong TextBlock. _startBacktestingButton và _cBotsComboBox bị vô hiệu hóa trong suốt quá trình backtest.
Tổng quan
Backtest thông qua plugin là một tính năng mạnh mẽ cho phép bạn xây dựng các tiện ích mở rộng giao diện người dùng trên nền tảng logic backtest mạnh mẽ đã có sẵn của cTrader. Kết hợp với các thành viên API khác như AlgoRegistry, backtest trong plugin mang đến nhiều khả năng cho bất kỳ ai bán và phát triển thuật toán cTrader.