跳转至

Python 算法中的可自定义参数

在开发 Python 交易 机器人、技术 指标插件 时,您可能需要添加和配置可自定义的 参数。 本文解释了如何在 cTrader 的 Python 算法 中声明和使用可自定义参数。

注意

cTrader Python 算法的执行模型涉及 .NET/C# 引擎,该引擎要求所有可自定义参数在 .cs 文件中声明。 每次在 cTrader 中创建 Python 算法时,都会自动生成该算法的 .cs 文件并存储在相关文件夹中。

位置

Python 算法的 .cs 文件的位置取决于算法类型,无论是 cBot、指标还是插件,以及算法的名称。

  • 对于 cBots:Documents/cAlgo/Sources/Robots/{cBot-name}/{cBot-name}/
  • 对于指标:Documents/cAlgo/Sources/Indicators/{Indicator-name}/{Indicator-name}/
  • 对于插件:Documents/cAlgo/Sources/Indicators/{Plugin-name}/{Plugin-name}/

您可以按照以下说明定位 Python 算法的 .cs 文件:

  1. 右键单击 cTrader Windows 或 Mac 中的算法,然后选择 在文件夹中显示

  2. 导航到 {name-of-algorithm}/{name-of-algorithm},然后从文件列表中识别 .cs 文件(C# 源文件)。

.cs 文件通常与算法名称相同,所有空格已删除。 例如,名为 Amazing Aroon cBot 的算法会生成一个 AmazingArooncBot.cs 文件。

提示

在同一文件夹中,您可以访问算法的主要 Python 代码,存储在 .py 文件中,例如 Amazing Aroon cBot_main.py。 Python 文件遵循简单的命名约定:

  • 对于 cBots:cBot-name_main.py
  • 对于指标:Indicator-name_main.py
  • 对于插件:Plugin-name_main.py

内容

在任何文本或代码编辑器(如 NotePad 或 Visual Studio Code)中打开 .cs 文件,您应该会看到类似以下的代码:

 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; }
}

注意

如果您从头创建 Python cBot 而不使用模板,您将看到一个空类。

.cs 文件中声明的任何参数都可以在主要 Python 文件中使用,该文件包含代码编辑器中显示的代码。 以下主要 Python 代码说明了可自定义参数的用法:

 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")

# <strong>Import cAlgo API types</strong>
from cAlgo.API import *

# <strong>Import trading wrapper functions</strong>
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)

优势

可自定义参数的主要优势是它们出现在所有 cTrader 应用程序(包括 cTrader Windows、Mac、Web 和 Mobile)的 UI 中作为可编辑字段。 这些字段中的值可以轻松修改以适应任何需求或操作,而无需访问或修改算法的源代码。

cTrader 商店 中,卖家 经常使用可自定义参数来确保 买家 能够调整算法以适应其目标和工作流程。

Image title