Skip to content

Commit

Permalink
Merge pull request #1565 from quantopian/fix-offset-history
Browse files Browse the repository at this point in the history
BUG: Fix continuous future history with offsets.
  • Loading branch information
ehebert authored Oct 28, 2016
2 parents 575a8cf + 4235dbd commit 9a51efc
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 2 deletions.
54 changes: 54 additions & 0 deletions tests/test_continuous_futures.py
Original file line number Diff line number Diff line change
Expand Up @@ -456,6 +456,60 @@ def test_history_sid_session(self):
3,
"Should be FOJ16 on session after roll.")

def test_history_sid_session_secondary(self):
cf = self.data_portal.asset_finder.create_continuous_future(
'FO', 1, 'calendar')
window = self.data_portal.get_history_window(
[cf],
Timestamp('2016-03-04 18:01', tz='US/Eastern').tz_convert('UTC'),
30, '1d', 'sid')

self.assertEqual(window.loc['2016-01-26', cf],
1,
"Should be FOG16 at beginning of window.")

self.assertEqual(window.loc['2016-01-27', cf],
2,
"Should be FOH16 after first roll.")

self.assertEqual(window.loc['2016-02-25', cf],
2,
"Should be FOH16 on session before roll.")

self.assertEqual(window.loc['2016-02-26', cf],
3,
"Should be FOJ16 on session with roll.")

self.assertEqual(window.loc['2016-02-29', cf],
3,
"Should be FOJ16 on session after roll.")

# Advance the window a month.
window = self.data_portal.get_history_window(
[cf],
Timestamp('2016-04-06 18:01', tz='US/Eastern').tz_convert('UTC'),
30, '1d', 'sid')

self.assertEqual(window.loc['2016-02-25', cf],
2,
"Should be FOH16 at beginning of window.")

self.assertEqual(window.loc['2016-02-26', cf],
3,
"Should be FOJ16 on session with roll.")

self.assertEqual(window.loc['2016-02-29', cf],
3,
"Should be FOJ16 on session after roll.")

self.assertEqual(window.loc['2016-03-24', cf],
4,
"Should be FOK16 on session with roll.")

self.assertEqual(window.loc['2016-03-28', cf],
4,
"Should be FOK16 on session after roll.")

def test_history_sid_session_volume_roll(self):
cf = self.data_portal.asset_finder.create_continuous_future(
'FO', 0, 'volume')
Expand Down
5 changes: 3 additions & 2 deletions zipline/assets/roll_finder.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ def get_rolls(self, root_symbol, start, end, offset):
is after the range.
"""
oc = self.asset_finder.get_ordered_contracts(root_symbol)
front = self.get_contract_center(root_symbol, end, offset)
front = self.get_contract_center(root_symbol, end, 0)
back = oc.contract_at_offset(front, 1, end.value)
if back is not None:
first = self._active_contract(oc, front, back, end)
Expand All @@ -106,7 +106,8 @@ def get_rolls(self, root_symbol, start, end, offset):
session_loc -= 1
roll_session = sessions[session_loc + 1]
if roll_session > start:
rolls.insert(0, (front, roll_session))
rolls.insert(0, (oc.contract_sids[i + offset],
roll_session))
i -= 1
auto_close_date = Timestamp(oc.auto_close_dates[i],
tz='UTC')
Expand Down

0 comments on commit 9a51efc

Please sign in to comment.