diff --git a/tests/modelling/test_engine.py b/tests/modelling/test_engine.py index d61bc95c..dc9b577f 100644 --- a/tests/modelling/test_engine.py +++ b/tests/modelling/test_engine.py @@ -244,6 +244,11 @@ class FrameInputTestCase(TestCase): cls.env.write_data(equities_df=asset_info) cls.asset_finder = cls.env.asset_finder + @classmethod + def tearDownClass(cls): + del cls.env + del cls.asset_finder + def setUp(self): self.dates = FrameInputTestCase.dates self.assets = FrameInputTestCase.assets @@ -375,6 +380,7 @@ class SyntheticBcolzTestCase(TestCase): @classmethod def tearDownClass(cls): + del cls.env cls.temp_dir.cleanup() def test_SMA(self): diff --git a/tests/modelling/test_modelling_algo.py b/tests/modelling/test_modelling_algo.py index 39d071d0..5a7b20ee 100644 --- a/tests/modelling/test_modelling_algo.py +++ b/tests/modelling/test_modelling_algo.py @@ -102,6 +102,10 @@ class FFCAlgorithmTestCase(TestCase): cls.dates = cls.raw_data[cls.AAPL].index.tz_localize('UTC') + @classmethod + def tearDownClass(cls): + del cls.env + @classmethod def create_bar_reader(cls, tempdir): resources = { diff --git a/tests/risk/test_risk_cumulative.py b/tests/risk/test_risk_cumulative.py index 578c72c6..42be8e1f 100644 --- a/tests/risk/test_risk_cumulative.py +++ b/tests/risk/test_risk_cumulative.py @@ -33,6 +33,10 @@ class TestRisk(unittest.TestCase): def setUpClass(cls): cls.env = TradingEnvironment() + @classmethod + def tearDownClass(cls): + del cls.env + def setUp(self): start_date = datetime.datetime( year=2006, diff --git a/tests/risk/test_risk_period.py b/tests/risk/test_risk_period.py index 83c73430..c66212b4 100644 --- a/tests/risk/test_risk_period.py +++ b/tests/risk/test_risk_period.py @@ -37,6 +37,10 @@ class TestRisk(unittest.TestCase): def setUpClass(cls): cls.env = TradingEnvironment() + @classmethod + def tearDownClass(cls): + del cls.env + def setUp(self): start_date = datetime.datetime( diff --git a/tests/test_algorithm.py b/tests/test_algorithm.py index 9205a01c..70e83536 100644 --- a/tests/test_algorithm.py +++ b/tests/test_algorithm.py @@ -112,6 +112,10 @@ class TestRecordAlgorithm(TestCase): cls.env = TradingEnvironment() cls.env.write_data(equities_identifiers=[133]) + @classmethod + def tearDownClass(cls): + del cls.env + def setUp(self): self.sim_params = factory.create_simulation_parameters(num_days=4, env=self.env) @@ -193,6 +197,10 @@ class TestMiscellaneousAPI(TestCase): equities_data=metadata, futures_data=futures_metadata) + @classmethod + def tearDownClass(cls): + del cls.env + def setUp(self): setup_logger(self) self.sim_params = factory.create_simulation_parameters( @@ -516,6 +524,10 @@ class TestTransformAlgorithm(TestCase): cls.env.write_data(equities_identifiers=[0, 1, 133], futures_data=futures_metadata) + @classmethod + def tearDownClass(cls): + del cls.env + def setUp(self): setup_logger(self) self.sim_params = factory.create_simulation_parameters(num_days=4, @@ -756,6 +768,10 @@ class TestAlgoScript(TestCase): equities_identifiers=[0, 1, 133] ) + @classmethod + def tearDownClass(cls): + del cls.env + def setUp(self): days = 251 # Note that create_simulation_parameters creates @@ -813,7 +829,7 @@ class TestAlgoScript(TestCase): metadata = {3: {'symbol': 'TEST', 'asset_type': 'equity'}} algo = TradingAlgorithm(script=api_get_environment_algo, - asset_metadata=metadata, + equities_metadata=metadata, platform=platform) algo.run(self.df) self.assertEqual(algo.environment, platform) @@ -823,7 +839,7 @@ class TestAlgoScript(TestCase): metadata = {3: {'symbol': 'TEST', 'asset_type': 'equity'}} algo = TradingAlgorithm(script=api_symbol_algo, - asset_metadata=metadata) + equities_metadata=metadata) algo.run(self.df) def test_fixed_slippage(self): @@ -1093,6 +1109,10 @@ class TestHistory(TestCase): ) cls.env.write_data(equities_identifiers=[0, 1]) + @classmethod + def tearDownClass(cls): + del cls.env + @property def source(self): return RandomWalkSource(start=self._start, end=self._end) @@ -1154,6 +1174,10 @@ class TestGetDatetime(TestCase): cls.env = TradingEnvironment() cls.env.write_data(equities_identifiers=[0, 1]) + @classmethod + def tearDownClass(cls): + del cls.env + def setUp(self): setup_logger(self) @@ -1218,6 +1242,10 @@ class TestTradingControls(TestCase): cls.env = TradingEnvironment() cls.env.write_data(equities_identifiers=[cls.sid]) + @classmethod + def tearDownClass(cls): + del cls.env + def setUp(self): self.sim_params = factory.create_simulation_parameters(num_days=4, env=self.env) @@ -1513,7 +1541,7 @@ class TestTradingControls(TestCase): metadata = {0: {'start_date': '1990-01-01', 'end_date': '2020-01-01'}} algo = SetAssetDateBoundsAlgorithm( - asset_metadata=metadata, + equities_metadata=metadata, sim_params=self.sim_params, env=temp_env, ) @@ -1525,7 +1553,7 @@ class TestTradingControls(TestCase): metadata = {0: {'start_date': '1989-01-01', 'end_date': '1990-01-01'}} algo = SetAssetDateBoundsAlgorithm( - asset_metadata=metadata, + equities_metadata=metadata, sim_params=self.sim_params, env=temp_env, ) @@ -1538,7 +1566,7 @@ class TestTradingControls(TestCase): metadata = {0: {'start_date': '2020-01-01', 'end_date': '2021-01-01'}} algo = SetAssetDateBoundsAlgorithm( - asset_metadata=metadata, + equities_metadata=metadata, sim_params=self.sim_params, env=temp_env, ) @@ -1556,6 +1584,10 @@ class TestAccountControls(TestCase): equities_identifiers=[cls.sidint] ) + @classmethod + def tearDownClass(cls): + del cls.env + def setUp(self): self.sim_params = factory.create_simulation_parameters( num_days=4, env=self.env diff --git a/tests/test_algorithm_gen.py b/tests/test_algorithm_gen.py index 5f869bac..177c0dc4 100644 --- a/tests/test_algorithm_gen.py +++ b/tests/test_algorithm_gen.py @@ -91,6 +91,10 @@ class AlgorithmGeneratorTestCase(TestCase): cls.env = trading.TradingEnvironment() cls.env.write_data(equities_identifiers=[8229]) + @classmethod + def tearDownClass(cls): + del cls.env + def setUp(self): setup_logger(self) diff --git a/tests/test_assets.py b/tests/test_assets.py index d746cb38..098194e5 100644 --- a/tests/test_assets.py +++ b/tests/test_assets.py @@ -721,6 +721,10 @@ class TestFutureChain(TestCase): env.write_data(futures_data=metadata) cls.asset_finder = env.asset_finder + @classmethod + def tearDownClass(cls): + del cls.asset_finder + def test_len(self): """ Test the __len__ method of FutureChain. """ diff --git a/tests/test_batchtransform.py b/tests/test_batchtransform.py index 1ec1a864..92810103 100644 --- a/tests/test_batchtransform.py +++ b/tests/test_batchtransform.py @@ -139,6 +139,10 @@ class TestBatchTransformMinutely(TestCase): cls.env = TradingEnvironment() cls.env.write_data(equities_identifiers=[0]) + @classmethod + def tearDownClass(cls): + del cls.env + def setUp(self): setup_logger(self) start = pd.datetime(1990, 1, 3, 0, 0, 0, 0, pytz.utc) @@ -184,6 +188,10 @@ class TestBatchTransform(TestCase): cls.env = TradingEnvironment() cls.env.write_data(equities_identifiers=[0]) + @classmethod + def tearDownClass(cls): + del cls.env + def setUp(self): setup_logger(self) self.sim_params = factory.create_simulation_parameters( diff --git a/tests/test_blotter.py b/tests/test_blotter.py index 81186139..2b012e38 100644 --- a/tests/test_blotter.py +++ b/tests/test_blotter.py @@ -40,6 +40,10 @@ class BlotterTestCase(TestCase): cls.env = trading.TradingEnvironment() cls.env.write_data(equities_identifiers=[24]) + @classmethod + def tearDownClass(cls): + del cls.env + def setUp(self, env=None): setup_logger(self) diff --git a/tests/test_events_through_risk.py b/tests/test_events_through_risk.py index f98410cc..b40ea1ca 100644 --- a/tests/test_events_through_risk.py +++ b/tests/test_events_through_risk.py @@ -47,6 +47,10 @@ class TestEventsThroughRisk(unittest.TestCase): cls.env = TradingEnvironment() cls.env.write_data(equities_identifiers=[1]) + @classmethod + def tearDownClass(cls): + del cls.env + def test_daily_buy_and_hold(self): start_date = datetime.datetime( diff --git a/tests/test_exception_handling.py b/tests/test_exception_handling.py index 848455da..a642e574 100644 --- a/tests/test_exception_handling.py +++ b/tests/test_exception_handling.py @@ -45,6 +45,10 @@ class ExceptionTestCase(TestCase): cls.env = TradingEnvironment() cls.env.write_data(equities_identifiers=[133]) + @classmethod + def tearDownClass(cls): + del cls.env + def setUp(self): self.zipline_test_config = { 'sid': 133, diff --git a/tests/test_finance.py b/tests/test_finance.py index 5f4b6b25..4197de07 100644 --- a/tests/test_finance.py +++ b/tests/test_finance.py @@ -63,6 +63,10 @@ class FinanceTestCase(TestCase): cls.env = TradingEnvironment() cls.env.write_data(equities_identifiers=[1, 133]) + @classmethod + def tearDownClass(cls): + del cls.env + def setUp(self): self.zipline_test_config = { 'sid': 133, @@ -372,6 +376,10 @@ class TradingEnvironmentTestCase(TestCase): def setUpClass(cls): cls.env = TradingEnvironment() + @classmethod + def tearDownClass(cls): + del cls.env + @timed(DEFAULT_TIMEOUT) def test_is_trading_day(self): # holidays taken from: http://www.nyse.com/press/1191407641943.html diff --git a/tests/test_history.py b/tests/test_history.py index d3641fde..f0862a61 100644 --- a/tests/test_history.py +++ b/tests/test_history.py @@ -150,6 +150,10 @@ class TestHistoryIndex(TestCase): def setUpClass(cls): cls.environment = TradingEnvironment() + @classmethod + def tearDownClass(cls): + del cls.environment + @parameterized.expand( [(name, case['input'], case['expected']) for name, case in INDEX_TEST_CASES.items()] @@ -169,6 +173,10 @@ class TestHistoryContainer(TestCase): def setUpClass(cls): cls.env = TradingEnvironment() + @classmethod + def tearDownClass(cls): + del cls.env + def bar_data_dt(self, bar_data, require_unique=True): """ Get a dt to associate with the given BarData object. @@ -415,6 +423,10 @@ class TestHistoryAlgo(TestCase): cls.env = trading.TradingEnvironment() cls.env.write_data(equities_identifiers=[0, 1]) + @classmethod + def tearDownClass(cls): + del cls.env + def setUp(self): np.random.seed(123) @@ -1119,6 +1131,10 @@ class TestHistoryContainerResize(TestCase): def setUpClass(cls): cls.env = TradingEnvironment() + @classmethod + def tearDownClass(cls): + del cls.env + @parameterized.expand( (freq, field, data_frequency, construct_digest) for freq in ('1m', '1d') diff --git a/tests/test_perf_tracking.py b/tests/test_perf_tracking.py index 2b144e48..23e27835 100644 --- a/tests/test_perf_tracking.py +++ b/tests/test_perf_tracking.py @@ -514,6 +514,10 @@ class TestDividendPerformance(unittest.TestCase): cls.env = TradingEnvironment() cls.env.write_data(equities_identifiers=[1, 2]) + @classmethod + def tearDownClass(cls): + del cls.env + def setUp(self): self.sim_params, self.dt, self.end_dt = \ create_random_simulation_parameters() @@ -967,6 +971,10 @@ class TestPositionPerformance(unittest.TestCase): cls.env = TradingEnvironment() cls.env.write_data(equities_identifiers=[1, 2]) + @classmethod + def tearDownClass(cls): + del cls.env + def setUp(self): self.sim_params, self.dt, self.end_dt = \ create_random_simulation_parameters() @@ -1738,6 +1746,10 @@ class TestPerformanceTracker(unittest.TestCase): cls.env = TradingEnvironment() cls.env.write_data(equities_identifiers=[1, 2, 133, 134]) + @classmethod + def tearDownClass(cls): + del cls.env + NumDaysToDelete = collections.namedtuple( 'NumDaysToDelete', ('start', 'middle', 'end')) @@ -2157,6 +2169,10 @@ class TestPositionTracker(unittest.TestCase): cls.env.write_data(equities_data=equities_metadata, futures_data=futures_metadata) + @classmethod + def tearDownClass(cls): + del cls.env + def test_empty_positions(self): """ make sure all the empty position stats return a numeric 0 diff --git a/tests/test_rolling_panel.py b/tests/test_rolling_panel.py index aa3cc62e..196553f2 100644 --- a/tests/test_rolling_panel.py +++ b/tests/test_rolling_panel.py @@ -32,6 +32,10 @@ class TestRollingPanel(unittest.TestCase): def setUpClass(cls): cls.env = TradingEnvironment() + @classmethod + def tearDownClass(cls): + del cls.env + def test_alignment(self): items = ('a', 'b') sids = (1, 2) diff --git a/tests/test_security_list.py b/tests/test_security_list.py index 9dbb116c..e0bd58fd 100644 --- a/tests/test_security_list.py +++ b/tests/test_security_list.py @@ -65,6 +65,10 @@ class SecurityListTestCase(TestCase): cls.env.write_data(equities_identifiers=['AAPL', 'GOOG', 'BZQ', 'URTY', 'JFT']) + @classmethod + def tearDownClass(cls): + del cls.env + def setUp(self, env=None): self.extra_knowledge_date = \ diff --git a/tests/test_serialization.py b/tests/test_serialization.py index 103de348..681dc1ff 100644 --- a/tests/test_serialization.py +++ b/tests/test_serialization.py @@ -41,6 +41,10 @@ class SerializationTestCase(TestCase): def setUpClass(cls): cls.env = TradingEnvironment() + @classmethod + def tearDownClass(cls): + del cls.env + @parameterized.expand(object_serialization_cases()) def test_object_serialization(self, _, diff --git a/tests/test_transforms.py b/tests/test_transforms.py index 02b01688..08195ee7 100644 --- a/tests/test_transforms.py +++ b/tests/test_transforms.py @@ -144,6 +144,10 @@ class TransformTestCase(TestCase): )), } + @classmethod + def tearDownClass(cls): + del cls.env + def tearDown(self): """ Each test consumes a source, we need to rewind it. diff --git a/tests/test_transforms_talib.py b/tests/test_transforms_talib.py index e8cc6d81..366abadc 100644 --- a/tests/test_transforms_talib.py +++ b/tests/test_transforms_talib.py @@ -37,6 +37,10 @@ class TestTALIB(TestCase): def setUpClass(cls): cls.env = TradingEnvironment() + @classmethod + def tearDownClass(cls): + del cls.env + def setUp(self): setup_logger(self) sim_params = factory.create_simulation_parameters( diff --git a/tests/utils/test_events.py b/tests/utils/test_events.py index 504bb12d..e600c755 100644 --- a/tests/utils/test_events.py +++ b/tests/utils/test_events.py @@ -217,6 +217,10 @@ class RuleTestCase(TestCase): cls.env = TradingEnvironment() cls.class_ = None # Mark that this is the base class. + @classmethod + def tearDownClass(cls): + del cls.env + def test_completeness(self): """ Tests that all rules are being tested. diff --git a/zipline/algorithm.py b/zipline/algorithm.py index dc95359f..3e4c6289 100644 --- a/zipline/algorithm.py +++ b/zipline/algorithm.py @@ -148,7 +148,7 @@ class TradingAlgorithm(object): Whether to fill orders immediately or on next bar. asset_finder : An AssetFinder object A new AssetFinder object to be used in this TradingEnvironment - asset_metadata: can be either: + equities_metadata : can be either: - dict - pandas.DataFrame - object with 'read' property @@ -166,7 +166,7 @@ class TradingAlgorithm(object): with the other metadata fields. identifiers : List Any asset identifiers that are not provided in the - asset_metadata, but will be traded by this TradingAlgorithm + equities_metadata, but will be traded by this TradingAlgorithm """ self.sources = [] @@ -199,8 +199,9 @@ class TradingAlgorithm(object): # Update the TradingEnvironment with the provided asset metadata self.trading_environment.write_data( - equities_data=kwargs.pop('asset_metadata', {}), + equities_data=kwargs.pop('equities_metadata', {}), equities_identifiers=kwargs.pop('identifiers', []), + futures_data=kwargs.pop('futures_metadata', {}), ) # set the capital base diff --git a/zipline/finance/performance/position_tracker.py b/zipline/finance/performance/position_tracker.py index b3a9bdd6..03149aa4 100644 --- a/zipline/finance/performance/position_tracker.py +++ b/zipline/finance/performance/position_tracker.py @@ -405,14 +405,6 @@ class PositionTracker(object): state_dict['asset_finder'] = self.asset_finder state_dict['positions'] = dict(self.positions) state_dict['unpaid_dividends'] = self._unpaid_dividends - - # Asset-finder dependent dicts must be serialized - state_dict['position_value_multipliers'] = \ - serialize_ordered_dict(self._position_value_multipliers) - state_dict['position_exposure_multipliers'] = \ - serialize_ordered_dict(self._position_exposure_multipliers) - state_dict['position_payout_multipliers'] = \ - serialize_ordered_dict(self._position_payout_multipliers) state_dict['auto_close_position_sids'] = self._auto_close_position_sids STATE_VERSION = 3 @@ -433,36 +425,14 @@ class PositionTracker(object): self._positions_store = zp.Positions() self._unpaid_dividends = state['unpaid_dividends'] - - # AssetFinder-dependent dicts are de-serialized - self._position_value_multipliers = \ - deserialize_ordered_dict(state['position_value_multipliers']) - self._position_exposure_multipliers = \ - deserialize_ordered_dict(state['position_exposure_multipliers']) - self._position_payout_multipliers = \ - deserialize_ordered_dict(state['position_payout_multipliers']) self._auto_close_position_sids = state['auto_close_position_sids'] # Arrays for quick calculations of positions value self._position_amounts = OrderedDict() self._position_last_sale_prices = OrderedDict() + self._position_value_multipliers = OrderedDict() + self._position_exposure_multipliers = OrderedDict() + self._position_payout_multipliers = OrderedDict() # Update positions is called without a finder self.update_positions(state['positions']) - - -def serialize_ordered_dict(ordered_dict): - """ - Converts an OrderedDict in to a list of key/value pair tuples - """ - return [(key, value) for key, value in ordered_dict.items()] - - -def deserialize_ordered_dict(serialized_ordered_dict): - """ - Converts a list of key/value pair tuples in to an OrderedDict - """ - result = OrderedDict() - for key, value in serialized_ordered_dict: - result[key] = value - return result diff --git a/zipline/utils/cli.py b/zipline/utils/cli.py index 4bd16c61..4d0fb507 100644 --- a/zipline/utils/cli.py +++ b/zipline/utils/cli.py @@ -240,7 +240,7 @@ def run_pipeline(print_algo=True, **kwargs): namespace=kwargs.get('namespace', {}), capital_base=float(kwargs['capital_base']), algo_filename=kwargs.get('algofile'), - asset_metadata=asset_metadata, + equities_metadata=asset_metadata, identifiers=symbols, start=start, end=end)