Pine Script 入門
30 分鐘寫出第一支策略
Pine Script 是 TradingView 的專屬程式語言, 全世界 千萬個交易者用它寫策略。學會它的好處: 直接在 TradingView 寫、改、回測,不用架伺服器。 搭配 Webhook 還能直接接到交易所自動下單。
本文目標:從沒寫過程式的人,30 分鐘後能讀懂 +修改 Pine 策略,並理解 indicator 與 strategy 的差別。
1. 開始之前:你需要什麼
- TradingView 帳號(免費版可以寫,但發 webhook 要付費方案)
- 會用任何圖表的基本操作
- 知道 K 線、open / high / low / close 是什麼
- 不需要任何程式背景
2. 第一支 Pine:把當前 close 印出來
打開 TradingView 任何圖表 → 下方 Pine Editor 標籤 → 點 New → 選 Blank Indicator。把這段貼上:
//@version=5
indicator("我的第一支 Pine", overlay=true)
plot(close, color=color.blue)按右上 Save 然後 Add to chart。
你會看到一條藍線跟 K 棒的收盤價完全重疊(因為 close就是收盤價)。恭喜,這就是你第一支 Pine!
每一行的意思
//@version=5:告訴 TradingView 用 v5 版(最新)indicator(...):宣告這是一個指標 (overlay=true 表示畫在 K 棒上面,false 會畫在下方面板)plot(close, ...):把 close 這個序列畫出來
3. 加上邏輯:算 RSI
Pine 內建幾百個技術指標函式,叫 ta.*。 把上面的程式改成:
//@version=5
indicator("RSI 顯示", overlay=false)
length = input.int(14, "RSI 週期")
rsi = ta.rsi(close, length)
plot(rsi, color=color.purple)
hline(70, "超買", color=color.red)
hline(30, "超賣", color=color.green)把 overlay 改成 false 是因為 RSI 是 0-100 的指標, 畫在 K 棒上會擠成一條。
新東西:input 跟 hline
input.int(14, "RSI 週期"):在 indicator 設定面板出現 一個整數輸入框,用戶可以改 14 為其他值,不用改程式hline(70, ...):畫水平線,標出超買/超賣門檻
4. indicator vs strategy 的差別
Pine 第一行可以宣告兩種:
| 類型 | 用途 | 回測 | Alert |
|---|---|---|---|
indicator() | 畫線、發訊號 | ❌ | ✅ alert() |
strategy() | 完整買賣策略 + 自動回測 | ✅ | ✅ strategy.entry() + alert_message |
做自動交易兩種都可,但 strategy() 多一個好處: TradingView 會自動幫你跑回測,秀出歷史 PnL、勝率、最大回撤。
5. 寫一支完整可回測的策略
以下是 RSI 反轉策略 — 超賣買、超買賣,可直接在 TradingView 回測:
//@version=5
strategy("RSI 反轉策略 v1", overlay=true,
initial_capital=10000, default_qty_type=strategy.percent_of_equity,
default_qty_value=100)
// === 參數 ===
length = input.int(14, "RSI 週期")
oversold = input.int(30, "超賣值")
overbought = input.int(70, "超買值")
// === 指標計算 ===
rsi = ta.rsi(close, length)
// === 進場條件 ===
longCondition = ta.crossover(rsi, oversold)
shortCondition = ta.crossunder(rsi, overbought)
// === 進場下單 ===
if (longCondition)
strategy.entry("Long", strategy.long)
if (shortCondition)
strategy.entry("Short", strategy.short)
// === 視覺輔助 ===
plot(rsi, "RSI", color=color.purple, display=display.pane)
hline(overbought, "超買", color=color.red)
hline(oversold, "超賣", color=color.green)貼到 Pine Editor → Save → Add to chart。 底下會出現「Strategy Tester」標籤,秀出 Net Profit、勝率、 回撤等回測數據。
① 全部期間 vs 後 30% 期間表現有沒有差很多
② 不同幣種 / 時間框架是否都有效
③ 最大回撤是否承受得住
通過這三點再考慮實盤,並先用 forward-test(前向實盤觀察期) 跑至少 14 天確認。
6. 怎麼讓策略發 Alert 給 webhook
兩種方式:
方式 A:strategy.entry 帶 alert_message
if (longCondition)
strategy.entry("Long", strategy.long,
alert_message='{"action":"buy","symbol":"{{ticker}}"}')
if (shortCondition)
strategy.entry("Short", strategy.short,
alert_message='{"action":"sell","symbol":"{{ticker}}"}')建 Alert 時 Message 欄填 {{strategy.order.alert_message}},Pine 會把 entry 時 設的 message 塞進 webhook payload。
方式 B:alert() 函式(適合 indicator)
if (longCondition)
alert('{"action":"buy","symbol":"' + syminfo.ticker + '"}',
alert.freq_once_per_bar_close)7. Pine 語法常用速查
取歷史值
close // 當前 K 棒收盤
close[1] // 前 1 根收盤
close[5] // 前 5 根收盤
high[2] // 前 2 根的最高價條件 / 交叉
ta.crossover(a, b) // a 從下往上穿過 b
ta.crossunder(a, b) // a 從上往下穿過 b
// 等價寫法
a > b and a[1] <= b[1] // crossover
a < b and a[1] >= b[1] // crossunder常用指標
ta.sma(close, 20) // 簡單均線
ta.ema(close, 20) // 指數均線
ta.rsi(close, 14) // RSI
ta.atr(14) // ATR(波動率)
ta.macd(close, 12, 26, 9) // 回傳 [macd, signal, hist]
ta.highest(high, 20) // 過去 20 根最高價
ta.lowest(low, 20) // 過去 20 根最低價畫圖
plot(close) // 線
plotshape(longCondition, location=location.belowbar, color=color.green)
plotchar(shortCondition, char="↓", location=location.abovebar, color=color.red)
bgcolor(rsi > 70 ? color.new(color.red, 90) : na)8. 常見錯誤
❌ 用 = 而非 := 改值
Pine 變數宣告用 =,修改值要用 :=。 否則編譯報錯。
❌ 條件寫顛倒
ta.crossover(rsi, 30) 是 RSI 從 30 以下穿到上面(超賣反彈訊號),不是「跌破 30」。新手常搞反。
❌ Once Per Bar 跟 Once Per Bar Close 沒搞清楚
Once Per Bar 條件成立瞬間就觸發,可能 K 棒收盤前訊號又消失 → 假訊號。Once Per Bar Close 等 K 棒收完才觸發,保守。 自動交易強烈建議用後者。
❌ 拿未來資料(Look-ahead bias)
回測時不小心用了 K 棒收盤後的資訊(例如 request.security沒設 lookahead=barmerge.lookahead_off)。 回測 100% 賺實盤 50% 虧的元兇。
Get started
把你的策略接上 TradingView webhook,自動下單到 Binance / OKX / Bybit 等 7 家交易所。
免費註冊 TVSBot9. 下一步學什麼
- 用法庫:TradingView 有龐大社群分享 Pine 腳本, 點圖表左下「指標」搜你想學的概念
- 官方文件: Pine Script v5 Reference(英文)
- 多時間框架:
request.security()用法 - 陣列與物件:v5 引入的進階資料結構
- 函式:把重複邏輯抽出來,提高可讀性
10. 寫完策略想直接自動跑?
Pine 寫好的 strategy 設 Alert → TVSBot 接 webhook → 自動下單到交易所。 完整流程見: