繪圖物件#

Pine Script 除了 plot 系列函式,還提供可以精確定位的 繪圖物件label(文字標籤)、line(線段)、box(矩形)。這些物件可以在圖表的任意位置和時間點繪製,非常適合標記特定 K 棒或畫出形態結構。

label — 文字標籤#

label.new() 在指定位置建立帶文字的標籤:

label.new(x, y, text, style, color, textcolor, size)
參數說明
x橫軸位置(通常用 bar_index
y縱軸位置(價格)
text顯示的文字
style標籤形狀
color標籤背景色
textcolor文字顏色
size文字大小
//@version=6
indicator("label 示範", overlay=true)

// 在每根陽線上方顯示標記
if close > open
    label.new(bar_index, high,
              text="▲",
              style=label.style_label_down,
              color=color.new(color.green, 20),
              textcolor=color.white,
              size=size.small)

label 樣式#

常用的 style 選項:

常數說明
label.style_label_up向上的標籤(箭頭在下)
label.style_label_down向下的標籤(箭頭在上)
label.style_label_left向左的標籤
label.style_label_right向右的標籤
label.style_label_center無箭頭的方框
label.style_circle圓形
label.style_diamond菱形
label.style_none只顯示文字,無背景

物件數量限制#

Pine Script 預設每種物件最多保留 500 個labellinebox 各自獨立計算)。超過後最舊的物件會自動刪除。

如果只需要顯示最新的幾個物件,應手動管理:

//@version=6
indicator("限制 label 數量", overlay=true)

var labelQueue = array.new<label>()

// 每根 K 棒建立新標籤
lbl = label.new(bar_index, high, str.tostring(math.round(close, 2)),
                style=label.style_label_down, size=size.tiny)
array.push(labelQueue, lbl)

// 只保留最新 10 個
if array.size(labelQueue) > 10
    label.delete(array.shift(labelQueue))

line — 線段#

line.new() 繪製連接兩點的線段:

line.new(x1, y1, x2, y2, color, width, style, extend)
//@version=6
indicator("趨勢線示範", overlay=true)

// 在最後一根 K 棒繪製近期高點連線
if barstate.islast
    // 找出最近兩個高點
    ph1 = ta.pivothigh(high, 5, 5)
    ph2 = ta.pivothigh(high, 10, 10)

    if not na(ph1) and not na(ph2)
        line.new(bar_index - 5,  ph1,
                 bar_index - 10, ph2,
                 color=color.red, width=2,
                 extend=extend.right)  // 向右延伸

延伸方向#

extend 參數控制線段是否向外延伸:

常數說明
extend.none不延伸(預設)
extend.right向右延伸到圖表邊緣
extend.left向左延伸到圖表邊緣
extend.both向兩側延伸

線條樣式#

常數說明
line.style_solid實線
line.style_dashed虛線
line.style_dotted點線
line.style_arrow_right帶右箭頭

box — 矩形#

box.new() 繪製矩形,常用於標記形態、供需區間等:

box.new(left, top, right, bottom, border_color, border_width, bgcolor)
//@version=6
indicator("矩形示範", overlay=true)

// 在前一根 K 棒繪製 K 棒實體矩形
prevOpen  = open[1]
prevClose = close[1]
prevHigh  = high[1]
prevLow   = low[1]

// 只在最後幾根 K 棒繪製,避免過多物件
if bar_index > last_bar_index - 20
    box.new(left=bar_index - 1, top=math.max(prevOpen, prevClose),
            right=bar_index,    bottom=math.min(prevOpen, prevClose),
            border_color=prevClose > prevOpen ? color.green : color.red,
            bgcolor=color.new(prevClose > prevOpen ? color.green : color.red, 70))

實用範例:標記突破點#

收盤突破 N 日高點 為例,綜合使用 labelline

//@version=6
indicator("突破標記", overlay=true)

lookback = input.int(20, "突破週期")

highestHigh = ta.highest(high, lookback)[1]  // 前一根 K 棒的回顧高點
breakout    = close > highestHigh

if breakout
    // 標記突破的 K 棒
    label.new(bar_index, low,
              "突破 " + str.tostring(lookback) + "日高\n" +
              str.tostring(math.round(close, 2)),
              style=label.style_label_up,
              color=color.new(color.green, 10),
              textcolor=color.white,
              size=size.normal)

    // 畫出被突破的水平線
    line.new(bar_index - lookback, highestHigh,
             bar_index, highestHigh,
             color=color.new(color.red, 30),
             style=line.style_dashed)

實用範例:在最後一根 K 棒顯示統計資訊#

//@version=6
indicator("K棒統計標籤", overlay=true)

var int bullCount = 0
var int bearCount = 0

if close > open
    bullCount := bullCount + 1
else
    bearCount := bearCount + 1

// 只在最後一根 K 棒顯示
if barstate.islast
    total  = bullCount + bearCount
    bullPct = total > 0 ? bullCount / total * 100 : 0.0

    label.new(bar_index + 5, high,
              "陽線: " + str.tostring(bullCount) + " (" +
              str.tostring(math.round(bullPct, 1)) + "%)\n" +
              "陰線: " + str.tostring(bearCount),
              style=label.style_label_left,
              color=color.new(color.navy, 10),
              textcolor=color.white,
              size=size.normal)

Reference#