1+ // -------------------------------------------------------------------------------------------------
2+ //
3+ // This code is a cTrader Algo API example.
4+ //
5+ // This code is intended to be used as a sample and does not guarantee any particular outcome or
6+ // profit of any kind. Use it at your own risk.
7+ //
8+ // This sample adds a new block into the ASP. The block contains a ComboBox, a custom Button,
9+ // and a TextBlock. Upon choosing one of their installed cBots in the ComboBox, the user can click
10+ // on the Button and backtest the chosen cBot on EURUSD h1. When backtesting is finished, the plugin
11+ // will display its results in the TextBlock.
12+ //
13+ // -------------------------------------------------------------------------------------------------
14+
15+ using System ;
16+ using cAlgo . API ;
17+ using cAlgo . API . Collections ;
18+ using cAlgo . API . Indicators ;
19+ using cAlgo . API . Internals ;
20+ using System . Linq ;
21+ using System . Text . Json ;
22+ using System . Text . Json . Nodes ;
23+
24+ namespace cAlgo . Plugins
25+ {
26+ [ Plugin ( AccessRights = AccessRights . None ) ]
27+ public class BacktestingInPluginsSample : Plugin
28+ {
29+
30+ // Declaring the necessary UI elements
31+ // and the cBot (RobotType) selected in the ComboBox
32+ private Grid _grid ;
33+ private ComboBox _cBotsComboBox ;
34+ private Button _startBacktestingButton ;
35+ private TextBlock _resultsTextBlock ;
36+ private RobotType _selectedRobotType ;
37+
38+ protected override void OnStart ( )
39+ {
40+ // Initialising and structuring the UI elements
41+ _grid = new Grid ( 3 , 1 ) ;
42+ _cBotsComboBox = new ComboBox ( ) ;
43+ _startBacktestingButton = new Button
44+ {
45+ BackgroundColor = Color . Green ,
46+ CornerRadius = new CornerRadius ( 5 ) ,
47+ Text = "Start Backtesting" ,
48+ } ;
49+ _resultsTextBlock = new TextBlock
50+ {
51+ HorizontalAlignment = HorizontalAlignment . Center ,
52+ VerticalAlignment = VerticalAlignment . Center ,
53+ Text = "Select a cBot..." ,
54+ } ;
55+
56+ _grid . AddChild ( _cBotsComboBox , 0 , 0 ) ;
57+ _grid . AddChild ( _startBacktestingButton , 1 , 0 ) ;
58+ _grid . AddChild ( _resultsTextBlock , 2 , 0 ) ;
59+
60+
61+ var block = Asp . SymbolTab . AddBlock ( "Backtesting Plugin" ) ;
62+
63+ block . Child = _grid ;
64+
65+ // Populating the ComboBox with existing cBots
66+ PopulateCBotsComboBox ( ) ;
67+
68+ // Assigning event handlers to the Button.Click,
69+ // ComboBox.SelectedItemChanged, and Backtesting.Completed events
70+ _startBacktestingButton . Click += StartBacktestingButton_Click ;
71+ _cBotsComboBox . SelectedItemChanged += CBotsComboBox_SelectedItemChanged ;
72+ Backtesting . Completed += Backtesting_Completed ;
73+
74+ }
75+
76+ protected void StartBacktestingButton_Click ( ButtonClickEventArgs obj )
77+ {
78+
79+ // Initialising and configuring the backtesting settings
80+ var backtestingSettings = new BacktestingSettings
81+ {
82+ DataMode = BacktestingDataMode . M1 ,
83+ StartTimeUtc = new DateTime ( 2023 , 6 , 1 ) ,
84+ EndTimeUtc = DateTime . UtcNow ,
85+ Balance = 10000 ,
86+ } ;
87+
88+ // Starting backtesting on EURUSD h1
89+ Backtesting . Start ( _selectedRobotType , "EURUSD" , TimeFrame . Hour , backtestingSettings ) ;
90+
91+ // Disabling other controls and changing
92+ // the text inside the TextBlock
93+ _cBotsComboBox . IsEnabled = false ;
94+ _startBacktestingButton . IsEnabled = false ;
95+ _resultsTextBlock . Text = "Backtesting in progress..." ;
96+ }
97+
98+ protected void PopulateCBotsComboBox ( )
99+ {
100+ // Iterating over the AlgoRegistry and
101+ // getting the names of all installed cBots
102+ foreach ( var robotType in AlgoRegistry . OfType < RobotType > ( ) )
103+ {
104+ _cBotsComboBox . AddItem ( robotType . Name ) ;
105+ }
106+ }
107+
108+ protected void Backtesting_Completed ( BacktestingCompletedEventArgs obj )
109+ {
110+ // Attaining the JSON results of backtesting
111+ string jsonResults = obj . JsonReport ;
112+
113+ // Converting the JSON string into a JsonNode
114+ JsonNode resultsNode = JsonNode . Parse ( jsonResults ) ;
115+
116+ // Attaining the ROI and net profit from backtesting results
117+ _resultsTextBlock . Text = $ "ROI: { resultsNode [ "main" ] [ "roi" ] } \n Net Profit: { resultsNode [ "main" ] [ "netProfit" ] } ";
118+
119+ // Re-enabling controls after backteting is finished
120+ _cBotsComboBox . IsEnabled = true ;
121+ _startBacktestingButton . IsEnabled = true ;
122+ }
123+
124+ protected void CBotsComboBox_SelectedItemChanged ( ComboBoxSelectedItemChangedEventArgs obj )
125+ {
126+ // Updading the variable to always contain
127+ // the cBto selected in the ComboBox
128+ _selectedRobotType = AlgoRegistry . Get ( obj . SelectedItem ) as RobotType ;
129+ }
130+
131+ }
132+ }
0 commit comments