import pandas as pd
import datetime
import tejapi
import os
import numpy as np
# set tej_key and base
os.environ['TEJAPI_KEY'] = "your key"
os.environ['TEJAPI_BASE'] = "https://api.tej.com.tw"
# set date
start='2023-01-01'
end='2023-10-24'
from logbook import Logger, StderrHandler, INFO
log_handler = StderrHandler(format_string='[{record.time:%Y-%m-%d %H:%M:%S.%f}]: ' +
'{record.level_name}: {record.func_name}: {record.message}',
level=INFO)
log_handler.push_application()
log = Logger('get_universe')
用來取得特定的股票池(使用此函數會消耗API流量)。
(start,
end=datetime.datetime.now().date().strftime('%Y-%m-%d'),
trading_calendar = get_calendar('TEJ_XTAI'),
**kwargs)
start(datetime or str)-Start date.
end(datetime or str, optional)-This defaults to the current date.
trading_calendar(TradingCalendar, optional)-設置交易日曆。預設是get_calendar('TEJ_XTAI')
。
available kwargs properties(至少需要指定一個,若沒指定則返回全樣本)
mkt(list or str)-市場別。
['TWSE', 'OTC']
。mkt_bd_c(list or str)-板塊別(中)。
['上市一般版', '上櫃一般版', '創新版']
。mkt_bd_e(list or str)-板塊別(英)。
['TSE', 'OTC', 'TIB']
。stktp_c(list or str)-證券種類(中)。
['指數', '普通股-海外', '普通股', '特別股', '封閉型基金', 'ETF', 'REIT', '國外ETF','開放式基金', '台灣存託憑證']
。'普通股-海外'
:僅包含F股或KY股。'普通股'
:不含F股或KY股。若同時想取得所有普通股,請使用stktp_c=['普通股-海外', '普通股']
。stktp_e(list or str)-證券種類(英)。
['Index', 'Common Stock-Foreign', 'Common Stock', 'Preferred Stock', 'Close End Fund', 'ETF', 'REIT', 'Foreign ETF', 'Open End Fund', 'TDR']
。'Common Stock-Foreign'
:僅包含F股或KY股。'Common Stock'
:不含F股或KY股。若同時想取得所有普通股,請使用stktp_e=['Common Stock-Foreign', 'Common Stock']
。main_ind_c(list or str)-主產業別(中)。(請輸入完整字串,例如:'M1100 水泥工業')
main_ind_e(list or str)-主產業別(英)。(請輸入完整字串,例如:'M1100 Cement')
sub_ind_c(list or str)-子產業別(中)。(請輸入完整字串,例如:'M1721 化學工業')
sub_ind_e(list or str)-子產業別(英)。(請輸入完整字串,例如:'M1721 Chemical')
idx_id(list or str)-指數名稱。(請輸入指數就好,後面的中文不需輸入,例如:'IX0001')
List of symbols.
list
main_ind_c
的特殊產業'金融與證券業'
:屬於以下4種產業的所有公司'一般產業'
:非以下4種產業的所有公司main_ind_e
的特殊產業'Financial and Securities Industry'
:屬於以下4種產業的所有公司'General Industry'
:非以下4種產業的所有公司
以 '普通股' 為例(不含F股或KY股):
'普通股' (stktp_c='普通股'):包含在證券交易所(mkt='TWSE')掛牌的普通股 與在櫃買中心(mkt='OTC')掛牌的普通股。
統整上述關係:
from zipline.sources.TEJ_Api_Data import get_universe
包含以下九個範例:
# 僅顯示前10筆
get_universe(start, end, mkt_bd_e=['TSE','OTC'])[0:10]
[2024-03-13 02:34:41.475876]: INFO: get_universe_TW: Filters:{'mkt_bd_e': ['TSE', 'OTC']}
Currently used TEJ API key call quota 37/9223372036854775807 (0.0%) Currently used TEJ API key data quota 110039/9223372036854775807 (0.0%)
['0050', '0051', '0052', '0053', '0055', '0056', '0057', '0061', '006201', '006203']
# 僅顯示前10筆
get_universe(start, end, mkt=['TWSE'])[0:10]
[2024-03-13 02:34:54.167604]: INFO: get_universe_TW: Filters:{'mkt': ['TWSE']}
Currently used TEJ API key call quota 38/9223372036854775807 (0.0%) Currently used TEJ API key data quota 565251/9223372036854775807 (0.0%)
['0050', '0051', '0052', '0053', '0055', '0056', '0057', '0061', '006203', '006204']
# 僅顯示前10筆
get_universe(start, end, mkt=['OTC'])[0:10]
[2024-03-13 02:35:25.138684]: INFO: get_universe_TW: Filters:{'mkt': ['OTC']}
Currently used TEJ API key call quota 39/9223372036854775807 (0.0%) Currently used TEJ API key data quota 837204/9223372036854775807 (0.0%)
['006201', '00679B', '00687B', '00694B', '00695B', '00696B', '00697B', '00718B', '00719B', '00720B']
case1與case2取出的證券不限於普通股,若僅想取出普通股(不含F股或KY股),則可以新增stktp_c=['普通股']
的條件。
以下兩種方式皆能取得曾經上市+上櫃的普通股(不含F股或KY股):
get_universe(start, end, mkt=['TWSE','OTC'], stktp_c=['普通股'])
get_universe(start, end, mkt_bd_e=['TSE','OTC','TIB'], mkt=['TWSE','OTC'], stktp_c=['普通股'])
若需要排除上市創新板(mkt_bd='TIB') 的普通股(不含F股或KY股),可使用:
get_universe(start, end, mkt_bd_e=['TSE','OTC'], mkt=['TWSE','OTC'], stktp_c=['普通股'])
# 僅顯示前10筆
get_universe(start, end, mkt=['TWSE','OTC'], stktp_c=['普通股'])[0:10]
[2024-03-13 02:35:34.084292]: INFO: get_universe_TW: Filters:{'mkt': ['TWSE', 'OTC'], 'stktp_c': ['普通股']}
Currently used TEJ API key call quota 40/9223372036854775807 (0.0%) Currently used TEJ API key data quota 1021182/9223372036854775807 (0.0%)
['1101', '1102', '1103', '1104', '1108', '1109', '1110', '1201', '1203', '1210']
get_universe(start, end, mkt_bd_e=['TSE','OTC','TIB'], mkt=['TWSE','OTC'], stktp_c=['普通股'])[0:10]
[2024-03-13 02:35:47.903112]: INFO: get_universe_TW: Filters:{'mkt_bd_e': ['TSE', 'OTC', 'TIB'], 'mkt': ['TWSE', 'OTC'], 'stktp_c': ['普通股']}
Currently used TEJ API key call quota 41/9223372036854775807 (0.0%) Currently used TEJ API key data quota 1342349/9223372036854775807 (0.0%)
['1101', '1102', '1103', '1104', '1108', '1109', '1110', '1201', '1203', '1210']
# 僅顯示前10筆
get_universe(start, end, sub_ind_c=['M2324 半導體業'])[0:10]
[2024-03-13 02:35:56.933903]: INFO: get_universe_TW: Filters:{'sub_ind_c': ['M2324 半導體業']}
Currently used TEJ API key call quota 42/9223372036854775807 (0.0%) Currently used TEJ API key data quota 1663516/9223372036854775807 (0.0%)
['2302', '2303', '2329', '2330', '2337', '2338', '2342', '2344', '2351', '2363']
# 僅顯示前10筆
get_universe(start, end, sub_ind_e=['M2324 Semiconductor',
'M2325 Computer and Peripheral Equipment'])[0:10]
[2024-03-13 02:36:01.704376]: INFO: get_universe_TW: Filters:{'sub_ind_e': ['M2324 Semiconductor', 'M2325 Computer and Peripheral Equipment']}
Currently used TEJ API key call quota 43/9223372036854775807 (0.0%) Currently used TEJ API key data quota 1679021/9223372036854775807 (0.0%)
['2301', '2302', '2303', '2305', '2324', '2329', '2330', '2331', '2337', '2338']
# 僅顯示前10筆
get_universe(start, end, stktp_c=['ETF', '普通股'], mkt=['TWSE'])[0:10]
[2024-03-13 02:36:09.262084]: INFO: get_universe_TW: Filters:{'stktp_c': ['ETF', '普通股'], 'mkt': ['TWSE']}
Currently used TEJ API key call quota 44/9223372036854775807 (0.0%) Currently used TEJ API key data quota 1705929/9223372036854775807 (0.0%)
['0050', '0051', '0052', '0053', '0055', '0056', '0057', '006203', '006204', '006208']
# 僅顯示前10筆
get_universe(start, end, idx_id='IX0002')[0:10]
[2024-03-13 02:36:14.450108]: INFO: get_universe_TW: Filters:{'idx_id': ['IX0002']}
Currently used TEJ API key call quota 45/9223372036854775807 (0.0%) Currently used TEJ API key data quota 1885914/9223372036854775807 (0.0%)
['1101', '1216', '1301', '1303', '1326', '1402', '1590', '1605', '2002', '2207']
# 僅顯示前10筆
get_universe(start, end, main_ind_c='M1100 水泥工業', idx_id='IX0006')[0:10]
[2024-03-13 02:36:17.542984]: INFO: get_universe_TW: Filters:{'main_ind_c': 'M1100 水泥工業', 'idx_id': ['IX0006']}
Currently used TEJ API key call quota 45/9223372036854775807 (0.0%) Currently used TEJ API key data quota 1885914/9223372036854775807 (0.0%)
['1102']
# 僅顯示前10筆
get_universe(start, end, main_ind_c=['一般產業']
, stktp_c='普通股')[0:10]
[2024-03-13 02:36:23.861031]: INFO: get_universe_TW: Filters:{'main_ind_c': ['一般產業'], 'stktp_c': '普通股'}
Currently used TEJ API key call quota 48/9223372036854775807 (0.0%) Currently used TEJ API key data quota 1887612/9223372036854775807 (0.0%)
['1101', '1102', '1103', '1104', '1108', '1109', '1110', '1201', '1203', '1210']