Customisable parameters in Python algorithms
When developing Python trading bots, technical indicators or plugins for cTrader, you may need to add and configure customisable parameters. This article explains how to declare and use customisable parameters for a Python algorithm in .cs and .json files.
Note
Define all customisable parameters for a Python algorithm in either its .cs or .json file, but not in both.
Location
The location of the .cs or .json file for a Python algorithm depends on the algorithm type, whether it is a cBot, indicator or plugin, as well as the name of the algorithm.
- For cBots:
Documents/cAlgo/Sources/Robots/{cBot-name}/{cBot-name}/ - For indicators:
Documents/cAlgo/Sources/Indicators/{Indicator-name}/{Indicator-name}/ - For plugins:
Documents/cAlgo/Sources/Plugins/{Plugin-name}/{Plugin-name}/
Note
When you create a Python algorithm in cTrader Windows or Mac, the necessary .cs and .json files are automatically generated in the relevant folder.
You can follow these instructions to locate the required folder:
-
Right-click the algorithm in cTrader Windows or Mac, then select Show in folder.
-
Navigate through
{name-of-algorithm}/{name-of-algorithm}, then identify the.cs(C# source file) or.json(JSON source file) item.
The
.csfile typically has the same name as the algorithm, with all spaces removed. For example, an algorithm named Amazing Aroon cBot results in aAmazingArooncBot.csfile.Tip
In the same folder, you can access the main Python code for your algorithm, stored in a
.pyfile such asAmazing Aroon cBot_main.py. The Python file follows a simple naming convention:- For cBots:
cBot-name_main.py - For indicators:
Indicator-name_main.py - For plugins:
Plugin-name_main.py
config.jsonis always the exact name.Tip
In the same folder, you can access the main Python code for your algorithm, stored in a
.pyfile such asUltimate Indicator_main.py. The Python file follows a simple naming convention:- For cBots:
cBot-name_main.py - For indicators:
Indicator-name_main.py - For plugins:
Plugin-name_main.py
- For cBots:
Content
Open the .cs or .json file in any editor, such as Visual Studio Code, and you should see code similar to this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | |
Note
You should see an empty class if you created your Python algorithm from scratch without using a template.
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 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 | |
Any parameter declared in the .cs or .json file can be used in the main Python file, which houses the code displayed in the code editor.
The main Python code below illustrates the usage of the customisable parameters:
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 | |
The main Python code below illustrates the usage of a customisable parameter:
1 2 3 4 5 6 7 8 9 10 11 12 13 | |
Advantages
The primary advantage of customisable parameters is that they appear as editable fields in the UI of all cTrader apps, including cTrader Mobile, Web, Windows and Mac. The values in these fields can be changed easily to suit any need or operation, without the need to access or modify the algorithm's source code.
In cTrader Store, sellers often use customisable parameters to ensure that buyers of algorithms are able to adjust the algos to suit their goals and workflows.
