标签归档:calendar

Exchange_calendars 一个能获取全世界所有市场交易日历的Python模块

Exchange_calendars 是一个用于查询证券交易日历的 Python 库。开箱即用,内含世界50+个交易所的交易日历,包括中国市场和港股市场,非常方便。

同时,如果你找到了Exchange_calendars 上没有的市场的交易日历,可以向他们提交PR,创建一个新的交易所日历

注意,本模块最低支持的Python版本为3.8.

1.准备

开始之前,你要确保Python和pip已经成功安装在电脑上,如果没有,请访问这篇文章:超详细Python安装指南 进行安装。

(可选1) 如果你用Python的目的是数据分析,可以直接安装Anaconda:Python数据分析与挖掘好帮手—Anaconda,它内置了Python和pip.

(可选2) 此外,推荐大家用VSCode编辑器来编写小型Python项目:Python 编程的最好搭档—VSCode 详细指南

Windows环境下打开Cmd(开始—运行—CMD),苹果系统环境下请打开Terminal(command+空格输入Terminal),输入命令安装依赖:

pip install exchange_calendars

2.Exchange_calendars 基本使用

获取可用日历的列表:

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]]')

我们的文章到此就结束啦,如果你喜欢今天的 Python 教程,请持续关注Python实用宝典。

有任何问题,可以在公众号后台回复:加群,回答相应验证信息,进入互助群询问。

原创不易,希望你能在下面点个赞和在看支持我继续创作,谢谢!

给作者打赏,选择打赏金额
¥1¥5¥10¥20¥50¥100¥200 自定义

​Python实用宝典 ( pythondict.com )
不只是一个宝典
欢迎关注公众号:Python实用宝典