Skip to content

Commit

Permalink
Merge pull request #1730 from quantopian/no-current-contract
Browse files Browse the repository at this point in the history
Add safeguard if current contract of continuous future is None
  • Loading branch information
dmichalowicz authored Mar 30, 2017
2 parents 164838c + 99dfe59 commit 0cc1836
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 6 deletions.
10 changes: 10 additions & 0 deletions tests/test_continuous_futures.py
Original file line number Diff line number Diff line change
Expand Up @@ -467,6 +467,16 @@ def test_get_value_contract_daily(self):
'Auto close at beginning of session so FOG16 is now '
'the current contract.')

# Test that the current contract outside of the continuous future's
# start and end dates is None.
contract = self.data_portal.get_spot_value(
cf_primary,
'contract',
self.START_DATE - self.trading_calendar.day,
'daily',
)
self.assertIsNone(contract)

def test_get_value_close_daily(self):
cf_primary = self.asset_finder.create_continuous_future(
'FO', 0, 'calendar', None)
Expand Down
5 changes: 5 additions & 0 deletions zipline/data/continuous_future_reader.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import numpy as np
import pandas as pd
from zipline.data.session_bars import SessionBarReader


Expand Down Expand Up @@ -162,6 +163,8 @@ def get_last_traded_dt(self, asset, dt):
sid = (rf.get_contract_center(asset.root_symbol,
dt,
asset.offset))
if sid is None:
return pd.NaT
contract = rf.asset_finder.retrieve_asset(sid)
return self._bar_reader.get_last_traded_dt(contract, dt)

Expand Down Expand Up @@ -346,6 +349,8 @@ def get_last_traded_dt(self, asset, dt):
sid = (rf.get_contract_center(asset.root_symbol,
dt,
asset.offset))
if sid is None:
return pd.NaT
contract = rf.asset_finder.retrieve_asset(sid)
return self._bar_reader.get_last_traded_dt(contract, dt)

Expand Down
14 changes: 8 additions & 6 deletions zipline/data/data_portal.py
Original file line number Diff line number Diff line change
Expand Up @@ -500,10 +500,10 @@ def get_single_asset_value(asset):
session_label > asset.end_date):
if field == "volume":
return 0
elif field != "last_traded":
return np.NaN
elif field == "contract":
return None
elif field != "last_traded":
return np.NaN

if data_frequency == "daily":
if field == "contract":
Expand Down Expand Up @@ -1368,7 +1368,9 @@ def get_current_future_chain(self, continuous_future, dt):

def _get_current_contract(self, continuous_future, dt):
rf = self._roll_finders[continuous_future.roll_style]
return self.asset_finder.retrieve_asset(
rf.get_contract_center(continuous_future.root_symbol,
dt,
continuous_future.offset))
contract_sid = rf.get_contract_center(continuous_future.root_symbol,
dt,
continuous_future.offset)
if contract_sid is None:
return None
return self.asset_finder.retrieve_asset(contract_sid)

0 comments on commit 0cc1836

Please sign in to comment.