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+ }
0 commit comments