Skip to content

Parameter Types in cTrader Automate

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
public int MaPeriod { get; set; }

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
[Parameter("MA Period")]
public int MaPeriod { get; set; }

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
[Parameter("SMA Period", DefaultValue = 14, Group = "MA", MaxValue = 100, MinValue = 1, Step = 1)]
public int MaPeriod { get; set; }

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
[Parameter("MA Type", Group = "MA")]
public MovingAverageType MaType { get; set; }

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
[Parameter("Source", Group = "MA")]
public DataSeries Source { get; set; }

Now that the required parameters are declared we will define and initialise our moving average.

1
2
3
4
5
6
private MovingAverage movingAverage;

protected override void Initialize()
{
    movingAverage = Indicators.MovingAverage(Source, MaPeriod, MaType);
}

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
[Output("Result", LineColor = "Orange")]
public IndicatorDataSeries Result { get; set; }

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
public override void Calculate(int index)
{
    var average = movingAverage.Result[index];

    double sum = 0;

    for (var period = 0; period < MaPeriod; period++)
    {
        sum += Math.Pow(Source[index - period] - average, 2.0);
    }

    Result[index] = Math.Sqrt(sum / MaPeriod);
}

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
[Parameter("Show Line", DefaultValue = true, Group = "Line")]
public bool ShowLine { get; set; }

The LineLevel parameter will allow us to determine the price level at which the line should be drawn.

1
2
[Parameter("Line Level", DefaultValue = 0.001, Step = 0.001, Group = "Line")]
public double LineLevel { get; set; }

We will also add a parameter allowing for choosing the line colour.

1
2
[Parameter("Line Color", DefaultValue = "Blue", Group = "Line")]
public Color LineColor { get; set; }

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 (ShowLine)
{
    Chart.IndicatorAreas[0].DrawHorizontalLine("Line", LineLevel, LineColor);
}

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
[Parameter("Timeframe")]
public TimeFrame TF { get; set; }

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
protected override void Initialize()
{
    if (TimeFrame != TF)
        return;

    movingAverage = Indicators.MovingAverage(Source, MaPeriod, MaType);

    if (ShowLine)
    {
        Chart.IndicatorAreas[0].DrawHorizontalLine("Line", LineLevel, LineColor);
    }
}

We hope that this guide has been helpful in understanding parameters and how they work in cTrader algo trading. To learn more about other features, consult our extensive technical documentation and subscribe to our YouTube channel to be alerted every time we publish a new video.

Subscribe to our YouTube channel