Commit Graph

29 Commits

Author SHA1 Message Date
Conner Fromknecht 99efa7a9f3 Fixed catalyst tests except example tests 2017-06-19 14:43:10 -07:00
Andrew Daniels d155d894fe MAINT: Pass data_frequency to get_history_window
This allows us to remove the check for whether the provided dt had a
time of midnight, which was a flimsy way to infer if the data frequency
was 'daily'. Besides the explicit check being preferable, this method
was broken on the futures calendar, since midnight is a valid market
minute.
2017-05-09 09:34:39 -04:00
dmichalowicz 67dd149660 BUG: Ordered contracts could end prematurely 2017-04-21 15:52:21 -04:00
dmichalowicz 6f1d4b4a5f BUG: OrderedContracts chain could sometimes terminate on first contract 2017-04-07 10:01:22 -04:00
dmichalowicz 99dfe5961d BUG: Getting continuous future current contract failed on None 2017-03-30 12:09:55 -04:00
dmichalowicz 7829541112 EHN: Make continuous future adjustment style an argument 2017-03-29 08:49:12 -04:00
dmichalowicz bb801344e9 ENH: Better error message for non-existent root symbol 2017-03-16 11:18:17 -04:00
dmichalowicz 6d47a36166 ENH: Eliminate potential look-ahead bias in volume rolls 2017-02-16 09:01:16 -05:00
Eddie Hebert 512c2bfb83 TST: Remove duplicate key in fixture config.
Should be no functional change.
2017-02-02 23:10:14 -05:00
Eddie Hebert d3ff536de4 BUG: Allow rolls to skip over contracts.
For futures that behave like GC, use the latest roll as the back contract when
walking backwards over the window, so that when the front contract is skipped
because it never has more volume between its auto close date and the previous
auto close date, the back contract which did have volume is still used when
making comparisons to construct the chain.
2016-12-05 22:33:03 -05:00
Eddie Hebert 117d228fc2 ENH: Allow future chains to only use certain delivery months.
To support contracts such as `PL` which should roll from F->J->N->V, add the
ability to pass a predicate function to the ordered contract chain contstrution
which returns `True` if the contract is allowed in the chain.
2016-12-01 13:26:07 -05:00
Eddie Hebert c6577a6518 BUG: Fix 1m history for volume rolls with adjustments.
Convert the end minute to the its session label before calling `_active_contract`,
otherwise the volume roll finder's attempt to use the session bar reader fails
due to a non-session label Timestamp.
2016-11-30 13:13:53 -05:00
Eddie Hebert 82bc2a6d3d BUG: Support futures which do not roll month to month.
Fix multiple errors when attempting to generate rolls for futures which do not
roll month to month, e.g. the Eurodollar.

These errors were caused by logic that always incremented from contract to
contract by delivery month, with errors when the next contract was not part of
the quarterly roll chain and thus had not yet begun trading even though the
previous contract had autoclosed. Instead, filter out these contracts and only
allow contracts that have begun trading before the previous contract's autoclose.

This is in lieu of a more explicit specification of quarterly rolls.
2016-11-30 11:23:43 -05:00
Eddie Hebert d147397983 MAINT: Use a doubly linked list for contract chain.
Instead of requiring the roll finder to juggle the indices into the ordered
contracts, use a doubly linked list where the nodes element is the contract
with members pointing to the previous and next contracts in the chain.

Besides improving legibility in the roll finder code, this change is on the path
to adding a predicate to exclude contracts from the chain, e.g. contracts in ED
which are not in the roll schedule.

Change test results for primary chain, since new implementaton does not stop at
contract in which has not yet started when constructing the chain.
2016-11-30 06:01:59 -05:00
Eddie Hebert 4d434acab7 BUG: Fix bounds errors in roll finder.
Fix common error condition which was triggered whenever the session at the end
of the prefetched history window was a session where the back contract was
active. When the back contract was the active contract, the next contract for
consideration was the front contract at the end of the window, which
definitionally always has an autoclose after the end of the window.
Instead, just start seeking backwards from the end of the window.

Also prevent lookahead bias in volume rolls, which was caused by the using the
volume for a session to determine whether that session had rolled. Information
that would not have been available at the beginning of the session.

This change makes the volume rolls overly conservative, and may be improved by
looking at vectors of the preceding volume and making the roll off of momentum.
2016-11-23 10:35:45 -05:00
Eddie Hebert e415c0f350 BUG: Fix continuous future end dates.
The end date of the last contract with a sufficient start date was being
used for the continuous future overall end date; however the end date of
that contract (which is the last day for which there is data for the
contract) is not necessarily the greatest end date out of all contracts.
It is possible for the furthest out contract to have some, but very
few, trades before it is more actively traded. Which would give it a
start date within in the range of the simulation, but an end date is
earlier than the other contracts which are active during the simulation.

This bug would result in `nan`s when getting the current price because
of the `end_date` check in `get_spot_value`. When the current simulation
time was greater than the `end_date` of the last contract the condition
which guards against attempting to get data for an instrument past its
end date would return a `nan`, even when the current underlying contract
did have data for that date.

Use max end date of all contracts instead of the last one, to ensure
that the continuous future last date is always great enough to allow
access to all contracts with in the chain.

Also, use min start date to accurately mirror the end date behavior.
2016-11-09 16:19:19 -05:00
Eddie Hebert 512e62b13e BUG: Fix bad attribute lookup on session continuous future reader.
Use `roll_style` not `roll`.

Also, add test case to cover using the session bar reader `get_value`,
by adding a test which uses `close`, since only `contract` was being
exercised, which does not exercise the session daily bar reader.
2016-11-08 15:48:28 -05:00
Andrew Daniels 993b694d49 BUG: Allows 'contract' in get_spot_value with daily frequency (#1582)
Also removes duplicate check in test_current_contract.
2016-11-07 16:28:48 -05:00
Eddie Hebert abc4f55f64 ENH: Allow configurable history prefetch length.
To support using a `DataPortal` and `HistoryLoader` in a notebook, allow
the prefetch length to be configurable, so that it can be set to 0.
Unlike backtesting where the prefetch is useful for repeated history
windows viewed from datetimes which are monotonically increasing by a
small amount, the notebook usage of history windows needs only to
retrieve the exact data needed for the window specified.

This patch also fixes some boundary conditions related to rolls and
adjustments which were uncovered by querying for the adjustments with an
end date near the end of the window.
2016-11-04 13:30:30 -04:00
Eddie Hebert 9a51efc7d2 Merge pull request #1565 from quantopian/fix-offset-history
BUG: Fix continuous future history with offsets.
2016-10-28 09:44:34 -04:00
Eddie Hebert 575a8cf048 BUG: Protect against contract offset at end of range. (#1564)
This boundary case was exposed with internal fixture data which used a
continuous future with a contract chain of size one.
2016-10-27 16:48:34 -04:00
Eddie Hebert 4235dbd758 BUG: Fix continuous future history with offsets.
Apply offset value when writing out the rolls in a continuous future
which is offset from the primary.
2016-10-27 16:23:03 -04:00
Eddie Hebert aa021531d9 BUG: Use proxy for settlement on future adjustments.
Instead of using the difference between the session close of the front
contract before the roll and and the open of back contract on the
beginning of the roll, use the close of both at the end of the session
before the roll.

The closes of the session prior to roll is in lieu of settlement data.
2016-10-27 12:40:59 -04:00
Eddie Hebert afbe3cdcd7 ENH: Volume based rolls for futures.
Add roll style which takes the volume of the contracts into account.
If the volume moves from the front to the back before the auto close
date, the roll is put at that session.

Also, factors out some of the common logic shared with calendar based rolls.
2016-10-25 14:08:21 -04:00
Eddie Hebert 5b425d54d0 ENH: Add adjusted history for continuous futures.
Add `.adj('mul')` and `.adj('add')` methods on ContinuousFuture, which
when used with `history`, will calculate and apply adjustments so that
the values are adjusted to account for discounts and premiums during
rolls.

Example usage in an algo:

```
from zipline.api import continuous_future

def initialize(context):
    context.cl_add = continuous_future('CL', offset=0, roll='calendar').adj('add')
    context.cl_mul = continuous_future('CL', offset=0, roll='calendar').adj('mul')
    context.cl = continuous_future('CL', offset=0, roll='calendar')
    schedule_function(print_history)

def print_history(context, data):
    frame = data.history([context.cl, context.cl_add, context.cl_mul],
                         ['price', 'sid'],
                         20,
                         '1d')
    print 'unadjusted'
    print frame.loc[:, :, context.cl]
    print 'adjusted add'
    print frame.loc[:, :, context.cl_add]
    print 'adjusted mul'
    print frame.loc[:, :, context.cl_mul]
```
2016-10-21 10:18:12 -04:00
Eddie Hebert a5f0df30dd TST: Encode minutes in continuous future tests.
Include minutes (in addition to the days) in the price encoding for
continuous futures tests.

Need for different values minute to minute arose when working on tests
for adjusted values.
2016-10-19 11:40:53 -04:00
Eddie Hebert 73b03de63e ENH: Add history for continuous futures.
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.
2016-10-16 22:40:08 -04:00
Eddie Hebert ca8950bf9c 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.
2016-10-11 16:16:16 -04:00
Eddie Hebert ec6f298972 ENH: Add continuous future current contract.
Add the ability for an algorithm to request the current contract for a
future chain via `data.current`.

e.g.:
```
data.current(ContinuousFuture('CL', offset=0, roll='calendar'),
'contract')
```
2016-10-07 18:26:23 -04:00