TradeTime是针对交易开发的日期时间工具,在计算上和python
标准库datetime
完美结合,可以灵活对日期时间进行运算操作。
datetime
结合使用;pip install tradetime
import tradetime
通过以下场景来看看tradetime的作用:
当前交易日
判断某一天是否为交易日
判断某一天非交易日类型
获取一段时间的交易日历
日期运算符
获取某月月底最后一个交易日
格式转换
…
# 假设今天为 2022-03-20
>>> tradetime.date.current()
date(year=2022, month=3, day=21, freq='D')
>>> tradetime.date.current('W')
date(year=2022, month=3, day=25, freq='W')
>>> tradetime.date.current('Q')
date(year=2022, month=3, day=31, freq='Q')
# 假设今天为 2022-01-01非交易日
>>> tradetime.date.current()
Traceback (most recent call last):
...
ValueError: The date is external break, missing param if_break.
>>> tradetime.date.current(if_break='past')
date(year=2019, month=12, day=31, freq='D')
>>> tradetime.date.is_trading(datetime.date(2022, 3, 18))
True
>>> tradetime.date.is_trading(datetime.date(2022, 3, 19))
False
>>> tradetime.date.break_type(20200101, 'D')
'external break'
>>> tradetime.date.break_type(20200101, 'W')
'internal break'
>>> tradetime.date.break_type(20200101, 'M')
'external break'
>>> tradetime.date.break_type(20200101, 'Q')
'external break'
>>> tradetime.date.break_type(20200101, 'Y')
'external break'
# 一段时间的open bars
>>> tradetime.date.bars(datetime.date(2022, 2, 1), datetime.date(2022, 3, 12), freq='W', is_open=True)
0 2022-02-07
1 2022-02-14
2 2022-02-21
3 2022-02-28
4 2022-03-07
# 一段时间的close bars
>>> tradetime.date.bars(datetime.date(2022, 2, 1), datetime.date(2022, 3, 12), freq='W')
0 2022-02-11
1 2022-02-18
2 2022-02-25
3 2022-03-04
4 2022-03-11
Name: date, dtype: object
# 允许溢出
>>> tradetime.date.bars(datetime.date(2022, 2, 1), datetime.date(2022, 3, 12), freq='W', overflow=True)
0 2022-02-11
1 2022-02-18
2 2022-02-25
3 2022-03-04
4 2022-03-11
5 2022-03-18
和int
,进行bar位移
# 加法
>>> tradetime.date(2022, 3, 18, freq='D') + 3
date(year=2022, month=3, day=23, freq='D')
>>> tradetime.date(2022, 3, 18, freq='W') + 3
date(year=2022, month=4, day=8, freq='W')
# 减法
>>> tradetime.date(2022, 3, 18, freq='D') - 3
date(year=2022, month=3, day=15, freq='D')
>>> tradetime.date(2022, 3, 18, freq='W') - 3
date(year=2022, month=2, day=25, freq='W')
# 不建议反向进行加减,这样做会很奇怪
>>> tradetime.date.operation_inverse = True # 必须设置后才可以反向运算
>>> 3 + tradetime.date(2022, 3, 18, freq='D')
date(year=2022, month=3, day=23, freq='D')
>>> 3 - tradetime.date(2022, 3, 18, freq='D')
date(year=2022, month=3, day=15, freq='D')
和tradetime.bardelta
,进行bar位移
>>> tradetime.date(2022, 3, 18, freq='D') + tradetime.bardelta(date_bars=2)
date(year=2022, month=3, day=22, freq='D')
# 必须频率一致
>>> tradetime.date(2022, 3, 18, freq='D') + tradetime.bardelta(date_bars=2, date_freq='Q')
Traceback (most recent call last):
...
ValueError: 2022-03-18 is not in freq 'Q'
>>> tradetime.date(2022, 3, 18, freq='D') - tradetime.bardelta(date_bars=2)
date(year=2022, month=3, day=16, freq='D')
# 同样不建议反向进行加减,这样做会很奇怪
和datetime.delta
,进行时间位移,而不是bar位移,不考虑是否为交易日;
>>> tradetime.date(2022, 3, 18, freq='D') + datetime.timedelta(days=2)
date(year=2022, month=3, day=20, freq='D')
>>> tradetime.date(2022, 3, 18, freq='W') + datetime.timedelta(days=3)
date(year=2022, month=3, day=21, freq='W')
# 同样不建议反向进行加减,这样做会很奇怪
>>> tradetime.date.get_close(2021, m=7)
date(year=2021, month=7, day=30, freq='M')
>>> tradetime.date.get_close(2021, q=3)
date(year=2021, month=9, day=30, freq='Q')
>>> tradetime.date(2022, 1, 4).py_date()
datetime.date(2022, 1, 4)
>>> tradetime.date(2022, 1, 4).pd_date()
DatetimeIndex(['2022-01-04'], dtype='datetime64[ns]', freq=None)
欢迎关注~ SandQuant 专注于全球金融数据和量化投资策略