跳转至

在 cTrader 中创建 cBot

本文概述了在 cTrader 中创建用于算法交易的交易机器人的步骤,使用 PythonC#,部署机器人并在 cTrader 中运行。

提示

使用 cTrader Windows 或 Mac,您可以快速创建交易机器人,或获得AI 的协助

Algo 应用的 cBots 选项卡中,点击 New 按钮打开算法创建向导。

为您的 cBot 输入一个名称,然后在 C#Python 之间选择一种编程语言。

从以下方法中选择创建方式:

  • 从头开始 - 新的交易机器人将只包含基本模板。

  • 使用模板 - 您可以从 Python#C# 模板列表中选择预制算法,涵盖各种类型的 cBot 和自动交易操作。

注意

预制算法已经包含交易逻辑和可自定义参数。 这些 cBot 一旦保存和构建就可以运行。

点击 Create 后,代码编辑器将打开,您可以开始编辑交易机器人的代码。

编辑代码

根据您选择的创建方法,交易机器人示例包含以下一个或多个元素:

 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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
[Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
public class SupercBot : Robot
{
    // === Fields ===
    // Declare indicators, variables or parameters, state flags, etc. here

    protected override void OnStart()
    {
        // Called once when the cBot starts
        // Initialize indicators, variables, parameters, or subscribe to events
    }

    protected override void OnTick()
    {
        // Called on every market tick
        // Useful for high-frequency strategies (but may be CPU-intensive)
    }

    protected override void OnBar()
    {
        // Called at the start of each new bar (candle)
        // Preferred for most strategies to reduce CPU load
    }

    protected override void OnStop()
    {
        // Called when the cBot is stopped
        // Useful for cleanup, logging, or resetting state
    }

    // === Custom Methods ===
    // Add your own helper methods below to modularize your logic

    private void ExampleMethod()
    {
        // A placeholder for your custom logic (e.g., strategy evaluation)
    }

    private void ClosePositions()
    {
        // Example of a method that could close open positions
    }

    private bool CheckCondition()
    {
        // Example of a method that could return a boolean condition
        return false;
    }
}

cBot 的 Robot 属性及其可选属性(如 TimeZoneAccessRights)位于 cBot 类(SupercBot)声明之前。

这些方法通常默认包含:

  • OnStart() 方法在每次启动 cBot 实例时调用。
  • OnTick() 方法在每个报价变动时调用。
  • OnBar() 方法在每个柱形图更新时调用。
  • OnStop() 方法在每次新 cBot 实例停止运行时调用。

OnTick() 方法在每个报价变动时执行某个操作,这使得它对 CPU 的要求很高。 在许多交易情况下,没有必要在每个报价变动时执行交易操作。 使用 OnBar() 方法更加实用。

上面示例代码片段中显示的方法和代码类型并不是设置和配置机器人行为的唯一方式。 然而,如果您刚开始编码,这些是最容易使用的方法。 要了解更多关于算法的信息,请从 C# 基础 开始,并探索 cBot 代码示例

注意

参考 包含了在 cTrader 中构建算法所需的所有类、事件、方法、变量等,而完整的算法示例和模板可在 GitHub 仓库中找到。

 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
32
33
34
35
36
37
38
39
40
class SupercBot():
    # === Fields ===
    # Declare indicators, variables, state flags, etc. here
    # self.example_indicator = None
    # self.some_flag = True

    def on_start(self):
        # Called once when the cBot starts
        # Initialize indicators, variables, parameters, or subscribe to events
        pass

    def on_tick(self):
        # Called on every market tick
        # Useful for high-frequency strategies (but may be CPU-intensive)
        pass

    def on_bar_closed(self):
        # Called at the start of each new bar (candle)
        # Preferred for most strategies to reduce CPU load
        pass

    def on_stop(self):
        # Called when the cBot is stopped
        # Useful for cleanup, logging, or resetting state
        pass

    # === Custom Methods ===
    # Add your own helper methods below to modularize your logic

    def example_method(self):
        # A placeholder for your custom logic (e.g., strategy evaluation)
        pass

    def close_positions(self):
        # Example of a method that could close open positions
        pass

    def check_condition(self):
        # Example of a method that could return a boolean condition
        return False

这些方法通常默认包含:

  • on_start 方法在每次启动 cBot 实例时调用。
  • on_tick 方法在每个报价变动时调用。
  • on_bar_closed 方法在每个柱形图更新时调用。
  • on_stop 方法在每次新 cBot 实例停止运行时调用。

on_tick 方法在每个报价变动时执行某个操作,这使得它对 CPU 的要求很高。 在许多交易情况下,没有必要在每个报价变动时执行交易操作。 使用 on_bar_closed 方法更加实用。

上面示例代码片段中显示的方法和代码类型并不是设置和配置 cBot 行为的唯一方式。 然而,如果您刚开始编码,这些是最容易使用的方法。 要了解更多关于算法的信息,请从 Python 基础 开始,并探索 cBot 代码示例

应用您的新知识来编辑交易机器人代码并使其适应您的需求。

保存和构建

通过点击代码编辑器顶部的 Save 按钮或使用 Ctrl+S 快捷键来保存您的代码。

在使用 cBot 之前,您需要通过构建 cBot 项目来验证其代码。 点击代码编辑器顶部的 Build 按钮或按 Ctrl+B

通过点击代码编辑器顶部的 Save 图标或使用 Cmd+S 快捷键来保存您的代码。

在使用 cBot 之前,您需要通过构建 cBot 项目来验证其代码。 点击代码编辑器顶部的 Build 图标或按 Cmd+B

当构建成功时,您会在 构建结果 中看到确认消息。 如果构建失败,则会显示所有遇到的错误摘要。

如果自上次构建以来代码有变更,Build 图标旁边会出现一个星号。 在这种情况下,您应该在启动 cBot 实例之前再次构建 cBot。

一旦成功构建 cBot,cBot 就准备就绪,但只有当您启动一个实例时它才开始工作。

Image title