完整教學 · 自動交易

TradingView Webhook 完整教學
從零到自動下單

2026-06-03·12 分鐘閱讀·初學 ~ 中階

你看到一個技術分析策略想自動跑:RSI 跌破 30 自動買、突破前高自動加倉。 但 TradingView 本身**只能畫圖、不能下單**。這篇講怎麼用Webhook 把 TradingView 的訊號接到你交易所帳號,全程自動。

看完你會知道:① Webhook 是什麼、② 怎麼在 TradingView 設 Alert、 ③ Pine Script 訊號怎麼送、④ 接到交易所的兩條路徑(自己架 / 用 SaaS)、 ⑤ 常見坑與安全注意。

TradingView → TVSBot → 交易所 訊號流程TradingView 觸發 Alert,HTTP POST 到 TVSBot webhook URL, TVSBot 解析 payload 後用你的 API key 對 Binance / OKX 等交易所下單。TradingViewPine 觸發 Alert{"action":"buy",...}HTTP POSTTVSBot解析 / 風控用你的 API key 下單BinanceOKXBybit
圖:TradingView Alert → TVSBot webhook → 交易所自動下單

1. Webhook 是什麼?跟一般 API 有什麼差別

Webhook 簡單講就是「**事件觸發的 HTTP POST**」。 一般 API 是你問對方答(你呼叫對方的 endpoint); Webhook 反過來,**事件發生時對方主動打你的 URL**。

TradingView 的 Alert 就是事件來源 — 當你的 Pine 條件成立(例如 RSI < 30),TradingView 會立刻 HTTP POST 到你提供的 URL, payload 帶上你預先寫好的訊息。

重要前提
TradingView Webhook 是 付費功能,最低 Essential 方案 $14.95/月。Free 方案沒這功能。完整方案比較見 TradingView 定價頁

2. 在 TradingView 設 Alert + Webhook

Step 1:打開圖表 + 加上你的策略

進任何圖表(例如 BTCUSDT.P 永續),點左下角的 指標,搜你要的 Pine 策略加上去。 或自己寫一支(下面範例)。

Step 2:點右側鈴鐺 → Create Alert

Condition 選你的策略;Trigger 選「Once Per Bar Close」(保守、不會盤中跳動)。

Step 3:Notifications tab

勾選 Webhook URL,貼上你的接收端 URL,例如:

https://api.tvsbot.com/webhook/your-secret-token

Step 4:Message 欄填 JSON

這是 TradingView 會 POST 過去的內容。最簡單版本:

json
{
  "secret": "your-webhook-secret",
  "action": "{{strategy.order.action}}",
  "symbol": "{{ticker}}",
  "price": {{close}}
}

{{strategy.order.action}}{{ticker}} 這些是 TradingView 內建變數,會在 Alert 觸發時自動填上實際值 (buy / sell / BTCUSDT 等)。

3. Pine Script 怎麼寫才能觸發 Alert

Pine Script 有兩種發 Alert 的方式:

方式 A:strategy.entry() + Alert message

pine
//@version=5
strategy("RSI 反轉示範", overlay=true)

rsi = ta.rsi(close, 14)

if (ta.crossover(rsi, 30))
    strategy.entry("Long", strategy.long,
      alert_message='{"action":"buy","symbol":"{{ticker}}"}')

if (ta.crossunder(rsi, 70))
    strategy.entry("Short", strategy.short,
      alert_message='{"action":"sell","symbol":"{{ticker}}"}')

建 Alert 時,Message 欄輸入 {{strategy.order.alert_message}},Pine 就會把 上面 alert_message 內容塞到 Webhook payload。

方式 B:alert() 函式(不下單只發訊號)

pine
//@version=5
indicator("純訊號示範", overlay=true)

rsi = ta.rsi(close, 14)

if (ta.crossover(rsi, 30))
    alert('{"action":"buy","symbol":"' + syminfo.ticker + '"}',
          alert.freq_once_per_bar_close)
strategy 與 indicator 的選擇
strategy:適合做完整回測,TradingView 會幫你算 PnL;indicator:純訊號發射器,回測結果要自己算。 要自動交易兩種都可,看你習慣。

4. 接到交易所有兩條路徑

路徑 A:自己架接收伺服器

自己寫一個簡單的 Flask / FastAPI server,跑在 Heroku / Fly / VPS 上。 收到 webhook 後用 ccxt 或交易所官方 SDK 下單。

python
from fastapi import FastAPI, Request, HTTPException
import ccxt

app = FastAPI()
exchange = ccxt.binance({
    "apiKey": "你的_KEY",
    "secret": "你的_SECRET",
    "options": {"defaultType": "future"},
})

@app.post("/webhook/{secret}")
async def receive(secret: str, request: Request):
    if secret != "你的_webhook_secret":
        raise HTTPException(401, "invalid secret")
    body = await request.json()

    side = body["action"]   # buy / sell
    symbol = body["symbol"] # BTCUSDT
    qty = 0.001  # 寫死或從 body 拿

    order = exchange.create_market_order(symbol, side, qty)
    return {"ok": True, "order_id": order["id"]}

優點是完全掌控。缺點是要懂部署、要自己處理:

  • API key 安全儲存(不要明碼存 .env 上 Github)
  • HTTPS(TradingView 只支援 HTTPS webhook)
  • 下單錯誤處理(餘額不足、最小單量、IP 白名單等)
  • 多策略管理(每個策略一個 endpoint,或路由)
  • 風控(停損、最大每日虧損、緊急 kill switch)
  • 多帳號同步(想同時跑 3 個交易所要怎麼設)

路徑 B:用 SaaS(推薦給不想架伺服器的人)

TVSBot、3Commas、Cryptohopper 這類平台幫你把上面 全部處理掉。你只要:

  1. 到平台註冊 + 綁定你的交易所 API key(read+trade 權限就好,禁 withdraw)
  2. 在平台建一個「策略」拿到專屬 webhook URL
  3. 把 URL 貼進 TradingView Alert
  4. Pine 發出來的訊號,平台幫你下單
為什麼選 TVSBot
TVSBot 是非託管架構 — 你的 API key 用 Fernet AES-128 加密儲存,平台從不直接持有資金。可同時綁定 7 家交易所 (Binance / OKX / Bitget / Bybit / Gate / BingX / Hyperliquid), 一個訊號一鍵同步多帳戶。 看定價

5. 完整 payload 範例(TVSBot 通用版)

一個完整的訊號,跟 TVSBot 對接的 payload:

json
{
  "secret": "your-webhook-secret",
  "action": "buy",
  "symbol": "BTC/USDT:USDT",
  "qty_type": "margin_pct",
  "qty": 5,
  "leverage": 5,
  "tp_pct": 3,
  "sl_pct": 1.5,
  "exchange": "binance",
  "market_type": "futures"
}

意思是:用「保證金的 5%」開一單、5x leverage、3% 止盈、1.5% 止損。 TVSBot 會自動換算成實際下單 qty(會用你綁的 API key 抓 balance + 現價算)。

6. 安全與常見坑

① Secret 一定要有

沒 secret 的 webhook 等於門戶大開 — 任何人知道 URL 就能下假單。 payload 裡務必帶 secret 欄位,接收端比對才執行。

② API key 禁用 Withdraw 權限

幫策略開的 API key 只勾 Trade 絕對不勾 Withdraw。 這樣就算 key 外洩,駭客只能下亂單,不能把錢提走。

2022 3Commas 事件參考
3Commas 託管型平台被駭,10 萬把 API key 外洩,連有 Withdraw 權限的 key 都被搬空。詳見 為什麼 3Commas 被偷 $22M 這篇。

③ Once Per Bar vs Once Per Bar Close

Once Per Bar 會在條件成立的當下立刻觸發,但 K 棒收盤前 條件可能反轉 → 假訊號。Once Per Bar Close 等 K 棒收完才觸發,保守得多。 初學者預設用後者。

④ TradingView 免費版限制

免費版同時只能 1 個 active alert。要跑多策略只能升級。 或用 TVSBot 的「單 Alert 多策略路由」功能 — payload 帶 strategy 欄位指定要送哪個策略, 一個 alert quota 跑 N 個策略。

⑤ 沒有重試機制

TradingView Webhook 失敗不會自動 retry。接收端短暫 down 機就漏訊號。 要嘛確保你的 server uptime 高、要嘛用有 SLA 的 SaaS。

Get started

想把今天學到的東西自動化跑起來?

把你的策略接上 TradingView webhook,自動下單到 Binance / OKX / Bybit 等 7 家交易所。

免費註冊 TVSBot

7. 進一步學習

看完這篇你已經能跑起基本流程。接下來建議學:

  • 常用指標:RSI / MACD / EMA 是怎麼算的, 什麼情境用什麼好(下方延伸閱讀)
  • 風控設定:每日最大虧損、最大持倉、kill switch
  • 多時間框架:日線判斷大方向 + 小時線執行進場
  • 回測 vs 實盤:策略回測賺錢實盤未必,要 forward-test 觀察至少 14 天