You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何无需第三方平台(如Heroku)实现TradingView信号在Binance执行?

实现方案

由于TradingView的Pine脚本无法生成Binance API要求的HMAC-SHA256签名,因此无法完全跳过中间层直接对接Binance,但可以用更轻量、可控的方式替代Heroku,甚至用本地服务实现无第三方中转。

一、优先方案:用Serverless云函数替代Heroku

Serverless云函数按需运行,成本极低且无需维护服务器,是Heroku的理想替代:

  • 编写一个简单的云函数(Python/Node.js均可),接收TradingView的Webhook信号,生成Binance API签名后调用交易接口。
  • 示例Python代码(适配AWS Lambda/阿里云函数计算等主流平台):
    import hmac
    import hashlib
    import requests
    from urllib.parse import urlencode
    
    def handler(event, context):
        # 解析TradingView发送的Webhook数据
        tv_signal = event['body']
        api_key = "你的Binance API密钥"
        api_secret = "你的Binance API密钥"
        
        # 构造Binance现货下单参数
        params = {
            'symbol': tv_signal['symbol'],
            'side': tv_signal['side'],  # BUY/SELL
            'type': 'MARKET',
            'quantity': tv_signal['quantity']
        }
        # 生成Binance要求的签名
        query_string = urlencode(params)
        signature = hmac.new(api_secret.encode('utf-8'), query_string.encode('utf-8'), hashlib.sha256).hexdigest()
        params['signature'] = signature
        
        # 调用Binance交易API
        response = requests.post(
            'https://api.binance.com/api/v3/order',
            headers={'X-MBX-APIKEY': api_key},
            params=params
        )
        return response.json()
    
  • 部署后将云函数的公网URL配置到TradingView Alert的Webhook中即可。

二、无第三方中转方案:本地运行HTTP服务

如果完全不想依赖任何第三方平台,可以在本地设备(如树莓派、家用服务器)运行一个简单的HTTP服务:

  • 用Flask/Express搭建服务,接收TradingView的Webhook,处理签名后调用Binance API。
  • 示例Flask代码:
    from flask import Flask, request
    import hmac
    import hashlib
    import requests
    from urllib.parse import urlencode
    
    app = Flask(__name__)
    API_KEY = "你的Binance API密钥"
    API_SECRET = "你的Binance API密钥"
    WEBHOOK_SECRET = "自定义的TradingView验证密钥"
    
    @app.route('/tv-webhook', methods=['POST'])
    def handle_webhook():
        # 验证TradingView请求合法性(可选,防止恶意调用)
        if request.headers.get('X-Secret') != WEBHOOK_SECRET:
            return "Unauthorized", 401
        
        tv_data = request.get_json()
        # 构造下单参数并生成签名
        params = {
            'symbol': tv_data['symbol'],
            'side': tv_data['side'],
            'type': 'MARKET',
            'quantity': tv_data['quantity']
        }
        query_string = urlencode(params)
        signature = hmac.new(API_SECRET.encode(), query_string.encode(), hashlib.sha256).hexdigest()
        params['signature'] = signature
        
        # 调用Binance API执行交易
        res = requests.post(
            'https://api.binance.com/api/v3/order',
            headers={'X-MBX-APIKEY': API_KEY},
            params=params
        )
        return res.json()
    
    if __name__ == '__main__':
        app.run(host='0.0.0.0', port=5000)
    
  • 注意:需要确保本地设备有稳定公网IP,同时在路由器上配置端口转发;若没有公网IP,可使用内网穿透工具(但会引入第三方依赖,不优先推荐)。

三、其他Heroku替代选项

如果不想用Serverless或本地服务,还可以选择:

  • 低价VPS:如DigitalOcean、Vultr的入门级VPS,部署简单的HTTP服务,无休眠限制,稳定性优于Heroku免费版。
  • Replit:在线代码平台,可部署永久运行的免费服务,适合快速测试策略逻辑。

关键说明

TradingView无法直接对接Binance的核心原因是:Binance交易API要求请求必须带HMAC签名,而Pine脚本的加密能力不足以生成该签名,因此必须通过中间层完成签名转换。

内容的提问来源于stack exchange,提问作者mnd

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.18 00:45:28