Skip to content

Commit df42db7

Browse files
committed
BacktestingInPlugins Sample added
1 parent 322891b commit df42db7

File tree

3 files changed

+168
-0
lines changed

3 files changed

+168
-0
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 16
4+
VisualStudioVersion = 16.0.30011.22
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BacktestingInPlugins Sample", "BacktestingInPlugins Sample\BacktestingInPlugins Sample.csproj", "{8fec9cbb-2ba1-4aba-901f-813140762451}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
Release|Any CPU = Release|Any CPU
12+
EndGlobalSection
13+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
14+
{8fec9cbb-2ba1-4aba-901f-813140762451}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{8fec9cbb-2ba1-4aba-901f-813140762451}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{8fec9cbb-2ba1-4aba-901f-813140762451}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{8fec9cbb-2ba1-4aba-901f-813140762451}.Release|Any CPU.Build.0 = Release|Any CPU
18+
EndGlobalSection
19+
GlobalSection(SolutionProperties) = preSolution
20+
HideSolutionNode = FALSE
21+
EndGlobalSection
22+
EndGlobal
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
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"]}\nNet 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+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project Sdk="Microsoft.NET.Sdk">
3+
<PropertyGroup>
4+
<TargetFramework>net6.0</TargetFramework>
5+
</PropertyGroup>
6+
<ItemGroup>
7+
<PackageReference Include="cTrader.Automate" Version="*" />
8+
</ItemGroup>
9+
<ItemGroup>
10+
<Reference Include="cAlgo.API, Version=1.0.0.0, Culture=neutral, PublicKeyToken=3499da3018340880">
11+
<HintPath>..\..\..\..\..\..\AppData\Local\Spotware\cTrader\330f49df8243756a8a4dc7f7f7ee6dfe\app_5.1.25171.25171\cAlgo.API.dll</HintPath>
12+
</Reference>
13+
</ItemGroup>
14+
</Project>

0 commit comments

Comments
 (0)