mirror of
https://github.com/wassname/catalyst.git
synced 2026-09-12 12:12:04 +08:00
ENH: Add current chain for continuous futures.
Add `chain`field to current, as well as supporting methods in DataPortal
and OrderedContracts.
Enables the following example:
```
from zipline.api import continuous_future
def initialize(context):
context.primary_cl = continuous_future('CL', offset=0, roll='calendar')
schedule_function(print_current_chain)
def print_current_chain(context, data):
chain = data.current_chain(context.primary_cl)
print 'datetime={0}'.format(get_datetime())
print 'primary={0}'.format(chain[0])
print 'secondary={0}'.format(chain[1])
print 'tertiary={0}'.format(chain[2])
```
```
datetime=2015-12-23 14:31:00+00:00
primary=Future(1058201602 [CLG16])
secondary=Future(1058201603 [CLH16])
tertiary=Future(1058201604 [CLJ16])
```
Also:
- make return types of OrderedContracts methods compatible across
architectures. (Noticed while adding `active_chain` method.)
- Add year suffix to future contract names in test data.
This commit is contained in:
@@ -1239,6 +1239,27 @@ class DataPortal(object):
|
||||
|
||||
return ret
|
||||
|
||||
def get_current_future_chain(self, continuous_future, dt):
|
||||
"""
|
||||
Retrieves the future chain for the contract at the given `dt` according
|
||||
the `continuous_future` specification.
|
||||
|
||||
Returns:
|
||||
future_chain : list[Future]
|
||||
A list of active futures, where the first index is the current
|
||||
contract specified by the continuous future definition, the second
|
||||
is the next upcoming contract and so on.
|
||||
"""
|
||||
rf = self._roll_finders[continuous_future.roll_style]
|
||||
session = self.trading_calendar.minute_to_session_label(dt)
|
||||
contract_center = rf.get_contract_center(
|
||||
continuous_future.root_symbol, session,
|
||||
continuous_future.offset)
|
||||
oc = self.asset_finder.get_ordered_contracts(
|
||||
continuous_future.root_symbol)
|
||||
chain = oc.active_chain(contract_center, session.value)
|
||||
return self.asset_finder.retrieve_all(chain)
|
||||
|
||||
def _get_current_contract(self, continuous_future, dt):
|
||||
rf = self._roll_finders[continuous_future.roll_style]
|
||||
return self.asset_finder.retrieve_asset(
|
||||
|
||||
Reference in New Issue
Block a user