Skip to content

Commit e6e500c

Browse files
authored
Merge pull request spotware#24 from spotware/CNT-677--Custom-Toolbar-Button-Sample
es/CNT-677--Custom-Toolbar-Button-Sample
2 parents a4925e4 + 570be99 commit e6e500c

File tree

4 files changed

+109
-0
lines changed

4 files changed

+109
-0
lines changed

Plugins/.samples.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@
3232
{
3333
"name": "Custom Frame Sample"
3434
},
35+
{
36+
"name": "Custom Toolbar Button"
37+
},
3538
{
3639
"name": "Custom Window Plugin"
3740
},
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}") = "Custom Toolbar Button", "Custom Toolbar Button\Custom Toolbar Button.csproj", "{57aed799-0d95-49c7-ae8d-a8765b3052d6}"
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+
{57aed799-0d95-49c7-ae8d-a8765b3052d6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{57aed799-0d95-49c7-ae8d-a8765b3052d6}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{57aed799-0d95-49c7-ae8d-a8765b3052d6}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{57aed799-0d95-49c7-ae8d-a8765b3052d6}.Release|Any CPU.Build.0 = Release|Any CPU
18+
EndGlobalSection
19+
GlobalSection(SolutionProperties) = preSolution
20+
HideSolutionNode = FALSE
21+
EndGlobalSection
22+
EndGlobal
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
// -------------------------------------------------------------------------------------------------
2+
//
3+
// This code is a cTrader Algo API example.
4+
//
5+
// The code is provided as a sample only and does not guarantee any particular outcome or profit of any kind. Use it at your own risk.
6+
//
7+
// This example plugin adds a new button with a pop-up menu to the Chart Toolbar, featuring a 'Close All Positions' button that closes open positions when clicked.
8+
//
9+
// For a detailed tutorial on creating this plugin, watch the video at: [to:do]
10+
//
11+
// -------------------------------------------------------------------------------------------------
12+
13+
using System;
14+
using cAlgo.API;
15+
using cAlgo.API.Collections;
16+
using cAlgo.API.Indicators;
17+
using cAlgo.API.Internals;
18+
19+
namespace cAlgo.Plugins
20+
{
21+
[Plugin(AccessRights = AccessRights.None)]
22+
public class CustomToolbarButton : Plugin
23+
{
24+
protected override void OnStart()
25+
{
26+
var icon = new SvgIcon(@"<svg class='w-6 h-6 text-gray-800 dark:text-white' aria-hidden='true' xmlns='http://www.w3.org/2000/svg' fill='none' viewBox='0 0 24 24'>
27+
<path stroke='#BFBFBF' stroke-linecap='round' stroke-linejoin='round' stroke-width='2' d='M11 6.5h2M11 18h2m-7-5v-2m12 2v-2M5 8h2a1 1 0 0 0 1-1V5a1 1 0 0 0-1-1H5a1 1 0 0 0-1 1v2a1 1 0 0 0 1 1Zm0 12h2a1 1 0 0 0 1-1v-2a1 1 0 0 0-1-1H5a1 1 0 0 0-1 1v2a1 1 0 0 0 1 1Zm12 0h2a1 1 0 0 0 1-1v-2a1 1 0 0 0-1-1h-2a1 1 0 0 0-1 1v2a1 1 0 0 0 1 1Zm0-12h2a1 1 0 0 0 1-1V5a1 1 0 0 0-1-1h-2a1 1 0 0 0-1 1v2a1 1 0 0 0 1 1Z'/>
28+
</svg>");
29+
30+
var command = Commands.Add(CommandType.ChartContainerToolbar, OpenPositions, icon);
31+
command.ToolTip = "Open Positions";
32+
33+
Commands.Add(CommandType.ChartContainerToolbar, CloseAllPositions, icon);
34+
}
35+
36+
private CommandResult CloseAllPositions (CommandArgs args)
37+
{
38+
var buttonStyle = new Style();
39+
40+
buttonStyle.Set(ControlProperty.Margin, new Thickness(0, 5, 0, 0));
41+
buttonStyle.Set(ControlProperty.Width, 150);
42+
43+
var closePositionsButton = new Button
44+
{
45+
Text = "Close All Positions",
46+
Style = buttonStyle
47+
};
48+
49+
closePositionsButton.Click += args =>
50+
{
51+
foreach (var position in Positions)
52+
{
53+
position.Close();
54+
}
55+
};
56+
57+
var stackPanel = new StackPanel();
58+
stackPanel.AddChild(closePositionsButton);
59+
60+
return new CommandResult(stackPanel);
61+
}
62+
63+
private void OpenPositions(CommandArgs args)
64+
{
65+
ExecuteMarketOrder(TradeType.Buy, "EURUSD", 1000);
66+
ExecuteMarketOrder(TradeType.Buy, "USDJPY", 1000);
67+
ExecuteMarketOrder(TradeType.Buy, "EURGBP", 1000);
68+
}
69+
70+
protected override void OnStop()
71+
{
72+
// Handle Plugin stop here
73+
}
74+
}
75+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<PropertyGroup>
3+
<TargetFramework>net6.0</TargetFramework>
4+
</PropertyGroup>
5+
6+
<ItemGroup>
7+
<PackageReference Include="cTrader.Automate" Version="*" />
8+
</ItemGroup>
9+
</Project>

0 commit comments

Comments
 (0)