PERF: Re-use the faster get_open_and_closes from tradingcalendar

This commit is contained in:
Richard Frank
2015-09-06 21:48:30 -04:00
parent 510dc2ae7b
commit dcc0a31782
3 changed files with 50 additions and 59 deletions
+3 -2
View File
@@ -390,7 +390,7 @@ def get_open_and_close(day, early_closes):
return market_open, market_close
def get_open_and_closes(trading_days, early_closes):
def get_open_and_closes(trading_days, early_closes, get_open_and_close):
open_and_closes = pd.DataFrame(index=trading_days,
columns=('market_open', 'market_close'))
@@ -401,4 +401,5 @@ def get_open_and_closes(trading_days, early_closes):
return open_and_closes
open_and_closes = get_open_and_closes(trading_days, early_closes)
open_and_closes = get_open_and_closes(trading_days, early_closes,
get_open_and_close)
+24 -29
View File
@@ -18,7 +18,8 @@ import pytz
from datetime import datetime
from dateutil import rrule
from zipline.utils.tradingcalendar import end, canonicalize_datetime
from zipline.utils.tradingcalendar import end, canonicalize_datetime, \
get_open_and_closes
start = pd.Timestamp('1994-01-01', tz='UTC')
@@ -288,33 +289,27 @@ def get_early_closes(start, end):
early_closes = get_early_closes(start, end)
def get_open_and_closes(trading_days, early_closes):
open_and_closes = pd.DataFrame(index=trading_days,
columns=('market_open', 'market_close'))
for day in trading_days:
# only "early close" event in Bovespa actually is a late start
# as the market only opens at 1pm
open_hour = 13 if day in quarta_cinzas else 10
market_open = pd.Timestamp(
datetime(
year=day.year,
month=day.month,
day=day.day,
hour=open_hour,
minute=00),
tz='America/Sao_Paulo').tz_convert('UTC')
market_close = pd.Timestamp(
datetime(
year=day.year,
month=day.month,
day=day.day,
hour=16),
tz='America/Sao_Paulo').tz_convert('UTC')
def get_open_and_close(day, early_closes):
# only "early close" event in Bovespa actually is a late start
# as the market only opens at 1pm
open_hour = 13 if day in quarta_cinzas else 10
market_open = pd.Timestamp(
datetime(
year=day.year,
month=day.month,
day=day.day,
hour=open_hour,
minute=00),
tz='America/Sao_Paulo').tz_convert('UTC')
market_close = pd.Timestamp(
datetime(
year=day.year,
month=day.month,
day=day.day,
hour=16),
tz='America/Sao_Paulo').tz_convert('UTC')
open_and_closes.loc[day, 'market_open'] = market_open
open_and_closes.loc[day, 'market_close'] = market_close
return market_open, market_close
return open_and_closes
open_and_closes = get_open_and_closes(trading_days, early_closes)
open_and_closes = get_open_and_closes(trading_days, early_closes,
get_open_and_close)
+23 -28
View File
@@ -19,7 +19,8 @@ import pytz
from datetime import datetime
from dateutil import rrule
from zipline.utils.tradingcalendar import end, canonicalize_datetime
from zipline.utils.tradingcalendar import end, canonicalize_datetime, \
get_open_and_closes
start = pd.Timestamp('1994-01-01', tz='UTC')
@@ -338,32 +339,26 @@ def get_early_closes(start, end):
early_closes = get_early_closes(start, end)
def get_open_and_closes(trading_days, early_closes, tz='US/Eastern'):
open_and_closes = pd.DataFrame(index=trading_days,
columns=('market_open', 'market_close'))
for day in trading_days:
market_open = pd.Timestamp(
datetime(
year=day.year,
month=day.month,
day=day.day,
hour=9,
minute=31),
tz='US/Eastern').tz_convert('UTC')
# 1 PM if early close, 4 PM otherwise
close_hour = 13 if day in early_closes else 16
market_close = pd.Timestamp(
datetime(
year=day.year,
month=day.month,
day=day.day,
hour=close_hour),
tz='US/Eastern').tz_convert('UTC')
def get_open_and_close(day, early_closes):
market_open = pd.Timestamp(
datetime(
year=day.year,
month=day.month,
day=day.day,
hour=9,
minute=31),
tz='US/Eastern').tz_convert('UTC')
# 1 PM if early close, 4 PM otherwise
close_hour = 13 if day in early_closes else 16
market_close = pd.Timestamp(
datetime(
year=day.year,
month=day.month,
day=day.day,
hour=close_hour),
tz='US/Eastern').tz_convert('UTC')
open_and_closes.loc[day, 'market_open'] = market_open
open_and_closes.loc[day, 'market_close'] = market_close
return market_open, market_close
return open_and_closes
open_and_closes = get_open_and_closes(trading_days, early_closes)
open_and_closes = get_open_and_closes(trading_days, early_closes,
get_open_and_close)