Skip to content

Commit

Permalink
Merge pull request #73 from quantopian/peru
Browse files Browse the repository at this point in the history
Peru Calendar
  • Loading branch information
dmichalowicz authored Sep 23, 2019
2 parents 08e1b57 + 233c219 commit 341668f
Show file tree
Hide file tree
Showing 7 changed files with 8,005 additions and 4 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ A Python library of exchange calendars meant to be used with [Zipline](https://g
| Santiago Stock Exchange | XSGO | Chile | * | http://inter.bolsadesantiago.com/sitios/en/Paginas/home.aspx |
| Colombia Securities Exchange | XBOG | Colombia | * | https://www.bvc.com.co/nueva/index_en.html |
| Mexican Stock Exchange | XMEX | Mexico | * | https://www.bmv.com.mx |
| Lima Stock Exchange | XLIM | Peru | * | https://www.bvl.com.pe |

Calendars marked with an asterisk (*) have not yet been released.

Expand Down
7,714 changes: 7,714 additions & 0 deletions tests/resources/xlim.csv

Large diffs are not rendered by default.

117 changes: 117 additions & 0 deletions tests/test_xlim_calendar.py
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)
2 changes: 2 additions & 0 deletions trading_calendars/calendar_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
from .exchange_calendar_xhkg import XHKGExchangeCalendar
from .exchange_calendar_xice import XICEExchangeCalendar
from .exchange_calendar_xkrx import XKRXExchangeCalendar
from .exchange_calendar_xlim import XLIMExchangeCalendar
from .exchange_calendar_xlis import XLISExchangeCalendar
from .exchange_calendar_xlon import XLONExchangeCalendar
from .exchange_calendar_xmad import XMADExchangeCalendar
Expand Down Expand Up @@ -59,6 +60,7 @@
'XHKG': XHKGExchangeCalendar,
'XICE': XICEExchangeCalendar,
'XKRX': XKRXExchangeCalendar,
'XLIM': XLIMExchangeCalendar,
'XLIS': XLISExchangeCalendar,
'XLON': XLONExchangeCalendar,
'XMAD': XMADExchangeCalendar,
Expand Down
15 changes: 15 additions & 0 deletions trading_calendars/common_holidays.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,21 @@ def midsummer_eve(start_date=None, end_date=None):
)


def saint_peter_and_saint_paul_day(start_date=None,
end_date=None,
observance=None,
days_of_week=None):
return Holiday(
'Saint Peter and Saint Paul Day',
month=6,
day=29,
start_date=start_date,
end_date=end_date,
observance=observance,
days_of_week=days_of_week,
)


def assumption_day(start_date=None,
end_date=None,
observance=None,
Expand Down
154 changes: 154 additions & 0 deletions trading_calendars/exchange_calendar_xlim.py
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,
)
)
6 changes: 2 additions & 4 deletions trading_calendars/exchange_calendar_xsgo.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
new_years_day,
maundy_thursday,
european_labour_day,
saint_peter_and_saint_paul_day,
assumption_day,
all_saints_day,
immaculate_conception,
Expand Down Expand Up @@ -97,10 +98,7 @@ def not_2010(datetime_index):

NavyDay = Holiday('Navy Day', month=5, day=21)

SaintPeterAndSaintPaulDay = Holiday(
'Saint Peter and Saint Paul Day',
month=6,
day=29,
SaintPeterAndSaintPaulDay = saint_peter_and_saint_paul_day(
observance=nearest_monday,
)

Expand Down

0 comments on commit 341668f

Please sign in to comment.