跳轉到

Block.get_table 積木參數與合法值

所屬章節:積木 Block 以 2026-09-21 實際執行結果為準

Overview

回傳每個積木的 python 編碼 id 與可填參數的合法值清單。con 只接受這裡列出的值。

api.Block.get_table_tw()
api.Block.get_table_us()

沒有參數。回傳 dict:

{積木名稱: {'id': 編碼, 參數名: [合法值, ...]}}

id 就是條件 dict 裡的 'id',查表和寫條件可以一次完成:

t = api.Block.get_table_tw()
t['dataA<>dataB+C']['id']      # 'a1'
api.Block.con_tw(id='a1', left='日_K.Volume', operator='>', const=3000)

其餘的 key 都是參數名,值為合法值清單(通用積木為自由值,清單為空)。

台股 416 筆(含 11 筆以 a1/a3 寫成的具名積木)、美股 260 筆。資料來源是 block_spec.json(由 bulid_spec/build_spec.py 從「台股積木API.xlsx / 美股積木API.xlsx」離線產生,再複製進 snowyowl/blocks/)。

Manual Sample Input/Output

Example 1:查單一積木能填什麼

input

api.Block.get_table_tw()['今日收盤價<>開高低P%']

output

{'id': 'o1',
 'operator': ['<', '>'],
 'ohlc_field': ['adj_Open', 'adj_High', 'adj_Low'],
 'percent': [1, 2, 3, 5, 7, 9]}

Example 2:前四筆

input

for k, v in list(api.Block.get_table_tw().items())[:4]:
    print(f"{k!r}: {v}")

output

'今日收盤價<>開高低P%': {'id': 'o1', 'operator': ['<', '>'], 'ohlc_field': ['adj_Open', 'adj_High', 'adj_Low'], 'percent': [1, 2, 3, 5, 7, 9]}
'今日收盤價距今日最低價 <>-P%': {'id': 'o10', 'percent': [3, 5, 7, 9]}
'今日收盤價(開低高)與近D日內最高價<P%': {'id': 'o11', 'close_field': ['adj_Open', 'adj_High', 'adj_Low', 'adj_Close'], 'period': [5, 10, 20], 'percent': [3, 5, 8, 10, 15, 20]}
'今日收盤價(開高低)與近D日內最低價<-P%': {'id': 'o12', 'close_field': ['adj_Open', 'adj_High', 'adj_Low', 'adj_Close'], 'period': [5, 10, 20], 'percent': [3, 5, 8, 10, 15, 20]}

無參數的積木只有 id:

input

api.Block.get_table_tw()['月KD 黃金交叉']

output

{'id': 't10'}

Example 3:美股同名積木的參數名稱不同

input

api.Block.get_table_us()['今日收盤價<>開高低P%']

output

{'id': 'o1',
 'operator': ['<', '>'],
 'field1': ['Open', 'High', 'Low'],
 'percent': [1, 2, 3, 5, 7, 9]}

台股叫 ohlc_field、美股叫 field1,值也不同(台股是還原價 adj_*, 美股是未還原 Open/High/Low)。同名積木不能假設參數一致,兩邊都要各查一次。


Example 4:搜尋積木

get_table 的 key 就是積木名稱,直接用字串比對即可:

input

[n for n in api.Block.get_table_tw() if '均線' in n][:5]

output

['D日均線糾結<P%',
 '均線連三日持續向上(下)',
 'D日均線<>T日均線',
 'D日均線<>T日均線<>H日均線',
 '今日收盤價<>D日(周月)均線']

注意:規格固定值不在這張表裡

有 33 個積木的部分參數被 Excel 規格釘死(例如 今日收盤價<>開高低P% 的 period 固定為 1)。這些值不是選項、無從挑選,所以不會出現在 get_table 的 輸出中。

自己手寫條件 dict 時漏掉它們,condition class 會退回自己 __init__ 的預設值, 而那未必等於規格值——實測有 8 個積木兩者不同,結果會靜靜地跑掉:

# 錯:漏掉規格固定值 period=1,class 預設是 100
api.Block.screener_tw([{'id': 'o1', 'operator': '>', 'percent': 3}])   # 1779 檔

# 對:交給 con_tw 補齊
api.Block.screener_tw([api.Block.con_tw(id='o1', percent=3)])          # 595 檔

所以手寫 dict 建議只用在通用積木(a1/a2/a3,沒有固定值), 其他積木一律走 con——它會自動補上固定值,也會擋掉打錯的參數名。

完整清單(含每個積木的邏輯說明、預設值)見 台股積木完整參考、美股積木完整參考。