def test_2012(): nyse = NYSEExchangeCalendar() # holidays we expect: holidays_2012 = [ pd.Timestamp("2012-01-02", tz='UTC'), pd.Timestamp("2012-01-16", tz='UTC'), pd.Timestamp("2012-02-20", tz='UTC'), pd.Timestamp("2012-04-06", tz='UTC'), pd.Timestamp("2012-05-28", tz='UTC'), pd.Timestamp("2012-07-04", tz='UTC'), pd.Timestamp("2012-09-03", tz='UTC'), pd.Timestamp("2012-11-22", tz='UTC'), pd.Timestamp("2012-12-25", tz='UTC') ] for session_label in holidays_2012: assert session_label not in nyse.valid_days('2012-01-01', '2012-12-31') # early closes we expect: early_closes_2012 = [ pd.Timestamp("2012-07-03", tz='UTC'), pd.Timestamp("2012-11-23", tz='UTC'), pd.Timestamp("2012-12-24", tz='UTC') ] expected = nyse.early_closes(nyse.schedule('2012-01-01', '2012-12-31')) assert len(expected) == 3 for early_close_session_label in early_closes_2012: assert early_close_session_label in expected.index
def test_day_after_thanksgiving(): # November 2012 # Su Mo Tu We Th Fr Sa # 1 2 3 # 4 5 6 7 8 9 10 # 11 12 13 14 15 16 17 # 18 19 20 21 22 23 24 # 25 26 27 28 29 30 nyse = NYSEExchangeCalendar() good_dates = nyse.schedule('2001-01-01', '2016-12-31') fourth_friday_open = pd.Timestamp('11/23/2012 11:00AM', tz='America/New_York') fourth_friday = pd.Timestamp('11/23/2012 3:00PM', tz='America/New_York') assert fourth_friday_open > good_dates.loc['2012-11-23', 'market_open'] assert fourth_friday > good_dates.loc['2012-11-23', 'market_close'] # November 2013 # Su Mo Tu We Th Fr Sa # 1 2 # 3 4 5 6 7 8 9 # 10 11 12 13 14 15 16 # 17 18 19 20 21 22 23 # 24 25 26 27 28 29 30 fifth_friday_open = pd.Timestamp('11/29/2013 11:00AM', tz='America/New_York') fifth_friday = pd.Timestamp('11/29/2013 3:00PM', tz='America/New_York') assert fifth_friday_open > good_dates.loc['2012-11-23', 'market_open'] assert fifth_friday > good_dates.loc['2012-11-23', 'market_close']
def test_custom_open_close(): cal = NYSEExchangeCalendar(open_time=dt.time(9), close_time=dt.time(10)) sched = cal.schedule("2021-08-16", "2021-08-16") assert sched.market_open.iat[0] == pd.Timestamp( "2021-08-16 13:00:00+00:00") assert sched.market_close.iat[0] == pd.Timestamp( "2021-08-16 14:00:00+00:00") assert not NYSEExchangeCalendar.regular_market_times is cal.regular_market_times
def test_merge_schedules(): cal1 = FakeCalendar() cal2 = NYSEExchangeCalendar() # cal1 is open on 2016-07-04 and cal2 is not sch1 = cal1.schedule('2016-07-01', '2016-07-06') sch2 = cal2.schedule('2016-07-01', '2016-07-06') # outer join will include July 4th and have expected = pd.DataFrame( { 'market_open': [ pd.Timestamp(x, tz='UTC') for x in [ '2016-07-01 02:13', '2016-07-04 02:13', '2016-07-05 02:13', '2016-07-06 02:13' ] ], 'market_close': [ pd.Timestamp(x, tz='UTC') for x in [ '2016-07-01 20:00', '2016-07-04 02:49', '2016-07-05 20:00', '2016-07-06 20:00' ] ] }, columns=['market_open', 'market_close'], index=pd.DatetimeIndex( ['2016-07-01', '2016-07-04', '2016-07-05', '2016-07-06'])) actual = mcal.merge_schedules([sch1, sch2], how='outer') assert_frame_equal(actual, expected) # inner join will exclude July 4th because not open for both expected = pd.DataFrame( { 'market_open': [ pd.Timestamp(x, tz='UTC') for x in ['2016-07-01 13:30', '2016-07-05 13:30', '2016-07-06 13:30'] ], 'market_close': [ pd.Timestamp(x, tz='UTC') for x in ['2016-07-01 02:49', '2016-07-05 02:49', '2016-07-06 02:49'] ] }, columns=['market_open', 'market_close'], index=pd.DatetimeIndex(['2016-07-01', '2016-07-05', '2016-07-06'])) actual = mcal.merge_schedules([sch1, sch2], how='inner') assert_frame_equal(actual, expected) # joining more than two calendars works correctly actual = mcal.merge_schedules([sch1, sch1, sch1], how='inner') assert_frame_equal(actual, sch1) with pytest.raises(ValueError): mcal.merge_schedules([sch1, sch2], how='left')
def test_special_early_close_is_not_trading_day(): """ Performs a test for generating a schedule when a date that is a special early close is also an adhoc holiday so that the process ignores the early close for the missing date. """ nyse = NYSEExchangeCalendar() # 1956-12-24 is a full day holiday and also will show as early close actual = nyse.schedule('1956-12-20', '1956-12-30') dates = [pd.Timestamp('1956-12-' + x) for x in ['20', '21', '26', '27', '28']] expected = pd.DatetimeIndex(dates) assert_index_equal(actual.index, expected)
def test_early_close_independence_day_thursday(): """ Prior to 2013, the market closed early the Friday after an Independence Day on Thursday. Since and including 2013, the early close is on Wednesday. """ # July 2002 # Su Mo Tu We Th Fr Sa # 1 2 3 4 5 6 # 7 8 9 10 11 12 13 # 14 15 16 17 18 19 20 # 21 22 23 24 25 26 27 # 28 29 30 31 nyse = NYSEExchangeCalendar() schedule = nyse.schedule('2001-01-01', '2019-12-31') wednesday_before = pd.Timestamp('7/3/2002 3:00PM', tz='America/New_York') friday_after_open = pd.Timestamp('7/5/2002 11:00AM', tz='America/New_York') friday_after = pd.Timestamp('7/5/2002 3:00PM', tz='America/New_York') assert nyse.open_at_time(schedule, wednesday_before) is True assert nyse.open_at_time(schedule, friday_after_open) is True assert nyse.open_at_time(schedule, friday_after) is False # July 2013 # Su Mo Tu We Th Fr Sa # 1 2 3 4 5 6 # 7 8 9 10 11 12 13 # 14 15 16 17 18 19 20 # 21 22 23 24 25 26 27 # 28 29 30 31 wednesday_before = pd.Timestamp('7/3/2013 3:00PM', tz='America/New_York') friday_after_open = pd.Timestamp('7/5/2013 11:00AM', tz='America/New_York') friday_after = pd.Timestamp('7/5/2013 3:00PM', tz='America/New_York') assert nyse.open_at_time(schedule, wednesday_before) is False assert nyse.open_at_time(schedule, friday_after_open) is True assert nyse.open_at_time(schedule, friday_after) is True # July 2019 # Su Mo Tu We Th Fr Sa # 1 2 3 4 5 6 # 7 8 9 10 11 12 13 # 14 15 16 17 18 19 20 # 21 22 23 24 25 26 27 # 28 29 30 31 wednesday_before = pd.Timestamp('7/3/2019 3:00PM', tz='America/New_York') friday_after_open = pd.Timestamp('7/5/2019 11:00AM', tz='America/New_York') friday_after = pd.Timestamp('7/5/2019 3:00PM', tz='America/New_York') assert nyse.open_at_time(schedule, wednesday_before) is False assert nyse.open_at_time(schedule, friday_after_open) is True assert nyse.open_at_time(schedule, friday_after) is True