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 |
|
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.