策略基礎#
策略 (Strategy) 是 Pine Script 中用於回測交易邏輯的腳本類型。與指標不同,策略可以發出買賣指令,並由 TradingView 自動計算損益、勝率、最大回撤等績效數據。
宣告策略#
腳本第一行改用 strategy() 宣告:
//@version=6
strategy("策略名稱", overlay=true, initial_capital=100000, currency=currency.TWD)常用參數:
| 參數 | 說明 | 預設值 |
|---|---|---|
overlay | 是否疊加在主圖上 | false |
initial_capital | 初始資金 | 100000 |
currency | 貨幣單位 | currency.USD |
default_qty_type | 下單數量類型 | strategy.fixed |
default_qty_value | 預設下單數量 | 1 |
進場與出場指令#
strategy.entry — 開倉#
strategy.entry(id, direction, qty, comment)| 參數 | 說明 |
|---|---|
id | 訂單 ID(自訂字串,用於後續管理) |
direction | strategy.long(做多)或 strategy.short(做空) |
qty | 下單數量(省略則使用預設值) |
comment | 圖表上顯示的備註文字 |
strategy.exit — 平倉(條件出場)#
strategy.exit(id, from_entry, stop, limit, trail_points, trail_offset, comment)| 參數 | 說明 |
|---|---|
id | 出場訂單 ID |
from_entry | 對應的進場訂單 ID |
stop | 停損價格 |
limit | 停利價格 |
strategy.close — 市價平倉#
strategy.close(id, comment)立即以市價平掉指定 ID 的倉位。
strategy.close_all — 平掉所有倉位#
strategy.close_all(comment)第一個策略:均線交叉#
//@version=6
strategy("均線交叉策略", overlay=true, initial_capital=100000)
fastLen = input.int(5, "短期均線")
slowLen = input.int(20, "長期均線")
fastMA = ta.sma(close, fastLen)
slowMA = ta.sma(close, slowLen)
plot(fastMA, "短均線", color=color.blue)
plot(slowMA, "長均線", color=color.orange)
// 黃金交叉:做多進場
if ta.crossover(fastMA, slowMA)
strategy.entry("Long", strategy.long, comment="金叉")
// 死亡交叉:多單平倉
if ta.crossunder(fastMA, slowMA)
strategy.close("Long", comment="死叉")加入停損停利#
//@version=6
strategy("均線 + 停損停利", overlay=true, initial_capital=100000)
fastLen = input.int(5, "短期均線")
slowLen = input.int(20, "長期均線")
stopPct = input.float(2.0, "停損(%)", step=0.5) / 100
targetPct = input.float(4.0, "停利(%)", step=0.5) / 100
fastMA = ta.sma(close, fastLen)
slowMA = ta.sma(close, slowLen)
// 黃金交叉進場
if ta.crossover(fastMA, slowMA)
strategy.entry("Long", strategy.long)
// 以進場價計算停損停利
if strategy.position_size > 0
entryPrice = strategy.position_avg_price
stopPrice = entryPrice * (1 - stopPct)
limitPrice = entryPrice * (1 + targetPct)
strategy.exit("Exit Long", from_entry="Long",
stop=stopPrice, limit=limitPrice)strategy.position_size — 查詢持倉#
strategy.position_size 回傳當前持倉數量:
- 正數:持有多頭部位
- 負數:持有空頭部位
- 0:無持倉
// 只在無持倉時才進場(避免重複進場)
if ta.crossover(fastMA, slowMA) and strategy.position_size == 0
strategy.entry("Long", strategy.long)雙向交易(多空皆做)#
//@version=6
strategy("雙向均線策略", overlay=true, initial_capital=100000)
fastLen = input.int(5, "短期均線")
slowLen = input.int(20, "長期均線")
fastMA = ta.sma(close, fastLen)
slowMA = ta.sma(close, slowLen)
// 黃金交叉:進多單(同時平空單)
if ta.crossover(fastMA, slowMA)
strategy.entry("Long", strategy.long, comment="金叉做多")
// 死亡交叉:進空單(同時平多單)
if ta.crossunder(fastMA, slowMA)
strategy.entry("Short", strategy.short, comment="死叉做空")當新的
strategy.entry方向與持倉方向相反時,TradingView 會自動先平掉舊倉位再開新倉。
回測報告#
套用策略後,點選圖表下方的 Strategy Tester 面板可以看到:
| 項目 | 說明 |
|---|---|
| 淨利潤 | 總損益金額 |
| 總交易次數 | 進出場的完整交易次數 |
| 勝率 | 獲利交易佔總交易的比例 |
| 獲利因子 | 總獲利 / 總虧損 |
| 最大回撤 | 峰值到谷值的最大虧損幅度 |
| 夏普比率 | 風險調整後報酬率 |
與 MultiCharts 的比較#
有 MultiCharts 背景的讀者可以參考以下對照:
| Pine Script | PowerLanguage | |
|---|---|---|
| 進場 | strategy.entry("id", strategy.long) | Buy("id") next bar at market |
| 出場 | strategy.close("id") | Sell("id") next bar at market |
| 停損 | strategy.exit(..., stop=price) | SetStopLoss(amount) |
| 持倉查詢 | strategy.position_size | MarketPosition |
| 回測報告 | Strategy Tester 面板 | Performance Report |