def test_contract_at_offset(self): contract_sids = array([1, 2, 3, 4], dtype=int64) start_dates = pd.date_range('2015-01-01', periods=4, tz="UTC") auto_close_dates = pd.date_range('2016-04-01', periods=4, tz="UTC") oc = OrderedContracts('FO', contract_sids, start_dates.astype('int64'), auto_close_dates.astype('int64')) self.assertEquals(1, oc.contract_at_offset(1, 0, start_dates[-1].value), "Offset of 0 should return provided sid") self.assertEquals(2, oc.contract_at_offset(1, 1, start_dates[-1].value), "Offset of 1 should return next sid in chain.") self.assertEquals(None, oc.contract_at_offset(4, 1, start_dates[-1].value), "Offset at end of chain should not crash.")
def test_contract_at_offset(self): contract_sids = array([1, 2, 3, 4], dtype=int64) start_dates = pd.date_range('2015-01-01', periods=4, tz="UTC") contracts = deque(self.asset_finder.retrieve_all(contract_sids)) oc = OrderedContracts('FO', contracts) self.assertEquals(1, oc.contract_at_offset(1, 0, start_dates[-1].value), "Offset of 0 should return provided sid") self.assertEquals(2, oc.contract_at_offset(1, 1, start_dates[-1].value), "Offset of 1 should return next sid in chain.") self.assertEquals(None, oc.contract_at_offset(4, 1, start_dates[-1].value), "Offset at end of chain should not crash.")
def test_active_chain(self): contract_sids = array([1, 2, 3, 4], dtype=int64) start_dates = pd.date_range('2015-01-01', periods=4, tz="UTC") auto_close_dates = pd.date_range('2016-04-01', periods=4, tz="UTC") oc = OrderedContracts('FO', contract_sids, start_dates.astype('int64'), auto_close_dates.astype('int64')) # Test sid 1 as days increment, as the sessions march forward # a contract should be added per day, until all defined contracts # are returned. chain = oc.active_chain(1, pd.Timestamp('2014-12-31', tz='UTC').value) self.assertEquals([], list(chain), "On session before first start date, no contracts " "in chain should be active.") chain = oc.active_chain(1, pd.Timestamp('2015-01-01', tz='UTC').value) self.assertEquals([1], list(chain), "[1] should be the active chain on 01-01, since all " "other start dates occur after 01-01.") chain = oc.active_chain(1, pd.Timestamp('2015-01-02', tz='UTC').value) self.assertEquals([1, 2], list(chain), "[1, 2] should be the active contracts on 01-02.") chain = oc.active_chain(1, pd.Timestamp('2015-01-03', tz='UTC').value) self.assertEquals([1, 2, 3], list(chain), "[1, 2, 3] should be the active contracts on 01-03.") chain = oc.active_chain(1, pd.Timestamp('2015-01-04', tz='UTC').value) self.assertEquals(4, len(chain), "[1, 2, 3, 4] should be the active contracts on " "01-04, this is all defined contracts in the test " "case.") chain = oc.active_chain(1, pd.Timestamp('2015-01-05', tz='UTC').value) self.assertEquals(4, len(chain), "[1, 2, 3, 4] should be the active contracts on " "01-05. This tests the case where all start dates " "are before the query date.") # Test querying each sid at a time when all should be alive. chain = oc.active_chain(2, pd.Timestamp('2015-01-05', tz='UTC').value) self.assertEquals([2, 3, 4], list(chain)) chain = oc.active_chain(3, pd.Timestamp('2015-01-05', tz='UTC').value) self.assertEquals([3, 4], list(chain)) chain = oc.active_chain(4, pd.Timestamp('2015-01-05', tz='UTC').value) self.assertEquals([4], list(chain)) # Test defined contract to check edge conditions. chain = oc.active_chain(4, pd.Timestamp('2015-01-03', tz='UTC').value) self.assertEquals([], list(chain), "No contracts should be active, since 01-03 is " "before 4's start date.") chain = oc.active_chain(4, pd.Timestamp('2015-01-04', tz='UTC').value) self.assertEquals([4], list(chain), "[4] should be active beginning at its start date.")