[1]:
from __future__ import print_function, unicode_literals
import pandas as pd
import datetime
from data_provider.nestlib.trading_cal import TradeCal
[2]:
tc = TradeCal()
1、str2date
[3]:
print(tc.str2date('20160105'))
print(tc.str2date('2016-01-05'))
2016-01-05 00:00:00
2016-01-05 00:00:00
2、判断某日是否为交易日
[4]:
date_str0 = '20170701'
date_str1 = '20170705'
print(tc.is_trading_day(date_str0))
print(tc.is_trading_day(date_str1))
False
True
3、获取一段时间内交易日历
使用get_trading_day_list可以获取交易日历
3.1. 参数
begin_date
: 开始日期,datetime格式end_date
: 结束日期 datetime格式frequency
: 字符串类型,可以是以下值:'daily'
: 日交易日历, 不填默认此值'week_start'
: 周初交易日历(每周的第一个交易日)'weed_end'
: 周末交易日历(每周的最后一个交易日)'month_start'
: 月初交易日历'month_endmonth_end'
: 月末交易日历'annual_start'
: 年初交易日历'annual_end'
: 年末交易日历'season_start'
: 季初交易日历'season_end'
: 季末交易日历'season_1_start'
: 一季度初初交易日历'season_1_end'
: 一季度末交易日历'season_2_start'
: 二季度初交易日历'season_2_end'
: 二季度末交易日历'season_3_start'
: 三季度初交易日历'season_3_end'
: 三季度末交易日历
3.2. 示例
获取从20160701到20170101的“日”交易日历
[5]:
begin_date = '20160701'
end_date = '20170101'
tc.get_trading_day_list(begin_date, end_date)[:10]
[5]:
['20160701',
'20160704',
'20160705',
'20160706',
'20160707',
'20160708',
'20160711',
'20160712',
'20160713',
'20160714']
获取从20160701到20170101的“周初”交易日历
[6]:
begin_date = '20160701'
end_date = '20170101'
tc.get_trading_day_list(begin_date, end_date, frequency='week_start')[:10]
[6]:
['20160704',
'20160711',
'20160718',
'20160725',
'20160801',
'20160808',
'20160815',
'20160822',
'20160829',
'20160905']
获取从20160701到20170101的“季度末”交易日历
[7]:
begin_date = '20160701'
end_date = '20170101'
tc.get_trading_day_list(begin_date, end_date, frequency='season_end')[:10]
[7]:
['20160930', '20161230']
4、日期平移
获取date_str向前/向后移动N个交易日所对应的交易日
[8]:
tc.shift_date('20170705', 8, direction='forward')
[8]:
'20170717'