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 → 自动下单到交易所。 完整流程见: