Skip to content

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 show you how to configure the 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.

Define parameters

Parameters are configurable values that can be set when adding, starting or using an algorithm. They allow you and other users to customise the behaviour of cBots, indicators and plugins without modifying the source code.

Using parameters in cBots and indicators

In cBots, you can use parameters to configure variables or settings that influence execution, such as stop-loss and take-profit levels. In indicators, you can use parameters to configure variables that affect indicator outputs, such as the periods considered or the data source to be used.

As shown in this tutorial, cTrader supports eight parameter types:

  • int
  • double
  • string
  • bool
  • DataSeries
  • TimeFrame
  • enum
  • Color

Create a simple indicator with parameters

To demonstrate how you can use parameters in custom indicators, we will create a new indicator and name 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 we add our indicator to a chart, we should see all our parameters in the Add instance window.

Add 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 code to draw the line on the chart where 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 example below, parameters can also be used to ensure 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 timeframe 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.

Subscribe to our YouTube channel