返回《給 CFD 交易者的 TradingView PineScript》
第三部分

讀懂回測結果:那些數字到底是什麼意思

淨利是最糟的指標。真正重要的是最大回撤、獲利因子,以及曝險調整後的報酬。

中級閱讀時間約 9 分鐘初學 → 進階第 12 / 17 堂
在 TradingView 實際跑一次
同一個策略,但指標顯示在畫面上

那個表格顯示淨利、最大回撤、勝率與獲利因子。拿它跟 Strategy Tester 分頁交叉比對——並注意 slippage=2 如何改變一切。

//@version=5
strategy("Lesson 12 - Reading the report", overlay=true,
     initial_capital=10000,
     default_qty_type=strategy.percent_of_equity,
     default_qty_value=10,
     commission_type=strategy.commission.cash_per_contract,
     commission_value=0,
     slippage=2)   // 2 ticks — realistic, not optimistic

fast = ta.ema(close, 9)
slow = ta.ema(close, 21)

strategy.entry("Long", strategy.long, when=ta.crossover(fast, slow))
strategy.exit("Long TP/SL", "Long",
     stop  = strategy.position_avg_price * 0.98,
     limit = strategy.position_avg_price * 1.04)
strategy.close("Long", when=ta.crossunder(fast, slow))

plot(fast, "Fast EMA", color=color.blue, linewidth=2)
plot(slow, "Slow EMA", color=color.orange, linewidth=2)

// Live metrics — the four numbers that actually matter.
if barstate.islast
    var table t = table.new(position.top_right, 2, 5, border_width=1)
    table.cell(t, 0, 0, "Net profit",    text_color=color.white)
    table.cell(t, 1, 0, str.tostring(strategy.netprofit, "#.##"))
    table.cell(t, 0, 1, "Max drawdown",  text_color=color.white)
    table.cell(t, 1, 1, str.tostring(strategy.max_drawdown, "#.##"))
    table.cell(t, 0, 2, "Win rate %",    text_color=color.white)
    table.cell(t, 1, 2, str.tostring(strategy.wintrades / math.max(strategy.closedtrades, 1) * 100, "#.#"))
    table.cell(t, 0, 3, "Profit factor", text_color=color.white)
    table.cell(t, 1, 3, str.tostring(strategy.grossprofit / math.max(strategy.grossloss, 1), "#.##"))
    table.cell(t, 0, 4, "Closed trades", text_color=color.white)
    table.cell(t, 1, 4, str.tostring(strategy.closedtrades))
如何執行
  1. 1. 在 TradingView 打開任一圖表,點選下方的 Pine Editor
  2. 2. 把編輯器裡的內容全選,用這段程式覆蓋過去,然後按 Save
  3. 3. 點選 Add to chart。如果是策略, 再打開 Strategy Tester 分頁。

Overview 分頁

淨利、最大回撤、勝率、獲利因子、平均每筆交易。多數初學者第一個看淨利。那是錯的指標——一個賺 5 萬但最大回撤 60% 的策略,在風險調整的基礎上,遠比一個賺 3 萬但回撤 12% 的策略糟。

先看什麼

最大回撤(max drawdown)是回測期間最嚴重的一次權益峰谷虧損。如果它超過 25%,這個策略有存活能力的問題——許多零售 CFD 帳戶撐不過 25% 的回撤。

獲利因子(profit factor)是毛利除以毛損。1.5 以上算不錯;2.0 以上算強;低於 1.2 很脆弱。

平均每筆/平均獲利/平均虧損告訴你結構。如果平均獲利是平均虧損的 0.8 倍,你需要超過 55% 的勝率才能打平。多數 CFD 策略要不就是高勝率小獲利(均值回歸),要不就是低勝率大獲利(順勢)。

Performance Summary

逐年報酬、逐月報酬、多空分解。一個把錢全賺在某一個離群年份的策略不是一個策略——它是一個尾部事件。要找的是跨年度的一致性。

List of Trades

每一筆模擬交易,帶進場 K 線、出場 K 線、損益,以及權益曲線上的那一點。把它捲過一遍。如果你看到大額虧損聚在一起,這個策略有環境依賴性。如果你看到一簇獲利後面跟著一簇虧損,那就是優勢在衰退——一個紅旗。

常見的回測陷阱

過度配適——太多參數被調校到歷史上。前視偏誤——用到了策略在決策當下不會有的未來資料。存活者偏誤——只測試今天還在交易的商品。低估滑價——假設成交在回測價格上而沒有滑價;對 CFD 來說,每邊至少加 1 個 tick 的滑價。

誠實的答案

一個好到不像真的回測,就是不像真的。如果最大回撤低於 5% 而權益曲線是一條平滑向上的線,你幾乎肯定是有一個 bug 或一個前視。

你剛完成了什麼

給 CFD 交易者的 TradingView PineScript》的第 12 / 17 堂課。跑完範例或讀完這段之後, 把它勾起來,然後進下一堂課。