為什麼畫圖重要
每根 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.delete、line.delete、box.delete 移除舊的,或在 indicator/strategy 宣告裡用 max_lines_count、max_labels_count、max_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, "#.##") + "%")
該從這裡帶走什麼
有註解的圖是一個工具。沒有註解的圖只是一條價格線。花時間讓那個策略一眼就能讀懂——它會改變你在實盤交易時的反應。