mirror of
https://github.com/wassname/catalyst.git
synced 2026-08-08 11:16:58 +08:00
Enable unadjusted history for continuous futures. The history array is filled by the values for the underlying contracts, where the contract used changes based on rolls. e.g., if a `1d` history window was over the range `2016-01-20` -> `2016-02-29` with contracts with a suffix of `F16` that rolls at the beginning of the session on `2016-01-26`, `G16` on `2016-02-26`, and `H16` on `2016-03-26`. The `2016-01-20` -> `2016-01-25` portion would use the values for `F16', the `2016-01-26` -> `2016-02-25` portion would use `G16` and the `2016-02-26` -> `2016-02-29` portion would use `H16`. Using the same contracts as above, a `1m` history window over the range (using a timezone of US/Eastern) `2016-01-25 4:00PM` -> `2016-01-25 7:00PM` would fill the `4:00PM` -> `6:00PM` portion with data for `F16` and the `6:01PM` -> `7:00PM` portion with data for `G16`, since the beginning of the `2016-01-26` session is `2016-01-25 6:01PM`. Supports `1d` and `1m`. Also adds the `sid` field to `history` to assist in showing the active contract at each dt in the window.
113 lines
3.7 KiB
Python
113 lines
3.7 KiB
Python
#
|
|
# Copyright 2016 Quantopian, Inc.
|
|
#
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
# you may not use this file except in compliance with the License.
|
|
# You may obtain a copy of the License at
|
|
#
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
#
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
# See the License for the specific language governing permissions and
|
|
# limitations under the License.
|
|
from abc import ABCMeta, abstractmethod
|
|
from six import with_metaclass
|
|
|
|
from pandas import Timestamp
|
|
|
|
|
|
class RollFinder(with_metaclass(ABCMeta, object)):
|
|
"""
|
|
Abstract base class for calculating when futures contracts are the active
|
|
contract.
|
|
"""
|
|
|
|
@abstractmethod
|
|
def get_contract_center(self, root_symbol, dt, offset):
|
|
"""
|
|
Parameters
|
|
----------
|
|
root_symbol : str
|
|
The root symbol for the contract chain.
|
|
dt : Timestamp
|
|
The datetime for which to retrieve the current contract.
|
|
offset : int
|
|
The offset from the primary contract.
|
|
0 is the primary, 1 is the secondary, etc.
|
|
|
|
Returns
|
|
-------
|
|
Future
|
|
The active future contract at the given dt.
|
|
"""
|
|
raise NotImplemented
|
|
|
|
@abstractmethod
|
|
def get_rolls(self, root_symbol, start, end, offset):
|
|
"""
|
|
Get the rolls, i.e. the session at which to hop from contract to
|
|
contract in the chain.
|
|
|
|
Parameters
|
|
----------
|
|
root_symbol : str
|
|
The root symbol for which to calculate rolls.
|
|
start : Timestamp
|
|
Start of the date range.
|
|
end : Timestamp
|
|
End of the date range.
|
|
offset : int
|
|
Offset from the primary.
|
|
|
|
Returns
|
|
-------
|
|
rolls - list[tuple(sid, roll_date)]
|
|
A list of rolls, where first value is the first active `sid`,
|
|
and the `roll_date` on which to hop to the next contract.
|
|
The last pair in the chain has a value of `None` since the roll
|
|
is after the range.
|
|
"""
|
|
raise NotImplemented
|
|
|
|
|
|
class CalendarRollFinder(RollFinder):
|
|
"""
|
|
The CalendarRollFinder calculates contract rolls based purely on the
|
|
contract's auto close date.
|
|
"""
|
|
|
|
def __init__(self, trading_calendar, asset_finder):
|
|
self.trading_calendar = trading_calendar
|
|
self.asset_finder = asset_finder
|
|
|
|
def get_contract_center(self, root_symbol, dt, offset):
|
|
oc = self.asset_finder.get_ordered_contracts(root_symbol)
|
|
session = self.trading_calendar.minute_to_session_label(dt)
|
|
primary_candidate = oc.contract_before_auto_close(session.value)
|
|
|
|
# Here is where a volume check would be.
|
|
primary = primary_candidate
|
|
return oc.contract_at_offset(primary, offset)
|
|
|
|
def get_rolls(self, root_symbol, start, end, offset):
|
|
oc = self.asset_finder.get_ordered_contracts(root_symbol)
|
|
primary_at_end = self.get_contract_center(root_symbol, end, 0)
|
|
for i, sid in enumerate(oc.contract_sids):
|
|
if sid == primary_at_end:
|
|
break
|
|
i += offset
|
|
first = oc.contract_sids[i]
|
|
rolls = [(first, None)]
|
|
i -= 1
|
|
auto_close_date = Timestamp(oc.auto_close_dates[i - offset], tz='UTC')
|
|
while auto_close_date > start and i > -1:
|
|
rolls.insert(0, (oc.contract_sids[i - offset],
|
|
auto_close_date))
|
|
i -= 1
|
|
auto_close_date = Timestamp(oc.auto_close_dates[i - offset],
|
|
tz='UTC')
|
|
|
|
return rolls
|