diff --git a/tests/test_continuous_futures.py b/tests/test_continuous_futures.py index 628977e1..3530f8da 100644 --- a/tests/test_continuous_futures.py +++ b/tests/test_continuous_futures.py @@ -34,6 +34,7 @@ from zipline.assets.continuous_futures import ( delivery_predicate ) from zipline.data.minute_bars import FUTURES_MINUTES_PER_DAY +from zipline.errors import SymbolNotFound from zipline.testing.fixtures import ( WithAssetFinder, WithCreateBarData, @@ -417,6 +418,11 @@ class ContinuousFuturesTestCase(WithCreateBarData, self.assertNotEqual(cf_primary, cf_secondary) + # Assert that the proper exception is raised if the given root symbol + # does not exist. + with self.assertRaises(SymbolNotFound): + self.asset_finder.create_continuous_future('NO', 0, 'calendar') + def test_current_contract(self): cf_primary = self.asset_finder.create_continuous_future( 'FO', 0, 'calendar') diff --git a/zipline/assets/assets.py b/zipline/assets/assets.py index 263430fd..9a4b2ce0 100644 --- a/zipline/assets/assets.py +++ b/zipline/assets/assets.py @@ -996,8 +996,13 @@ class AssetFinder(object): fields = (fc_cols.exchange,) - return sa.select(fields).where( - fc_cols.root_symbol == root_symbol).execute().fetchone()[0] + exchange = sa.select(fields).where( + fc_cols.root_symbol == root_symbol).execute().scalar() + + if exchange is not None: + return exchange + else: + raise SymbolNotFound(symbol=root_symbol) def get_ordered_contracts(self, root_symbol): try: