常用技術指標#

Pine Script 的 ta 命名空間內建了數十個技術指標函式,涵蓋均線、震盪指標、波動率指標等。本章介紹最常用的指標及其參數。

均線類#

ta.sma — 簡單移動平均#

ta.sma(source, length)
sma20 = ta.sma(close, 20)
plot(sma20, "SMA 20", color=color.orange)

ta.ema — 指數移動平均#

ta.ema(source, length)

EMA 對近期價格的權重更高,反應速度比 SMA 快:

ema12 = ta.ema(close, 12)
ema26 = ta.ema(close, 26)
plot(ema12, "EMA 12", color=color.blue)
plot(ema26, "EMA 26", color=color.orange)

其他均線#

函式說明
ta.wma(src, len)加權移動平均 (WMA)
ta.vwma(src, len)成交量加權移動平均 (VWMA)
ta.hma(src, len)船體移動平均 (HMA),平滑且延遲低
ta.alma(src, len, offset, sigma)Arnaud Legoux 移動平均
ta.swma(src)對稱加權移動平均(固定長度 4)

震盪指標#

ta.rsi — 相對強弱指數#

ta.rsi(source, length)
//@version=6
indicator("RSI")

rsiValue = ta.rsi(close, 14)
plot(rsiValue, "RSI", color=color.purple)

hline(70, "超買", color=color.red,   linestyle=hline.style_dashed)
hline(30, "超賣", color=color.green, linestyle=hline.style_dashed)
hline(50, "中線", color=color.gray,  linestyle=hline.style_dotted)

ta.macd — 指數平滑異同移動平均#

ta.macd(source, fastLength, slowLength, signalLength)
// 回傳 [macdLine, signalLine, histLine]
//@version=6
indicator("MACD")

[macdLine, signalLine, hist] = ta.macd(close, 12, 26, 9)

plot(hist,       "Histogram", style=plot.style_histogram,
     color=hist >= 0 ? color.new(color.green, 20) : color.new(color.red, 20))
plot(macdLine,   "MACD",   color=color.blue)
plot(signalLine, "Signal", color=color.orange)
hline(0, color=color.gray)

ta.stoch — 隨機指標 (KD)#

ta.stoch(close, high, low, length)
// 回傳 %K 值;%D = ta.sma(%K, 3)
//@version=6
indicator("KD 指標")

kValue = ta.stoch(close, high, low, 14)
dValue = ta.sma(kValue, 3)

plot(kValue, "K", color=color.blue)
plot(dValue, "D", color=color.orange)
hline(80, "超買", color=color.red)
hline(20, "超賣", color=color.green)

ta.cci — 商品通道指數#

ta.cci(source, length)

ta.williams — Williams %R#

ta.williams(high, low, close, length)

趨勢指標#

ta.adx — 平均方向指數#

[plusDI, minusDI, adx] = ta.dmi(diLength, adxSmoothing)
//@version=6
indicator("ADX / DMI")

[plusDI, minusDI, adxValue] = ta.dmi(14, 14)

plot(plusDI,  "+DI",  color=color.green)
plot(minusDI, "-DI",  color=color.red)
plot(adxValue, "ADX", color=color.blue, linewidth=2)
hline(25, "趨勢門檻", color=color.gray, linestyle=hline.style_dashed)

波動率指標#

ta.bb — 布林通道#

ta.bb(source, length, mult)
// 回傳 [middle, upper, lower]
//@version=6
indicator("布林通道", overlay=true)

[middle, upper, lower] = ta.bb(close, 20, 2)

plot(upper,  "上軌", color=color.blue)
plot(middle, "中軌", color=color.orange)
plot(lower,  "下軌", color=color.blue)

fill(plot(upper, display=display.none),
     plot(lower, display=display.none),
     color=color.new(color.blue, 92))

ta.atr — 平均真實波幅#

ta.atr(length)

ATR 常用於設定動態止損距離:

//@version=6
indicator("ATR 止損示範", overlay=true)

atrValue = ta.atr(14)
multiplier = input.float(2.0, "ATR 倍數")

stopLong  = close - atrValue * multiplier
stopShort = close + atrValue * multiplier

plot(stopLong,  "多頭止損", color=color.red,   style=plot.style_stepline)
plot(stopShort, "空頭止損", color=color.green, style=plot.style_stepline)

成交量指標#

ta.obv — 能量潮#

ta.obv

ta.vwap — 成交量加權平均價#

ta.vwap(source)
// 或直接使用內建 VWAP:ta.vwap
//@version=6
indicator("VWAP", overlay=true)

vwapValue = ta.vwap(hlc3)
plot(vwapValue, "VWAP", color=color.purple, linewidth=2)

轉折點偵測#

ta.pivothigh / ta.pivotlow#

偵測高低轉折點,常用於自動畫趨勢線:

ta.pivothigh(source, leftBars, rightBars)
ta.pivotlow(source, leftBars, rightBars)
// 回傳轉折點的價格;若當前 K 棒不是轉折點則回傳 na
//@version=6
indicator("轉折點", overlay=true)

pivotH = ta.pivothigh(high, 5, 5)
pivotL = ta.pivotlow(low,  5, 5)

plotshape(pivotH, "高點", shape.triangledown,
          location.abovebar, color.red,   size=size.small)
plotshape(pivotL, "低點", shape.triangleup,
          location.belowbar, color.green, size=size.small)

穿越偵測#

函式說明
ta.crossover(a, b)a 從下方向上穿越 b(黃金交叉)
ta.crossunder(a, b)a 從上方向下穿越 b(死亡交叉)
ta.cross(a, b)a 與 b 任意方向穿越
//@version=6
indicator("均線穿越", overlay=true)

ma5  = ta.sma(close, 5)
ma20 = ta.sma(close, 20)

golden = ta.crossover(ma5, ma20)
death  = ta.crossunder(ma5, ma20)

plotshape(golden, "黃金交叉", shape.labelup,
          location.belowbar, color.green, text="金", textcolor=color.white)
plotshape(death,  "死亡交叉", shape.labeldown,
          location.abovebar, color.red,   text="死", textcolor=color.white)

Reference#