資料表格#
table 物件可以在圖表的固定位置顯示格狀資訊,非常適合製作儀表板、顯示多個指標的當前數值、或呈現策略統計數據。與 label 不同,table 固定在畫面的某個角落,不會隨 K 棒移動。
建立表格#
table.new(position, columns, rows, bgcolor, border_color, border_width, frame_color, frame_width)| 參數 | 說明 |
|---|---|
position | 表格在畫面中的位置 |
columns | 欄數 |
rows | 列數 |
bgcolor | 表格背景色 |
border_color | 儲存格框線顏色 |
border_width | 框線寬度(px) |
位置常數#
| 常數 | 位置 |
|---|---|
position.top_left | 左上角 |
position.top_center | 上方中間 |
position.top_right | 右上角 |
position.middle_left | 左側中間 |
position.middle_center | 正中央 |
position.middle_right | 右側中間 |
position.bottom_left | 左下角 |
position.bottom_center | 下方中間 |
position.bottom_right | 右下角 |
填入儲存格#
建立表格後,使用 table.cell() 填入每個儲存格的內容:
table.cell(table_id, column, row, text, text_color, text_size, bgcolor, text_halign)儲存格的索引從 左上角 (0, 0) 開始,欄往右增加,列往下增加。
基本範例#
//@version=6
indicator("簡單表格", overlay=true)
// 只在最後一根 K 棒建立表格(避免每根 K 棒都重建)
if barstate.islast
t = table.new(position.top_right, 2, 3,
bgcolor=color.new(color.navy, 10),
border_color=color.new(color.white, 50),
border_width=1)
// 標題列
table.cell(t, 0, 0, "指標", text_color=color.white, text_size=size.small)
table.cell(t, 1, 0, "數值", text_color=color.white, text_size=size.small)
// RSI
rsiVal = ta.rsi(close, 14)
table.cell(t, 0, 1, "RSI(14)", text_color=color.white, text_size=size.small)
table.cell(t, 1, 1, str.tostring(math.round(rsiVal, 2)),
text_color=rsiVal > 70 ? color.red : rsiVal < 30 ? color.green : color.white,
text_size=size.small)
// 成交量 vs 均量
volMA = ta.sma(volume, 20)
volRatio = volume / volMA
table.cell(t, 0, 2, "量比", text_color=color.white, text_size=size.small)
table.cell(t, 1, 2, str.tostring(math.round(volRatio, 2)) + "x",
text_color=volRatio > 1.5 ? color.yellow : color.white,
text_size=size.small)使用 var 持久化表格#
用 var 宣告表格可避免每根 K 棒重新建立,只需更新儲存格內容:
//@version=6
indicator("持久化表格", overlay=true)
// 只建立一次表格
var t = table.new(position.top_right, 2, 4,
bgcolor=color.new(color.black, 20),
border_color=color.gray, border_width=1)
// 每根 K 棒更新數值(只更新數值欄,不重建)
if barstate.islast or barstate.isrealtime
ma20 = ta.sma(close, 20)
rsi = ta.rsi(close, 14)
atr = ta.atr(14)
// 標題欄(只需設定一次,但寫在這裡也不影響效能)
table.cell(t, 0, 0, "指標", bgcolor=color.new(color.blue, 50), text_color=color.white)
table.cell(t, 1, 0, "數值", bgcolor=color.new(color.blue, 50), text_color=color.white)
table.cell(t, 0, 1, "MA20", text_color=color.white)
table.cell(t, 1, 1, str.tostring(math.round(ma20, 2)),
text_color=close > ma20 ? color.green : color.red)
table.cell(t, 0, 2, "RSI(14)", text_color=color.white)
table.cell(t, 1, 2, str.tostring(math.round(rsi, 1)),
text_color=rsi > 70 ? color.red : rsi < 30 ? color.lime : color.white)
table.cell(t, 0, 3, "ATR(14)", text_color=color.white)
table.cell(t, 1, 3, str.tostring(math.round(atr, 2)), text_color=color.white)合併儲存格#
table.merge_cells() 可以合併相鄰儲存格,常用於製作標題:
//@version=6
indicator("合併儲存格", overlay=true)
if barstate.islast
t = table.new(position.top_left, 3, 3,
bgcolor=color.new(color.navy, 20),
border_color=color.white, border_width=1)
// 合併第一列的三個儲存格作為標題
table.cell(t, 0, 0, "多重均線儀表板",
text_color=color.white, text_size=size.normal)
table.merge_cells(t, 0, 0, 2, 0) // 從 (0,0) 合併到 (2,0)
// 資料列
headers = array.from("週期", "均線", "位置")
periods = array.from(5, 20, 60)
for col = 0 to 2
table.cell(t, col, 1, array.get(headers, col),
text_color=color.gray, text_size=size.small)
for row = 0 to 2
period = array.get(periods, row)
maVal = ta.sma(close, period)
pos = close > maVal ? "上方 ▲" : "下方 ▼"
posColor = close > maVal ? color.green : color.red
table.cell(t, 0, row + 2, str.tostring(period) + "日",
text_color=color.white, text_size=size.small)
table.cell(t, 1, row + 2, str.tostring(math.round(maVal, 2)),
text_color=color.white, text_size=size.small)
table.cell(t, 2, row + 2, pos,
text_color=posColor, text_size=size.small)