Parameter Types in cTrader Algo¶
In this article and its corresponding video we will demonstrate how you can declare and use parameters in your cBots and indicators. We will develop a simple indicator and we will show you how to configure its parameters from the cTrader UI. We will also explain what kind of parameters you can use in a cTrader cBot or indicator and illustrate how they are declared and used in your code.
Defining Parameters¶
Parameters are configurable values that can be set by the user when adding an indicator on a chart or before the execution of a cBot. Parameters are a useful feature for cBots and indicators as they allow you and the users of your algos to configure your algorithms without having to do any 'hard coding'.
Using Parameters in Indicators
You can use parameters in indicators to configure variables affecting indicator outputs such as the periods to be considered or the data source that needs to be used.
Using Parameters in cBots
In cBots, you can use parameters to configure variables such as stop loss or take profit levels or any other settings that affect execution.
As shown in this tutorial, cTrader supports eight parameter types.
int
double
string
bool
DataSeries
TimeFrame
enum
Color
Note
Different parameter types are represented differently in the cTrader UI.
Creating a Simple Indicator With Parameters¶
To demonstrate how you can use parameters in custom indicators, we will create a new indicator and call it 'Standard Deviation'.
To calculate the standard deviation we first need to calculate the moving average of the price. Subsequently, we will declare the parameters needed for a moving average indicator starting with the moving average period.
1 |
|
Any public property that has one of the supported types can become an indicator parameter. To transform our MaPeriod
property into a parameter, all we have to do is add the required declaration.
1 2 |
|
The Parameter
attribute instructs cTrader to present this property as a parameter in the indicator parameters panel using the defined name ("MA Period"
).
As shown below, the Parameter
attribute also allows you to define some properties of the parameter such as the displayed name, the default value, the group in which this parameter will be placed in, the maximum and minimum values, as well as the step that the parameter will change when up and down arrows are pressed (only for numerical values).
1 2 |
|
We will now add the rest of the parameters needed for a moving average indicator. The MaType
parameter will help us determine the moving average type. It is an enum
that will appear as a dropdown menu in the cTrader UI.
1 2 |
|
The Source
parameter will give users the option to specify the data source for our moving average indicator. This parameter is of the DataSeries
type.
1 2 |
|
Now that the required parameters are declared we will define and initialise our moving average.
1 2 3 4 5 6 |
|
At this point, we are ready to implement the calculation for our standard deviation. The default code template already contains the Output
property which we can use to display the results of the calculation.
1 2 |
|
Last but not least, we will implement the standard deviation calculation in the Calculate()
method.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
|
If add our indicator to a chart, we should see all our parameters in the 'Add instance' window.
Adding Additional Parameters¶
With our standard deviation completed, we can also add a horizontal line that will serve as a visual threshold for the indicator. For this purpose we will need to add some more parameters.
The first one will be a boolean parameter that will determine if our line will be shown or not.
1 2 |
|
The LineLevel
parameter will allow us to determine the price level at which the line should be drawn.
1 2 |
|
We will also add a parameter allowing for choosing the line colour.
1 2 |
|
With all the necessary parameters in place, we can write the code to draw the line on the chart to which our indicator is attached.
1 2 3 4 |
|
If we build the indicator again and add a new instance, we will be able to hide/show the line, move it up or down and select the colour of our choice as the line colour.
As shown in the below example, parameters can also be used to make sure that our indicator is only shown when added to charts for a specific timeframe.
1 2 |
|
In the Initialize()
method we will check the time frame of the chart to which our indicator is attached. If it is not equal to the value of the TF
parameter, we will prevent the indicator from being displayed.
1 2 3 4 5 6 7 8 9 10 11 12 |
|
We hope that this guide has been helpful in understanding parameters and how they work in cTrader algo trading. To learn more about the cTrader Algo API, see our documentation or post a question on our forum.