一聚教程网:一个值得你收藏的教程网站

最新下载

热门教程

pandas_market_calendars:实践指南

时间:2026-09-12 15:38:01 编辑:袖梨 来源:一聚教程网

在项目中评估pandas_market_calendars,可以先看清用途边界:与 pandas 一起用于交易应用程序的交换日历。把它放到市场与研究流程中,最容易暴露的是数据时效、指标口径和风险解释容易混在一起,不能只看演示是否顺利。我建议用一只熟悉标的回放一段历史区间,重点记录数据来源、计算口径、风险提示和结果可追溯性,再与现有方案比较。整体来看,它适合需要辅助研究而非自动替代决策的使用者拿来做对照测试,最终决定仍应回到真实结果和维护状态。

pandas_market_calendars

与 pandas 一起用于交易应用程序的市场日历。

.. 图片:: https://badge.fury.io/py/pandas-market-calendars.svg :target: https://badge.fury.io/py/pandas-market-calendars

.. 图片:: https://readthedocs.org/projects/pandas-market-calendars/badge/?version=latest :目标:http://pandas-market-calendars.readthedocs.io/en/latest/?badge=latest :alt: 文档状态

.. 图片:: https://coveralls.io/repos/github/rsheftel/pandas_market_calendars/badge.svg?branch=master :target: https://coveralls.io/github/rsheftel/pandas_market_calendars?branch=master

文档 http://pandas-market-calendars.readthedocs.io/en/latest/

概述 Pandas 包广泛应用于领域,特别是用于时间序列分析。它包括优秀的 生成日期序列的功能和自定义假日日历的功能,但作为显式 设计选择它不包括特定交易所或 OTC 市场的实际假期日历。

pandas_market_calendars 包希望通过假期、晚开和早关日历来填补这一角色 对于特定的交易所和 OTC 约定。 pandas_market_calendars 还添加了几个函数来操作 市场日历并包含 date_range 函数来创建仅包含日期时间的 pandas DatetimeIndex 当市场开放时。此外,该包还包含用于未来交换的产品特定日历, 根据产品类型,有不同的市场开市、休市、休息和假期。

该软件包提供对全球和市场 50 多个独特交易日历的访问。

日历数据和更新

Calendars and their rules are shipped as package code. pandas_market_calendars does not request market hours from a
server at runtime. To receive corrected or updated market hours, install a newer package release or update the source
code. Calendars mirrored from ``exchange_calendars`` likewise use the version installed in the local Python environment,
not a live data feed.

This package is a fork of the Zipline package from Quantopian and extracts just the relevant parts. All credit for
their excellent work to Quantopian.

Major Releases

从 v1.0 开始,此包仅适用于 Python3。这与 Pandas 放弃对 Python2 的支持是一致的。

自 v1.4 起,该套餐现在具有交易日休息的概念。例如,这可以容纳亚洲人 有午休时间的市场,或 24 小时开放并在一天中进行交易处理的市场。

从 v2.0 开始,此软件包提供了 exchange_calendars <https://github.com/gerrymanoim/exchange_calendars>_ 中所有日历的镜像 包,它本身是原始 Trading_calendars 包的现在维护的分支。这会添加 50 多个日历。

从 v3.0 开始,函数 date_range() 更加完整和一致,有关该主题的更多讨论请参阅 PR #142 和 Issue #138。

从 v4.0 开始,此包提供了向日历添加中断的框架。这些也可以添加到日程表中并使用查看 新的 Interruptions_df 属性。完整的更改列表可以在 PR #210 中找到。

从 v5.0 开始,该软件包使用新的 zoneinfo 标准来划分时区,并贬低并删除了 pytz。最低 python 版本现在是 3.9

源位置

Hosted on GitHub: https://github.com/rsheftel/pandas_market_calendars

Installation

pip install pandas_market_calendars

Arch Linux 软件包可在此处获取:https://aur.archlinux.org/packages/python-pandas_market_calendars/

日历 available calendars <https://pandas-market-calendars.readthedocs.io/en/latest/calendars.html>_的列表

快速入门 .. 代码:: python

import pandas_market_calendars as mcal
# Create a calendar
nyse = mcal.get_calendar('NYSE')

# Show available calendars
print(mcal.get_calendar_names())

.. 代码:: python

early = nyse.schedule(start_date='2012-07-01', end_date='2012-07-10')
early

.. 解析文字::

                  market_open             market_close
=========== ========================= =========================
 2012-07-02 2012-07-02 13:30:00+00:00 2012-07-02 20:00:00+00:00
 2012-07-03 2012-07-03 13:30:00+00:00 2012-07-03 17:00:00+00:00
 2012-07-05 2012-07-05 13:30:00+00:00 2012-07-05 20:00:00+00:00
 2012-07-06 2012-07-06 13:30:00+00:00 2012-07-06 20:00:00+00:00
 2012-07-09 2012-07-09 13:30:00+00:00 2012-07-09 20:00:00+00:00
 2012-07-10 2012-07-10 13:30:00+00:00 2012-07-10 20:00:00+00:00

.. 代码:: python

mcal.date_range(early, frequency='1D')

.. 解析文字::

DatetimeIndex(['2012-07-02 20:00:00+00:00', '2012-07-03 17:00:00+00:00',
               '2012-07-05 20:00:00+00:00', '2012-07-06 20:00:00+00:00',
               '2012-07-09 20:00:00+00:00', '2012-07-10 20:00:00+00:00'],
              dtype='datetime64[ns, UTC]', freq=None)

.. 代码:: python

mcal.date_range(early, frequency='1H')

.. 解析文字::

DatetimeIndex(['2012-07-02 14:30:00+00:00', '2012-07-02 15:30:00+00:00',
               '2012-07-02 16:30:00+00:00', '2012-07-02 17:30:00+00:00',
               '2012-07-02 18:30:00+00:00', '2012-07-02 19:30:00+00:00',
               '2012-07-02 20:00:00+00:00', '2012-07-03 14:30:00+00:00',
               '2012-07-03 15:30:00+00:00', '2012-07-03 16:30:00+00:00',
               '2012-07-03 17:00:00+00:00', '2012-07-05 14:30:00+00:00',
               '2012-07-05 15:30:00+00:00', '2012-07-05 16:30:00+00:00',
               '2012-07-05 17:30:00+00:00', '2012-07-05 18:30:00+00:00',
               '2012-07-05 19:30:00+00:00', '2012-07-05 20:00:00+00:00',
               '2012-07-06 14:30:00+00:00', '2012-07-06 15:30:00+00:00',
               '2012-07-06 16:30:00+00:00', '2012-07-06 17:30:00+00:00',
               '2012-07-06 18:30:00+00:00', '2012-07-06 19:30:00+00:00',
               '2012-07-06 20:00:00+00:00', '2012-07-09 14:30:00+00:00',
               '2012-07-09 15:30:00+00:00', '2012-07-09 16:30:00+00:00',
               '2012-07-09 17:30:00+00:00', '2012-07-09 18:30:00+00:00',
               '2012-07-09 19:30:00+00:00', '2012-07-09 20:00:00+00:00',
               '2012-07-10 14:30:00+00:00', '2012-07-10 15:30:00+00:00',
               '2012-07-10 16:30:00+00:00', '2012-07-10 17:30:00+00:00',
               '2012-07-10 18:30:00+00:00', '2012-07-10 19:30:00+00:00',
               '2012-07-10 20:00:00+00:00'],
              dtype='datetime64[ns, UTC]', freq=None)

贡献 欢迎以拉取请求的形式进行所有改进和附加(和更正)。该包将在 价值和正确性越多,关注的目光就越多。

要添加新功能,请包含标准 pytest 格式的测试。

使用 pytest 运行测试套件。

使用黑色进行格式化。

有关贡献的完整信息,请参阅 CONTRIBUTING.md_

.. _CONTRIBUTING.md: https://github.com/rsheftel/pandas_market_calendars/blob/master/CONTRIBUTING.md

未来 该软件包是在 MIT 许可证下开源的。欢迎大家添加更多交易所或者OTC市场,确认 或者更正现有的日历,并且通常可以使用此代码执行他们想要的任何操作。

热门栏目