示例
本頁透過兩個完整的 Pine 腳本,逐行說明每行程式碼的用途。每個示例都以貼近實際的方式展示核心語言特性。
示例一 — SMA 均線交叉指標
該指標繪製快速和慢速簡單移動平均線,並在圖表上標記金叉和死叉事件。涉及變數、series、ta 模組、plot 和 plotshape 等特性。
pine
//@version=6
// ① 將此腳本宣告為指標。
// - title : 顯示在圖例中的名稱
// - overlay=true : 疊加在 K 線上繪製(而非獨立面板)
indicator("SMA Crossover", overlay = true)
// ② 使用者可設定的輸入項。
// input.int() 建立一個整數輸入,顯示在腳本設定面板中。
// 第二個參數為預設值;title 為在 UI 中顯示的標籤。
fastLen = input.int(9, title = "Fast Length")
slowLen = input.int(21, title = "Slow Length")
// ③ 計算兩條 SMA series。
// 這些是 series<float>:每個 Bar 產生一個值。
fast = ta.sma(close, fastLen)
slow = ta.sma(close, slowLen)
// ④ 偵測金叉和死叉事件。
// ta.crossover(a, b) → 在 a 向上穿越 b 的那根 Bar 返回 true
// ta.crossunder(a, b) → 在 a 向下穿越 b 的那根 Bar 返回 true
crossUp = ta.crossover(fast, slow)
crossDown = ta.crossunder(fast, slow)
// ⑤ 將兩條均線繪製為連續折線。
// linewidth=2 使線條略粗於預設值 1。
plot(fast, title = "Fast SMA", color = color.blue, linewidth = 2)
plot(slow, title = "Slow SMA", color = color.orange, linewidth = 2)
// ⑥ 在金叉 Bar 的下方繪製向上的三角形標記。
// plotshape 僅在第一個參數為 true 的 Bar 上繪製。
plotshape(crossUp,
title = "Bullish Cross",
style = shape.triangleup,
location = location.belowbar,
color = color.green,
size = size.small)
// ⑦ 在死叉 Bar 的上方繪製向下的三角形標記。
plotshape(crossDown,
title = "Bearish Cross",
style = shape.triangledown,
location = location.abovebar,
color = color.red,
size = size.small)
// ⑧ 在金叉或死叉 Bar 上為圖表背景新增淡色高亮。
// color.new(c, transp) 建立帶透明度的顏色(0=不透明,100=完全透明)。
// 三元運算符 ?: 根據事件類型選擇顏色。
bgColor = crossUp ? color.new(color.green, 85) :
crossDown ? color.new(color.red, 85) : na
bgcolor(bgColor)涉及的核心概念
| 概念 | 位置 |
|---|---|
| 腳本宣告 | indicator(...) — 行 ① |
| 使用者輸入 | input.int — 行 ② |
series<float> 變數 | fast、slow、crossUp、crossDown — 行 ③④ |
ta 模組 | ta.sma、ta.crossover、ta.crossunder — 行 ③④ |
| 繪製折線 | plot(...) — 行 ⑤ |
| 繪製形狀標記 | plotshape(...) — 行 ⑥⑦ |
| 透明顏色 | color.new(c, transp) — 行 ⑧ |
| 三元運算符 | c ? a : b — 行 ⑧ |
na 表示「無值」 | na 用於跳過背景著色 — 行 ⑧ |
示例二 — 布林帶策略
該策略在收盤價向上突破上軌時做多,跌回中軌以下時平多;反之,向下突破下軌時做空,反彈回中軌以上時平空。涉及 strategy()、ta.bb、元組解構、歷史運算符和 strategy.entry / strategy.close 等特性。
pine
//@version=6
// ① 將此腳本宣告為策略。
// - default_qty_type : 使用固定數量(合約/股數)
// - default_qty_value : 每筆交易預設 1 手
strategy("Bollinger Bands Breakout",
default_qty_type = strategy.fixed,
default_qty_value = 1)
// ② 計算布林帶。
// ta.bb 返回一個元組:[中軌, 上軌, 下軌]。
// [a, b, c] = ... 語法將元組解構為三個變數。
[basis, upper, lower] = ta.bb(close, 20, 2.0)
// ③ 進場條件。
// close[1] 引用*上一根* Bar 的收盤價(歷史運算符 [])。
// 結合當前 Bar 與上一 Bar 的值,可精確定位價格穿越軌道的那根 Bar。
longEntry = close > upper and close[1] <= upper[1]
shortEntry = close < lower and close[1] >= lower[1]
// ④ 出場條件:價格穿越中軌。
longExit = close < basis
shortExit = close > basis
// ⑤ 發出策略指令。
// strategy.entry 開倉;strategy.close 按交易 ID 平倉。
if longEntry
strategy.entry("Long", strategy.long)
if shortEntry
strategy.entry("Short", strategy.short)
if longExit
strategy.close("Long")
if shortExit
strategy.close("Short")涉及的核心概念
| 概念 | 位置 |
|---|---|
| 策略宣告 | strategy(...) — 行 ① |
| 元組解構 | [basis, upper, lower] = ta.bb(...) — 行 ② |
ta.bb 布林帶 | 行 ② |
歷史運算符 [] | close[1]、upper[1] — 行 ③ |
| 布林 series | longEntry、shortEntry、longExit、shortExit — 行 ③④ |
if 語句 | 策略指令區塊 — 行 ⑤ |
strategy.entry / strategy.close | 開倉與平倉 — 行 ⑤ |