strategy策略与回测
指标告诉你「什么时候该买」,策略脚本则更进一步——模拟实际的买入卖出、计算盈亏、输出完整的回测报告。本章把 Pine 策略脚本的核心机制讲透:声明、下单、止盈止损、回测配置与报告解读。
策略 vs 指标
| 指标 indicator | 策略 strategy | |
|---|---|---|
| 画图 | 可以 | 可以 |
| 模拟交易 | 不行 | 可以 |
| 回测报告 | 无 | 有(Strategy Tester) |
| 预警 | alertcondition + alert | 只能 alert + 订单成交事件 |
策略和指标语法基本一致,多出来的就是 strategy.* 命名空间的下单命令。
声明 strategy
//@version=6
strategy("双均线策略", overlay = true, initial_capital = 100000, default_qty_type = strategy.percent_of_equity, default_qty_value = 100, commission_type = strategy.commission.percent, commission_value = 0.1)
常用 strategy 参数:
| 参数 | 含义 | 默认值 |
|---|---|---|
title | 策略名称(必填) | — |
overlay | 叠加主图 | false |
initial_capital | 初始资金 | 1000000 |
currency | 账户币种 | currency.NONE(跟随品种) |
default_qty_type | 默认下单数量类型 | strategy.fixed |
default_qty_value | 默认下单数量 | 1 |
commission_type | 佣金类型 | strategy.commission.percent |
commission_value | 佣金比例 | 0 |
slippage | 滑点(tick 数) | 0 |
pyramiding | 同方向最大加仓次数 | 1 |
下单数量类型可选 strategy.fixed(固定数量)、strategy.cash(固定金额)、strategy.percent_of_equity(权益百分比)。
下单命令
Pine 提供五个下单命令,覆盖开仓、平仓、基础订单:
| 命令 | 用途 |
|---|---|
strategy.entry(id, direction) | 开仓/反手(受 pyramiding 限制,自动反手) |
strategy.order(id, direction) | 基础下单(不受 pyramiding,不自动反手) |
strategy.exit(id, from_entry) | 平仓出场(可挂止盈/止损/追踪止损) |
strategy.close(id) | 按入场 ID 市价平仓 |
strategy.close_all() | 全部平仓 |
基础示例:双均线策略
//@version=6
strategy("双均线策略", overlay = true, initial_capital = 10000)
fastMA = ta.sma(close, 10)
slowMA = ta.sma(close, 30)
longCondition = ta.crossover(fastMA, slowMA)
if longCondition
strategy.entry("多", strategy.long)
shortCondition = ta.crossunder(fastMA, slowMA)
if shortCondition
strategy.entry("空", strategy.short)
关键点:
strategy.entry("多", strategy.long):以 ID「多」开多仓。默认生成市价单,在下一根 K 线开盘成交。strategy.entry的核心特性是自动反手:如果当前持空仓 5 手,调用strategy.long会先平掉 5 手空仓,再开多仓。- 历史回测时,市价单在下一根 bar 的 open 价成交。
止盈止损 strategy.exit
//@version=6
strategy("带止盈止损", overlay = true)
// 入场
longCondition = ta.crossover(ta.sma(close, 10), ta.sma(close, 30))
if longCondition and strategy.position_size == 0
strategy.entry("多", strategy.long)
// 止盈止损(以 tick 为单位的距离)
strategy.exit("离场", "多", profit = 200, loss = 100)
| 参数 | 含义 |
|---|---|
profit | 止盈距离(tick 数) |
loss | 止损距离(tick 数) |
limit | 止盈价格(绝对值) |
stop | 止损价格(绝对值) |
trail_points | 追踪止损激活距离 |
trail_offset | 追踪止损回撤偏移 |
qty | 平仓数量 |
strategy.exit("离场ID", "入场ID", ...) 第一个参数是出场单 ID,第二个参数指定为哪个入场单平仓。
追踪止损示例
strategy.exit("追踪离场", "多", trail_points = 100, trail_offset = 50)
含义:价格朝有利方向走 100 个 tick 后激活追踪止损,之后从最高盈利点回撤 50 个 tick 则止损。
平仓 strategy.close / close_all
// 平掉特定入场 ID 的仓位
strategy.close("多")
// 不管什么仓位,全部平掉
strategy.close_all()
这两个命令发的是市价单,strategy.exit 发的是限价/止损单。
策略执行机制
Pine 策略默认的执行行为对回测结果影响很大,务必理解:
- 历史 K 线:每根 bar 收盘时计算一次,下单指令在下一根 bar 开盘成交。
- 实时 K 线:默认只在收盘时计算(和指标不同,指标每 tick 都算)。
- 可用三个参数改变默认行为:
| 参数 | 效果 |
|---|---|
calc_on_every_tick = true | 每 tick 都计算(更接近指标行为) |
calc_on_order_fills = true | 每次订单成交后重新计算 |
process_orders_on_close = true | 在当根 bar 收盘时就成交(而非下根开盘) |
Bar Magnifier(
use_bar_magnifier = true)让回测引擎用更小周期数据推断 bar 内价格走势,成交更精确。Premium 以上套餐可用。
仓位信息变量
| 变量 | 含义 |
|---|---|
strategy.position_size | 当前持仓数量(正=多,负=空,0=空仓) |
strategy.position_avg_price | 持仓均价 |
strategy.position_entry_name | 当前仓位的入场 ID |
strategy.opentrades | 当前开仓交易数 |
strategy.equity | 当前账户权益 |
strategy.netprofit | 累计净盈亏 |
if strategy.position_size > 0
// 持有多仓时的逻辑
回测报告 Strategy Tester
策略添加到图表后,下方「策略测试器」(Strategy Tester)面板展示四个标签页:
概览 Overview
- 净利润、总收益、最大回撤
- 买持收益曲线 vs 买入持有曲线对比图
- 回撤柱状图
业绩摘要 Performance Summary
- 总交易次数、盈利交易占比(胜率)
- 平均盈利 / 平均亏损 / 盈亏比
- 利润因子(Profit Factor = 总盈利 / 总亏损)
- 最大连胜 / 连亏
- Sharpe 比率、Sortino 比率
交易列表 List of Trades
- 每笔交易的入场/出场时间、价格、数量、盈亏
- 点击可跳转到对应 K 线
属性 Properties
- 回测日期范围、品种信息、输入参数、策略属性
一个完整策略示例
//@version=6
strategy("RSI 策略 + 止损", overlay = true, initial_capital = 10000, default_qty_type = strategy.percent_of_equity, default_qty_value = 50)
// 参数
rsiLen = input.int(14, "RSI 周期")
stopLoss = input.float(2.0, "止损 ATR 倍数")
// 指标
rsi = ta.rsi(close, rsiLen)
atr = ta.atr(14)
// 入场:RSI 低于 30 后回升
longCondition = ta.crossover(rsi, 30) and strategy.position_size == 0
if longCondition
strategy.entry("多", strategy.long)
// 止损:入场价下方 N 倍 ATR
if strategy.position_size > 0
stopPrice = strategy.position_avg_price - stopLoss * atr
strategy.exit("止损", "多", stop = stopPrice)
// 出场:RSI 超过 70
takeProfit = rsi > 70
if takeProfit and strategy.position_size > 0
strategy.close("多")
plot(rsi > 70 ? color.red : rsi < 30 ? color.green : na)
小结
strategy()声明策略,配置初始资金、下单方式、佣金滑点。strategy.entry()开仓(自动反手),strategy.exit()挂止盈止损,strategy.close()市价平仓。- 默认每根 bar 收盘计算、下根开盘成交;可用
calc_on_every_tick等参数调整。 - Strategy Tester 从概览、业绩、交易明细、属性四个维度评估策略表现。
策略回测是 Pine 的核心实战能力,最后一步了解版本迁移和进阶技巧: