finlab.data
finlab.data.get(dataset, save_to_storage=True)
下載歷史資料
請至歷史資料目錄 來獲得所有歷史資料的名稱,即可使用此函式來獲取歷史資料。
假設 save_to_storage
為 True
則,程式會自動在本地複製一份,以避免重複下載大量數據。
Parameters:
Name | Type | Description | Default |
---|---|---|---|
dataset |
str
|
The name of dataset. |
required |
save_to_storage |
bool
|
Whether to save the dataset to storage for later use. |
True
|
Returns:
Type | Description |
---|---|
pd.DataFrame
|
financial data |
Examples:
欲下載所有上市上櫃之收盤價歷史資料,只需要使用此函式即可:
date | 0015 | 0050 | 0051 | 0052 | 0053 |
---|---|---|---|---|---|
2007-04-23 | 9.54 | 57.85 | 32.83 | 38.4 | nan |
2007-04-24 | 9.54 | 58.1 | 32.99 | 38.65 | nan |
2007-04-25 | 9.52 | 57.6 | 32.8 | 38.59 | nan |
2007-04-26 | 9.59 | 57.7 | 32.8 | 38.6 | nan |
2007-04-27 | 9.55 | 57.5 | 32.72 | 38.4 | nan |
finlab.data.indicator(indname, adjust_price=False, resample='D', market=TWMarketInfo(), kwargs)
支援 Talib 和 pandas_ta 上百種技術指標,計算 2000 檔股票、10年的所有資訊。
在使用這個函式前,需要安裝計算技術指標的 Packages
Parameters:
Name | Type | Description | Default |
---|---|---|---|
indname |
str
|
指標名稱, 以 TA-Lib 舉例,例如 SMA, STOCH, RSI 等,可以參考 talib 文件。 以 Pandas-ta 舉例,例如 supertrend, ssf 等,可以參考 Pandas-ta 文件。 |
required |
adjust_price |
bool
|
是否使用還原股價計算。 |
False
|
resample |
str
|
技術指標價格週期,ex: |
'D'
|
**kwargs |
dict
|
技術指標的參數設定,TA-Lib 中的 RSI 為例,調整項為計算週期 |
{}
|
建議使用者可以先參考以下範例,並且搭配 talib官方文件,就可以掌握製作技術指標的方法了。
finlab.data.universe(market='ALL', category='ALL')
當呼叫 data.get
或是 data.indicator
時,返回產業相關類股。
Parameters:
Name | Type | Description | Default |
---|---|---|---|
market |
str
|
Universe market type. ex: |
'ALL'
|
category |
str
|
Stock categories, can be either a string or a list. ex: |
'ALL'
|
Examples:
想要當鋼鐵人、航海王,可以用以下方法將這些類股一次選出來
date | 2002 | 2006 | .. | 2008 | 2009 |
---|---|---|---|---|---|
2007-04-23 | 39.65 | 38.3 | .. | 7.8 | 17.55 |
2007-04-24 | 39.85 | 38.85 | .. | 8.34 | 17.5 |
2007-04-25 | 39.25 | 38.1 | .. | 8.25 | 17.3 |
2007-04-26 | 39 | 37.85 | .. | 8.2 | 17.3 |
2007-04-27 | 38.2 | 37.2 | .. | 7.88 | 16.6 |
finlab.data.set_storage(storage)
設定本地端儲存歷史資料的方式
假設使用 data.get
獲取歷史資料則,在預設情況下,程式會自動在本地複製一份,以避免重複下載大量數據。
storage 就是用來儲存歷史資料的接口。我們提供兩種 storage
接口,分別是 finlab.data.CacheStorage
(預設) 以及
finlab.data.FileStorage
。前者是直接存在記憶體中,後者是存在檔案中。詳情請參考 CacheStorage
和 FileStorage
來獲得更詳細的資訊。
在預設情況下,程式會自動使用 finlab.data.CacheStorage
來存取重複索取之歷史資料。
Parameters:
Name | Type | Description | Default |
---|---|---|---|
storage |
data.Storage
|
The interface of storage |
required |
Examples:
欲切換成以檔案方式儲存,可以用以下之方式:
可以在本地端的 ./finlab_db/price:收盤價.pickle
中,看到下載的資料,
可以使用 pickle
調閱歷史資料:
finlab.data.CacheStorage()
finlab.data.FileStorage(path='finlab_db')
finlab.data.get_strategies(api_token=None)
取得已上傳量化平台的策略回傳資料。
可取得自己策略儀表板上的數據,例如每個策略的報酬率曲線、報酬率統計、夏普率、近期部位、近期換股日..., 這些數據可以用來進行多策略彙整的應用喔!
Parameters:
Name | Type | Description | Default |
---|---|---|---|
api_token |
str
|
若未帶入finlab模組的api_token,會自動跳出GUI頁面, 複製網頁內的api_token貼至輸入欄位即可。 |
None
|
Returns:
Type | Description |
---|---|
dict
|
strategies data |
Response detail
{
strategy1:{
'asset_type': '',
'drawdown_details': {
'2015-06-04': {
'End': '2015-11-03',
'Length': 152,
'drawdown': -0.19879090089478024
},
...
},
'fee_ratio': 0.000475,
'last_trading_date': '2022-06-10',
'last_updated': 'Sun, 03 Jul 2022 12:02:27 GMT',
'ndays_return': {
'1': -0.01132480035770611,
'10': -0.0014737286933147464,
'20': -0.06658015749110646,
'5': -0.002292995729485159,
'60': -0.010108700314771735
},
'next_trading_date': '2022-06-10',
'positions': {
'1413 宏洲': {
'entry_date': '2022-05-10',
'entry_price': 10.05,
'exit_date': '',
'next_weight': 0.1,
'return': -0.010945273631840613,
'status': '買進',
'weight': 0.1479332345384493
},
'last_updated': 'Sun, 03 Jul 2022 12:02:27 GMT',
'next_trading_date': '2022-06-10',
'trade_at': 'open',
'update_date': '2022-06-10'
},
'return_table': {
'2014': {
'Apr': 0.0,
'Aug': 0.06315180932606546,
'Dec': 0.0537589857541485,
'Feb': 0.0,
'Jan': 0.0,
'Jul': 0.02937490104459939,
'Jun': 0.01367930162104769,
'Mar': 0.0,
'May': 0.0,
'Nov': -0.0014734320286596825,
'Oct': -0.045082529665408266,
'Sep': 0.04630906972509852,
'YTD': 0.16626214846456966
},
...
},
'returns': {
'time': [
'2014-06-10',
'2014-06-11',
'2014-06-12',
...
],
'value': [
100,
99.9,
100.2,
...
]
},
'stats': {
'avg_down_month': -0.03304015302646822,
'avg_drawdown': -0.0238021414698247,
'avg_drawdown_days': 19.77952755905512,
'avg_up_month': 0.05293384465715908,
'cagr': 0.33236021285588846,
'calmar': 1.65261094975066,
'daily_kurt': 4.008888367138843,
'daily_mean': 0.3090784769257415,
'daily_sharpe': 1.747909002374217,
'daily_skew': -0.6966018726321078,
'daily_sortino': 2.8300677082214034,
...
},
'tax_ratio': 0.003,
'trade_at': 'open',
'update_date': '2022-06-10'
},
strategy2:{...},
...}
finlab.data.search(keyword=None, display_info=['name', 'description', 'type'])
查詢與取得資料庫目錄資訊。
查詢FinLab資料庫目錄中的資料表名稱或項目欄位名稱是否包含關鍵字,方便搜尋資料庫是否存在目標資料。
Parameters:
Name | Type | Description | Default |
---|---|---|---|
keyword |
str
|
欲查詢關鍵字。 |
None
|
display_info |
list
|
指定顯示的資訊項目,預設只顯示「資料表英文代稱、資料表的中文描述、資料表欄位名稱」,若設定為None,則顯示全部資訊。 |
['name', 'description', 'type']
|
Returns:
Type | Description |
---|---|
list
|
database information |
Response detail
[{'name': 'financial_statement',
'description': '財報',
'type': {'存貨': 'float-資產負債表(仟元)', '存貨_增加_減少': 'float-單季現金流量表(仟元)'}},
{'name': 'fundamental_features',
'description': '財務指標',
'type': {'存貨週轉率': 'float-(%)'}},
{'name': 'tw_total_pmi',
'description': '台灣製造業採購經理人指數',
'type': {'存貨': 'float', '客戶存貨': 'float'}},...]
Examples: