STY: Flake8

This commit is contained in:
jfkirk
2016-04-14 16:31:05 -04:00
committed by Jean Bredeche
parent 4b7390ac81
commit 241abda2a5
11 changed files with 174 additions and 172 deletions
+3 -3
View File
@@ -55,6 +55,6 @@ def main(env, do_upload):
if __name__ == '__main__':
env = os.environ.copy()
main(env,
do_upload=(env.get('ANACONDA_TOKEN') and
env.get('APPVEYOR_REPO_BRANCH') == 'master') and
'APPVEYOR_PULL_REQUEST_NUMBER' not in env)
do_upload=((env.get('ANACONDA_TOKEN')
and env.get('APPVEYOR_REPO_BRANCH') == 'master')
and 'APPVEYOR_PULL_REQUEST_NUMBER' not in env))
+3 -2
View File
@@ -800,9 +800,10 @@ class BcolzMinuteBarTestCase(TestCase):
start_minute_loc = \
default_nyse_schedule.all_execution_minutes.get_loc(minutes[0])
minute_locs = [
default_nyse_schedule.all_execution_minutes.get_loc(minute) \
default_nyse_schedule.all_execution_minutes.get_loc(minute)
- start_minute_loc
for minute in minutes]
for minute in minutes
]
for i, col in enumerate(columns):
for j, sid in enumerate(sids):
+136 -137
View File
@@ -122,180 +122,179 @@ class NYSECalendarTestCase(ExchangeCalendarTestBase, TestCase):
calendar_class = NYSEExchangeCalendar
def test_newyears(self):
"""
Check whether tradingcalendar 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
"""
Check whether tradingcalendar 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
start_dt = Timestamp('1/1/12', tz='UTC')
end_dt = Timestamp('12/31/13', tz='UTC')
trading_days = self.calendar.trading_days(start=start_dt,
end=end_dt)
start_dt = Timestamp('1/1/12', tz='UTC')
end_dt = Timestamp('12/31/13', tz='UTC')
trading_days = self.calendar.trading_days(start=start_dt, end=end_dt)
day_after_new_years_sunday = datetime(
2012, 1, 2, tzinfo=pytz.utc)
day_after_new_years_sunday = datetime(
2012, 1, 2, tzinfo=pytz.utc)
self.assertNotIn(day_after_new_years_sunday,
trading_days.index,
"""
self.assertNotIn(day_after_new_years_sunday,
trading_days.index,
"""
If NYE falls on a weekend, {0} the Monday after is a holiday.
""".strip().format(day_after_new_years_sunday)
)
)
first_trading_day_after_new_years_sunday = datetime(
2012, 1, 3, tzinfo=pytz.utc)
first_trading_day_after_new_years_sunday = datetime(
2012, 1, 3, tzinfo=pytz.utc)
self.assertIn(first_trading_day_after_new_years_sunday,
trading_days.index,
"""
self.assertIn(first_trading_day_after_new_years_sunday,
trading_days.index,
"""
If NYE falls on a weekend, {0} the Tuesday after is the first trading day.
""".strip().format(first_trading_day_after_new_years_sunday)
)
)
# 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
# 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
new_years_day = datetime(
2013, 1, 1, tzinfo=pytz.utc)
new_years_day = datetime(
2013, 1, 1, tzinfo=pytz.utc)
self.assertNotIn(new_years_day,
trading_days.index,
"""
self.assertNotIn(new_years_day,
trading_days.index,
"""
If NYE falls during the week, e.g. {0}, it is a holiday.
""".strip().format(new_years_day)
)
)
first_trading_day_after_new_years = datetime(
2013, 1, 2, tzinfo=pytz.utc)
first_trading_day_after_new_years = datetime(
2013, 1, 2, tzinfo=pytz.utc)
self.assertIn(first_trading_day_after_new_years,
trading_days.index,
"""
self.assertIn(first_trading_day_after_new_years,
trading_days.index,
"""
If the day after NYE falls during the week, {0} \
is the first trading day.
""".strip().format(first_trading_day_after_new_years)
)
)
def test_thanksgiving(self):
"""
Check tradingcalendar 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
"""
Check tradingcalendar 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
start_dt = Timestamp('1/1/05', tz='UTC')
end_dt = Timestamp('12/31/12', tz='UTC')
trading_days = self.calendar.trading_days(start=start_dt,
end=end_dt)
start_dt = Timestamp('1/1/05', tz='UTC')
end_dt = Timestamp('12/31/12', tz='UTC')
trading_days = self.calendar.trading_days(start=start_dt,
end=end_dt)
thanksgiving_with_four_weeks = datetime(
2005, 11, 24, tzinfo=pytz.utc)
thanksgiving_with_four_weeks = datetime(
2005, 11, 24, tzinfo=pytz.utc)
self.assertNotIn(thanksgiving_with_four_weeks,
trading_days.index,
"""
self.assertNotIn(thanksgiving_with_four_weeks,
trading_days.index,
"""
If Nov has 4 Thursdays, {0} Thanksgiving is the last Thursady.
""".strip().format(thanksgiving_with_four_weeks)
)
)
# 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
thanksgiving_with_five_weeks = datetime(
2006, 11, 23, tzinfo=pytz.utc)
# 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
thanksgiving_with_five_weeks = datetime(
2006, 11, 23, tzinfo=pytz.utc)
self.assertNotIn(thanksgiving_with_five_weeks,
trading_days.index,
"""
self.assertNotIn(thanksgiving_with_five_weeks,
trading_days.index,
"""
If Nov has 5 Thursdays, {0} Thanksgiving is not the last week.
""".strip().format(thanksgiving_with_five_weeks)
)
)
first_trading_day_after_new_years_sunday = datetime(
2012, 1, 3, tzinfo=pytz.utc)
first_trading_day_after_new_years_sunday = datetime(
2012, 1, 3, tzinfo=pytz.utc)
self.assertIn(first_trading_day_after_new_years_sunday,
trading_days.index,
"""
self.assertIn(first_trading_day_after_new_years_sunday,
trading_days.index,
"""
If NYE falls on a weekend, {0} the Tuesday after is the first trading day.
""".strip().format(first_trading_day_after_new_years_sunday)
)
)
def test_day_after_thanksgiving(self):
# November 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
fourth_friday_open = Timestamp('11/23/2012 11:00AM', tz='EST')
fourth_friday = Timestamp('11/23/2012 3:00PM', tz='EST')
self.assertTrue(self.calendar.is_open_on_minute(fourth_friday_open))
self.assertFalse(self.calendar.is_open_on_minute(fourth_friday))
# November 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
fourth_friday_open = Timestamp('11/23/2012 11:00AM', tz='EST')
fourth_friday = Timestamp('11/23/2012 3:00PM', tz='EST')
self.assertTrue(self.calendar.is_open_on_minute(fourth_friday_open))
self.assertFalse(self.calendar.is_open_on_minute(fourth_friday))
# November 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
fifth_friday_open = Timestamp('11/29/2013 11:00AM', tz='EST')
fifth_friday = Timestamp('11/29/2013 3:00PM', tz='EST')
self.assertTrue(self.calendar.is_open_on_minute(fifth_friday_open))
self.assertFalse(self.calendar.is_open_on_minute(fifth_friday))
# November 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
fifth_friday_open = Timestamp('11/29/2013 11:00AM', tz='EST')
fifth_friday = Timestamp('11/29/2013 3:00PM', tz='EST')
self.assertTrue(self.calendar.is_open_on_minute(fifth_friday_open))
self.assertFalse(self.calendar.is_open_on_minute(fifth_friday))
def test_early_close_independence_day_thursday(self):
"""
Until 2013, the market closed early the Friday after an
Independence Day on Thursday. Since then, the early close is on
Wednesday.
"""
# July 2002
# 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
wednesday_before = Timestamp('7/3/2002 3:00PM', tz='EST')
friday_after_open = Timestamp('7/5/2002 11:00AM', tz='EST')
friday_after = Timestamp('7/5/2002 3:00PM', tz='EST')
self.assertTrue(self.calendar.is_open_on_minute(wednesday_before))
self.assertTrue(self.calendar.is_open_on_minute(friday_after_open))
self.assertFalse(self.calendar.is_open_on_minute(friday_after))
"""
Until 2013, the market closed early the Friday after an
Independence Day on Thursday. Since then, the early close is on
Wednesday.
"""
# July 2002
# 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
wednesday_before = Timestamp('7/3/2002 3:00PM', tz='EST')
friday_after_open = Timestamp('7/5/2002 11:00AM', tz='EST')
friday_after = Timestamp('7/5/2002 3:00PM', tz='EST')
self.assertTrue(self.calendar.is_open_on_minute(wednesday_before))
self.assertTrue(self.calendar.is_open_on_minute(friday_after_open))
self.assertFalse(self.calendar.is_open_on_minute(friday_after))
# July 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
wednesday_before = Timestamp('7/3/2013 3:00PM', tz='EST')
friday_after_open = Timestamp('7/5/2013 11:00AM', tz='EST')
friday_after = Timestamp('7/5/2013 3:00PM', tz='EST')
self.assertFalse(self.calendar.is_open_on_minute(wednesday_before))
self.assertTrue(self.calendar.is_open_on_minute(friday_after_open))
self.assertTrue(self.calendar.is_open_on_minute(friday_after))
# July 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
wednesday_before = Timestamp('7/3/2013 3:00PM', tz='EST')
friday_after_open = Timestamp('7/5/2013 11:00AM', tz='EST')
friday_after = Timestamp('7/5/2013 3:00PM', tz='EST')
self.assertFalse(self.calendar.is_open_on_minute(wednesday_before))
self.assertTrue(self.calendar.is_open_on_minute(friday_after_open))
self.assertTrue(self.calendar.is_open_on_minute(friday_after))
+9 -7
View File
@@ -680,9 +680,9 @@ class MinuteEquityHistoryTestCase(WithHistory, ZiplineTestCase):
# since asset2 and asset3 both started trading on 1/5/2015, let's do
# some history windows that are completely before that
minutes = default_nyse_schedule.execution_minutes_for_day(
default_nyse_schedule.previous_execution_day(
pd.Timestamp('2015-01-05', tz='UTC')
)
default_nyse_schedule.previous_execution_day(pd.Timestamp(
'2015-01-05', tz='UTC'
))
)[0:60]
for idx, minute in enumerate(minutes):
@@ -1108,9 +1108,9 @@ class MinuteEquityHistoryTestCase(WithHistory, ZiplineTestCase):
asset1_minutes = \
default_nyse_schedule.execution_minutes_for_days_in_range(
start=self.ASSET1.start_date,
end=self.ASSET1.end_date
)
start=self.ASSET1.start_date,
end=self.ASSET1.end_date
)
asset1_idx = asset1_minutes.searchsorted(
default_nyse_schedule.start_and_end(day)[0]
@@ -1645,7 +1645,9 @@ class DailyEquityHistoryTestCase(WithHistory, ZiplineTestCase):
# trading_start is 2/3/2014
# get a history window that starts before that, and ends after that
second_day = default_nyse_schedule.next_execution_day(self.TRADING_START_DT)
second_day = default_nyse_schedule.next_execution_day(
self.TRADING_START_DT
)
exp_msg = (
'History window extends before 2014-01-03. To use this history '
-2
View File
@@ -13,7 +13,6 @@
# See the License for the specific language governing permissions and
# limitations under the License.
import datetime
from functools import partial
from inspect import isabstract
import random
from unittest import TestCase
@@ -240,7 +239,6 @@ class RuleTestCase(TestCase):
cls.after_open = AfterOpen(hours=1, minutes=5)
cls.class_ = None # Mark that this is the base class.
def test_completeness(self):
"""
Tests that all rules are being tested.
+1 -8
View File
@@ -26,6 +26,7 @@ from zipline.utils.calendars import default_nyse_schedule
log = logbook.Logger('Trading')
class TradingEnvironment(object):
"""
The financial simulations in zipline depend on information
@@ -92,14 +93,6 @@ class TradingEnvironment(object):
self.bm_symbol,
)
#if max_date:
# tr_c = self.treasury_curves
# # Mask the treasury curves down to the current date.
# # In the case of live trading, the last date in the treasury
# # curves would be the day before the date considered to be
# # 'today'.
# self.treasury_curves = tr_c[tr_c.index <= max_date]
self.exchange_tz = exchange_tz
if isinstance(asset_db_path, string_types):
+2 -2
View File
@@ -705,8 +705,8 @@ class FakeDataPortal(DataPortal):
end_idx = \
self.trading_schedule.all_execution_days.searchsorted(end_dt)
days = self.trading_schedule.all_execution_days[
(end_idx - bar_count + 1):(end_idx + 1)
]
(end_idx - bar_count + 1):(end_idx + 1)
]
df = pd.DataFrame(
np.full((bar_count, len(assets)), 100),
+3 -2
View File
@@ -450,6 +450,7 @@ class WithSimParams(WithTradingEnvironment):
SIM_PARAMS_NUM_DAYS = None
SIM_PARAMS_DATA_FREQUENCY = 'daily'
SIM_PARAMS_EMISSION_RATE = 'daily'
SIM_PARAMS_TRADING_SCHEDULE = default_nyse_schedule
SIM_PARAMS_START = alias('START_DATE')
SIM_PARAMS_END = alias('END_DATE')
@@ -464,7 +465,7 @@ class WithSimParams(WithTradingEnvironment):
capital_base=cls.SIM_PARAMS_CAPITAL_BASE,
data_frequency=cls.SIM_PARAMS_DATA_FREQUENCY,
emission_rate=cls.SIM_PARAMS_EMISSION_RATE,
env=cls.env,
trading_schedule=cls.SIM_PARAMS_TRADING_SCHEDULE,
)
@classmethod
@@ -498,7 +499,7 @@ class WithNYSETradingDays(object):
def init_class_fixtures(cls):
super(WithNYSETradingDays, cls).init_class_fixtures()
all_days = tradingcalendar.trading_days
all_days = default_nyse_schedule.all_execution_days
start_loc = all_days.get_loc(cls.DATA_MIN_DAY, 'bfill')
end_loc = all_days.get_loc(cls.DATA_MAX_DAY, 'ffill')
@@ -24,6 +24,7 @@ def normalize_date(date):
date = pd.Timestamp(date, tz='UTC')
return pd.tseries.tools.normalize_date(date)
def delta_from_time(t):
"""
Convert a datetime.time into a timedelta.
@@ -34,6 +35,7 @@ def delta_from_time(t):
seconds=t.second,
)
def _get_index(dt, all_trading_days):
"""
Return the index of the given @dt, or the index of the preceding
@@ -50,6 +52,7 @@ def _get_index(dt, all_trading_days):
# class' methods. These methods live in the helpers module to avoid code
# duplication.
def next_scheduled_day(date, last_trading_day, is_scheduled_day_hook):
dt = normalize_date(date)
delta = pd.Timedelta(days=1)
@@ -60,6 +63,7 @@ def next_scheduled_day(date, last_trading_day, is_scheduled_day_hook):
return dt
return None
def previous_scheduled_day(date, first_trading_day, is_scheduled_day_hook):
dt = normalize_date(date)
delta = pd.Timedelta(days=-1)
@@ -70,14 +74,17 @@ def previous_scheduled_day(date, first_trading_day, is_scheduled_day_hook):
return dt
return None
def next_open_and_close(date, open_and_close_hook,
next_scheduled_day_hook):
return open_and_close_hook(next_scheduled_day_hook(date))
def previous_open_and_close(date, open_and_close_hook,
previous_scheduled_day_hook):
return open_and_close_hook(previous_scheduled_day_hook(date))
def scheduled_day_distance(first_date, second_date, all_days):
first_date = normalize_date(first_date)
second_date = normalize_date(second_date)
@@ -92,10 +99,12 @@ def scheduled_day_distance(first_date, second_date, all_days):
assert distance >= 0
return distance
def minutes_for_day(day, open_and_close_hook):
start, end = open_and_close_hook(day)
return pd.date_range(start, end, freq='T')
def days_in_range(start, end, all_days):
"""
Get all execution days between start and end,
@@ -107,6 +116,7 @@ def days_in_range(start, end, all_days):
mask = ((all_days >= start_date) & (all_days <= end_date))
return all_days[mask]
def minutes_for_days_in_range(start, end, days_in_range_hook,
minutes_for_day_hook):
"""
@@ -124,6 +134,7 @@ def minutes_for_days_in_range(start, end, days_in_range_hook,
# Concatenate all minutes and truncate minutes before start/after end.
return pd.DatetimeIndex(np.concatenate(all_minutes), copy=False, tz='UTC')
def add_scheduled_days(n, date, next_scheduled_day_hook,
previous_scheduled_day_hook, all_trading_days):
"""
@@ -154,11 +165,13 @@ def add_scheduled_days(n, date, next_scheduled_day_hook,
return all_trading_days[idx]
def all_scheduled_minutes(all_days, minutes_for_days_in_range_hook):
first_day = all_days[0]
last_day = all_days[-1]
return minutes_for_days_in_range_hook(first_day, last_day)
def next_scheduled_minute(start, is_scheduled_day_hook, open_and_close_hook,
next_open_and_close_hook):
"""
@@ -178,6 +191,7 @@ def next_scheduled_minute(start, is_scheduled_day_hook, open_and_close_hook,
# then return the open of the *next* trading day.
return next_open_and_close_hook(start)[0]
def previous_scheduled_minute(start, is_scheduled_day_hook,
open_and_close_hook,
previous_open_and_close_hook):
+1 -1
View File
@@ -460,11 +460,11 @@ class ExchangeCalendar(with_metaclass(ABCMeta)):
raise NotImplementedError()
_static_calendars = {}
_lazy_calendar_names = ['NYSE']
def get_calendar(name):
"""
Retrieves an instance of an ExchangeCalendar whose name is given.
+2 -8
View File
@@ -17,19 +17,13 @@
"""
Factory functions to prepare useful data.
"""
import pytz
import pandas as pd
import numpy as np
from datetime import timedelta
from zipline.protocol import Event, DATASOURCE_TYPE
from zipline.sources import (SpecificEquityTrades,
DataFrameSource,
DataPanelSource)
from zipline.finance.trading import (
SimulationParameters, TradingEnvironment, noop_load
)
from zipline.sources import SpecificEquityTrades
from zipline.finance.trading import SimulationParameters
from zipline.sources.test_source import create_trade
from zipline.data.loader import ( # For backwards compatibility
load_from_yahoo,