跳至内容

Webhook 使用方法

配置

通过在配置文件中添加 webhook 部分,并将 webhook.enabled 设置为 true 来启用 Webhook。

示例配置(使用 IFTTT 测试)。

  "webhook": {
        "enabled": true,
        "url": "https://maker.ifttt.com/trigger/<YOUREVENT>/with/key/<YOURKEY>/",
        "entry": {
            "value1": "Buying {pair}",
            "value2": "limit {limit:8f}",
            "value3": "{stake_amount:8f} {stake_currency}"
        },
        "entry_cancel": {
            "value1": "Cancelling Open Buy Order for {pair}",
            "value2": "limit {limit:8f}",
            "value3": "{stake_amount:8f} {stake_currency}"
        },
         "entry_fill": {
            "value1": "Buy Order for {pair} filled",
            "value2": "at {open_rate:8f}",
            "value3": ""
        },
        "exit": {
            "value1": "Exiting {pair}",
            "value2": "limit {limit:8f}",
            "value3": "profit: {profit_amount:8f} {stake_currency} ({profit_ratio})"
        },
        "exit_cancel": {
            "value1": "Cancelling Open Exit Order for {pair}",
            "value2": "limit {limit:8f}",
            "value3": "profit: {profit_amount:8f} {stake_currency} ({profit_ratio})"
        },
        "exit_fill": {
            "value1": "Exit Order for {pair} filled",
            "value2": "at {close_rate:8f}.",
            "value3": ""
        },
        "status": {
            "value1": "Status: {status}",
            "value2": "",
            "value3": ""
        }
    },

webhook.url 中的 URL 应指向你的 Webhook 的正确地址。如果你使用的是IFTTT(如上面示例所示),请将你的事件名称和密钥插入到 URL 中。

你可以将 POST 请求体格式设置为表单编码(默认)、JSON 编码或原始数据。分别使用 "format": "form""format": "json""format": "raw"。用于集成 Mattermost Cloud 的示例配置:

  "webhook": {
        "enabled": true,
        "url": "https://<YOURSUBDOMAIN>.cloud.mattermost.com/hooks/<YOURHOOK>",
        "format": "json",
        "status": {
            "text": "Status: {status}"
        }
    },

结果将是一个 POST 请求,请求体例如为 {"text":"状态:运行中"},请求头为 Content-Type: application/json,这将在 Mattermost 频道中显示“状态:运行中”的消息。

当使用表单编码或 JSON 编码配置时,你可以配置任意数量的负载值,键和值都会输出到 POST 请求中。然而,当使用原始数据格式时,你只能配置一个值,并且该值的键必须"data"。在这种情况下,数据的键不会出现在 POST 请求中,仅输出其值。例如:

  "webhook": {
        "enabled": true,
        "url": "https://<YOURHOOKURL>",
        "format": "raw",
        "webhookstatus": {
            "data": "Status: {status}"
        }
    },

结果将是一个 POST 请求,请求体例如为 状态:运行中,请求头为 Content-Type: text/plain

嵌套 Webhook 配置

某些 Webhook 目标需要嵌套结构。可以通过将内容设置为字典或列表,而不是直接设置为文本,来实现这一点。

此功能仅支持 JSON 格式。

"webhook": {
    "enabled": true,
    "url": "https://<yourhookurl>",
    "format": "json",
    "status": {
        "msgtype": "text",
        "text": {
            "content": "Status update: {status}"
        }
    }
}

结果将是一个 POST 请求,请求体例如为 {"msgtype":"text","text":{"content":"状态更新:运行中"}},请求头为 Content-Type: application/json

其他配置

webhook.retries 参数可用于设置当 Webhook 请求失败时(即 HTTP 响应状态码不是 200)的最大重试次数。默认值为 0,表示不重试。还可以设置额外的 webhook.retry_delay 参数,用于指定重试之间的间隔时间(单位为秒)。默认值为 0.1(即 100 毫秒)。请注意,如果 Webhook 存在连接问题,增加重试次数或重试延迟可能会减慢交易器的运行速度。你还可以指定 webhook.timeout,用于定义机器人在认为对方主机无响应前的等待时间(默认为 10 秒)。

重试的示例配置:

  "webhook": {
        "enabled": true,
        "url": "https://<YOURHOOKURL>",
        "timeout": 10,
        "retries": 3,
        "retry_delay": 0.2,
        "status": {
            "status": "Status: {status}"
        }
    },

可以通过策略中的 self.dp.send_msg() 函数向 Webhook 端点发送自定义消息。要启用此功能,请将 allow_custom_messages 选项设置为 true

  "webhook": {
        "enabled": true,
        "url": "https://<YOURHOOKURL>",
        "allow_custom_messages": true,
        "strategy_msg": {
            "status": "StrategyMessage: {msg}"
        }
    },

可以为不同事件配置不同的负载。并非所有字段都是必需的,但你至少应配置其中一个字典,否则 Webhook 将永远不会被调用。

Webhook 消息类型

开仓 / 开仓成交

webhook.entrywebhook.entry_fill 中的字段会在机器人下达做多/做空订单以增加仓位,或该订单成交时被填充。参数通过 string.format 填充。可能的参数包括:

  • trade_id
  • exchange
  • 交易对
  • direction
  • leverage
  • limit # 已弃用 - 不应再使用。
  • open_rate
  • amount
  • 开仓时间
  • stake_amount
  • stake_currency
  • base_currency
  • quote_currency
  • fiat_currency
  • order_type
  • current_rate
  • enter_tag

入场取消

webhook.entry_cancel 中的字段会在机器人取消做多/做空订单时被填充。参数通过 string.format 填充。可能的参数包括:

  • trade_id
  • exchange
  • 交易对
  • direction
  • leverage
  • 限价
  • amount
  • 开仓时间
  • stake_amount
  • stake_currency
  • base_currency
  • quote_currency
  • fiat_currency
  • order_type
  • current_rate
  • enter_tag

出场 / 出场成交

webhook.exitwebhook.exit_fill 中的字段会在机器人下达出场订单,或该出场订单成交时被填充。参数通过 string.format 填充。可能的参数包括:

  • trade_id
  • exchange
  • 交易对
  • direction
  • leverage
  • gain
  • amount
  • open_rate
  • close_rate
  • current_rate
  • profit_amount
  • profit_ratio
  • stake_currency
  • base_currency
  • quote_currency
  • fiat_currency
  • enter_tag
  • 离场原因
  • order_type
  • 开仓时间
  • close_date
  • sub_trade
  • is_final_exit

出场取消

webhook.exit_cancel 中的字段会在机器人取消出场订单时被填充。参数通过 string.format 填充。可能的参数包括:

  • trade_id
  • exchange
  • 交易对
  • direction
  • leverage
  • gain
  • order_rate
  • amount
  • open_rate
  • current_rate
  • profit_amount
  • profit_ratio
  • stake_currency
  • base_currency
  • quote_currency
  • fiat_currency
  • 离场原因
  • order_type
  • 开仓时间
  • close_date

状态

webhook.status 中的字段用于常规状态消息(如“已开始”/“已停止”等)。参数通过 string.format 填充。

此处唯一可用的值是 {status}

Discord

为 Discord 提供了一种特殊形式的 webhook。你可以按如下方式配置:

"discord": {
    "enabled": true,
    "webhook_url": "https://discord.com/api/webhooks/<Your webhook URL ...>",
    "exit_fill": [
        {"Trade ID": "{trade_id}"},
        {"Exchange": "{exchange}"},
        {"Pair": "{pair}"},
        {"Direction": "{direction}"},
        {"Open rate": "{open_rate}"},
        {"Close rate": "{close_rate}"},
        {"Amount": "{amount}"},
        {"Open date": "{open_date:%Y-%m-%d %H:%M:%S}"},
        {"Close date": "{close_date:%Y-%m-%d %H:%M:%S}"},
        {"Profit": "{profit_amount} {stake_currency}"},
        {"Profitability": "{profit_ratio:.2%}"},
        {"Enter tag": "{enter_tag}"},
        {"Exit Reason": "{exit_reason}"},
        {"Strategy": "{strategy}"},
        {"Timeframe": "{timeframe}"},
    ],
    "entry_fill": [
        {"Trade ID": "{trade_id}"},
        {"Exchange": "{exchange}"},
        {"Pair": "{pair}"},
        {"Direction": "{direction}"},
        {"Open rate": "{open_rate}"},
        {"Amount": "{amount}"},
        {"Open date": "{open_date:%Y-%m-%d %H:%M:%S}"},
        {"Enter tag": "{enter_tag}"},
        {"Strategy": "{strategy} {timeframe}"},
    ]
}

以上为默认设置(entry_fillexit_fill 为可选项,若不设置则使用上述默认值),当然也可以进行修改。若要禁用其中任一默认值(entry_fill / exit_fill),可将其设为空数组(例如:exit_fill: [])。

可用字段与 webhook 的字段相对应,具体文档请参见相应的 webhook 章节。

通知默认将显示如下样式。

discord-notification

策略可以通过 dataprovider.send_msg() 函数向 Discord 终端发送自定义消息。要启用此功能,请将 allow_custom_messages 选项设置为 true

  "discord": {
        "enabled": true,
        "webhook_url": "https://discord.com/api/webhooks/<Your webhook URL ...>",
        "allow_custom_messages": true,
    },