Create trading holiday calendar with Pandas

后端 未结 3 877
忘了有多久
忘了有多久 2020-12-04 08:38

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 consiste

相关标签:
3条回答
  • 2020-12-04 08:52

    If it helps, I had a similar need for exchange trading calendars. There was some excellent code buried in the Zipline project by Quantopian. I extracted out the relevant part and created a new project for creating market exchange trading calendars in pandas. The links are here, with some of the functionality described below.

    https://github.com/rsheftel/pandas_market_calendars

    https://pypi.python.org/pypi/pandas-market-calendars

    Here is what it can do by creating a pandas DatetimeIndex of all of the valid open hours for the NYSE:

    import pandas_market_calendars as mcal
    nyse = mcal.get_calendar('NYSE')
    
    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
    
    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)
    
    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)
    

    If you just want to get the pandas Holiday Calendar that can be used in other pandas functions that take that as an argument:

    holidays = nyse.holidays()
    
    holidays.holidays[-5:]
    (numpy.datetime64('2030-05-27'),
     numpy.datetime64('2030-07-04'),
     numpy.datetime64('2030-09-02'),
     numpy.datetime64('2030-11-28'),
     numpy.datetime64('2030-12-25'))
    
    0 讨论(0)
  • 2020-12-04 08:55

    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)
    
    0 讨论(0)
  • 2020-12-04 08:59

    You have to create new instance of class: cal1 = tradingCal(). This works for me.

    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)
    print tradingCal.rules
    
    #new instance of class
    cal1 = tradingCal()
    
    print cal1.holidays(datetime(2014, 12, 31), datetime(2016, 12, 31))
    
    #DatetimeIndex(['2015-01-01', '2015-01-19', '2015-02-16', '2015-04-03',
    #               '2015-05-25', '2015-07-03', '2015-09-07', '2015-11-26',
    #               '2015-12-25', '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, tz=None)
    
    0 讨论(0)
提交回复
热议问题