def test_special_holidays(): # 9/11 # Sept 11, 12, 13, 14 2001 nyse = NYSEExchangeCalendar() good_dates = nyse.valid_days('1985-01-01', '2016-12-31') assert pd.Timestamp("9/11/2001") not in good_dates assert pd.Timestamp("9/12/2001") not in good_dates assert pd.Timestamp("9/13/2001") not in good_dates assert pd.Timestamp("9/14/2001") not in good_dates # Hurricane Gloria # Sept 27, 1985 assert pd.Timestamp("9/27/1985") not in good_dates # Hurricane Sandy # Oct 29, 30 2012 assert pd.Timestamp("10/29/2012") not in good_dates assert pd.Timestamp("10/30/2012") not in good_dates # various national days of mourning # Gerald Ford - 1/2/2007 assert pd.Timestamp("1/2/2007") not in good_dates # Ronald Reagan - 6/11/2004 assert pd.Timestamp("6/11/2004") not in good_dates # Richard Nixon - 4/27/1994 assert pd.Timestamp("4/27/1994") not in good_dates
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_thanksgiving(): """ Check MarketCalendar Thanksgiving dates. """ # November 2005 # 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.valid_days('2001-01-01', '2016-12-31') # If Nov has 4 Thursdays, {0} Thanksgiving is the last Thursday. thanksgiving_with_four_weeks = pd.Timestamp("2005-11-24", tz='UTC') assert thanksgiving_with_four_weeks not in good_dates # November 2006 # 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 # If Nov has 5 Thursdays, {0} Thanksgiving is not the last week. thanksgiving_with_five_weeks = pd.Timestamp("2006-11-23", tz='UTC') assert thanksgiving_with_five_weeks not in good_dates # If NYE falls on a weekend, {0} the Tuesday after is the first trading day. first_trading_day_after_new_years_sunday = pd.Timestamp("2012-01-03", tz='UTC') assert first_trading_day_after_new_years_sunday in good_dates
def test_days_at_time_open(): cal = NYSEExchangeCalendar() # check if market_open before/after 1985 is correct valid = cal.valid_days("1984-12-30", "1985-01-03") at_open = cal.days_at_time(valid, "market_open") assert_index_equal( at_open, pd.DatetimeIndex([ '1984-12-31 10:00:00', '1985-01-02 09:30:00', '1985-01-03 09:30:00' ], dtype='datetime64[ns]', freq=None).tz_localize(cal.tz).tz_convert("UTC")) # check if it is rounded valid = cal.valid_days("1901-12-13", "1901-12-16") at_open = cal.days_at_time(valid, "market_open") assert_index_equal( at_open, pd.DatetimeIndex([ '1901-12-13 10:00:00', '1901-12-14 10:00:00', '1901-12-16 10:00:00' ], dtype='datetime64[ns]', freq=None).tz_localize(cal.tz).tz_convert("UTC")) # check if chosen time is kept cal = NYSEExchangeCalendar(open_time=dt.time(9)) at_open = cal.days_at_time(valid, "market_open") assert_index_equal( at_open, pd.DatetimeIndex([ '1901-12-13 09:00:00', '1901-12-14 09:00:00', '1901-12-16 09:00:00' ], dtype='datetime64[ns]', freq=None).tz_localize(cal.tz).tz_convert("UTC"))
def test_new_years(): """ Check whether the MarketCalendar contains certain dates. """ # January 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 31 nyse = NYSEExchangeCalendar() good_dates = nyse.valid_days('2001-01-01', '2016-12-31') # If New Years falls on a weekend, {0} the Monday after is a holiday. day_after_new_years_sunday = pd.Timestamp("2012-01-02", tz='UTC') assert day_after_new_years_sunday not in good_dates # If New Years falls on a weekend, {0} the Tuesday after is the first trading day. first_trading_day_after_new_years_sunday = pd.Timestamp("2012-01-03", tz='UTC') assert first_trading_day_after_new_years_sunday in good_dates # January 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 # If New Years falls during the week, e.g. {0}, it is a holiday. new_years_day = pd.Timestamp("2013-01-01", tz='UTC') assert new_years_day not in good_dates # If the day after NYE falls during the week, {0} is the first trading day. first_trading_day_after_new_years = pd.Timestamp("2013-01-02", tz='UTC') assert first_trading_day_after_new_years in good_dates
def test_days_at_time_close(): cal = NYSEExchangeCalendar() # test market_close before/after 1952-09-29 valid = cal.valid_days("1952-09-26", "1952-09-30") at_close = cal.days_at_time(valid, "market_close") assert_index_equal( at_close, pd.DatetimeIndex([ '1952-09-26 15:00:00', '1952-09-29 15:30:00', '1952-09-30 15:30:00' ], dtype='datetime64[ns]', freq=None).tz_localize(cal.tz).tz_convert("UTC")) # market_close before/after 1974-01-01 valid = cal.valid_days("1973-12-28", "1974-01-02") at_close = cal.days_at_time(valid, "market_close") assert_index_equal( at_close, pd.DatetimeIndex([ '1973-12-28 15:30:00', '1973-12-31 15:30:00', '1974-01-02 16:00:00' ], dtype='datetime64[ns]', freq=None).tz_localize(cal.tz).tz_convert("UTC")) # test all three market_closes valid = cal.valid_days("1952-09-26", "1974-01-02") at_close = cal.days_at_time(valid, "market_close").tz_convert(cal.tz) assert at_close[0] == pd.Timestamp('1952-09-26 15:00:00').tz_localize( cal.tz) assert at_close[1] == pd.Timestamp('1952-09-29 15:30:00').tz_localize( cal.tz) assert at_close[-2] == pd.Timestamp('1973-12-31 15:30:00').tz_localize( cal.tz) assert at_close[-1] == pd.Timestamp('1974-01-02 16:00:00').tz_localize( cal.tz) # test Saturday closes valid = cal.valid_days("1952-05-23", "1952-05-26") at_close = cal.days_at_time(valid, "market_close") assert_index_equal( at_close, pd.DatetimeIndex([ '1952-05-23 15:00:00', '1952-05-24 12:00:00', '1952-05-26 15:00:00' ], dtype='datetime64[ns]', freq=None).tz_localize(cal.tz).tz_convert("UTC")) # check if it is rounded valid = cal.valid_days("1901-12-13", "1901-12-16") at_close = cal.days_at_time(valid, "market_close") assert_index_equal( at_close, pd.DatetimeIndex([ '1901-12-13 15:00:00', '1901-12-14 12:00:00', '1901-12-16 15:00:00' ], dtype='datetime64[ns]', freq=None).tz_localize(cal.tz).tz_convert("UTC")) # check if chosen time is kept cal = NYSEExchangeCalendar(close_time=dt.time(10)) at_close = cal.days_at_time(valid, "market_close") assert_index_equal( at_close, pd.DatetimeIndex([ '1901-12-13 10:00:00', '1901-12-14 10:00:00', '1901-12-16 10:00:00' ], dtype='datetime64[ns]', freq=None).tz_localize(cal.tz).tz_convert("UTC"))