Pine Script · 實作範例

用 Pine Script 寫自訂指標
均線交叉、RSI、KD

每段程式碼都可直接複製貼進 TradingView 執行(v6)

看完 Pine Script 入門 會畫一條均線後,這篇給你三個最常用的實作範例:均線黃金交叉(含標記與警示)、RSI、KD。程式都用 v6 語法,複製貼進 TradingView 的 Pine 編輯器、按「新增到圖表」就能跑。

範例一:均線黃金交叉(標記 + 警示)

畫出快線與慢線,並在黃金交叉(快線由下往上穿過慢線)時標一個向上箭頭、死亡交叉標向下箭頭,最後加一個警示條件:

//@version=6
indicator("均線黃金交叉", overlay = true)

fastLen = input.int(5,  "快線天數")
slowLen = input.int(20, "慢線天數")

fast = ta.sma(close, fastLen)
slow = ta.sma(close, slowLen)

plot(fast, "快線", color = color.red,  linewidth = 2)
plot(slow, "慢線", color = color.blue, linewidth = 2)

golden = ta.crossover(fast, slow)
death  = ta.crossunder(fast, slow)

plotshape(golden, title = "黃金交叉", style = shape.triangleup,
          location = location.belowbar, color = color.red,   size = size.small)
plotshape(death,  title = "死亡交叉", style = shape.triangledown,
          location = location.abovebar, color = color.green, size = size.small)

alertcondition(golden, title = "黃金交叉", message = "出現黃金交叉!")

*台股紅漲綠跌,所以這裡黃金交叉用紅色、死亡交叉用綠色。

重點函式:ta.crossover(a, b) 在 a 由下往上穿過 b 時回傳 true;ta.crossunder 相反。plotshape 負責在圖上畫箭頭;alertcondition 建立一個警示條件——存檔後在圖表上按「鬧鐘」新增警示,就能在交叉時收到通知。

範例二:RSI(畫在副圖)

RSI 不是畫在 K 線上,而是獨立副圖,所以把 overlay 設成 false

//@version=6
indicator("我的 RSI", overlay = false)

length = input.int(14, "RSI 期數")
rsi = ta.rsi(close, length)

plot(rsi, "RSI", color = color.purple, linewidth = 2)
hline(70, "超買", color = color.gray)
hline(30, "超賣", color = color.gray)

*hline 畫水平參考線;一般 RSI > 70 視為超買、< 30 視為超賣(RSI 怎麼看)。

範例三:KD(隨機指標,副圖)

先用 ta.stoch 取得 RSV,再平滑成 K 與 D:

//@version=6
indicator("我的 KD", overlay = false)

length = input.int(9, "RSV 期數")
rsv = ta.stoch(close, high, low, length)
k = ta.sma(rsv, 3)
d = ta.sma(k, 3)

plot(k, "K", color = color.blue,   linewidth = 2)
plot(d, "D", color = color.orange, linewidth = 2)
hline(80, "超買", color = color.gray)
hline(20, "超賣", color = color.gray)
⚠️ 台股 KD 和這個算出來會有一點不同
上面用移動平均(ta.sma)平滑,是 TradingView 常見的「慢速 KD」。但台股券商的 KD 用的是「遞迴平滑」:K = 前一日 K × 2/3 + 當日 RSV × 1/3,D 同理。想完全復刻台股 KD,要用到「前一根數值 [1]」與 := 賦值(進階):
//@version=6
indicator("台股式 KD (9,3,3)", overlay = false)

length = input.int(9, "RSV 期數")
rsv = ta.stoch(close, high, low, length)

var float k = na
var float d = na
k := na(k[1]) ? 50.0 : k[1] * 2 / 3 + rsv / 3
d := na(d[1]) ? 50.0 : d[1] * 2 / 3 + k / 3

plot(k, "K", color = color.blue,   linewidth = 2)
plot(d, "D", color = color.orange, linewidth = 2)
這裡 k[1] 是「前一根的 K」、:= 是把新值賦回同一個變數、na(...)?...:... 是處理第一根還沒有前值的情況(給起始值 50)。
⚠️ 誠實提醒

這些指標能畫出來,不代表能賺。KD、RSI、均線都有鈍化與假訊號的老問題,寫成 Pine Script 只是幫你自動標記、自動提醒,不會提高勝率。把它當「輔助判讀 + 少盯盤」的工具就好,別把交叉箭頭當成穩賺訊號。

把這些貼進 TradingView 跑跑看

開一張圖 → 下方 Pine 編輯器 → 貼上 → 新增到圖表,馬上看到自己寫的指標。

前往 TradingView →

常見問題

怎麼標出黃金交叉並設警示?
用 ta.crossover(fast, slow) 判斷黃金交叉、ta.crossunder 判斷死亡交叉,用 plotshape 畫箭頭標記,再用 alertcondition 建立警示條件,就能在交叉時收到 TradingView 通知。
RSI、KD 怎麼畫在副圖?
在 indicator() 把 overlay 設成 false,指標就會畫在 K 線下方獨立副圖。RSI 用 ta.rsi、KD 用 ta.stoch 取 RSV 再平滑,搭配 hline 畫超買超賣線。
TradingView 的 KD 和台股券商一樣嗎?
不完全一樣。TradingView 常用移動平均平滑,台股券商 KD 用遞迴平滑(K=前一日K×2/3+當日RSV×1/3)。要完全復刻台股 KD 需用遞迴寫法([1] 與 :=),屬進階,本文有附範例。
這些範例可以直接用嗎?
可以直接複製貼進 Pine 編輯器執行,程式以 v6 撰寫。但指標只是輔助工具、會有鈍化與假訊號,能畫出來不代表能獲利,請搭配風險控管。
延伸閱讀
🌱 Pine Script 新手入門(第一支指標)→ 📈 Pine Script 策略回測入門(strategy)→ 📋 Pine Script 常用語法速查(新手版)→ 📊 KD、MACD、RSI 怎麼看?→ 📉 均線怎麼看?黃金交叉是什麼 →
本文為 Pine Script 程式教學,並非投資建議,也不保證任何指標能獲利。程式以 TradingView Pine Script v6 撰寫,語法若因官方改版而異動,以 TradingView 官方文件為準。技術指標有鈍化與假訊號等限制,請自行評估、量力而為。TradingView 連結為聯盟連結,不影響你的費用與本文中立。