Chuyển đến nội dung chính
ATK Pine Script®

Tham chiếu các hàm table.*

Tham chiếu cho table.new, table.cell, và ctx.table trong ATK PyneScript V6 — widget table gốc và table dashboard được quản lý.

Tổng quan table.*#

Bề mặtDùng khiGiai đoạn khuyến nghị
table.new(...)Cấu trúc table là tĩnh và có thể khai báo một lần khi import.Phạm vi module hoặc giai đoạn render cho widget đơn giản.
table.cell(...)Bạn muốn điền hoặc làm mới nội dung ô trên một table handle được quản lý.Giai đoạn render khi giá trị phụ thuộc vào lát cắt frame hiện tại.
ctx.table.new(...)Bạn muốn table được tạo tại runtime có nguồn gốc từ lát cắt frame hiện tại.Chỉ trong build_visuals(...).

table.new()#

Tạo widget table được quản lý tại vị trí cố định. Khai báo tại phạm vi module cho table tĩnh.

# Parameters: position, columns, rows, key, title
dashboard = table.new("top_right", 2, 3, key="dashboard", title="Summary")
table.cell(dashboard, 0, 0, "Metric")
table.cell(dashboard, 1, 0, "Value")

Các giá trị vị trí#

Giá trịVị trí
"top_left"Góc trên trái
"top_center"Giữa trên
"top_right"Góc trên phải
"middle_left"Giữa trái
"middle_center"Giữa chart
"middle_right"Giữa phải
"bottom_left"Góc dưới trái
"bottom_center"Giữa dưới
"bottom_right"Góc dưới phải

table.cell()#

Điền hoặc cập nhật một ô đơn trong table handle được quản lý.

table.cell(
    handle,           # table handle returned by table.new()
    column,           # 0-indexed column number
    row,              # 0-indexed row number
    text,             # cell content string
    text_color=None,  # optional hex color string
    bgcolor=None,     # optional background hex color string
)

ctx.table.new()#

Tạo table runtime từ bên trong build_visuals. Dùng khi giá trị ô phụ thuộc vào lát cắt frame hiện tại.

def build_visuals(frame, params=None, ctx=None):
    runtime_table = ctx.table.new("bottom_left", 2, 2, key="runtime_dashboard", title="Runtime")
    ctx.table.cell(runtime_table, 0, 0, "Bars")
    ctx.table.cell(runtime_table, 1, 0, str(len(frame)))
    return runtime_table

Mẫu table gốc đầy đủ#

from source import indicator, table


indicator("Native Table Demo", overlay=True, max_bars_back=200)

dashboard = table.new("top_right", 2, 3, key="dashboard", title="Summary")
table.cell(dashboard, 0, 0, "Metric")
table.cell(dashboard, 1, 0, "Value")


def build_visuals(frame, params=None, ctx=None):
    runtime_table = ctx.table.new("bottom_left", 2, 2, key="runtime_dashboard", title="Runtime")
    ctx.table.cell(runtime_table, 0, 0, "Bars")
    ctx.table.cell(runtime_table, 1, 0, str(len(frame)))
    return runtime_table

Bridge ctx.atk.table()#

Để có table dashboard gốc ATK phong phú hơn, dùng ctx.atk.table từ bên trong build_visuals. Xây dựng payload trong frame builder và render tại đây.

def build_indicator_frame(df, params=None):
    frame = df.copy().reset_index(drop=True)
    if not frame.empty:
        last = frame.iloc[-1]
        frame.attrs["visual_table_payload"] = {
            "position": "top-left",
            "title": "Signal Summary",
            "columns": 2,
            "rows": 3,
            "cells": [
                {"column": 0, "row": 0, "text": "Metric", "text_color": "#ffffff", "bgcolor": "#0f172a"},
                {"column": 1, "row": 0, "text": "Value",  "text_color": "#ffffff", "bgcolor": "#0f172a"},
                {"column": 0, "row": 1, "text": "Close"},
                {"column": 1, "row": 1, "text": f"{float(last['close']):.2f}", "text_color": "#00c853"},
            ],
        }
    return frame


def build_visuals(frame, params=None, ctx=None):
    payload = dict(frame.attrs.get("visual_table_payload") or {})
    if not payload:
        return None
    return ctx.atk.table(key="annotated_dashboard", **payload)

Dùng table.new / table.cell cho widget tĩnh đơn giản khai báo tại phạm vi module. Chuyển sang ctx.atk.table khi table cần cột động, header được tạo kiểu, hoặc là phần của payload bridge ATK lớn hơn.

Xem thêm: