Exchange_calendars 是一個用于查詢證券交易日歷的 Python 庫。開箱即用,內含世界50+個交易所的交易日歷,包括中國市場和港股市場,非常方便。
同時,如果你找到了Exchange_calendars 上沒有的市場的交易日歷,可以向他們提交PR,創建一個新的交易所日歷:
https://github.com/gerrymanoim/exchange_calendars
注意,本模塊最低支持的Python版本為3.8.
1.準備
開始之前,你要確保Python和pip已經成功安裝在電腦上,如果沒有,可以訪問這篇文章:超詳細Python安裝指南 進行安裝。
**(可選1) **如果你用Python的目的是數據分析,可以直接安裝Anaconda:Python數據分析與挖掘好幫手—Anaconda,它內置了Python和pip.
**(可選2) **此外,推薦大家用VSCode編輯器,它有許多的優點:Python 編程的最好搭檔—VSCode 詳細指南。
請選擇以下任一種方式輸入命令安裝依賴 :
- Windows 環境 打開 Cmd (開始-運行-CMD)。
- MacOS 環境 打開 Terminal (command+空格輸入Terminal)。
- 如果你用的是 VSCode編輯器 或 Pycharm,可以直接使用界面下方的Terminal.
pip install exchange_calendars
2.基本使用
獲取可用日歷的列表:
import exchange_calendars as xcals
print(xcals.get_calendar_names(include_aliases=False))
結果如下:
['24/5', '24/7', 'AIXK', 'ASEX', 'BVMF', 'CMES', 'IEPA', 'XAMS', 'XASX', 'XBKK', 'XBOG', 'XBOM', 'XBRU', 'XBSE', 'XBUD', 'XBUE', 'XCBF', 'XCSE', 'XDUB', 'XETR', 'XFRA', 'XHEL', 'XHKG', 'XICE', 'XIDX', 'XIST', 'XJSE', 'XKAR', 'XKLS', 'XKRX', 'XLIM', 'XLIS', 'XLON', 'XMAD', 'XMEX', 'XMIL', 'XMOS', 'XNYS', 'XNZE', 'XOSL', 'XPAR', 'XPHS', 'XPRA', 'XSES', 'XSGO', 'XSHG', 'XSTO', 'XSWX', 'XTAE', 'XTAI', 'XTKS', 'XTSE', 'XWAR', 'XWBO', 'us_futures']
其中,XSHG就是中國的滬市、XHKG就是港股。
獲取滬市日歷:
import exchange_calendars as xcals
xshg = xcals.get_calendar("XSHG")
查詢某個區間日程市場是否有開盤:
import exchange_calendars as xcals
xshg = xcals.get_calendar("XSHG")
print(xshg.schedule.loc["2021-12-29":"2022-08-12"])
效果如下:
open break_start break_end close
2021-12-29 2021-12-29 01:30:00+00:00 2021-12-29 03:30:00+00:00 2021-12-29 05:00:00+00:00 2021-12-29 07:00:00+00:00
2021-12-30 2021-12-30 01:30:00+00:00 2021-12-30 03:30:00+00:00 2021-12-30 05:00:00+00:00 2021-12-30 07:00:00+00:00
2021-12-31 2021-12-31 01:30:00+00:00 2021-12-31 03:30:00+00:00 2021-12-31 05:00:00+00:00 2021-12-31 07:00:00+00:00
2022-01-04 2022-01-04 01:30:00+00:00 2022-01-04 03:30:00+00:00 2022-01-04 05:00:00+00:00 2022-01-04 07:00:00+00:00
2022-01-05 2022-01-05 01:30:00+00:00 2022-01-05 03:30:00+00:00 2022-01-05 05:00:00+00:00 2022-01-05 07:00:00+00:00
... ... ... ... ...
2022-08-08 2022-08-08 01:30:00+00:00 2022-08-08 03:30:00+00:00 2022-08-08 05:00:00+00:00 2022-08-08 07:00:00+00:00
2022-08-09 2022-08-09 01:30:00+00:00 2022-08-09 03:30:00+00:00 2022-08-09 05:00:00+00:00 2022-08-09 07:00:00+00:00
2022-08-10 2022-08-10 01:30:00+00:00 2022-08-10 03:30:00+00:00 2022-08-10 05:00:00+00:00 2022-08-10 07:00:00+00:00
2022-08-11 2022-08-11 01:30:00+00:00 2022-08-11 03:30:00+00:00 2022-08-11 05:00:00+00:00 2022-08-11 07:00:00+00:00
2022-08-12 2022-08-12 01:30:00+00:00 2022-08-12 03:30:00+00:00 2022-08-12 05:00:00+00:00 2022-08-12 07:00:00+00:00
[151 rows x 4 columns]
可見,他直接返回了這個區間中所有開盤的日期作為index,并附帶了開盤和收盤的UTC時間(需要+8)。
如果你只需要開市的日期,可以這樣:
# 公眾號:二七阿爾量化
import exchange_calendars as xcals
xshg = xcals.get_calendar("XSHG")
xshg_range = xshg.schedule.loc["2021-12-29":"2022-08-12"]
print(xshg_range.index.strftime("%Y-%m-%d").tolist())
效果如下:
['2021-12-29', '2021-12-30', '2021-12-31', '2022-01-04', '2022-01-05', '2022-01-06', '2022-01-07', '2022-01-10', '2022-01-11', '2022-01-12', '2022-01-13', '2022-01-14', '2022-01-17', '2022-01-18', '2022-01-19', '2022-01-20', '2022-01-21', '2022-01-24', '2022-01-25', '2022-01-26', '2022-01-27', '2022-01-28', '2022-02-07', '2022-02-08', '2022-02-09', '2022-02-10', '2022-02-11', '2022-02-14', '2022-02-15', '2022-02-16', '2022-02-17', '2022-02-18', '2022-02-21', '2022-02-22', '2022-02-23', '2022-02-24', '2022-02-25', '2022-02-28', '2022-03-01', '2022-03-02', '2022-03-03', '2022-03-04', '2022-03-07', '2022-03-08', '2022-03-09', '2022-03-10', '2022-03-11', '2022-03-14', '2022-03-15', '2022-03-16', '2022-03-17', '2022-03-18', '2022-03-21', '2022-03-22', '2022-03-23', '2022-03-24', '2022-03-25', '2022-03-28', '2022-03-29', '2022-03-30', '2022-03-31', '2022-04-01', '2022-04-06', '2022-04-07', '2022-04-08', '2022-04-11', '2022-04-12', '2022-04-13', '2022-04-14', '2022-04-15', '2022-04-18', '2022-04-19', '2022-04-20', '2022-04-21', '2022-04-22', '2022-04-25', '2022-04-26', '2022-04-27', '2022-04-28', '2022-04-29', '2022-05-05', '2022-05-06', '2022-05-09', '2022-05-10', '2022-05-11', '2022-05-12', '2022-05-13', '2022-05-16', '2022-05-17', '2022-05-18', '2022-05-19', '2022-05-20', '2022-05-23', '2022-05-24', '2022-05-25', '2022-05-26', '2022-05-27', '2022-05-30', '2022-05-31', '2022-06-01', '2022-06-02', '2022-06-06', '2022-06-07', '2022-06-08', '2022-06-09', '2022-06-10', '2022-06-13', '2022-06-14', '2022-06-15', '2022-06-16', '2022-06-17', '2022-06-20', '2022-06-21', '2022-06-22', '2022-06-23', '2022-06-24', '2022-06-27', '2022-06-28', '2022-06-29', '2022-06-30', '2022-07-01', '2022-07-04', '2022-07-05', '2022-07-06', '2022-07-07', '2022-07-08', '2022-07-11', '2022-07-12', '2022-07-13', '2022-07-14', '2022-07-15', '2022-07-18', '2022-07-19', '2022-07-20', '2022-07-21', '2022-07-22', '2022-07-25', '2022-07-26', '2022-07-27', '2022-07-28', '2022-07-29', '2022-08-01', '2022-08-02', '2022-08-03', '2022-08-04', '2022-08-05', '2022-08-08', '2022-08-09', '2022-08-10', '2022-08-11', '2022-08-12']
3.高級使用
判斷某一天是否為交易日:
# 公眾號:二七阿爾量化
import exchange_calendars as xcals
xshg = xcals.get_calendar("XSHG")
print(xshg.is_session("2022-12-30"))
# True
獲取某一天后的N個交易日:
# 公眾號:二七阿爾量化
import exchange_calendars as xcals
xshg = xcals.get_calendar("XSHG")
print(xshg.sessions_window("2022-12-30", 7))
# DatetimeIndex(['2022-12-30', '2023-01-03', '2023-01-04', '2023-01-05',
# '2023-01-06', '2023-01-09', '2023-01-10'],
# dtype='datetime64[ns]', freq='C')
獲取某一天后的下一個交易日:
# 公眾號:二七阿爾量化
import exchange_calendars as xcals
xshg = xcals.get_calendar("XSHG")
print(xshg.date_to_session("2022-01-01", direction="next"))
# 2022-01-04 00:00:00
獲取某一天后的上一個交易日:
# 公眾號:二七阿爾量化
import exchange_calendars as xcals
xshg = xcals.get_calendar("XSHG")
print(xshg.date_to_session("2022-01-01", direction="previous"))
# 2021-12-31 00:00:00
獲取分鐘級交易區間:
import exchange_calendars as xcals
xshg = xcals.get_calendar("XSHG")
print(xshg.session_minutes("2022-01-04"))
# DatetimeIndex(['2022-01-04 01:30:00+00:00', '2022-01-04 01:31:00+00:00',
# '2022-01-04 01:32:00+00:00', '2022-01-04 01:33:00+00:00',
# '2022-01-04 01:34:00+00:00', '2022-01-04 01:35:00+00:00',
# '2022-01-04 01:36:00+00:00', '2022-01-04 01:37:00+00:00',
# '2022-01-04 01:38:00+00:00', '2022-01-04 01:39:00+00:00',
# ...
# '2022-01-04 06:50:00+00:00', '2022-01-04 06:51:00+00:00',
# '2022-01-04 06:52:00+00:00', '2022-01-04 06:53:00+00:00',
# '2022-01-04 06:54:00+00:00', '2022-01-04 06:55:00+00:00',
# '2022-01-04 06:56:00+00:00', '2022-01-04 06:57:00+00:00',
# '2022-01-04 06:58:00+00:00', '2022-01-04 06:59:00+00:00'],
# dtype='datetime64[ns, UTC]', length=240, freq=None)
交易時間按指定數字分割:
import exchange_calendars as xcals
xshg = xcals.get_calendar("XSHG")
print(xshg.trading_index(
"2021-12-30", "2021-12-31", period="30T", force=True
))
# IntervalIndex([[2021-12-30 01:30:00, 2021-12-30 02:00:00), [2021-12-30 02:00:00, 2021-12-30 02:30:00), [2021-12-30 02:30:00, 2021-12-30 03:00:00), [2021-12-30 03:00:00, 2021-12-30 03:30:00), [2021-12-30 05:00:00, 2021-12-30 05:30:00) ... [2021-12-31 03:00:00, 2021-12-31 03:30:00), [2021-12-31 05:00:00, 2021-12-31 05:30:00), [2021-12-31 05:30:00, 2021-12-31 06:00:00), [2021-12-31 06:00:00, 2021-12-31 06:30:00), [2021-12-31 06:30:00, 2021-12-31 07:00:00)],
# closed='left',
# dtype='interval[datetime64[ns, UTC]]')
-
WINDOWS
+關注
關注
3文章
3541瀏覽量
88626 -
編輯器
+關注
關注
1文章
805瀏覽量
31163 -
數據分析
+關注
關注
2文章
1445瀏覽量
34050 -
python
+關注
關注
56文章
4792瀏覽量
84628
發布評論請先 登錄
相關推薦
評論