Skip to content

Commit

Permalink
TST: Adds TradingSchedule test fixture
Browse files Browse the repository at this point in the history
  • Loading branch information
jfkirk authored and Jean Bredeche committed Jun 8, 2016
1 parent 31f9f06 commit d9fc514
Show file tree
Hide file tree
Showing 14 changed files with 154 additions and 209 deletions.
15 changes: 6 additions & 9 deletions tests/finance/test_slippage.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@
WithSimParams,
ZiplineTestCase,
)
from zipline.utils.calendars import default_nyse_schedule


class SlippageTestCase(WithSimParams, WithDataPortal, ZiplineTestCase):
Expand Down Expand Up @@ -92,9 +91,10 @@ def test_volume_share_slippage(self):
start=normalize_date(self.minutes[0]),
end=normalize_date(self.minutes[-1])
)
with tmp_bcolz_minute_bar_reader(self.env, days, assets) as reader:
with tmp_bcolz_minute_bar_reader(self.trading_schedule, days, assets) \
as reader:
data_portal = DataPortal(
self.env, default_nyse_schedule,
self.env, self.trading_schedule,
first_trading_day=reader.first_trading_day,
equity_minute_reader=reader,
)
Expand Down Expand Up @@ -481,14 +481,11 @@ def test_orders_stop(self, name, order_data, event_data, expected):
start=normalize_date(self.minutes[0]),
end=normalize_date(self.minutes[-1])
)
with tmp_bcolz_minute_bar_reader(self.env, days, assets) as reader:
with tmp_bcolz_minute_bar_reader(self.trading_schedule, days, assets) \
as reader:
data_portal = DataPortal(
<<<<<<< HEAD
self.env,
self.env, self.trading_schedule,
first_trading_day=reader.first_trading_day,
=======
self.env, default_nyse_schedule,
>>>>>>> ENH: Adds ExchangeCalendar, TradingSchedule, and implementations
equity_minute_reader=reader,
)

Expand Down
12 changes: 6 additions & 6 deletions tests/pipeline/test_engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -931,7 +931,7 @@ def make_equity_info(cls):
cls.equity_info = ret = make_rotating_equity_info(
num_assets=6,
first_start=cls.first_asset_start,
frequency=cls.TRADING_ENV_TRADING_CALENDAR.trading_day,
frequency=cls.trading_schedule.day,
periods_between_starts=4,
asset_lifetime=8,
)
Expand Down Expand Up @@ -992,9 +992,9 @@ def test_SMA(self):
window_length = 5
asset_ids = self.all_asset_ids
dates = date_range(
self.first_asset_start + self.env.trading_day,
self.first_asset_start + self.trading_schedule.day,
self.last_asset_end,
freq=self.env.trading_day,
freq=self.trading_schedule.day,
)
dates_to_test = dates[window_length:]

Expand All @@ -1014,7 +1014,7 @@ def test_SMA(self):
# **previous** day's data.
expected_raw = rolling_mean(
expected_bar_values_2d(
dates - self.env.trading_day,
dates - self.trading_schedule.day,
self.equity_info,
'close',
),
Expand Down Expand Up @@ -1046,9 +1046,9 @@ def test_drawdown(self):
window_length = 5
asset_ids = self.all_asset_ids
dates = date_range(
self.first_asset_start + self.env.trading_day,
self.first_asset_start + self.trading_schedule.day,
self.last_asset_end,
freq=self.env.trading_day,
freq=self.trading_schedule.day,
)
dates_to_test = dates[window_length:]

Expand Down
33 changes: 18 additions & 15 deletions tests/test_algorithm.py
Original file line number Diff line number Diff line change
Expand Up @@ -751,7 +751,7 @@ def make_daily_bar_data(cls):
[100, 100, 100, 300],
timedelta(days=1),
cls.sim_params,
cls.env
cls.trading_schedule,
) for sid in cls.sids
},
index=cls.sim_params.trading_days,
Expand Down Expand Up @@ -1023,10 +1023,11 @@ class TestBeforeTradingStart(WithDataPortal,

@classmethod
def make_minute_bar_data(cls):
asset_minutes = cls.env.minutes_for_days_in_range(
cls.data_start,
cls.END_DATE,
)
asset_minutes = \
cls.trading_schedule.execution_minutes_for_days_in_range(
cls.data_start,
cls.END_DATE,
)
minutes_count = len(asset_minutes)
minutes_arr = np.arange(minutes_count) + 1
split_data = pd.DataFrame(
Expand All @@ -1042,13 +1043,13 @@ def make_minute_bar_data(cls):
split_data.iloc[780:] = split_data.iloc[780:] / 2.0
for sid in (1, 8554):
yield sid, create_minute_df_for_asset(
cls.env,
cls.trading_schedule,
cls.data_start,
cls.sim_params.period_end,
)

yield 2, create_minute_df_for_asset(
cls.env,
cls.trading_schedule,
cls.data_start,
cls.sim_params.period_end,
50,
Expand All @@ -1069,7 +1070,7 @@ def make_splits_data(cls):
def make_daily_bar_data(cls):
for sid in cls.ASSET_FINDER_EQUITY_SIDS:
yield sid, create_daily_df_for_asset(
cls.env,
cls.trading_schedule,
cls.data_start,
cls.sim_params.period_end,
)
Expand Down Expand Up @@ -1397,7 +1398,8 @@ def make_equity_info(cls):

@classmethod
def make_daily_bar_data(cls):
days = len(cls.env.days_in_range(cls.START_DATE, cls.END_DATE))
days = len(cls.trading_schedule.execution_days_in_range(cls.START_DATE,
cls.END_DATE))
return trades_by_sid_to_dfs(
{
0: factory.create_trade_history(
Expand All @@ -1406,14 +1408,14 @@ def make_daily_bar_data(cls):
[100] * days,
timedelta(days=1),
cls.sim_params,
cls.env),
cls.trading_schedule),
3: factory.create_trade_history(
3,
[10.0] * days,
[100] * days,
timedelta(days=1),
cls.sim_params,
cls.env)
cls.trading_schedule)
},
index=cls.sim_params.trading_days,
)
Expand Down Expand Up @@ -3169,10 +3171,11 @@ def handle_data(context, data):

@classmethod
def make_minute_bar_data(cls):
asset_minutes = cls.env.minutes_for_days_in_range(
cls.sim_params.period_start,
cls.sim_params.period_end,
)
asset_minutes = \
cls.trading_schedule.execution_minutes_for_days_in_range(
cls.sim_params.period_start,
cls.sim_params.period_end,
)

minutes_count = len(asset_minutes)
minutes_arr = np.arange(1, 1 + minutes_count)
Expand Down
6 changes: 3 additions & 3 deletions tests/test_bar_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,21 +111,21 @@ def make_minute_bar_data(cls):
# illiquid_split_asset trades every 10 minutes
for sid in (1, cls.SPLIT_ASSET_SID):
yield sid, create_minute_df_for_asset(
cls.env,
cls.trading_schedule,
cls.bcolz_minute_bar_days[0],
cls.bcolz_minute_bar_days[-1],
)

for sid in (2, cls.ILLIQUID_SPLIT_ASSET_SID):
yield sid, create_minute_df_for_asset(
cls.env,
cls.trading_schedule,
cls.bcolz_minute_bar_days[0],
cls.bcolz_minute_bar_days[-1],
10,
)

yield cls.HILARIOUSLY_ILLIQUID_ASSET_SID, create_minute_df_for_asset(
cls.env,
cls.trading_schedule,
cls.bcolz_minute_bar_days[0],
cls.bcolz_minute_bar_days[-1],
50,
Expand Down
8 changes: 4 additions & 4 deletions tests/test_benchmark.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,13 +147,13 @@ def test_asset_IPOed_same_day(self):
)

tmp_reader = tmp_bcolz_minute_bar_reader(
self.env,
self.env.trading_days,
self.trading_schedule,
self.trading_schedule.all_execution_days,
create_minute_bar_data(minutes, [2]),
)
with tmp_reader as reader:
data_portal = DataPortal(
self.env,
self.env, self.trading_schedule,
first_trading_day=reader.first_trading_day,
equity_minute_reader=reader,
equity_daily_reader=self.bcolz_daily_bar_reader,
Expand All @@ -163,7 +163,7 @@ def test_asset_IPOed_same_day(self):
source = BenchmarkSource(
2,
self.env,
default_nyse_schedule,
self.trading_schedule,
self.sim_params.trading_days,
data_portal
)
Expand Down
120 changes: 0 additions & 120 deletions tests/test_finance.py
Original file line number Diff line number Diff line change
Expand Up @@ -394,44 +394,6 @@ class TradingEnvironmentTestCase(WithLogger,
"""
Tests for date management utilities in zipline.finance.trading.
"""
@timed(DEFAULT_TIMEOUT)
def test_is_trading_day(self):
# holidays taken from: http://www.nyse.com/press/1191407641943.html
new_years = datetime(2008, 1, 1, tzinfo=pytz.utc)
mlk_day = datetime(2008, 1, 21, tzinfo=pytz.utc)
presidents = datetime(2008, 2, 18, tzinfo=pytz.utc)
good_friday = datetime(2008, 3, 21, tzinfo=pytz.utc)
memorial_day = datetime(2008, 5, 26, tzinfo=pytz.utc)
july_4th = datetime(2008, 7, 4, tzinfo=pytz.utc)
labor_day = datetime(2008, 9, 1, tzinfo=pytz.utc)
tgiving = datetime(2008, 11, 27, tzinfo=pytz.utc)
christmas = datetime(2008, 5, 25, tzinfo=pytz.utc)
a_saturday = datetime(2008, 8, 2, tzinfo=pytz.utc)
a_sunday = datetime(2008, 10, 12, tzinfo=pytz.utc)
holidays = [
new_years,
mlk_day,
presidents,
good_friday,
memorial_day,
july_4th,
labor_day,
tgiving,
christmas,
a_saturday,
a_sunday
]

for holiday in holidays:
self.assertTrue(not self.cal.is_open_on_day(holiday))

first_trading_day = datetime(2008, 1, 2, tzinfo=pytz.utc)
last_trading_day = datetime(2008, 12, 31, tzinfo=pytz.utc)
workdays = [first_trading_day, last_trading_day]

for workday in workdays:
self.assertTrue(self.cal.is_open_on_day(workday))

def test_simulation_parameters(self):
sp = SimulationParameters(
period_start=datetime(2008, 1, 1, tzinfo=pytz.utc),
Expand Down Expand Up @@ -477,85 +439,3 @@ def test_sim_params_days_in_period(self):
self.assertEquals(num_expected_trading_days, params.days_in_period)
np.testing.assert_array_equal(expected_trading_days,
params.trading_days.tolist())

@timed(DEFAULT_TIMEOUT)
def test_minute_window(self):

# January 2008
# 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

us_east = pytz.timezone('US/Eastern')
utc = pytz.utc

# 10:01 AM Eastern on January 7th..
start = us_east.localize(datetime(2008, 1, 7, 10, 1))
utc_start = pd.Timestamp(start.astimezone(utc))

# Get the next 10 minutes
minutes = self.cal.trading_minute_window(
utc_start, 10,
)
self.assertEqual(len(minutes), 10)
for i in range(10):
self.assertEqual(minutes[i], utc_start + timedelta(minutes=i))

# Get the previous 10 minutes.
minutes = self.cal.trading_minute_window(
utc_start, 10, step=-1,
)
self.assertEqual(len(minutes), 10)
for i in range(10):
self.assertEqual(minutes[i], utc_start + timedelta(minutes=-i))

# Get the next 900 minutes, including utc_start, rolling over into the
# next two days.
# Should include:
# Today: 10:01 AM -> 4:00 PM (360 minutes)
# Tomorrow: 9:31 AM -> 4:00 PM (390 minutes, 750 total)
# Last Day: 9:31 AM -> 12:00 PM (150 minutes, 900 total)
minutes = self.cal.trading_minute_window(
start, 900,
)
today = self.cal.trading_minutes_for_day(utc_start)[30:]
tomorrow = self.cal.trading_minutes_for_day(
start + timedelta(days=1)
)
last_day = self.cal.trading_minutes_for_day(
start + timedelta(days=2))[:150]

self.assertEqual(len(minutes), 900)
self.assertEqual(minutes[0], utc_start)
self.assertTrue(all(today == minutes[:360]))
self.assertTrue(all(tomorrow == minutes[360:750]))
self.assertTrue(all(last_day == minutes[750:]))

# Get the previous 801 minutes, including utc_start, rolling over into
# Friday the 4th and Thursday the 3rd.
# Should include:
# Today: 10:01 AM -> 9:31 AM (31 minutes)
# Friday: 4:00 PM -> 9:31 AM (390 minutes, 421 total)
# Thursday: 4:00 PM -> 9:41 AM (380 minutes, 801 total)
minutes = self.cal.trading_minute_window(
start, 801, step=-1,
)

today = self.cal.trading_minutes_for_day(utc_start)[30::-1]
# minus an extra two days from each of these to account for the two
# weekend days we skipped
friday = self.cal.trading_minutes_for_day(
start + timedelta(days=-3),
)[::-1]
thursday = self.cal.trading_minutes_for_day(
start + timedelta(days=-4),
)[:9:-1]

self.assertEqual(len(minutes), 801)
self.assertEqual(minutes[0], utc_start)
self.assertTrue(all(today == minutes[:31]))
self.assertTrue(all(friday == minutes[31:421]))
self.assertTrue(all(thursday == minutes[421:]))
3 changes: 2 additions & 1 deletion tests/test_history.py
Original file line number Diff line number Diff line change
Expand Up @@ -1136,7 +1136,7 @@ def test_minute_different_lifetimes(self):
def test_history_window_before_first_trading_day(self):
# trading_start is 2/3/2014
# get a history window that starts before that, and ends after that
self.data_portal._first_trading_day = self.TRADING_START_DT
self.data_portal.set_first_trading_day(self.TRADING_START_DT)
first_day_minutes = default_nyse_schedule.execution_minutes_for_day(
self.TRADING_START_DT
)
Expand Down Expand Up @@ -1645,6 +1645,7 @@ def test_history_window_before_first_trading_day(self):
# trading_start is 2/3/2014
# get a history window that starts before that, and ends after that

self.data_portal.set_first_trading_day(self.TRADING_START_DT)
second_day = default_nyse_schedule.next_execution_day(
self.TRADING_START_DT
)
Expand Down
Loading

0 comments on commit d9fc514

Please sign in to comment.