-
Notifications
You must be signed in to change notification settings - Fork 389
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #73 from quantopian/peru
Peru Calendar
- Loading branch information
Showing
7 changed files
with
8,005 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
from unittest import TestCase | ||
|
||
import pandas as pd | ||
from pytz import UTC | ||
|
||
from trading_calendars.exchange_calendar_xlim import XLIMExchangeCalendar | ||
|
||
from .test_trading_calendar import NoDSTExchangeCalendarTestBase | ||
|
||
|
||
class XLIMCalendarTestCase(NoDSTExchangeCalendarTestBase, TestCase): | ||
|
||
answer_key_filename = 'xlim' | ||
calendar_class = XLIMExchangeCalendar | ||
|
||
# The XLIM is open from 9:00AM to 4:00PM. | ||
MAX_SESSION_HOURS = 7 | ||
|
||
HAVE_EARLY_CLOSES = False | ||
|
||
def test_regular_holidays(self): | ||
all_sessions = self.calendar.all_sessions | ||
|
||
expected_holidays = [ | ||
pd.Timestamp('2019-01-01', tz=UTC), # New Year's Day | ||
pd.Timestamp('2019-04-18', tz=UTC), # Maundy Thursday | ||
pd.Timestamp('2019-04-19', tz=UTC), # Good Friday | ||
pd.Timestamp('2019-05-01', tz=UTC), # Labour Day | ||
pd.Timestamp('2018-06-29', tz=UTC), # St. Peter and St. Paul Day | ||
pd.Timestamp('2016-07-28', tz=UTC), # Independence Day 1 | ||
pd.Timestamp('2016-07-29', tz=UTC), # Independence Day 2 | ||
pd.Timestamp('2019-08-30', tz=UTC), # Santa Rosa | ||
pd.Timestamp('2019-10-08', tz=UTC), # Battle of Angamos | ||
pd.Timestamp('2019-11-01', tz=UTC), # All Saints' Day | ||
pd.Timestamp('2017-12-08', tz=UTC), # Immaculate Conception | ||
pd.Timestamp('2019-12-25', tz=UTC), # Christmas Day | ||
] | ||
|
||
for holiday_label in expected_holidays: | ||
self.assertNotIn(holiday_label, all_sessions) | ||
|
||
def test_holidays_fall_on_weekend(self): | ||
all_sessions = self.calendar.all_sessions | ||
|
||
# All holidays falling on a weekend should not be made up, so verify | ||
# that the surrounding Fridays/Mondays are trading days. | ||
expected_sessions = [ | ||
# New Year's Day on a Sunday. | ||
pd.Timestamp('2016-12-30', tz=UTC), | ||
pd.Timestamp('2017-01-02', tz=UTC), | ||
# Labour Day (May 1st) on a Sunday. | ||
pd.Timestamp('2016-04-29', tz=UTC), | ||
pd.Timestamp('2016-05-02', tz=UTC), | ||
# Saint Peter and Saint Paul Day (June 29th) on a Saturday. | ||
pd.Timestamp('2019-06-28', tz=UTC), | ||
pd.Timestamp('2019-07-01', tz=UTC), | ||
# Independence Days (July 28th and 29th) on a Saturday and Sunday. | ||
pd.Timestamp('2018-07-27', tz=UTC), | ||
pd.Timestamp('2018-07-30', tz=UTC), | ||
# Santa Rosa (August 30th) on a Sunday. | ||
pd.Timestamp('2015-08-28', tz=UTC), | ||
pd.Timestamp('2015-08-31', tz=UTC), | ||
# Battle of Angamos (October 8th) on a Sunday. | ||
pd.Timestamp('2017-10-06', tz=UTC), | ||
pd.Timestamp('2017-10-09', tz=UTC), | ||
# All Saints' Day (November 1st) on a Sunday. | ||
pd.Timestamp('2015-10-30', tz=UTC), | ||
pd.Timestamp('2015-11-02', tz=UTC), | ||
# Immaculate Conception (December 8th) on a Sunday. | ||
pd.Timestamp('2019-12-06', tz=UTC), | ||
pd.Timestamp('2019-12-09', tz=UTC), | ||
# Christmas on a Sunday. | ||
pd.Timestamp('2016-12-23', tz=UTC), | ||
pd.Timestamp('2016-12-26', tz=UTC), | ||
] | ||
|
||
for session_label in expected_sessions: | ||
self.assertIn(session_label, all_sessions) | ||
|
||
def test_new_years_eve(self): | ||
""" | ||
New Year's Eve ceased being a holiday after 2007. | ||
""" | ||
all_sessions = self.calendar.all_sessions | ||
|
||
for year in range(2000, 2008): | ||
self.assertNotIn( | ||
pd.Timestamp('{}-12-31'.format(year), tz=UTC), | ||
all_sessions, | ||
) | ||
|
||
self.assertIn(pd.Timestamp('2008-12-31', tz=UTC), all_sessions) | ||
self.assertIn(pd.Timestamp('2009-12-31', tz=UTC), all_sessions) | ||
self.assertIn(pd.Timestamp('2010-12-31', tz=UTC), all_sessions) | ||
|
||
def test_adhoc_holidays(self): | ||
all_sessions = self.calendar.all_sessions | ||
|
||
expected_holidays = [ | ||
# Exchange holidays. | ||
pd.Timestamp('2009-01-02', tz=UTC), | ||
pd.Timestamp('2009-07-27', tz=UTC), | ||
pd.Timestamp('2015-01-02', tz=UTC), | ||
pd.Timestamp('2015-07-27', tz=UTC), | ||
pd.Timestamp('2015-10-09', tz=UTC), | ||
# ASPA Summit. | ||
pd.Timestamp('2012-10-01', tz=UTC), | ||
pd.Timestamp('2012-10-02', tz=UTC), | ||
# APEC Summit. | ||
pd.Timestamp('2016-11-17', tz=UTC), | ||
pd.Timestamp('2016-11-18', tz=UTC), | ||
# 8th Summit of the Americas. | ||
pd.Timestamp('2018-04-13', tz=UTC), | ||
] | ||
|
||
for holiday_label in expected_holidays: | ||
self.assertNotIn(holiday_label, all_sessions) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,154 @@ | ||
# | ||
# Copyright 2019 Quantopian, Inc. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
from datetime import time | ||
from itertools import chain | ||
|
||
import pandas as pd | ||
from pandas.tseries.holiday import GoodFriday, Holiday | ||
from pytz import timezone, UTC | ||
|
||
from .common_holidays import ( | ||
new_years_day, | ||
maundy_thursday, | ||
european_labour_day, | ||
saint_peter_and_saint_paul_day, | ||
all_saints_day, | ||
immaculate_conception, | ||
christmas, | ||
new_years_eve, | ||
) | ||
from .trading_calendar import HolidayCalendar, TradingCalendar | ||
|
||
|
||
#################### | ||
# Regular Holidays # | ||
#################### | ||
NewYearsDay = new_years_day() | ||
|
||
MaundyThursday = maundy_thursday() | ||
|
||
LabourDay = european_labour_day() | ||
|
||
SaintPeterAndSaintPaulDay = saint_peter_and_saint_paul_day() | ||
|
||
IndependenceDay1 = Holiday('Independence Day', month=7, day=28) | ||
IndependenceDay2 = Holiday('Independence Day', month=7, day=29) | ||
|
||
SantaRosa = Holiday('Santa Rosa', month=8, day=30) | ||
|
||
BattleOfAngamos = Holiday('Battle of Angamos', month=10, day=8) | ||
|
||
AllSaintsDay = all_saints_day() | ||
|
||
ImmaculateConception = immaculate_conception() | ||
|
||
Christmas = christmas() | ||
|
||
NewYearsEve = new_years_eve(end_date='2008') | ||
|
||
|
||
################## | ||
# Adhoc Holidays # | ||
################## | ||
ExchangeHolidays = [ | ||
pd.Timestamp('2009-01-02', tz=UTC), | ||
pd.Timestamp('2009-07-27', tz=UTC), | ||
pd.Timestamp('2015-07-27', tz=UTC), | ||
pd.Timestamp('2015-10-09', tz=UTC), | ||
] | ||
|
||
NationalHolidays = [ | ||
pd.Timestamp('2015-01-02', tz=UTC), | ||
] | ||
|
||
ASPASummit = [ | ||
pd.Timestamp('2012-10-01', tz=UTC), | ||
pd.Timestamp('2012-10-02', tz=UTC), | ||
] | ||
|
||
APECSummit = [ | ||
pd.Timestamp('2016-11-17', tz=UTC), | ||
pd.Timestamp('2016-11-18', tz=UTC), | ||
] | ||
|
||
EighthSummitOfTheAmericas = [pd.Timestamp('2018-04-13', tz=UTC)] | ||
|
||
|
||
class XLIMExchangeCalendar(TradingCalendar): | ||
""" | ||
Calendar for the Lima Stock Exchange (Bolsa de Valores de Lima) in Peru. | ||
Open Time: 9:00 AM, Peruvian Time | ||
Close Time: 4:00 PM, Peruvian Time | ||
Regularly-Observed Holidays: | ||
- New Year's Day | ||
- Maundy Thursday | ||
- Good Friday | ||
- Labour Day | ||
- Saint Paul and Saint Peter Day | ||
- Independence Day | ||
- Santa Rosa | ||
- Battle of Angamos | ||
- All Saints' Day | ||
- Immaculate Conception | ||
- Christmas Day | ||
Holidays No Longer Observed: | ||
- New Year's Eve | ||
Early Closes: | ||
- None | ||
""" | ||
name = 'XLIM' | ||
tz = timezone('America/Lima') | ||
|
||
open_times = ( | ||
(None, time(9)), | ||
) | ||
close_times = ( | ||
(None, time(16)), | ||
) | ||
|
||
@property | ||
def regular_holidays(self): | ||
return HolidayCalendar([ | ||
NewYearsDay, | ||
MaundyThursday, | ||
GoodFriday, | ||
LabourDay, | ||
SaintPeterAndSaintPaulDay, | ||
IndependenceDay1, | ||
IndependenceDay2, | ||
SantaRosa, | ||
BattleOfAngamos, | ||
AllSaintsDay, | ||
ImmaculateConception, | ||
Christmas, | ||
NewYearsEve, | ||
]) | ||
|
||
@property | ||
def adhoc_holidays(self): | ||
return list( | ||
chain( | ||
ExchangeHolidays, | ||
NationalHolidays, | ||
ASPASummit, | ||
APECSummit, | ||
EighthSummitOfTheAmericas, | ||
) | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters