Commit Graph

1318 Commits

Author SHA1 Message Date
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 f1929342d9 Merge pull request #1556 from quantopian/volume-based-rolls
ENH: Volume based rolls for futures.
2016-10-25 15:21:41 -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 321afc1626 Merge pull request #1555 from quantopian/return-nan-from-bcolz-daily-reader
MAINT: Return nan from daily bcolz get_value.
2016-10-25 11:49:51 -04:00
Eddie Hebert 098d38ac76 MAINT: Return nan from daily bcolz get_value.
Match the behavior of the minute bar reader, now that the session and
minute bar readers share a common interface.

isnull is slightly slower than checking against -1; however, n cases
where we check against illiquid trades in a tight loop, volume is
checked which is not using nan. The change here should be marginal with
regards to performance.
2016-10-25 11:25:09 -04:00
Eddie Hebert 3c7df3e997 TST: Make test dates aligned with test data.
Move dates queried near beginning of test data so that the range of data
covered does not extend beyond the beginning of the range.
i.e. the windows were covering 2016-01-25, which had no test data generated.
(Does not matter for the calendar based rolls, but is needed for volume
based rolls.)

Also, make room for having the first roll to be a day before the first auto
close by moving the first contracts auto close date back a day.

In preparation for testing volume rolls.
2016-10-25 10:21:23 -04:00
Eddie Hebert fccbae25ed BUG: Fix session from minute reader's last traded.
The last traded dt provided from the session bar reader which resamples
from minutes should provide a dt that is a session label, not one that
is at the minute frequency.
2016-10-24 13:58:58 -04:00
Eddie Hebert a4205a0500 PERF: Speedup minute to session sampling.
The minute to session sampling reading was creating two DataFrame
objects, the first to hold the minute data, and then a second returned
by the `DataFrame.groupby` to sample down to sessions.

Instead use the arrays returned by the minute readers `load_raw_arrays`
and implement sampling logic which takes advantage that the minutes
being passed start with the first minute of the first session and end
with the last minute of the last session.

On my machine this takes the tests in `test/test_continuous_futures`
from ~4.0 to about ~0.1 seconds.
2016-10-24 09:59:22 -04:00
Maya Tydykov e1f008edcd TST: update adjustment tests - add gaps between sids
TST: add a seed for permuting
2016-10-21 16:53:56 -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 a1a99dd9aa MAINT: Limit perspective offset.
Limit the perspective offset to 1. There is a possibility that if a
consumer of the AdjustedArrayWindow does not fetch adjustments between
the end of the data window and the vantage points beyond the end of the
window.

Until that case has a solution, e.g. having the consumer of the
AdjustedArrayWindow include the perspective offset when calculating the
query for adjustments, limit the offsets to 1.
2016-10-17 15:08:11 -04:00
Scott Sanderson 0244f03411 DOC: Fix typo in comment. 2016-10-17 14:23:39 -04:00
Scott Sanderson 9738c14271 MAINT: Use perspective_offset in more tests.
- Refactor `test_adjusted_array` to test a range of perspective_offsets in
  all tests.

- Make perspective_offset a parameter to `AdjustedArray.traverse`
  instead of `AdjustedArray`.
2016-10-17 14:23:39 -04:00
Eddie Hebert 7049d11c1f MAINT: Perspective offset for load adjustments.
Add a perspective offset to `AdjustedArrayWindow` and `AdjustedArray`,
so that `HistoryLoader` does not need to twiddle with offsets to support
viewing the data from the bar after end of the window, (Which is the
case when a '1d' history window is retrieved in minute mode, which is
explained in the docstring for `HistoryLoader.history`)

Presently, this simplifies the logic in
`HistoryLoader._get_adjustments_in_range`, and other incoming
AdjustmentReader's, (e.g. the roll based adjustment reader for continous
futures.) This patch should also make it easier for history and pipeline
to converge on a singular `load_adjustments` method.
2016-10-17 14:23:39 -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
Joe Jevnik abcb6276ff BUG: fix blaze query in ffill_query_in_range to correct issue in events loader 2016-10-13 15:27:44 -04:00
Scott Sanderson 053206327b ENH: Name overrides in preprocessor factories.
Allows ``__funcname`` to be passed to preprocessors like expect_types
and expect_dtypes to override the name displayed in error messages.
This is useful for providing clearer errors for ``__init__`` and
``__new__`` methods in classes.
2016-10-12 15:50:10 -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
Maya Tydykov fe00452b7b Merge pull request #1525 from quantopian/fix_estimates_overwrites_bug
Fix estimates overwrites bug
2016-10-10 09:02:40 -04:00
Maya Tydykov 4efe99017a TST: add test condition to check for bug 2016-10-10 08:41:02 -04:00
Scott Sanderson acc46f5fe3 Merge pull request #1530 from quantopian/add-specific-assets
ENH: Add `SpecificAssets` filter.
2016-10-09 14:09:14 -04:00
Eddie Hebert 3ab251c90c Merge pull request #1529 from quantopian/current-contract
ENH: Add continuous future current contract.
2016-10-07 23:39:01 -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
Scott Sanderson f874e670c4 ENH: Add SpecificAssets filter.
Adds a filter that matches a set of assets.  Mainly useful for testing
and debugging.
2016-10-07 18:11:01 -04:00
Andrew Daniels eba02da271 ENH: Adds last_available_{session, minute} args to DataPortal (#1528)
This allows optionally setting the last available dts in the DataPortal
explicitly. If these args aren't provided, we fall back to inferring
these from the underlying readers, which was the previous behavior.
2016-10-06 20:46:54 -04:00
Scott Sanderson 8465ea18d0 TEST: Simplify testing of restriction orderings. 2016-10-05 14:42:17 -04:00
Andrew Liang b0aba20a6e BUG: Restrictions passed into HistoricalRestrictions not sorted correctly 2016-10-05 14:09:26 -04:00
Andrew Liang 2104a35af8 ENH: _UnionRestrictions for combining multiple Restrictions 2016-09-30 16:35:24 -04:00
Andrew Liang 3b5031a829 MAINT: Rename restrictions.py to asset_restrictions.py
For clarity as to what sort of restrictions these are
2016-09-30 16:35:24 -04:00
Scott Sanderson d47144dfb8 DOC: Rename NoopRestrictions to NoRestrictions. 2016-09-30 16:35:23 -04:00
Scott Sanderson 523b05ef3f TEST: Clarify test_restrictions a bit.
- Use parameter_space instead of `parameterized.expand`.
- Use a timedelta instead of concatenating strings.
- Use a (possibly no-op) scramble function instead of reordering list
  literals.
- Use `freeze_dt, unfreeze_dt, re_freeze_dt` instead of `dts[n]`.
- Rename `assert_vectorized_results` to `assert_all_restrictions`.
2016-09-30 16:35:23 -04:00
Andrew Liang bf8b030417 MAINT: Deprecate set_do_not_order_list
In favor of a new method `set_restrictions` which takes a Restrictions
object. Calls to `set_do_not_order_list` should raise a deprecation
warning and create an equivalent Restrictions object, with which
`set_restrictions` will be called. For convenience, create a
RestrictionsSet from which the "restrictions" version of a security
list can be accessed
2016-09-30 16:35:23 -04:00
Andrew Liang 5e276d0e72 TEST: Modify tests for extra BarData parameter
Introducing a WithCreateBarData fixture which allows for the
creation of a BarData using only the `simulation_dt_func` and
`restrictions` params. Assumes that each suite uses the same
`data_portal`, `data_frequency` and `trading_calendar`
2016-09-29 10:11:15 -04:00
Andrew Liang e465f64f91 MAINT: Create SecurityListRestrictions that takes a SecurityList
The SecurityList implements a non-exposed method
`current_securities(dt)` which SecurityListRestrictions calls to
determine if an asset is restricted. Deprecate the `__iter__` and
`__contains__` methods of security lists in favor of
`current_securities(dt)`
2016-09-29 10:11:14 -04:00
Andrew Liang b70084c6bf ENH: can_trade should take restricted list into account
Additionally, create an option for a violation of a 'do not order'
trading control to log an error instead of failing
2016-09-29 10:11:14 -04:00
Andrew Liang 0119aba410 ENH: A point-in-time restricted list with restrictions stored in memory
An ABC Restrictions defines a group of restrictions responsible
for returning restriction information for sids on certain dts. An
InMemoryRestrictions is a point-in-time group of such restrictions,
with all restrictions and their dates passed in upon instantiation.
A StaticRestrictedList takes a list of sids, restricting them at all
dates
2016-09-27 18:19:51 -04:00
Maya Tydykov f528c01ca9 TST: add test for changing event dates and adjustments
BUG: get column names from column dict

BUG: fix name map
2016-09-27 16:53:36 -04:00
Joe Jevnik 8d2e6446ee PERF: only query for the columns requested + metadata
BUG: choose last event date for quarter shift
2016-09-27 09:54:46 -04:00
Maya Tydykov 65d15d3960 TST: add test for sid with no data
MAINT: optimization - only look at assets appearing in data

TST: simplify test

DOC: add documentation for checkpoints

MAINT: explicitly cast event date field to datetime

MAINT: add back import

TST: fix indexing to remove setting wtih copy warning
2016-09-27 09:54:44 -04:00
Maya Tydykov 90984be20c TST: add test for missing num_quarters and clean up tests 2016-09-27 09:54:43 -04:00
Maya Tydykov 2a09160ca8 TST: add test to check previous columns w/ multiple qtrs
MAINT: pass column to name dict

MAINT: make check for invalid num columns py3-compatible
2016-09-27 09:54:41 -04:00
Maya Tydykov cc07a00d16 TST: add test for requesting multiple datasets with multiple quarters 2016-09-27 09:54:41 -04:00
Maya Tydykov ebbe85b79a TST: add test for datetime array and update test
TST: fix quarter normalization test

TST: change test name

BUG: remove arg

BUG: look at dict keys

TST: add test for windowing

MAINT: raise ValueError instead of asserting

TST: add assertion to check windowing

TST: parametrize test over number of quarters forward/back.

BUG: fix adjustment calculation logic for quarter crossovers.

TST: add test for previous quarter windows

BUG: fix bugs in calculating previous windows

BUG: fix missing value for datetime

TST: add test case for missing quarter
2016-09-27 09:54:41 -04:00
Maya Tydykov 2975f9b2fd TST: add test for 1d array overwrite 2016-09-27 09:54:40 -04:00
Maya Tydykov d578463dcf TST: add cases for shifting release dates
BUG: fix bugs in blaze loader

BUG: call correct method

MAINT: explicitly cast dates column

MAINT: modify code to comply with pandas 0.16.1
2016-09-27 09:54:38 -04:00
Maya Tydykov 863da5932c TST: add tests for quarter rotation logic 2016-09-27 09:54:37 -04:00