问题描述
我正在尝试使用 Pandas 创建交易日历.我能够基于 USFederalHolidayCalendar 创建一个 cal 实例.USFederalHolidayCalendar 与交易日历不一致,因为交易日历不包括哥伦布日和退伍军人节.但是,交易日历包括耶稣受难日(不包括在 USFederalHolidayCalendar 中).以下代码中除了最后一行之外的所有内容都有效:
I'm trying to create a Trading calendar using Pandas. I'm able to create a cal instance based on the USFederalHolidayCalendar. The USFederalHolidayCalendar is not consistent with the Trading calendar in that the Trading calendar doesn't include Columbus Day and Veteran's Day. However, the Trading calendar includes Good Friday (not included in the USFederalHolidayCalendar). Everything except for the last line in following code works:
from pandas.tseries.holiday import get_calendar, HolidayCalendarFactory, GoodFriday
from datetime import datetime
cal = get_calendar('USFederalHolidayCalendar') # Create calendar instance
cal.rules.pop(7) # Remove Veteran's Day rule
cal.rules.pop(6) # Remove Columbus Day rule
tradingCal = HolidayCalendarFactory('TradingCalendar', cal, GoodFriday)
tradingCal 实例似乎在我能够查看假期规则的情况下工作.
The tradingCal instance seems to work in that I'm able to view the Holiday rules.
In[10]: tradingCal.rules
Out[10]:
[Holiday: Labor Day (month=9, day=1, offset=<DateOffset: kwds={'weekday': MO(+1)}>),
Holiday: Presidents Day (month=2, day=1, offset=<DateOffset: kwds={'weekday': MO(+3)}>),
Holiday: Good Friday (month=1, day=1, offset=[<Easter>, <-2 * Days>]),
Holiday: Dr. Martin Luther King Jr. (month=1, day=1, offset=<DateOffset: kwds={'weekday': MO(+3)}>),
Holiday: New Years Day (month=1, day=1, observance=<function nearest_workday at 0x000000000A190BA8>),
Holiday: Thanksgiving (month=11, day=1, offset=<DateOffset: kwds={'weekday': TH(+4)}>),
Holiday: July 4th (month=7, day=4, observance=<function nearest_workday at 0x000000000A190BA8>),
Holiday: Christmas (month=12, day=25, observance=<function nearest_workday at 0x000000000A190BA8>),
Holiday: MemorialDay (month=5, day=31, offset=<DateOffset: kwds={'weekday': MO(-1)}>)]
当我尝试列出某个日期范围内的假期时,我收到以下错误:
When I try to list the holidays in a date range, I get the following error:
In[11]: tradingCal.holidays(datetime(2014, 12, 31), datetime(2016, 12, 31))
Traceback (most recent call last):
File "C:Python27libsite-packagesIPythoncoreinteractiveshell.py", line 3035, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File "<ipython-input-12-2708cd2db7a0>", line 1, in <module>
tradingCal.holidays(datetime(2014, 12, 31), datetime(2016, 12, 31))
TypeError: unbound method holidays() must be called with TradingCalendar instance as first argument (got datetime instance instead)
有什么想法吗?
推荐答案
也许从头开始创建交易日历更直接,如下所示:
Perhaps it is more straightforward to create the trade calendar from scratch, like so:
import datetime as dt
from pandas.tseries.holiday import AbstractHolidayCalendar, Holiday, nearest_workday,
USMartinLutherKingJr, USPresidentsDay, GoodFriday, USMemorialDay,
USLaborDay, USThanksgivingDay
class USTradingCalendar(AbstractHolidayCalendar):
rules = [
Holiday('NewYearsDay', month=1, day=1, observance=nearest_workday),
USMartinLutherKingJr,
USPresidentsDay,
GoodFriday,
USMemorialDay,
Holiday('USIndependenceDay', month=7, day=4, observance=nearest_workday),
USLaborDay,
USThanksgivingDay,
Holiday('Christmas', month=12, day=25, observance=nearest_workday)
]
def get_trading_close_holidays(year):
inst = USTradingCalendar()
return inst.holidays(dt.datetime(year-1, 12, 31), dt.datetime(year, 12, 31))
if __name__ == '__main__':
print(get_trading_close_holidays(2016))
# DatetimeIndex(['2016-01-01', '2016-01-18', '2016-02-15', '2016-03-25',
# '2016-05-30', '2016-07-04', '2016-09-05', '2016-11-24',
# '2016-12-26'],
# dtype='datetime64[ns]', freq=None)
这篇关于使用 Pandas 创建交易假期日历的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!