Skip to content

Customisable parameters in Python algorithms

When developing Python trading bots, technical indicators or plugins, you may need to add and configure customisable parameters. This article explains how to declare and use customisable parameters in a Python algorithm for cTrader.

Note

The execution model for cTrader Python algorithms involves a .NET/C# engine, which requires all customisable parameters to be declared within .cs files. Every time you create a Python algorithm in cTrader, a .cs file for that algorithm is generated automatically and stored in the relevant folder.

Location

The location of the .cs 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/Indicators/{Plugin-name}/{Plugin-name}/

You can follow these instructions to locate the .cs file for a Python algorithm:

  1. Right-click the algorithm in cTrader Windows or Mac, then select Show in folder.

  2. Navigate through {name-of-algorithm}/{name-of-algorithm}, then identify the .cs file (C# source file) from the list of files.

The .cs file typically has the same name as the algorithm, with all spaces removed. For example, an algorithm named Amazing Aroon cBot results in a AmazingArooncBot.cs file.

Tip

In the same folder, you can access the main Python code for your algorithm, stored in a .py file such as Amazing 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

Content

Open the .cs file in any text or code editor, such as NotePad or 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
using cAlgo.API;

namespace cAlgo.Robots;

[Robot(AccessRights = AccessRights.None, AddIndicators = true)]
public partial class AmazingArooncBot : Robot
{
    [Parameter("Volume (Lots)", DefaultValue = 0.01)]
    public double VolumeInLots { get; set; }

    [Parameter("Stop Loss (Pips)", DefaultValue = 10, MaxValue = 100, MinValue = 1, Step = 1)]
    public double StopLossInPips { get; set; }

    [Parameter("Take Profit (Pips)", DefaultValue = 10, MaxValue = 100, MinValue = 1, Step = 1)]
    public double TakeProfitInPips { get; set; }

    [Parameter("Label", DefaultValue = "AmazingArooncBot")]
    public string Label { get; set; }

    [Parameter("Periods", DefaultValue = 25, Group = "Aroon", MinValue = 2)]
    public int Periods { get; set; }
}

Note

You will see an empty class if you created your Python cBot from scratch without using a template.

Any parameter declared in the .cs 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
import clr

clr.AddReference("cAlgo.API")

# Import cAlgo API types
from cAlgo.API import *

# Import trading wrapper functions
from robot_wrapper import *

class AmazingArooncBot():
    def on_start(self):
        self.volumeInUnits = api.Symbol.QuantityToVolumeInUnits(api.VolumeInLots)
        self.aroon = api.Indicators.Aroon(api.Periods)

    def on_bar_closed(self):
        if self.aroon.Up.Last(0) > self.aroon.Down.Last(0) and self.aroon.Up.Last(1) < self.aroon.Down.Last(1):
            self.close_positions(TradeType.Sell)
            api.ExecuteMarketOrder(TradeType.Buy, api.SymbolName, self.volumeInUnits, api.Label, api.StopLossInPips, api.TakeProfitInPips)
        elif self.aroon.Up.Last(0) < self.aroon.Down.Last(0) and self.aroon.Up.Last(1) > self.aroon.Down.Last(1):
            self.close_positions(TradeType.Buy)
            api.ExecuteMarketOrder(TradeType.Sell, api.SymbolName, self.volumeInUnits, api.Label, api.StopLossInPips, api.TakeProfitInPips)

    def get_bot_positions(self):
        return api.Positions.FindAll(api.Label)

    def close_positions(self, tradeType):
        for position in self.get_bot_positions():
            if position.TradeType != tradeType:
                continue
            api.ClosePosition(position)

Advantages

The primary advantage of customisable parameters is that they appear as editable fields in the UI of all cTrader apps, including cTrader Windows, Mac, Web and Mobile. The values in these fields can be modified 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.

Image title