Trade day handling utilities for A-share market
Project description
kepler-pulse
交易日历工具库,支持多套日历实例、多种频率周期。
安装
pip install kepler-pulse
快速开始
from kepler.pulse import Calendar
# 创建日历实例(需自行注入交易日数据)
cal = Calendar()
# 注入交易日数据(支持 list/tuple/pd.Series)
cal.inject(trade_dates)
# 或者创建时直接传入
cal = Calendar(trade_dates)
基础操作
获取日期范围
cal.range('20240101', '20240131')
# → pd.Series: ['20240102', '20240103', ..., '20240131']
调整到最近交易日
# 向前找(默认)
cal.adjust('20240101', 'prev')
# → '20231229'(上一个交易日)
# 向后找
cal.adjust('20240101', 'next')
# → '20240102'(下一个交易日)
按步长跳转
# 正数向前
cal.step('20240102', 3)
# → '20240105'(后第3个交易日)
# 负数向后
cal.step('20240105', -2)
# → '20240103'(前第2个交易日)
计算交易日数量
cal.delta('20240101', '20240131')
# → 22(两个日期间的交易日数量)
频率操作
支持的频率类型:
daily— 日频weekly— 周频biweekly— 双周频monthly— 月频quarterly— 季频halfyearly— 半年频yearly— 年频reportly— 财报频
获取周期边界日期
# 获取每月第一个交易日
cal.monthly.first('20240101', '20241231')
# → pd.Series: ['20240102', '20240201', '20240301', ...]
# 获取每月最后一个交易日
cal.monthly.last('20240101', '20241231')
# → pd.Series: ['20240131', '20240229', '20240329', ...]
# 获取月初+月末(去重排序)
cal.monthly.both('20240101', '20240331')
# → pd.Series: ['20240102', '20240131', '20240201', '20240229', ...]
获取下一个/上一个边界日期
# 下一个月初
cal.monthly.first.next('20240115')
# → '20240201'
# 下一个月末
cal.monthly.last.next('20240115')
# → '20240131'
# 上一个月初
cal.monthly.first.prev('20240115')
# → '20240102'
# 上一个月末
cal.monthly.last.prev('20240115')
# → '20231229'
# 支持步长
cal.monthly.first.next('20240115', step=2)
# → '20240301'(下下个月初)
其他频率示例
# 周频
cal.weekly.first('20240101', '20240131')
cal.weekly.last.next('20240115')
# 季频
cal.quarterly.first('20240101', '20241231')
cal.quarterly.last.prev('20240615')
# 年频
cal.yearly.first('20200101', '20241231')
cal.yearly.last.next('20240601')
期货操作
股指期货交割日
# 获取指定日期之前的交割日
cal.futures.stock('20240115')
# → pd.Series: ['20231117', '20231215', '20240119', ...]
# 获取所有交割日
cal.futures.stock(all=True)
# → list: ['20231117', '20231215', '20240119', ...]
国债期货交割日
cal.futures.treasury()
# → list: ['20231215', '20240315', ...]
主力合约
cal.futures.contracts('20240115')
# → pd.Series: ['2401', '2402'](当月+次月合约)
# 非换合约期
cal.futures.contracts('20240102')
# → pd.Series: ['2401'](仅当月合约)
多日历支持
# A股日历
cal_a = Calendar(a_share_dates)
# 港股日历
cal_hk = Calendar(hk_share_dates)
# 美股日历
cal_us = Calendar(us_share_dates)
# 独立使用
cal_a.monthly.first('20240101', '20241231')
cal_hk.monthly.first('20240101', '20241231')
API 对照表
基础操作
| 旧 API | 新 API |
|---|---|
get_trade_dts(begin, end) |
cal.range(begin, end) |
adjust_trade_dt(date, 'last') |
cal.adjust(date, 'prev') |
adjust_trade_dt(date, 'next') |
cal.adjust(date, 'next') |
step_trade_dt(date, n) |
cal.step(date, n) |
delta_trade_dt(begin, end) |
cal.delta(begin, end) |
频率操作
| 旧 API | 新 API |
|---|---|
Monthly(1).get(begin, end) |
cal.monthly.first(begin, end) |
Monthly(-1).get(begin, end) |
cal.monthly.last(begin, end) |
Monthly(1, -1).get(begin, end) |
cal.monthly.both(begin, end) |
Monthly(1).next(date) |
cal.monthly.first.next(date) |
Monthly(-1).next(date) |
cal.monthly.last.next(date) |
Monthly(1).prev(date) |
cal.monthly.first.prev(date) |
Monthly(-1).prev(date) |
cal.monthly.last.prev(date) |
期货操作
| 旧 API | 新 API |
|---|---|
get_delistdate(date) |
cal.futures.stock(date) |
get_delistdate_all() |
cal.futures.stock(all=True) |
get_delistdate_tf_all() |
cal.futures.treasury() |
get_contract(date) |
cal.futures.contracts(date) |
注意事项
- 日期格式:仅支持
YYYYMMDD格式的字符串或整数 - 日历实例化:不再使用全局单例,需自行创建
Calendar实例并注入交易日数据 - 缓存机制:每个
Calendar实例内部有缓存,相同参数不会重复计算
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
kepler_pulse-0.2.2.tar.gz
(22.8 kB
view details)
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file kepler_pulse-0.2.2.tar.gz.
File metadata
- Download URL: kepler_pulse-0.2.2.tar.gz
- Upload date:
- Size: 22.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
682be63426b941c89ef55fe1b59470713817efbb96b9bca82e695a76ce52c8ba
|
|
| MD5 |
33a7b450c7ab334b76ebd6b86d490680
|
|
| BLAKE2b-256 |
fab285e69acbc43a008dd6a509a090c22dc6afc3e4ca01f7f98fdb77fdc0d88c
|
File details
Details for the file kepler_pulse-0.2.2-py3-none-any.whl.
File metadata
- Download URL: kepler_pulse-0.2.2-py3-none-any.whl
- Upload date:
- Size: 21.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
3d2fd9eb38632b468a5a3a52283b3e678e771b915cdaf0bd84f938ab64a81412
|
|
| MD5 |
0914c43f6e106dfda63b5420049b8910
|
|
| BLAKE2b-256 |
b8ff932e9ee39323899695f205e7f6141936ef532845aa25a744ac0227bab07c
|