Skip to content

Plugins With Parameters

Plugins can only ever have one instance; despite this, it is still possible to have plugins with customisable parameters. In this guide, we can explain how you can easily create a plugin whose instance changes behaviour on user input.

Plugins With Parameters in One Minute!

  • Change the settings of a plugin on-the-fly by adding custom UI elements supporting user input!
  • Adding parameters is easy and can be done by declaring event handlers and assigning them to custom UI elements of your choosing.
  • Parameters in plugins are essential to the developers of trading panels but can also be valuable in other types of plugins.

Adding Parameters to Plugins

When a plugin is launched, its singleton instance immediatelly starts running and can only be stopped from the general settings.

If you add a parameter declaration (such as [Parameter(DefaultValue = 10000, MinValue = 1, Step = 1000)]), the plugin will operate using only the default value and changing the parameter from the cTrader UI will be impossible.

Nevertheless, you can still add customisable parameters by following these steps.

  • Create several fields or other elements that allow for user input (e.g., a TextField)
  • Reference the current content of these fields
  • Pass the reference to any suitable method inside your plugin code

Creating an Example Plugin

The example plugin does the following.

  • Adds a block with a custom 'Buy' button to the ASP display
  • Shows a custom window with the order volume entry field
  • On clicking the 'Buy' button, allows the user to place a market order with the volume from the volume entry field
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using cAlgo.API;
using cAlgo.API.Collections;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;

namespace cAlgo.Plugins
{
    [Plugin(AccessRights = AccessRights.None)]
    public class ParametersPlugin : Plugin
    {

        private Button _buttonTrade;
        private Window _window;
        private AspBlock _aspBlock;
        private TextBox _textBox;
        private double _orderVolume;


        protected override void OnStart()
        {
            _aspBlock = Asp.SymbolTab.AddBlock("Trader Plugin");
            _aspBlock.IsExpanded = true;
            _aspBlock.Height = 200;

            _buttonTrade = new Button
            {
                BackgroundColor = Color.SeaGreen,
                Height = 50,
                Text = "Buy"
            };
            _buttonTrade.Click += OnButtonClick;
            _window = new Window
            {
                Height = 150,
                Width = 400,
                Padding = new Thickness(5, 10, 10, 5),
            };
            _textBox = new TextBox
            {
                Text = "Enter order volume here...",
                Margin = 5,
                ForegroundColor = Color.White,
                HorizontalAlignment = HorizontalAlignment.Center,
            };


            _textBox.TextChanged += OnTextChanged;
            _aspBlock.Child = _buttonTrade;
            _window.Child = _textBox;
            _window.Show();
        }

        private void OnButtonClick(ButtonClickEventArgs args)
        {
            ExecuteMarketOrder(TradeType.Buy, "EURUSD", _orderVolume);
        }

        private void OnTextChanged(TextChangedEventArgs args) 
        {
            double.TryParse(args.TextBox.Text, out _orderVolume);
        }
    }
}

The plugin achieves all this by adding two custom event handlers to the Button.Click and the TextBox.TextChanged events. Whenever the text in the text box changes, the value is saved in the _orderVolume variable. The value of this variable is passed to the ExecuteMarketOrder method inside the OnButtonClick handler.

Summary

Adding parameters to plugins is a great solution for the developers of custom trading panels and other types of plugins that significantly extend the cTrader UI. You can also use this feature with other types of plugins to refine their functionality.