内置ta指标函数
Pine Script 把常用的经典技术指标全部封装在 ta.* 命名空间下——你不用从零写算法,一行调用就能拿到结果。本章按类别梳理最常用的指标函数,讲清每个返回什么、怎么用。
一、均线类
| 函数 | 含义 | 特点 |
|---|---|---|
ta.sma(source, length) | 简单移动平均 | 等权、稳定、有滞后 |
ta.ema(source, length) | 指数移动平均 | 近期权重大、更灵敏 |
ta.wma(source, length) | 线性加权移动平均 | 按时间线性加权 |
ta.rma(source, length) | 递归移动平均(Wilder) | RSI/ATR 内部使用 |
ta.vwma(source, length) | 成交量加权移动平均 | 结合量能 |
ta.alma(source, length, offset, sigma) | Arnaud Legoux 移动平均 | 平滑且低滞后 |
//@version=6
indicator("均线对比", overlay = true)
sma20 = ta.sma(close, 20)
ema20 = ta.ema(close, 20)
plot(sma20, color = color.blue, title = "SMA20")
plot(ema20, color = color.orange, title = "EMA20")
用法:均线看趋势方向;短期均线上穿长期均线(金叉)看多,下穿(死叉)看空。
二、交叉判断
| 函数 | 含义 |
|---|---|
ta.crossover(a, b) | a 上穿 b(a 从下方越过到上方) |
ta.crossunder(a, b) | a 下穿 b |
ta.cross(a, b) | a 与 b 相交(上穿或下穿) |
fastMA = ta.sma(close, 5)
slowMA = ta.sma(close, 20)
goldenCross = ta.crossover(fastMA, slowMA) // 金叉
deathCross = ta.crossunder(fastMA, slowMA) // 死叉
三、动量类
MACD
//@version=6
indicator("MACD", overlay = false)
[macdLine, signalLine, histLine] = ta.macd(close, 12, 26, 9)
plot(macdLine, color = color.blue, title = "MACD")
plot(signalLine, color = color.orange, title = "Signal")
plot(histLine, color = color.red, style = plot.style_histogram, title = "Hist")
ta.macd(source, fastLen, slowLen, sigLen) 返回三元组(元组):MACD 线、信号线、柱状图。用 [a, b, c] = 解构赋值。只取其中一个值可以用 _ 占位:[_, signal, _] = ta.macd(...)。
用法:MACD 线上穿信号线(金叉)看多;柱状图由负转正也偏多。
RSI
rsi = ta.rsi(close, 14)
plot(rsi, color = color.purple)
hline(70, color = color.red) // 超买线
hline(30, color = color.green) // 超卖线
ta.rsi(source, length) 返回 0~100 的相对强弱值。>70 偏强/超买,<30 偏弱/超卖。
KDJ(Stochastic)
k = ta.sma(ta.stoch(close, high, low, 14), 3)
d = ta.sma(k, 3)
ta.stoch(source, high, low, length) 返回随机指标 K 值。Pine 没有直接提供 KDJ,但用 ta.stoch + SMA 组合即可实现。
CCI / Williams %R
| 函数 | 含义 |
|---|---|
ta.cci(source, length) | 顺势指标,越过 ±100 视为趋势启动 |
ta.wpr(length) | 威廉指标,反映收盘在区间中的位置 |
ta.mfi(hlc3, volume, length) | 资金流量指标(带量的 RSI) |
四、波动类
| 函数 | 含义 |
|---|---|
ta.atr(length) | 平均真实波幅 ATR,常用于止损距离 |
ta.stdev(source, length) | 标准差 |
ta.bb(source, length, mult) | 布林带,返回 [中轨, 上轨, 下轨] |
ta.kc(source, length, mult) | 肯特纳通道 |
布林带示例
//@version=6
indicator("布林带", overlay = true)
[middle, upper, lower] = ta.bb(close, 20, 2.0)
plot(middle, color = color.orange)
plot(upper, color = color.gray)
plot(lower, color = color.gray)
// 带宽收窄 = 变盘预警
bandWidth = (upper - lower) / middle
用法:价格触上轨偏强/超买,触下轨偏弱/超卖,带宽收窄预示变盘临近。
ATR 动态止损
atr = ta.atr(14)
stopPrice = close - 2 * atr // 收盘价下方 2 倍 ATR 止损
五、量价类
| 函数 | 含义 |
|---|---|
ta.vwap(source, anchor) | 成交量加权平均价,anchor 控制重置时机 |
ta.obv | 能量潮(内置变量) |
//@version=6
indicator("VWAP", overlay = true)
v = ta.vwap(hlc3) // 每日自动重置的 VWAP
plot(v, color = color.blue, linewidth = 2)
VWAP 是日内交易者最常用的均价格局,反映当日成交量加权的平均成本。
六、高低极值
| 函数 | 含义 |
|---|---|
ta.highest(source, length) | 过去 length 根 K 线的最高值 |
ta.lowest(source, length) | 过去 length 根 K 线的最低值 |
ta.highestbars(source, length) | 最高值出现在几根前 |
ta.lowestbars(source, length) | 最低值出现在几根前 |
ta.change(source, length) | 与 length 根前的差值 |
highest20 = ta.highest(high, 20) // 20 日最高
lowest20 = ta.lowest(low, 20) // 20 日最低
change1 = ta.change(close) // 涨跌幅(默认 1 根)
七、综合示例:「均线多头 + MACD 金叉 + 放量」
//@version=6
indicator("三重共振", overlay = true)
// 均线
sma5 = ta.sma(close, 5)
sma20 = ta.sma(close, 20)
多头 = sma5 > sma20
// MACD 金叉
[macdLine, signalLine, _] = ta.macd(close, 12, 26, 9)
金叉 = ta.crossover(macdLine, signalLine)
// 放量
volMA = ta.sma(volume, 5)
放量 = volume > volMA * 1.5
// 共振信号
共振 = 多头 and 金叉 and 放量
plotshape(共振, style = shape.triangleup, location = location.belowbar, color = color.green, size = size.small)
小结
均线看趋势(SMA/EMA)、交叉看时机(crossover/crossunder)、动量看强弱(MACD/RSI)、波动看区间(BOLL/ATR)、量价看力度(VWAP)。 把它们和绘图函数组合,就能写出绝大多数实战指标。接下来学怎么把信号画出来并设置预警: