From c86798bc162ddc19c2f8072b89f1204eb7da345c Mon Sep 17 00:00:00 2001 From: dmichalowicz Date: Fri, 17 Mar 2017 14:11:23 -0400 Subject: [PATCH] ENH: Add ContinuousFuture to lookup_generic --- tests/test_assets.py | 21 ++++++++++++++++++++- zipline/assets/assets.py | 9 ++++++++- 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/tests/test_assets.py b/tests/test_assets.py index 729d8f9c..dbe9eef3 100644 --- a/tests/test_assets.py +++ b/tests/test_assets.py @@ -136,6 +136,7 @@ def build_lookup_generic_cases(asset_finder_type): { 'sid': fof14_sid, 'symbol': 'FOF14', + 'root_symbol': 'FO', 'start_date': unique_start.value, 'end_date': unique_end.value, 'exchange': 'FUT', @@ -143,13 +144,25 @@ def build_lookup_generic_cases(asset_finder_type): ], index='sid' ) - with tmp_assets_db(equities=equities, futures=futures) as assets_db: + + root_symbols = pd.DataFrame({ + 'root_symbol': ['FO'], + 'root_symbol_id': [1], + 'exchange': ['CME'], + }) + + with tmp_assets_db( + equities=equities, futures=futures, root_symbols=root_symbols) \ + as assets_db: finder = asset_finder_type(assets_db) dupe_0, dupe_1, unique = assets = [ finder.retrieve_asset(i) for i in range(3) ] fof14 = finder.retrieve_asset(fof14_sid) + cf = finder.create_continuous_future( + root_symbol=fof14.root_symbol, offset=0, roll_style='volume', + ) dupe_0_start = dupe_0.start_date dupe_1_start = dupe_1.start_date @@ -184,6 +197,9 @@ def build_lookup_generic_cases(asset_finder_type): # make sure that code path is exercised. (finder, fof14_sid, unique_start, fof14), + # ContinuousFuture + (finder, cf, None, cf), + ## # Iterables @@ -204,6 +220,9 @@ def build_lookup_generic_cases(asset_finder_type): # Futures and Equities (finder, ['FOF14', 0], None, [fof14, assets[0]]), + + # ContinuousFuture and Equity + (finder, [cf, 0], None, [cf, assets[0]]), ) diff --git a/zipline/assets/assets.py b/zipline/assets/assets.py index 9a4b2ce0..c7bbbecf 100644 --- a/zipline/assets/assets.py +++ b/zipline/assets/assets.py @@ -1177,6 +1177,10 @@ class AssetFinder(object): else: raise SymbolNotFound(symbol=asset_convertible_or_iterable) + # If the input is a ContinuousFuture just return it as-is. + elif isinstance(asset_convertible_or_iterable, ContinuousFuture): + return asset_convertible_or_iterable, missing + # Interpret input as iterable. try: iterator = iter(asset_convertible_or_iterable) @@ -1187,7 +1191,10 @@ class AssetFinder(object): ) for obj in iterator: - self._lookup_generic_scalar(obj, as_of_date, matches, missing) + if isinstance(obj, ContinuousFuture): + matches.append(obj) + else: + self._lookup_generic_scalar(obj, as_of_date, matches, missing) return matches, missing def map_identifier_index_to_sids(self, index, as_of_date):