Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor interesting periods #163

Merged
merged 4 commits into from
Oct 7, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 65 additions & 0 deletions pyfolio/interesting_periods.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
#
# Copyright 2015 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.

"""Generates a list of historical event dates that may have had
significant impact on markets. See extract_interesting_date_ranges."""

import pandas as pd

from collections import OrderedDict

PERIODS = OrderedDict()
# Dotcom bubble
PERIODS['Dotcom'] = (pd.Timestamp('20000310'), pd.Timestamp('20000910'))

# Lehmann Brothers
PERIODS['Lehmann'] = (pd.Timestamp('20080801'), pd.Timestamp('20081001'))

# 9/11
PERIODS['9/11'] = (pd.Timestamp('20010911'), pd.Timestamp('20011011'))

# 05/08/11 US down grade and European Debt Crisis 2011
PERIODS[
'US downgrade/European Debt Crisis'] = (pd.Timestamp('20110805'),
pd.Timestamp('20110905'))

# 16/03/11 Fukushima melt down 2011
PERIODS['Fukushima'] = (pd.Timestamp('20110316'), pd.Timestamp('20110416'))

# 01/08/03 US Housing Bubble 2003
PERIODS['US Housing'] = (
pd.Timestamp('20030108'), pd.Timestamp('20030208'))

# 06/09/12 EZB IR Event 2012
PERIODS['EZB IR Event'] = (
pd.Timestamp('20120910'), pd.Timestamp('20121010'))

# August 2007, March and September of 2008, Q1 & Q2 2009,
PERIODS['Aug07'] = (pd.Timestamp('20070801'), pd.Timestamp('20070901'))
PERIODS['Mar08'] = (pd.Timestamp('20080301'), pd.Timestamp('20070401'))
PERIODS['Sept08'] = (pd.Timestamp('20080901'), pd.Timestamp('20081001'))
PERIODS['2009Q1'] = (pd.Timestamp('20090101'), pd.Timestamp('20090301'))
PERIODS['2009Q2'] = (pd.Timestamp('20090301'), pd.Timestamp('20090601'))

# Flash Crash (May 6, 2010 + 1 week post),
PERIODS['Flash Crash'] = (
pd.Timestamp('20100505'), pd.Timestamp('20100510'))

# April and October 2014).
PERIODS['Apr14'] = (pd.Timestamp('20140401'), pd.Timestamp('20140501'))
PERIODS['Oct14'] = (pd.Timestamp('20141001'), pd.Timestamp('20141101'))

# Market down-turn in August/Sept 2015
PERIODS['Fall2015'] = (pd.Timestamp('20150815'), pd.Timestamp('20150930'))
61 changes: 2 additions & 59 deletions pyfolio/timeseries.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

from . import utils
from .utils import APPROX_BDAYS_PER_MONTH, APPROX_BDAYS_PER_YEAR
from .interesting_periods import PERIODS


def var_cov_var_normal(P, c, mu=0, sigma=1):
Expand Down Expand Up @@ -991,62 +992,6 @@ def cone_rolling(
return perf_ts_r


def gen_date_ranges_interesting():
"""Generates a list of historical event dates that may have had
significant impact on markets. See
extract_interesting_date_ranges.

Returns
-------
periods : OrderedDict
Significant events.

"""

periods = OrderedDict()
# Dotcom bubble
periods['Dotcom'] = (pd.Timestamp('20000310'), pd.Timestamp('20000910'))

# Lehmann Brothers
periods['Lehmann'] = (pd.Timestamp('20080801'), pd.Timestamp('20081001'))

# 9/11
periods['9/11'] = (pd.Timestamp('20010911'), pd.Timestamp('20011011'))

# 05/08/11 US down grade and European Debt Crisis 2011
periods[
'US downgrade/European Debt Crisis'] = (pd.Timestamp('20110805'),
pd.Timestamp('20110905'))

# 16/03/11 Fukushima melt down 2011
periods['Fukushima'] = (pd.Timestamp('20110316'), pd.Timestamp('20110416'))

# 01/08/03 US Housing Bubble 2003
periods['US Housing'] = (
pd.Timestamp('20030108'), pd.Timestamp('20030208'))

# 06/09/12 EZB IR Event 2012
periods['EZB IR Event'] = (
pd.Timestamp('20120910'), pd.Timestamp('20121010'))

# August 2007, March and September of 2008, Q1 & Q2 2009,
periods['Aug07'] = (pd.Timestamp('20070801'), pd.Timestamp('20070901'))
periods['Mar08'] = (pd.Timestamp('20080301'), pd.Timestamp('20070401'))
periods['Sept08'] = (pd.Timestamp('20080901'), pd.Timestamp('20081001'))
periods['2009Q1'] = (pd.Timestamp('20090101'), pd.Timestamp('20090301'))
periods['2009Q2'] = (pd.Timestamp('20090301'), pd.Timestamp('20090601'))

# Flash Crash (May 6, 2010 + 1 week post),
periods['Flash Crash'] = (
pd.Timestamp('20100505'), pd.Timestamp('20100510'))

# April and October 2014).
periods['Apr14'] = (pd.Timestamp('20140401'), pd.Timestamp('20140501'))
periods['Oct14'] = (pd.Timestamp('20141001'), pd.Timestamp('20141101'))

return periods


def extract_interesting_date_ranges(returns):
"""Extracts returns based on interesting events. See
gen_date_range_interesting.
Expand All @@ -1063,12 +1008,10 @@ def extract_interesting_date_ranges(returns):
Date ranges, with returns, of all valid events.

"""

periods = gen_date_ranges_interesting()
returns_dupe = returns.copy()
returns_dupe.index = returns_dupe.index.map(pd.Timestamp)
ranges = OrderedDict()
for name, (start, end) in periods.items():
for name, (start, end) in PERIODS.items():
try:
period = returns_dupe.loc[start:end]
if len(period) == 0:
Expand Down