From 147fd10b7ef28e32a61ead4b1777fb76c444ada6 Mon Sep 17 00:00:00 2001 From: jfkirk Date: Fri, 6 May 2016 14:24:12 -0400 Subject: [PATCH] TST: Refactors more tests to use WithTradingSchedule --- tests/data/test_minute_bars.py | 29 ++--- tests/pipeline/base.py | 15 +-- tests/pipeline/test_engine.py | 9 +- tests/pipeline/test_factor.py | 4 +- tests/pipeline/test_filter.py | 4 +- tests/risk/test_risk_cumulative.py | 23 ++-- tests/risk/test_risk_period.py | 49 ++++---- tests/test_algorithm.py | 54 ++++----- tests/test_api_shim.py | 15 ++- tests/test_assets.py | 6 +- tests/test_bar_data.py | 3 +- tests/test_benchmark.py | 15 +-- tests/test_data_portal.py | 7 +- tests/test_fetcher.py | 5 +- tests/test_finance.py | 21 ++-- tests/test_history.py | 63 +++++----- tests/test_perf_tracking.py | 120 ++++++++++---------- tests/test_security_list.py | 19 ++-- zipline/data/us_equity_loader.py | 3 +- zipline/finance/performance/tracker.py | 8 +- zipline/finance/trading.py | 2 - zipline/utils/calendars/trading_schedule.py | 2 - 22 files changed, 225 insertions(+), 251 deletions(-) diff --git a/tests/data/test_minute_bars.py b/tests/data/test_minute_bars.py index 5a2890da44..ed0d9105fc 100644 --- a/tests/data/test_minute_bars.py +++ b/tests/data/test_minute_bars.py @@ -15,8 +15,6 @@ from datetime import timedelta import os -from unittest import TestCase - from numpy import ( arange, array, @@ -44,7 +42,9 @@ US_EQUITIES_MINUTES_PER_DAY, BcolzMinuteWriterColumnMismatch ) -from zipline.utils.calendars import get_calendar, default_nyse_schedule +from zipline.utils.calendars import get_calendar + +from zipline.testing.fixtures import WithTradingSchedule, ZiplineTestCase # Calendar is set to cover several half days, to check a case where half # days would be read out of order in cases of windows which spanned over @@ -53,10 +53,11 @@ TEST_CALENDAR_STOP = Timestamp('2015-12-31', tz='UTC') -class BcolzMinuteBarTestCase(TestCase): +class BcolzMinuteBarTestCase(WithTradingSchedule, ZiplineTestCase): @classmethod - def setUpClass(cls): + def init_class_fixtures(cls): + super(BcolzMinuteBarTestCase, cls).init_class_fixtures() trading_days = get_calendar('NYSE').trading_days( TEST_CALENDAR_START, TEST_CALENDAR_STOP ) @@ -65,10 +66,15 @@ def setUpClass(cls): cls.test_calendar_start = cls.market_opens.index[0] cls.test_calendar_stop = cls.market_opens.index[-1] - def setUp(self): + def dir_cleanup(self): + self.dir_.cleanup() + + def init_instance_fixtures(self): + super(BcolzMinuteBarTestCase, self).init_instance_fixtures() self.dir_ = TempDirectory() self.dir_.create() + self.add_instance_callback(callback=self.dir_cleanup) self.dest = self.dir_.getpath('minute_bars') os.makedirs(self.dest) self.writer = BcolzMinuteBarWriter( @@ -80,9 +86,6 @@ def setUp(self): ) self.reader = BcolzMinuteBarReader(self.dest) - def tearDown(self): - self.dir_.cleanup() - def test_write_one_ohlcv(self): minute = self.market_opens[self.test_calendar_start] sid = 1 @@ -699,9 +702,9 @@ def test_unadjusted_minutes_early_close(self): data = {sids[0]: data_1, sids[1]: data_2} start_minute_loc = \ - default_nyse_schedule.all_execution_minutes.get_loc(minutes[0]) + self.trading_schedule.all_execution_minutes.get_loc(minutes[0]) minute_locs = [ - default_nyse_schedule.all_execution_minutes.get_loc(minute) + self.trading_schedule.all_execution_minutes.get_loc(minute) - start_minute_loc for minute in minutes ] @@ -723,7 +726,7 @@ def test_adjust_non_trading_minutes(self): 'close': arange(1, 781), 'volume': arange(1, 781) } - dts = array(default_nyse_schedule.execution_minutes_for_days_in_range( + dts = array(self.trading_schedule.execution_minutes_for_days_in_range( start_day, end_day )) self.writer.write_cols(sid, dts, cols) @@ -767,7 +770,7 @@ def test_adjust_non_trading_minutes_half_days(self): 'close': arange(1, 601), 'volume': arange(1, 601) } - dts = array(default_nyse_schedule.execution_minutes_for_days_in_range( + dts = array(self.trading_schedule.execution_minutes_for_days_in_range( start_day, end_day )) self.writer.write_cols(sid, dts, cols) diff --git a/tests/pipeline/base.py b/tests/pipeline/base.py index 4e83958eac..27e7d83d8b 100644 --- a/tests/pipeline/base.py +++ b/tests/pipeline/base.py @@ -2,7 +2,6 @@ Base class for Pipeline API unittests. """ from functools import wraps -from unittest import TestCase import numpy as np from numpy import arange, prod @@ -18,10 +17,10 @@ ExplodingObject, tmp_asset_finder, ) +from zipline.testing.fixtures import ZiplineTestCase, WithTradingSchedule from zipline.utils.functional import dzip_exact from zipline.utils.pandas_utils import explode -from zipline.utils.calendars import default_nyse_schedule def with_defaults(**default_funcs): @@ -51,12 +50,14 @@ def method(self, *args, **kwargs): with_default_shape = with_defaults(shape=lambda self: self.default_shape) -class BasePipelineTestCase(TestCase): +class BasePipelineTestCase(WithTradingSchedule, ZiplineTestCase): @classmethod - def setUpClass(cls): + def init_class_fixtures(cls): + super(BasePipelineTestCase, cls).init_class_fixtures() + cls.__calendar = date_range('2014', '2015', - freq=default_nyse_schedule.day) + freq=cls.trading_schedule.day) cls.__assets = assets = Int64Index(arange(1, 20)) cls.__tmp_finder_ctx = tmp_asset_finder( equities=make_simple_equity_info( @@ -71,10 +72,6 @@ def setUpClass(cls): include_start_date=False, ) - @classmethod - def tearDownClass(cls): - cls.__tmp_finder_ctx.__exit__() - @property def default_shape(self): """Default shape for methods that build test data.""" diff --git a/tests/pipeline/test_engine.py b/tests/pipeline/test_engine.py index 986412d62a..bf155b06a5 100644 --- a/tests/pipeline/test_engine.py +++ b/tests/pipeline/test_engine.py @@ -72,7 +72,6 @@ ZiplineTestCase, ) from zipline.utils.memoize import lazyval -from zipline.utils.calendars import default_nyse_schedule class RollingSumDifference(CustomFactor): @@ -814,7 +813,7 @@ def init_class_fixtures(cls): cls.dates = date_range( cls.start, cls.end, - freq=default_nyse_schedule.day, + freq=cls.trading_schedule.day, tz='UTC', ) cls.assets = cls.asset_finder.retrieve_all(cls.asset_ids) @@ -973,7 +972,7 @@ def write_nans(self, df): def test_SMA(self): engine = SimplePipelineEngine( lambda column: self.pipeline_loader, - default_nyse_schedule.all_execution_days, + self.trading_schedule.all_execution_days, self.asset_finder, ) window_length = 5 @@ -1027,7 +1026,7 @@ def test_drawdown(self): # valuable. engine = SimplePipelineEngine( lambda column: self.pipeline_loader, - default_nyse_schedule.all_execution_days, + self.trading_schedule.all_execution_days, self.asset_finder, ) window_length = 5 @@ -1071,7 +1070,7 @@ class ParameterizedFactorTestCase(WithTradingEnvironment, ZiplineTestCase): @classmethod def init_class_fixtures(cls): super(ParameterizedFactorTestCase, cls).init_class_fixtures() - day = default_nyse_schedule.day + day = cls.trading_schedule.day cls.dates = dates = date_range( '2015-02-01', diff --git a/tests/pipeline/test_factor.py b/tests/pipeline/test_factor.py index c5873c1fd7..79a755494c 100644 --- a/tests/pipeline/test_factor.py +++ b/tests/pipeline/test_factor.py @@ -87,8 +87,8 @@ class Mask(Filter): class FactorTestCase(BasePipelineTestCase): - def setUp(self): - super(FactorTestCase, self).setUp() + def init_instance_fixtures(self): + super(FactorTestCase, self).init_instance_fixtures() self.f = F() def test_bad_input(self): diff --git a/tests/pipeline/test_filter.py b/tests/pipeline/test_filter.py index 755e5f3a65..0f8c4f3bce 100644 --- a/tests/pipeline/test_filter.py +++ b/tests/pipeline/test_filter.py @@ -75,8 +75,8 @@ class Mask(Filter): class FilterTestCase(BasePipelineTestCase): - def setUp(self): - super(FilterTestCase, self).setUp() + def init_instance_fixtures(self): + super(FilterTestCase, self).init_instance_fixtures() self.f = SomeFactor() self.g = SomeOtherFactor() diff --git a/tests/risk/test_risk_cumulative.py b/tests/risk/test_risk_cumulative.py index e080b0638b..c89677be6f 100644 --- a/tests/risk/test_risk_cumulative.py +++ b/tests/risk/test_risk_cumulative.py @@ -13,31 +13,24 @@ # See the License for the specific language governing permissions and # limitations under the License. -import unittest - import datetime import numpy as np import pytz import zipline.finance.risk as risk from zipline.utils import factory -from zipline.finance.trading import SimulationParameters, TradingEnvironment -from zipline.utils.calendars import default_nyse_schedule +from zipline.testing.fixtures import WithTradingEnvironment, ZiplineTestCase + +from zipline.finance.trading import SimulationParameters from . import answer_key ANSWER_KEY = answer_key.ANSWER_KEY -class TestRisk(unittest.TestCase): - - @classmethod - def setUpClass(cls): - cls.env = TradingEnvironment() +class TestRisk(WithTradingEnvironment, ZiplineTestCase): - @classmethod - def tearDownClass(cls): - del cls.env + def init_instance_fixtures(self): + super(TestRisk, self).init_instance_fixtures() - def setUp(self): start_date = datetime.datetime( year=2006, month=1, @@ -51,7 +44,7 @@ def setUp(self): self.sim_params = SimulationParameters( period_start=start_date, period_end=end_date, - trading_schedule=default_nyse_schedule, + trading_schedule=self.trading_schedule, ) self.algo_returns_06 = factory.create_returns_from_list( @@ -62,7 +55,7 @@ def setUp(self): self.cumulative_metrics_06 = risk.RiskMetricsCumulative( self.sim_params, treasury_curves=self.env.treasury_curves, - trading_schedule=default_nyse_schedule, + trading_schedule=self.trading_schedule, ) for dt, returns in answer_key.RETURNS_DATA.iterrows(): diff --git a/tests/risk/test_risk_period.py b/tests/risk/test_risk_period.py index 62881b949d..b230d01d9a 100644 --- a/tests/risk/test_risk_period.py +++ b/tests/risk/test_risk_period.py @@ -13,7 +13,6 @@ # See the License for the specific language governing permissions and # limitations under the License. -import unittest import datetime import calendar import numpy as np @@ -25,8 +24,8 @@ import zipline.finance.risk as risk from zipline.utils import factory -from zipline.finance.trading import SimulationParameters, TradingEnvironment -from zipline.utils.calendars import default_nyse_schedule +from zipline.finance.trading import SimulationParameters +from zipline.testing.fixtures import WithTradingEnvironment, ZiplineTestCase from . import answer_key from . answer_key import AnswerKey @@ -35,17 +34,10 @@ RETURNS = ANSWER_KEY.RETURNS -class TestRisk(unittest.TestCase): +class TestRisk(WithTradingEnvironment, ZiplineTestCase): - @classmethod - def setUpClass(cls): - cls.env = TradingEnvironment() - - @classmethod - def tearDownClass(cls): - del cls.env - - def setUp(self): + def init_instance_fixtures(self): + super(TestRisk, self).init_instance_fixtures() start_date = datetime.datetime( year=2006, @@ -60,7 +52,7 @@ def setUp(self): self.sim_params = SimulationParameters( period_start=start_date, period_end=end_date, - trading_schedule=default_nyse_schedule, + trading_schedule=self.trading_schedule, ) self.algo_returns_06 = factory.create_returns_from_list( @@ -75,7 +67,7 @@ def setUp(self): self.algo_returns_06, self.sim_params, benchmark_returns=self.benchmark_returns_06, - trading_schedule=default_nyse_schedule, + trading_schedule=self.trading_schedule, treasury_curves=self.env.treasury_curves, ) @@ -96,12 +88,9 @@ def setUp(self): self.sim_params08 = SimulationParameters( period_start=start_08, period_end=end_08, - trading_schedule=default_nyse_schedule, + trading_schedule=self.trading_schedule, ) - def tearDown(self): - return - def test_factory(self): returns = [0.1] * 100 r_objects = factory.create_returns_from_list(returns, self.sim_params) @@ -117,7 +106,7 @@ def test_drawdown(self): returns.index[0], returns.index[-1], returns, - trading_schedule=default_nyse_schedule, + trading_schedule=self.trading_schedule, benchmark_returns=self.env.benchmark_returns, treasury_curves=self.env.treasury_curves, ) @@ -145,7 +134,7 @@ def test_benchmark_returns_06(self): def test_trading_days_06(self): returns = factory.create_returns_from_range(self.sim_params) metrics = risk.RiskReport(returns, self.sim_params, - trading_schedule=default_nyse_schedule, + trading_schedule=self.trading_schedule, treasury_curves=self.env.treasury_curves, benchmark_returns=self.env.benchmark_returns) self.assertEqual([x.num_trading_days for x in metrics.year_periods], @@ -372,7 +361,7 @@ def test_benchmark_variance_06(self): def test_benchmark_returns_08(self): returns = factory.create_returns_from_range(self.sim_params08) metrics = risk.RiskReport(returns, self.sim_params08, - trading_schedule=default_nyse_schedule, + trading_schedule=self.trading_schedule, treasury_curves=self.env.treasury_curves, benchmark_returns=self.env.benchmark_returns) @@ -421,7 +410,7 @@ def test_benchmark_returns_08(self): def test_trading_days_08(self): returns = factory.create_returns_from_range(self.sim_params08) metrics = risk.RiskReport(returns, self.sim_params08, - trading_schedule=default_nyse_schedule, + trading_schedule=self.trading_schedule, treasury_curves=self.env.treasury_curves, benchmark_returns=self.env.benchmark_returns) self.assertEqual([x.num_trading_days for x in metrics.year_periods], @@ -433,7 +422,7 @@ def test_trading_days_08(self): def test_benchmark_volatility_08(self): returns = factory.create_returns_from_range(self.sim_params08) metrics = risk.RiskReport(returns, self.sim_params08, - trading_schedule=default_nyse_schedule, + trading_schedule=self.trading_schedule, treasury_curves=self.env.treasury_curves, benchmark_returns=self.env.benchmark_returns) @@ -484,7 +473,7 @@ def test_benchmark_volatility_08(self): def test_treasury_returns_06(self): returns = factory.create_returns_from_range(self.sim_params) metrics = risk.RiskReport(returns, self.sim_params, - trading_schedule=default_nyse_schedule, + trading_schedule=self.trading_schedule, treasury_curves=self.env.treasury_curves, benchmark_returns=self.env.benchmark_returns) self.assertEqual([round(x.treasury_period_return, 4) @@ -550,13 +539,13 @@ def test_partial_month(self): sim_params90s = SimulationParameters( period_start=start, period_end=end, - trading_schedule=default_nyse_schedule, + trading_schedule=self.trading_schedule, ) returns = factory.create_returns_from_range(sim_params90s) returns = returns[:-10] # truncate the returns series to end mid-month metrics = risk.RiskReport(returns, sim_params90s, - trading_schedule=default_nyse_schedule, + trading_schedule=self.trading_schedule, treasury_curves=self.env.treasury_curves, benchmark_returns=self.env.benchmark_returns) total_months = 60 @@ -566,11 +555,11 @@ def check_year_range(self, start_date, years): sim_params = SimulationParameters( period_start=start_date, period_end=start_date.replace(year=(start_date.year + years)), - trading_schedule=default_nyse_schedule, + trading_schedule=self.trading_schedule, ) returns = factory.create_returns_from_range(sim_params) metrics = risk.RiskReport(returns, self.sim_params, - trading_schedule=default_nyse_schedule, + trading_schedule=self.trading_schedule, treasury_curves=self.env.treasury_curves, benchmark_returns=self.env.benchmark_returns) total_months = years * 12 @@ -659,7 +648,7 @@ def test_sparse_benchmark(self): self.algo_returns_06, self.sim_params, benchmark_returns=benchmark_returns, - trading_schedule=default_nyse_schedule, + trading_schedule=self.trading_schedule, treasury_curves=self.env.treasury_curves, ) for risk_period in chain.from_iterable(itervalues(report.to_dict())): diff --git a/tests/test_algorithm.py b/tests/test_algorithm.py index 566c629846..2589d847d8 100644 --- a/tests/test_algorithm.py +++ b/tests/test_algorithm.py @@ -95,6 +95,7 @@ WithSimParams, WithTradingEnvironment, WithTmpDir, + WithTradingSchedule, ZiplineTestCase, ) from zipline.test_algorithms import ( @@ -164,7 +165,6 @@ import zipline.utils.events from zipline.utils.events import DateRuleFactory, TimeRuleFactory, Always import zipline.utils.factory as factory -from zipline.utils.calendars import default_nyse_schedule # Because test cases appear to reuse some resources. @@ -953,7 +953,7 @@ def test_minute_data(self, algo_class): period_end=period_end, capital_base=float("1.0e5"), data_frequency='minute', - trading_schedule=default_nyse_schedule, + trading_schedule=self.trading_schedule, ) data_portal = create_data_portal( @@ -961,7 +961,7 @@ def test_minute_data(self, algo_class): tempdir, sim_params, equities.index, - default_nyse_schedule, + self.trading_schedule, ) algo = algo_class(sim_params=sim_params, env=env) algo.run(data_portal) @@ -1556,9 +1556,9 @@ def handle_data(context, data): ) set_algo_instance(test_algo) trades = factory.create_daily_trade_source( - [0], self.sim_params, self.env, default_nyse_schedule) + [0], self.sim_params, self.env, self.trading_schedule) data_portal = create_data_portal_from_trade_history( - self.env, default_nyse_schedule, tempdir, self.sim_params, + self.env, self.trading_schedule, tempdir, self.sim_params, {0: trades}) results = test_algo.run(data_portal) @@ -1631,7 +1631,7 @@ def test_order_dead_asset(self): params = SimulationParameters( period_start=pd.Timestamp("2007-01-03", tz='UTC'), period_end=pd.Timestamp("2007-01-05", tz='UTC'), - trading_schedule=default_nyse_schedule, + trading_schedule=self.trading_schedule, ) # order method shouldn't blow up @@ -2110,7 +2110,7 @@ def test_set_max_order_count(self): tempdir, sim_params, [1], - default_nyse_schedule, + self.trading_schedule, ) def handle_data(algo, data): @@ -2232,7 +2232,7 @@ def test_asset_date_bounds(self): tempdir, self.sim_params, [0], - default_nyse_schedule, + self.trading_schedule, ) algo.run(data_portal) @@ -2247,7 +2247,7 @@ def test_asset_date_bounds(self): tempdir, self.sim_params, [0], - default_nyse_schedule, + self.trading_schedule, ) algo = SetAssetDateBoundsAlgorithm( sim_params=self.sim_params, @@ -2267,7 +2267,7 @@ def test_asset_date_bounds(self): tempdir, self.sim_params, [0], - default_nyse_schedule, + self.trading_schedule, ) algo = SetAssetDateBoundsAlgorithm( sim_params=self.sim_params, @@ -2293,7 +2293,7 @@ def make_daily_bar_data(cls): [100, 100, 100, 300], timedelta(days=1), cls.sim_params, - default_nyse_schedule, + cls.trading_schedule, ), }, index=cls.sim_params.trading_days, @@ -2440,7 +2440,7 @@ def make_daily_bar_data(cls): [1e9, 1e9, 1e9], timedelta(days=1), cls.sim_params, - default_nyse_schedule, + cls.trading_schedule, ), }, index=cls.sim_params.trading_days, @@ -2450,7 +2450,7 @@ def make_daily_bar_data(cls): def test_flip_algo(self): metadata = {1: {'symbol': 'TEST', 'start_date': self.sim_params.trading_days[0], - 'end_date': default_nyse_schedule.next_execution_day( + 'end_date': self.trading_schedule.next_execution_day( self.sim_params.trading_days[-1]), 'multiplier': 5}} @@ -2595,7 +2595,7 @@ def prep_algo(self, cancelation_string, data_frequency="minute", sim_params=SimulationParameters( period_start=self.sim_params.period_start, period_end=self.sim_params.period_end, - trading_schedule=default_nyse_schedule, + trading_schedule=self.trading_schedule, data_frequency=data_frequency ) ) @@ -2770,7 +2770,7 @@ def handle_data(context, data): self.assertEqual(algo.data_lengths, self.live_asset_counts) -class TestEquityAutoClose(WithTmpDir, ZiplineTestCase): +class TestEquityAutoClose(WithTmpDir, WithTradingSchedule, ZiplineTestCase): """ Tests if delisted equities are properly removed from a portfolio holding positions in said equities. @@ -2778,7 +2778,7 @@ class TestEquityAutoClose(WithTmpDir, ZiplineTestCase): @classmethod def init_class_fixtures(cls): super(TestEquityAutoClose, cls).init_class_fixtures() - trading_days = default_nyse_schedule.all_execution_days + trading_days = cls.trading_schedule.all_execution_days start_date = pd.Timestamp('2015-01-05', tz='UTC') start_date_loc = trading_days.get_loc(start_date) test_duration = 7 @@ -2794,7 +2794,7 @@ def make_data(self, auto_close_delta, frequency, num_assets=3, start_date=self.test_days[0], first_end=self.first_asset_expiration, - frequency=default_nyse_schedule.day, + frequency=self.trading_schedule.day, periods_between_ends=2, auto_close_delta=auto_close_delta, ) @@ -2802,10 +2802,10 @@ def make_data(self, auto_close_delta, frequency, sids = asset_info.index env = self.enter_instance_context(tmp_trading_env(equities=asset_info)) - market_opens = default_nyse_schedule.schedule.market_open.loc[ + market_opens = self.trading_schedule.schedule.market_open.loc[ self.test_days ] - market_closes = default_nyse_schedule.schedule.market_close.loc[ + market_closes = self.trading_schedule.schedule.market_close.loc[ self.test_days ] @@ -2827,11 +2827,11 @@ def make_data(self, auto_close_delta, frequency, iteritems(trade_data_by_sid), ) data_portal = DataPortal( - env, default_nyse_schedule, + env, self.trading_schedule, equity_daily_reader=BcolzDailyBarReader(path) ) elif frequency == 'minute': - dates = default_nyse_schedule.execution_minutes_for_days_in_range( + dates = self.trading_schedule.execution_minutes_for_days_in_range( self.test_days[0], self.test_days[-1], ) @@ -2855,7 +2855,7 @@ def make_data(self, auto_close_delta, frequency, frequency=frequency ) data_portal = DataPortal( - env, default_nyse_schedule, + env, self.trading_schedule, equity_minute_reader=BcolzMinuteBarReader(self.tmpdir.path) ) else: @@ -2880,7 +2880,7 @@ def make_data(self, auto_close_delta, frequency, else: final_prices = { asset.sid: trade_data_by_sid[asset.sid].loc[ - default_nyse_schedule.start_and_end(asset.end_date)[1] + self.trading_schedule.start_and_end(asset.end_date)[1] ].close for asset in assets } @@ -2952,7 +2952,7 @@ def test_daily_delisted_equities(self, Make sure that after an equity gets delisted, our portfolio holds the correct number of equities and correct amount of cash. """ - auto_close_delta = default_nyse_schedule.day * auto_close_lag + auto_close_delta = self.trading_schedule.day * auto_close_lag resources = self.make_data(auto_close_delta, 'daily', capital_base) assets = resources.assets @@ -3112,7 +3112,7 @@ def test_cancel_open_orders(self): canceled. Unless an equity is auto closed, any open orders for that equity will persist indefinitely. """ - auto_close_delta = default_nyse_schedule.day + auto_close_delta = self.trading_schedule.day resources = self.make_data(auto_close_delta, 'daily') env = resources.env assets = resources.assets @@ -3184,7 +3184,7 @@ def orders_for_date(date): ) def test_minutely_delisted_equities(self): - resources = self.make_data(default_nyse_schedule.day, 'minute') + resources = self.make_data(self.trading_schedule.day, 'minute') env = resources.env assets = resources.assets @@ -3372,7 +3372,7 @@ def handle_data(context, data): sim_params=SimulationParameters( period_start=pd.Timestamp("2016-01-06", tz='UTC'), period_end=pd.Timestamp("2016-01-07", tz='UTC'), - trading_schedule=default_nyse_schedule, + trading_schedule=self.trading_schedule, data_frequency="minute" ) ) diff --git a/tests/test_api_shim.py b/tests/test_api_shim.py index a99ebd7b56..4b3363df10 100644 --- a/tests/test_api_shim.py +++ b/tests/test_api_shim.py @@ -19,7 +19,6 @@ ZiplineTestCase, ) from zipline.zipline_warnings import ZiplineDeprecationWarning -from zipline.utils.calendars import default_nyse_schedule simple_algo = """ from zipline.api import sid, order @@ -125,7 +124,7 @@ class TestAPIShim(WithDataPortal, WithSimParams, ZiplineTestCase): def make_minute_bar_data(cls): return { sid: create_minute_df_for_asset( - default_nyse_schedule, + cls.trading_schedule, cls.SIM_PARAMS_START, cls.SIM_PARAMS_END, ) @@ -136,7 +135,7 @@ def make_minute_bar_data(cls): def make_daily_bar_data(cls): for sid in cls.sids: yield sid, create_daily_df_for_asset( - default_nyse_schedule, + cls.trading_schedule, cls.SIM_PARAMS_START, cls.SIM_PARAMS_END, ) @@ -182,10 +181,10 @@ def test_old_new_data_api_paths(self): similar) and the new data API(data.current(sid(N), field) and similar) hit the same code paths on the DataPortal. """ - test_start_minute = default_nyse_schedule.execution_minutes_for_day( + test_start_minute = self.trading_schedule.execution_minutes_for_day( self.sim_params.trading_days[0] )[1] - test_end_minute = default_nyse_schedule.execution_minutes_for_day( + test_end_minute = self.trading_schedule.execution_minutes_for_day( self.sim_params.trading_days[0] )[-1] bar_data = BarData( @@ -263,7 +262,7 @@ def assert_get_history_window_called(fun, is_legacy): period_start=test_start_minute, period_end=test_end_minute, data_frequency="minute", - trading_schedule=default_nyse_schedule, + trading_schedule=self.trading_schedule, ) history_algorithm = self.create_algo( @@ -384,7 +383,7 @@ def test_history(self): capital_base=self.sim_params.capital_base, data_frequency=self.sim_params.data_frequency, emission_rate=self.sim_params.emission_rate, - trading_schedule=default_nyse_schedule, + trading_schedule=self.trading_schedule, ) algo = self.create_algo(history_algo, @@ -427,7 +426,7 @@ def test_simple_transforms(self): period_start=self.sim_params.trading_days[8], period_end=self.sim_params.trading_days[-1], data_frequency="minute", - trading_schedule=default_nyse_schedule, + trading_schedule=self.trading_schedule, ) algo = self.create_algo(simple_transforms_algo, diff --git a/tests/test_assets.py b/tests/test_assets.py index de1409c1e7..44b347cfbd 100644 --- a/tests/test_assets.py +++ b/tests/test_assets.py @@ -82,8 +82,8 @@ from zipline.testing.fixtures import ( WithAssetFinder, ZiplineTestCase, + WithTradingSchedule, ) -from zipline.utils.calendars import default_nyse_schedule @contextmanager @@ -396,7 +396,7 @@ def test_lookup_future_symbol(self): TestFuture.asset_finder.lookup_future_symbol('XXX99') -class AssetFinderTestCase(ZiplineTestCase): +class AssetFinderTestCase(WithTradingSchedule, ZiplineTestCase): asset_finder_type = AssetFinder def write_assets(self, **kwargs): @@ -776,7 +776,7 @@ def test_map_identifier_index_to_sids(self): def test_compute_lifetimes(self): num_assets = 4 - trading_day = default_nyse_schedule.day + trading_day = self.trading_schedule.day first_start = pd.Timestamp('2015-04-01', tz='UTC') frame = make_rotating_equity_info( diff --git a/tests/test_bar_data.py b/tests/test_bar_data.py index 5ef70b4ce6..b2555c5ecb 100644 --- a/tests/test_bar_data.py +++ b/tests/test_bar_data.py @@ -29,7 +29,6 @@ WithDataPortal, ZiplineTestCase, ) -from zipline.utils.calendars import default_nyse_schedule OHLC = ["open", "high", "low", "close"] OHLCP = OHLC + ["price"] @@ -617,7 +616,7 @@ def make_adjustment_writer_daily_bar_reader(cls): def make_daily_bar_data(cls): for sid in cls.sids: yield sid, create_daily_df_for_asset( - default_nyse_schedule, + cls.trading_schedule, cls.bcolz_daily_bar_days[0], cls.bcolz_daily_bar_days[-1], interval=2 - sid % 2 diff --git a/tests/test_benchmark.py b/tests/test_benchmark.py index 77b939ae8d..435af88532 100644 --- a/tests/test_benchmark.py +++ b/tests/test_benchmark.py @@ -30,12 +30,13 @@ from zipline.testing.fixtures import ( WithDataPortal, WithSimParams, + WithTradingSchedule, ZiplineTestCase, ) -from zipline.utils.calendars import default_nyse_schedule -class TestBenchmark(WithDataPortal, WithSimParams, ZiplineTestCase): +class TestBenchmark(WithDataPortal, WithSimParams, WithTradingSchedule, + ZiplineTestCase): START_DATE = pd.Timestamp('2006-01-03', tz='utc') END_DATE = pd.Timestamp('2006-12-29', tz='utc') @@ -86,7 +87,7 @@ def test_normal(self): days_to_use = self.sim_params.trading_days[1:] source = BenchmarkSource( - 1, self.env, default_nyse_schedule, days_to_use, self.data_portal + 1, self.env, self.trading_schedule, days_to_use, self.data_portal ) # should be the equivalent of getting the price history, then doing @@ -108,7 +109,7 @@ def test_asset_not_trading(self): BenchmarkSource( 3, self.env, - default_nyse_schedule, + self.trading_schedule, self.sim_params.trading_days[1:], self.data_portal ) @@ -123,7 +124,7 @@ def test_asset_not_trading(self): BenchmarkSource( 3, self.env, - default_nyse_schedule, + self.trading_schedule, self.sim_params.trading_days[120:], self.data_portal ) @@ -137,7 +138,7 @@ def test_asset_not_trading(self): def test_asset_IPOed_same_day(self): # gotta get some minute data up in here. # add sid 4 for a couple of days - minutes = default_nyse_schedule.execution_minutes_for_days_in_range( + minutes = self.trading_schedule.execution_minutes_for_days_in_range( self.sim_params.trading_days[0], self.sim_params.trading_days[5] ) @@ -187,7 +188,7 @@ def test_no_stock_dividends_allowed(self): with self.assertRaises(InvalidBenchmarkAsset) as exc: BenchmarkSource( - 4, self.env, default_nyse_schedule, + 4, self.env, self.trading_schedule, self.sim_params.trading_days, self.data_portal ) diff --git a/tests/test_data_portal.py b/tests/test_data_portal.py index c64ab447b0..116e529d08 100644 --- a/tests/test_data_portal.py +++ b/tests/test_data_portal.py @@ -16,7 +16,6 @@ from zipline.data.data_portal import DataPortal from zipline.testing.fixtures import WithTradingEnvironment, ZiplineTestCase -from zipline.utils.calendars import default_nyse_schedule import pandas as pd @@ -27,7 +26,7 @@ class TestDataPortal(WithTradingEnvironment, ZiplineTestCase): def init_instance_fixtures(self): super(TestDataPortal, self).init_instance_fixtures() - self.data_portal = DataPortal(self.env, default_nyse_schedule) + self.data_portal = DataPortal(self.env, self.trading_schedule) def test_bar_count_for_simple_transforms(self): # July 2015 @@ -41,7 +40,7 @@ def test_bar_count_for_simple_transforms(self): # half an hour into july 9, getting a 4-"day" window should get us # all the minutes of 7/6, 7/7, 7/8, and 31 minutes of 7/9 - july_9_dt = default_nyse_schedule.start_and_end( + july_9_dt = self.trading_schedule.start_and_end( pd.Timestamp("2015-07-09") )[0] + Timedelta("30 minutes") @@ -64,7 +63,7 @@ def test_bar_count_for_simple_transforms(self): # half an hour into nov 30, getting a 4-"day" window should get us # all the minutes of 11/24, 11/25, 11/27 (half day!), and 31 minutes # of 11/30 - nov_30_dt = default_nyse_schedule.start_and_end( + nov_30_dt = self.trading_schedule.start_and_end( pd.Timestamp("2015-11-30") )[0] + Timedelta("30 minutes") diff --git a/tests/test_fetcher.py b/tests/test_fetcher.py index f760b21ed5..5a1101ed58 100644 --- a/tests/test_fetcher.py +++ b/tests/test_fetcher.py @@ -28,7 +28,6 @@ WithSimParams, ZiplineTestCase, ) -from zipline.utils.calendars import default_nyse_schedule from .resources.fetcher_inputs.fetcher_test_data import ( AAPL_CSV_DATA, AAPL_IBM_CSV_DATA, @@ -110,7 +109,7 @@ def run_algo(self, code, sim_params=None, data_frequency="daily"): ) results = test_algo.run(FetcherDataPortal(self.env, - default_nyse_schedule)) + self.trading_schedule)) return results @@ -144,7 +143,7 @@ def handle_data(context, data): # the minutely emission packets here. TradingAlgorithm.run() only # returns daily packets. test_algo.data_portal = FetcherDataPortal(self.env, - default_nyse_schedule) + self.trading_schedule) gen = test_algo.get_generator() perf_packets = list(gen) diff --git a/tests/test_finance.py b/tests/test_finance.py index 49d203e5a6..ba3da7ca78 100644 --- a/tests/test_finance.py +++ b/tests/test_finance.py @@ -29,7 +29,6 @@ from zipline.assets.synthetic import make_simple_equity_info from zipline.finance.blotter import Blotter from zipline.finance.execution import MarketOrder, LimitOrder -from zipline.finance.trading import TradingEnvironment from zipline.finance.performance import PerformanceTracker from zipline.finance.trading import SimulationParameters from zipline.data.us_equity_pricing import BcolzDailyBarReader @@ -49,10 +48,6 @@ ) import zipline.utils.factory as factory -from zipline.utils.calendars import ( - default_nyse_schedule, - get_calendar, -) DEFAULT_TIMEOUT = 15 # seconds EXTENDED_TIMEOUT = 90 @@ -202,7 +197,7 @@ def transaction_sim(self, **params): data_frequency="minute" ) - minutes = default_nyse_schedule.execution_minute_window( + minutes = self.trading_schedule.execution_minute_window( sim_params.first_open, int((trade_interval.total_seconds() / 60) * trade_count) + 100) @@ -220,8 +215,8 @@ def transaction_sim(self, **params): } write_bcolz_minute_data( - default_nyse_schedule, - default_nyse_schedule.execution_days_in_range(minutes[0], + self.trading_schedule, + self.trading_schedule.execution_days_in_range(minutes[0], minutes[-1]), tempdir.path, assets @@ -230,7 +225,7 @@ def transaction_sim(self, **params): equity_minute_reader = BcolzMinuteBarReader(tempdir.path) data_portal = DataPortal( - env, default_nyse_schedule, + env, self.trading_schedule, equity_minute_reader=equity_minute_reader, ) else: @@ -257,7 +252,7 @@ def transaction_sim(self, **params): equity_daily_reader = BcolzDailyBarReader(path) data_portal = DataPortal( - env, default_nyse_schedule, + env, self.trading_schedule, equity_daily_reader=equity_daily_reader, ) @@ -277,7 +272,7 @@ def transaction_sim(self, **params): else: alternator = 1 - tracker = PerformanceTracker(sim_params, default_nyse_schedule, + tracker = PerformanceTracker(sim_params, self.trading_schedule, self.env) # replicate what tradesim does by going through every minute or day @@ -394,7 +389,7 @@ def test_simulation_parameters(self): period_start=datetime(2008, 1, 1, tzinfo=pytz.utc), period_end=datetime(2008, 12, 31, tzinfo=pytz.utc), capital_base=100000, - trading_schedule=default_nyse_schedule, + trading_schedule=self.trading_schedule, ) self.assertTrue(sp.last_close.month == 12) @@ -415,7 +410,7 @@ def test_sim_params_days_in_period(self): period_start=datetime(2007, 12, 31, tzinfo=pytz.utc), period_end=datetime(2008, 1, 7, tzinfo=pytz.utc), capital_base=100000, - trading_schedule=default_nyse_schedule, + trading_schedule=self.trading_schedule, ) expected_trading_days = ( diff --git a/tests/test_history.py b/tests/test_history.py index 9c77b4bff8..88485887d0 100644 --- a/tests/test_history.py +++ b/tests/test_history.py @@ -24,7 +24,6 @@ str_to_seconds, MockDailyBarReader, ) -from zipline.utils.calendars import default_nyse_schedule from zipline.testing.fixtures import ( WithBcolzMinuteBarReader, WithDataPortal, @@ -79,7 +78,7 @@ class WithHistory(WithDataPortal): @classmethod def init_class_fixtures(cls): super(WithHistory, cls).init_class_fixtures() - cls.trading_days = default_nyse_schedule.execution_days_in_range( + cls.trading_days = cls.trading_schedule.execution_days_in_range( start=cls.TRADING_START_DT, end=cls.TRADING_END_DT ) @@ -453,14 +452,14 @@ def make_minute_bar_data(cls): for sid in sids: asset = cls.asset_finder.retrieve_asset(sid) data[sid] = create_minute_df_for_asset( - default_nyse_schedule, + cls.trading_schedule, asset.start_date, asset.end_date, start_val=2, ) data[1] = create_minute_df_for_asset( - default_nyse_schedule, + cls.trading_schedule, pd.Timestamp('2014-01-03', tz='utc'), pd.Timestamp('2016-01-30', tz='utc'), start_val=2, @@ -468,7 +467,7 @@ def make_minute_bar_data(cls): asset3 = cls.asset_finder.retrieve_asset(3) data[3] = create_minute_df_for_asset( - default_nyse_schedule, + cls.trading_schedule, asset3.start_date, asset3.end_date, start_val=2, @@ -498,7 +497,7 @@ def handle_data(context, data): capital_base=float('1.0e5'), data_frequency='minute', emission_rate='daily', - trading_schedule=default_nyse_schedule, + trading_schedule=self.trading_schedule, ) test_algo = TradingAlgorithm( @@ -514,8 +513,8 @@ def handle_data(context, data): def test_minute_before_assets_trading(self): # 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( + minutes = self.trading_schedule.execution_minutes_for_day( + self.trading_schedule.previous_execution_day(pd.Timestamp( '2015-01-05', tz='UTC' )) )[0:60] @@ -564,7 +563,7 @@ def test_minute_regular(self, name, field, sid): # 10 minutes asset = self.env.asset_finder.retrieve_asset(sid) - minutes = default_nyse_schedule.execution_minutes_for_day( + minutes = self.trading_schedule.execution_minutes_for_day( pd.Timestamp('2015-01-05', tz='UTC') )[0:60] @@ -575,8 +574,8 @@ def test_minute_regular(self, name, field, sid): def test_minute_midnight(self): midnight = pd.Timestamp('2015-01-06', tz='UTC') - last_minute = default_nyse_schedule.start_and_end( - default_nyse_schedule.previous_execution_day(midnight) + last_minute = self.trading_schedule.start_and_end( + self.trading_schedule.previous_execution_day(midnight) )[1] midnight_bar_data = \ @@ -595,7 +594,7 @@ def test_minute_midnight(self): def test_minute_after_asset_stopped(self): # SHORT_ASSET's last day was 2015-01-06 # get some history windows that straddle the end - minutes = default_nyse_schedule.execution_minutes_for_day( + minutes = self.trading_schedule.execution_minutes_for_day( pd.Timestamp('2015-01-07', tz='UTC') )[0:60] @@ -690,7 +689,7 @@ def test_minute_splits_and_mergers(self): # before any of the adjustments, last 10 minutes of jan 5 window1 = self.data_portal.get_history_window( [asset], - default_nyse_schedule.start_and_end(jan5)[1], + self.trading_schedule.start_and_end(jan5)[1], 10, '1m', 'close' @@ -916,21 +915,21 @@ def test_minute_early_close(self): def test_minute_different_lifetimes(self): # at trading start, only asset1 existed - day = default_nyse_schedule.next_execution_day(self.TRADING_START_DT) + day = self.trading_schedule.next_execution_day(self.TRADING_START_DT) asset1_minutes = \ - default_nyse_schedule.execution_minutes_for_days_in_range( + self.trading_schedule.execution_minutes_for_days_in_range( start=self.ASSET1.start_date, end=self.ASSET1.end_date ) asset1_idx = asset1_minutes.searchsorted( - default_nyse_schedule.start_and_end(day)[0] + self.trading_schedule.start_and_end(day)[0] ) window = self.data_portal.get_history_window( [self.ASSET1, self.ASSET2], - default_nyse_schedule.start_and_end(day)[0], + self.trading_schedule.start_and_end(day)[0], 100, '1m', 'close' @@ -949,7 +948,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) - first_day_minutes = default_nyse_schedule.execution_minutes_for_day( + first_day_minutes = self.trading_schedule.execution_minutes_for_day( self.TRADING_START_DT ) exp_msg = ( @@ -995,15 +994,15 @@ def make_minute_bar_data(cls): asset2 = cls.asset_finder.retrieve_asset(2) return { asset1.sid: create_minute_df_for_asset( - default_nyse_schedule, + cls.trading_schedule, asset1.start_date, asset1.end_date, start_val=2, ), asset2.sid: create_minute_df_for_asset( - default_nyse_schedule, + cls.trading_schedule, asset2.start_date, - default_nyse_schedule.previous_execution_day(asset2.end_date), + cls.trading_schedule.previous_execution_day(asset2.end_date), start_val=2, minute_blacklist=[ pd.Timestamp('2015-01-08 14:31', tz='UTC'), @@ -1015,8 +1014,8 @@ def make_minute_bar_data(cls): @classmethod def create_df_for_asset(cls, start_day, end_day, interval=1, force_zeroes=False): - days = default_nyse_schedule.execution_days_in_range(start_day, - end_day) + days = cls.trading_schedule.execution_days_in_range(start_day, + end_day) days_count = len(days) # default to 2 because the low array subtracts 1, and we don't @@ -1045,7 +1044,7 @@ def create_df_for_asset(cls, start_day, end_day, interval=1, def test_daily_before_assets_trading(self): # asset2 and asset3 both started trading in 2015 - days = default_nyse_schedule.execution_days_in_range( + days = self.trading_schedule.execution_days_in_range( start=pd.Timestamp('2014-12-15', tz='UTC'), end=pd.Timestamp('2014-12-18', tz='UTC'), ) @@ -1083,9 +1082,9 @@ def test_daily_regular(self): # get the first 30 days of 2015 jan5 = pd.Timestamp('2015-01-04') - days = default_nyse_schedule.execution_days_in_range( + days = self.trading_schedule.execution_days_in_range( start=jan5, - end=default_nyse_schedule.add_execution_days(30, jan5) + end=self.trading_schedule.add_execution_days(30, jan5) ) for idx, day in enumerate(days): @@ -1128,7 +1127,7 @@ def test_daily_some_assets_stopped(self): def test_daily_after_asset_stopped(self): # SHORT_ASSET trades on 1/5, 1/6, that's it. - days = default_nyse_schedule.execution_days_in_range( + days = self.trading_schedule.execution_days_in_range( start=pd.Timestamp('2015-01-07', tz='UTC'), end=pd.Timestamp('2015-01-08', tz='UTC') ) @@ -1322,7 +1321,7 @@ def test_daily_history_blended(self): # January 2015 has both daily and minute data for ASSET2 day = pd.Timestamp('2015-01-07', tz='UTC') - minutes = default_nyse_schedule.execution_minutes_for_day(day) + minutes = self.trading_schedule.execution_minutes_for_day(day) # minute data, baseline: # Jan 5: 2 to 391 @@ -1378,7 +1377,7 @@ def test_daily_history_blended_gaps(self, field): # January 2015 has both daily and minute data for ASSET2 day = pd.Timestamp('2015-01-08', tz='UTC') - minutes = default_nyse_schedule.execution_minutes_for_day(day) + minutes = self.trading_schedule.execution_minutes_for_day(day) # minute data, baseline: # Jan 5: 2 to 391 @@ -1462,7 +1461,7 @@ def test_history_window_before_first_trading_day(self): # 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( + second_day = self.trading_schedule.next_execution_day( self.TRADING_START_DT ) @@ -1491,7 +1490,7 @@ def test_history_window_before_first_trading_day(self): # Use a minute to force minute mode. first_minute = \ - default_nyse_schedule.schedule.market_open[self.TRADING_START_DT] + self.trading_schedule.schedule.market_open[self.TRADING_START_DT] with self.assertRaisesRegexp(HistoryWindowStartsBeforeData, exp_msg): self.data_portal.get_history_window( @@ -1623,7 +1622,7 @@ def init_instance_fixtures(self): # Set up a fresh data portal for each test, since order of calling # needs to be tested. self.equity_daily_aggregator = DailyHistoryAggregator( - default_nyse_schedule.schedule.market_open, + self.trading_schedule.schedule.market_open, self.bcolz_minute_bar_reader, ) diff --git a/tests/test_perf_tracking.py b/tests/test_perf_tracking.py index 7f01ebda8d..63283dec10 100644 --- a/tests/test_perf_tracking.py +++ b/tests/test_perf_tracking.py @@ -58,6 +58,7 @@ WithSimParams, WithTmpDir, WithTradingEnvironment, + WithTradingSchedule, ZiplineTestCase, ) from zipline.utils.calendars import default_nyse_schedule @@ -280,7 +281,7 @@ def test_multiple_splits(self): # if multiple positions all have splits at the same time, verify that # the total leftover cash is correct perf_tracker = perf.PerformanceTracker(self.sim_params, - default_nyse_schedule, + self.trading_schedule, self.env) asset1 = self.asset_finder.retrieve_asset(1) @@ -310,14 +311,14 @@ def test_split_long_position(self): [100, 100], oneday, self.sim_params, - trading_schedule=default_nyse_schedule, + trading_schedule=self.trading_schedule, ) # set up a long position in sid 1 # 100 shares at $20 apiece = $2000 position data_portal = create_data_portal_from_trade_history( self.env, - default_nyse_schedule, + self.trading_schedule, self.tmpdir, self.sim_params, {1: events}, @@ -420,7 +421,7 @@ def test_commission_event(self): [100, 100, 100, 100, 100], oneday, self.sim_params, - trading_schedule=default_nyse_schedule, + trading_schedule=self.trading_schedule, ) # Test commission models and validate result @@ -432,7 +433,7 @@ def test_commission_event(self): data_portal = create_data_portal_from_trade_history( self.env, - default_nyse_schedule, + self.trading_schedule, self.tmpdir, self.sim_params, {1: trade_events}, @@ -512,12 +513,12 @@ def test_commission_zero_position(self): [100, 100, 100, 100, 100], oneday, self.sim_params, - trading_schedule=default_nyse_schedule, + trading_schedule=self.trading_schedule, ) data_portal = create_data_portal_from_trade_history( self.env, - default_nyse_schedule, + self.trading_schedule, self.tmpdir, self.sim_params, {1: events}, @@ -555,12 +556,12 @@ def test_commission_no_position(self): [100, 100, 100, 100, 100], oneday, self.sim_params, - trading_schedule=default_nyse_schedule, + trading_schedule=self.trading_schedule, ) data_portal = create_data_portal_from_trade_history( self.env, - default_nyse_schedule, + self.trading_schedule, self.tmpdir, self.sim_params, {1: events}, @@ -602,7 +603,7 @@ def test_market_hours_calculations(self): after = factory.get_next_trading_dt( before, timedelta(days=1), - default_nyse_schedule, + self.trading_schedule, ) self.assertEqual(after.hour, 13) @@ -614,7 +615,7 @@ def test_long_position_receives_dividend(self): [100, 100, 100, 100, 100, 100], oneday, self.sim_params, - trading_schedule=default_nyse_schedule, + trading_schedule=self.trading_schedule, ) dbpath = self.instance_tmpdir.getpath('adjustments.sqlite') @@ -622,7 +623,7 @@ def test_long_position_receives_dividend(self): writer = SQLiteAdjustmentWriter( dbpath, MockDailyBarReader(), - default_nyse_schedule.all_execution_days, + self.trading_schedule.all_execution_days, ) splits = mergers = create_empty_splits_mergers_frame() dividends = pd.DataFrame({ @@ -637,7 +638,7 @@ def test_long_position_receives_dividend(self): adjustment_reader = SQLiteAdjustmentReader(dbpath) data_portal = create_data_portal_from_trade_history( self.env, - default_nyse_schedule, + self.trading_schedule, self.instance_tmpdir, self.sim_params, {1: events}, @@ -680,7 +681,7 @@ def test_long_position_receives_stock_dividend(self): [100, 100, 100, 100, 100, 100], oneday, self.sim_params, - trading_schedule=default_nyse_schedule, + trading_schedule=self.trading_schedule, ) dbpath = self.instance_tmpdir.getpath('adjustments.sqlite') @@ -688,7 +689,7 @@ def test_long_position_receives_stock_dividend(self): writer = SQLiteAdjustmentWriter( dbpath, MockDailyBarReader(), - default_nyse_schedule.all_execution_days, + self.trading_schedule.all_execution_days, ) splits = mergers = create_empty_splits_mergers_frame() dividends = pd.DataFrame({ @@ -714,7 +715,7 @@ def test_long_position_receives_stock_dividend(self): data_portal = create_data_portal_from_trade_history( self.env, - default_nyse_schedule, + self.trading_schedule, self.instance_tmpdir, self.sim_params, events, @@ -755,7 +756,7 @@ def test_long_position_purchased_on_ex_date_receives_no_dividend(self): [100, 100, 100, 100, 100, 100], oneday, self.sim_params, - trading_schedule=default_nyse_schedule, + trading_schedule=self.trading_schedule, ) dbpath = self.instance_tmpdir.getpath('adjustments.sqlite') @@ -763,7 +764,7 @@ def test_long_position_purchased_on_ex_date_receives_no_dividend(self): writer = SQLiteAdjustmentWriter( dbpath, MockDailyBarReader(), - default_nyse_schedule.all_execution_days, + self.trading_schedule.all_execution_days, ) splits = mergers = create_empty_splits_mergers_frame() dividends = pd.DataFrame({ @@ -779,7 +780,7 @@ def test_long_position_purchased_on_ex_date_receives_no_dividend(self): data_portal = create_data_portal_from_trade_history( self.env, - default_nyse_schedule, + self.trading_schedule, self.instance_tmpdir, self.sim_params, {1: events}, @@ -817,7 +818,7 @@ def test_selling_before_dividend_payment_still_gets_paid(self): [100, 100, 100, 100, 100, 100], oneday, self.sim_params, - trading_schedule=default_nyse_schedule, + trading_schedule=self.trading_schedule, ) dbpath = self.instance_tmpdir.getpath('adjustments.sqlite') @@ -825,7 +826,7 @@ def test_selling_before_dividend_payment_still_gets_paid(self): writer = SQLiteAdjustmentWriter( dbpath, MockDailyBarReader(), - default_nyse_schedule.all_execution_days, + self.trading_schedule.all_execution_days, ) splits = mergers = create_empty_splits_mergers_frame() dividends = pd.DataFrame({ @@ -841,7 +842,7 @@ def test_selling_before_dividend_payment_still_gets_paid(self): data_portal = create_data_portal_from_trade_history( self.env, - default_nyse_schedule, + self.trading_schedule, self.instance_tmpdir, self.sim_params, {1: events}, @@ -880,14 +881,14 @@ def test_buy_and_sell_before_ex(self): [100, 100, 100, 100, 100, 100], oneday, self.sim_params, - trading_schedule=default_nyse_schedule, + trading_schedule=self.trading_schedule, ) dbpath = self.instance_tmpdir.getpath('adjustments.sqlite') writer = SQLiteAdjustmentWriter( dbpath, MockDailyBarReader(), - default_nyse_schedule.all_execution_days, + self.trading_schedule.all_execution_days, ) splits = mergers = create_empty_splits_mergers_frame() @@ -904,7 +905,7 @@ def test_buy_and_sell_before_ex(self): data_portal = create_data_portal_from_trade_history( self.env, - default_nyse_schedule, + self.trading_schedule, self.instance_tmpdir, self.sim_params, {1: events}, @@ -941,21 +942,21 @@ def test_ending_before_pay_date(self): [100, 100, 100, 100, 100, 100], oneday, self.sim_params, - trading_schedule=default_nyse_schedule, + trading_schedule=self.trading_schedule, ) pay_date = self.sim_params.first_open # find pay date that is much later. for i in range(30): pay_date = factory.get_next_trading_dt(pay_date, oneday, - default_nyse_schedule) + self.trading_schedule) dbpath = self.instance_tmpdir.getpath('adjustments.sqlite') writer = SQLiteAdjustmentWriter( dbpath, MockDailyBarReader(), - default_nyse_schedule.all_execution_days, + self.trading_schedule.all_execution_days, ) splits = mergers = create_empty_splits_mergers_frame() dividends = pd.DataFrame({ @@ -971,7 +972,7 @@ def test_ending_before_pay_date(self): data_portal = create_data_portal_from_trade_history( self.env, - default_nyse_schedule, + self.trading_schedule, self.instance_tmpdir, self.sim_params, {1: events}, @@ -1009,7 +1010,7 @@ def test_short_position_pays_dividend(self): [100, 100, 100, 100, 100, 100], oneday, self.sim_params, - trading_schedule=default_nyse_schedule, + trading_schedule=self.trading_schedule, ) dbpath = self.instance_tmpdir.getpath('adjustments.sqlite') @@ -1017,7 +1018,7 @@ def test_short_position_pays_dividend(self): writer = SQLiteAdjustmentWriter( dbpath, MockDailyBarReader(), - default_nyse_schedule.all_execution_days, + self.trading_schedule.all_execution_days, ) splits = mergers = create_empty_splits_mergers_frame() dividends = pd.DataFrame({ @@ -1033,7 +1034,7 @@ def test_short_position_pays_dividend(self): data_portal = create_data_portal_from_trade_history( self.env, - default_nyse_schedule, + self.trading_schedule, self.instance_tmpdir, self.sim_params, {1: events}, @@ -1068,7 +1069,7 @@ def test_no_position_receives_no_dividend(self): [100, 100, 100, 100, 100, 100], oneday, self.sim_params, - trading_schedule=default_nyse_schedule, + trading_schedule=self.trading_schedule, ) dbpath = self.instance_tmpdir.getpath('adjustments.sqlite') @@ -1076,7 +1077,7 @@ def test_no_position_receives_no_dividend(self): writer = SQLiteAdjustmentWriter( dbpath, MockDailyBarReader(), - default_nyse_schedule.all_execution_days, + self.trading_schedule.all_execution_days, ) splits = mergers = create_empty_splits_mergers_frame() dividends = pd.DataFrame({ @@ -1092,7 +1093,7 @@ def test_no_position_receives_no_dividend(self): data_portal = create_data_portal_from_trade_history( self.env, - default_nyse_schedule, + self.trading_schedule, self.instance_tmpdir, self.sim_params, {1: events}, @@ -1125,7 +1126,7 @@ def test_no_dividend_at_simulation_end(self): [100, 100, 100, 100, 100], oneday, self.sim_params, - trading_schedule=default_nyse_schedule, + trading_schedule=self.trading_schedule, ) dbpath = self.instance_tmpdir.getpath('adjustments.sqlite') @@ -1133,7 +1134,7 @@ def test_no_dividend_at_simulation_end(self): writer = SQLiteAdjustmentWriter( dbpath, MockDailyBarReader(), - default_nyse_schedule.all_execution_days, + self.trading_schedule.all_execution_days, ) splits = mergers = create_empty_splits_mergers_frame() dividends = pd.DataFrame({ @@ -1143,7 +1144,7 @@ def test_no_dividend_at_simulation_end(self): 'ex_date': np.array([events[-2].dt], dtype='datetime64[ns]'), 'record_date': np.array([events[0].dt], dtype='datetime64[ns]'), 'pay_date': np.array( - [default_nyse_schedule.next_execution_day(events[-1].dt)], + [self.trading_schedule.next_execution_day(events[-1].dt)], dtype='datetime64[ns]'), }) writer.write(splits, mergers, dividends) @@ -1158,11 +1159,11 @@ def test_no_dividend_at_simulation_end(self): ) sim_params.period_end = events[-1].dt - sim_params.update_internal_from_trading_schedule(default_nyse_schedule) + sim_params.update_internal_from_trading_schedule(self.trading_schedule) data_portal = create_data_portal_from_trade_history( self.env, - default_nyse_schedule, + self.trading_schedule, self.instance_tmpdir, sim_params, {1: events}, @@ -1202,7 +1203,8 @@ class TestDividendPerformanceHolidayStyle(TestDividendPerformance): END_DATE = pd.Timestamp('2003-12-08', tz='utc') -class TestPositionPerformance(WithInstanceTmpDir, ZiplineTestCase): +class TestPositionPerformance(WithInstanceTmpDir, WithTradingSchedule, + ZiplineTestCase): def create_environment_stuff(self, num_days=4, sids=[1, 2], @@ -1251,7 +1253,7 @@ def test_long_short_positions(self): [100, 100, 100, 100], oneday, self.sim_params, - trading_schedule=default_nyse_schedule, + trading_schedule=self.trading_schedule, ) trades_2 = factory.create_trade_history( @@ -1260,12 +1262,12 @@ def test_long_short_positions(self): [100, 100, 100, 100], oneday, self.sim_params, - trading_schedule=default_nyse_schedule, + trading_schedule=self.trading_schedule, ) data_portal = create_data_portal_from_trade_history( self.env, - default_nyse_schedule, + self.trading_schedule, self.instance_tmpdir, self.sim_params, {1: trades_1, 2: trades_2} @@ -1357,12 +1359,12 @@ def test_levered_long_position(self): [100, 100, 100, 100], oneday, self.sim_params, - trading_schedule=default_nyse_schedule, + trading_schedule=self.trading_schedule, ) data_portal = create_data_portal_from_trade_history( self.env, - default_nyse_schedule, + self.trading_schedule, self.instance_tmpdir, self.sim_params, {1: trades}) @@ -1449,12 +1451,12 @@ def test_long_position(self): [100, 100, 100, 100], oneday, self.sim_params, - trading_schedule=default_nyse_schedule, + trading_schedule=self.trading_schedule, ) data_portal = create_data_portal_from_trade_history( self.env, - default_nyse_schedule, + self.trading_schedule, self.instance_tmpdir, self.sim_params, {1: trades}) @@ -1565,14 +1567,14 @@ def test_short_position(self): [100, 100, 100, 100, 100, 100], oneday, self.sim_params, - trading_schedule=default_nyse_schedule, + trading_schedule=self.trading_schedule, ) trades_1 = trades[:-2] data_portal = create_data_portal_from_trade_history( self.env, - default_nyse_schedule, + self.trading_schedule, self.instance_tmpdir, self.sim_params, {1: trades}) @@ -1799,12 +1801,12 @@ def test_long_future_position(self): [100, 100, 100, 100], oneday, sim_params, - trading_schedule=default_nyse_schedule, + trading_schedule=self.trading_schedule, ) data_portal = create_data_portal_from_trade_history( self.env, - default_nyse_schedule, + self.trading_schedule, self.instance_tmpdir, self.sim_params, {3: trades} @@ -1919,12 +1921,12 @@ def test_short_future_position(self): [100, 100, 100, 100, 100, 100], oneday, self.sim_params, - trading_schedule=default_nyse_schedule, + trading_schedule=self.trading_schedule, ) data_portal = create_data_portal_from_trade_history( self.env, - default_nyse_schedule, + self.trading_schedule, self.instance_tmpdir, self.sim_params, {3: trades} @@ -2164,12 +2166,12 @@ def test_covering_short(self): [100, 100, 100, 100, 100, 100, 100, 100, 100, 100], oneday, self.sim_params, - trading_schedule=default_nyse_schedule, + trading_schedule=self.trading_schedule, ) data_portal = create_data_portal_from_trade_history( self.env, - default_nyse_schedule, + self.trading_schedule, self.instance_tmpdir, self.sim_params, {1: trades}) @@ -2251,14 +2253,14 @@ def test_cost_basis_calc(self): [100, 100, 100, 100, 100], oneday, self.sim_params, - default_nyse_schedule, + self.trading_schedule, ) trades = factory.create_trade_history(*history_args) transactions = factory.create_txn_history(*history_args)[:4] data_portal = create_data_portal_from_trade_history( self.env, - default_nyse_schedule, + self.trading_schedule, self.instance_tmpdir, self.sim_params, {1: trades}) @@ -2377,7 +2379,7 @@ def test_cost_basis_calc_close_pos(self): [200, -100, -100, 100, -300, 100, 500, 400], oneday, self.sim_params, - default_nyse_schedule, + self.trading_schedule, ) cost_bases = [10, 10, 0, 8, 9, 9, 13, 13.5] diff --git a/tests/test_security_list.py b/tests/test_security_list.py index cd0dfa72db..dacf490318 100644 --- a/tests/test_security_list.py +++ b/tests/test_security_list.py @@ -12,13 +12,16 @@ tmp_trading_env, tmp_dir, ) -from zipline.testing.fixtures import WithLogger, ZiplineTestCase +from zipline.testing.fixtures import ( + WithLogger, + WithTradingSchedule, + ZiplineTestCase, +) from zipline.utils import factory from zipline.utils.security_list import ( SecurityListSet, load_from_directory, ) -from zipline.utils.calendars import default_nyse_schedule LEVERAGED_ETFS = load_from_directory('leveraged_etf_list') @@ -64,7 +67,7 @@ def handle_data(self, data): self.found = True -class SecurityListTestCase(WithLogger, ZiplineTestCase): +class SecurityListTestCase(WithLogger, WithTradingSchedule, ZiplineTestCase): @classmethod def init_class_fixtures(cls): @@ -88,7 +91,7 @@ def init_class_fixtures(cls): cls.sim_params = factory.create_simulation_parameters( start=start, num_days=4, - trading_schedule=default_nyse_schedule + trading_schedule=cls.trading_schedule ) cls.sim_params2 = sp2 = factory.create_simulation_parameters( @@ -111,7 +114,7 @@ def init_class_fixtures(cls): tempdir=cls.tempdir, sim_params=cls.sim_params, sids=range(0, 5), - trading_schedule=default_nyse_schedule, + trading_schedule=cls.trading_schedule, ) cls.data_portal2 = create_data_portal( @@ -119,7 +122,7 @@ def init_class_fixtures(cls): tempdir=cls.tempdir2, sim_params=cls.sim_params2, sids=range(0, 5), - trading_schedule=default_nyse_schedule, + trading_schedule=cls.trading_schedule, ) def test_iterate_over_restricted_list(self): @@ -222,7 +225,7 @@ def test_algo_with_rl_violation_after_knowledge_date(self): self.tempdir, sim_params=sim_params, sids=range(0, 5), - trading_schedule=default_nyse_schedule, + trading_schedule=self.trading_schedule, ) algo = RestrictedAlgoWithoutCheck(symbol='BZQ', @@ -274,7 +277,7 @@ def test_algo_without_rl_violation_after_delete(self): new_tempdir, sim_params, range(0, 5), - trading_schedule=default_nyse_schedule, + trading_schedule=self.trading_schedule, ) algo = RestrictedAlgoWithoutCheck( diff --git a/zipline/data/us_equity_loader.py b/zipline/data/us_equity_loader.py index 21f6e9bdee..053d028430 100644 --- a/zipline/data/us_equity_loader.py +++ b/zipline/data/us_equity_loader.py @@ -82,7 +82,8 @@ class USEquityHistoryLoader(with_metaclass(ABCMeta)): """ FIELDS = ('open', 'high', 'low', 'close', 'volume') - def __init__(self, trading_schedule, reader, adjustment_reader, sid_cache_size=1000): + def __init__(self, trading_schedule, reader, adjustment_reader, + sid_cache_size=1000): self.trading_schedule = trading_schedule self._reader = reader self._adjustments_reader = adjustment_reader diff --git a/zipline/finance/performance/tracker.py b/zipline/finance/performance/tracker.py index f1b4c7b1cd..03579f3109 100644 --- a/zipline/finance/performance/tracker.py +++ b/zipline/finance/performance/tracker.py @@ -295,10 +295,10 @@ def check_upcoming_dividends(self, next_trading_day, adjustment_reader): ) stock_dividends = adjustment_reader.\ get_stock_dividends_with_ex_date( - held_sids, - next_trading_day, - self.asset_finder - ) + held_sids, + next_trading_day, + self.asset_finder + ) position_tracker.earn_dividends( cash_dividends, diff --git a/zipline/finance/trading.py b/zipline/finance/trading.py index 30fa1f7450..fe619133ce 100644 --- a/zipline/finance/trading.py +++ b/zipline/finance/trading.py @@ -26,7 +26,6 @@ log = logbook.Logger('Trading') - class TradingEnvironment(object): """ The financial simulations in zipline depend on information @@ -107,7 +106,6 @@ def __init__( else: self.asset_finder = None - def write_data(self, **kwargs): """Write data into the asset_db. diff --git a/zipline/utils/calendars/trading_schedule.py b/zipline/utils/calendars/trading_schedule.py index 84e5f199e2..0bd86aa305 100644 --- a/zipline/utils/calendars/trading_schedule.py +++ b/zipline/utils/calendars/trading_schedule.py @@ -20,8 +20,6 @@ ) from six import with_metaclass -from zipline.utils.memoize import remember_last - from .exchange_calendar import get_calendar from .calendar_helpers import ( next_scheduled_day,