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

在圖上畫東西:標籤、線與方框

讓一張圖變得可讀的註解:交易標記、停損線、目標區間,以及交易管理儀表板。

進階閱讀時間約 8 分鐘初學 → 進階第 15 / 17 堂
在 TradingView 實際跑一次
標籤、方框,以及一個即時儀表板

標籤標出每一次黃金交叉,方框把 EMA 區間上色,右上角的表格顯示即時數值。注意宣告裡的 max_*_count 上限。

//@version=5
indicator("Lesson 15 - Drawing", overlay=true,
     max_labels_count=500, max_lines_count=500, max_boxes_count=500)

ema20 = ta.ema(close, 20)
ema50 = ta.ema(close, 50)

plot(ema20, "EMA 20", color=color.blue, linewidth=2)
plot(ema50, "EMA 50", color=color.orange, linewidth=2)

crossUp = ta.crossover(ema20, ema50)

// Label each golden cross.
if crossUp
    label.new(bar_index, low, "Golden cross",
         style=label.style_label_upper_left, color=color.new(color.green, 20),
         textcolor=color.white, size=size.small)

// Box the zone between the two EMAs.
box.new(left=bar_index - 10, top=math.max(ema20, ema50),
        right=bar_index,     bottom=math.min(ema20, ema50),
        border_color=color.new(color.gray, 50),
        bgcolor=color.new(color.blue, 92))

// Dashboard — only drawn once, on the last bar.
if barstate.islast
    var table dash = table.new(position.top_right, 2, 3, border_width=1)
    table.cell(dash, 0, 0, "EMA 20", text_color=color.white)
    table.cell(dash, 1, 0, str.tostring(ema20, "#.##"))
    table.cell(dash, 0, 1, "EMA 50", text_color=color.white)
    table.cell(dash, 1, 1, str.tostring(ema50, "#.##"))
    table.cell(dash, 0, 2, "Trend",  text_color=color.white)
    table.cell(dash, 1, 2, ema20 > ema50 ? "Up" : "Down",
               text_color=ema20 > ema50 ? color.green : color.red)
如何執行
  1. 1. 在 TradingView 打開任一圖表,點選下方的 Pine Editor
  2. 2. 把編輯器裡的內容全選,用這段程式覆蓋過去,然後按 Save
  3. 3. 點選 Add to chart。如果是策略, 再打開 Strategy Tester 分頁。

為什麼畫圖重要

每根 K 線都畫一次的線是噪音。一條在進場時畫出、延伸到出場、並標上損益的線——那才是資訊。Pine 的繪圖物件就是你把一個策略測試器變成一張你看得懂的圖的方法。

標籤:文字註解

if strategy.position_size == 0 and longCondition
    label.new(bar_index, high, "Long", style=label.style_label_down, color=color.green)

標籤錨定在特定的 K 線與價格上。用它標進場、出場與訊號。

線:趨勢線與停損線

var line stopLine = na
if strategy.position_size != 0 and strategy.position_size[1] == 0
    stopLine := line.new(bar_index, close, bar_index + 50, close - atrVal, color=color.red, width=2)

線可以是水平、傾斜或延伸的。如果你給它們一個未來的 K 線,它們會自動延伸。

方框:區間

box.new(left, top, right, bottom) 畫一個矩形區間。用方框畫停損區、目標區、時段區間與盤整區間。

清掉舊的圖

繪圖物件會留在圖上。用 label.deleteline.deletebox.delete 移除舊的,或在 indicatorstrategy 宣告裡用 max_lines_countmax_labels_countmax_boxes_count 去限制 Pine 保留的總數。

一個交易管理儀表板

if barstate.islast
    var table dash = table.new(position.top_right, 2, 4)
    table.cell(dash, 0, 0, "Direction", text_color=color.white)
    table.cell(dash, 1, 0, strategy.position_size > 0 ? "Long" : strategy.position_size < 0 ? "Short" : "Flat")
    table.cell(dash, 0, 1, "Entry", text_color=color.gray)
    table.cell(dash, 1, 1, str.tostring(strategy.position_avg_price, "#.##"))
    table.cell(dash, 0, 2, "Unrealized", text_color=color.gray)
    table.cell(dash, 1, 2, str.tostring(strategy.openprofit, "#.##"))
    table.cell(dash, 0, 3, "Risk", text_color=color.gray)
    table.cell(dash, 1, 3, str.tostring(equityRisk, "#.##") + "%")

該從這裡帶走什麼

有註解的圖是一個工具。沒有註解的圖只是一條價格線。花時間讓那個策略一眼就能讀懂——它會改變你在實盤交易時的反應。

你剛完成了什麼

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