Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
367 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
from zipline.pipeline import Pipeline, run_chunked_pipeline | ||
from zipline.pipeline.data import USEquityPricing | ||
from zipline.pipeline.factors import Returns | ||
from zipline.testing import ZiplineTestCase | ||
from zipline.testing.fixtures import WithEquityPricingPipelineEngine | ||
|
||
|
||
class ChunkedPipelineTestCase(WithEquityPricingPipelineEngine, | ||
ZiplineTestCase): | ||
|
||
def test_run_chunked_pipeline(self): | ||
""" | ||
Test that running a pipeline in chunks produces the same result as if | ||
it were run all at once | ||
""" | ||
pipe = Pipeline( | ||
columns={ | ||
'close': USEquityPricing.close.latest, | ||
'returns': Returns(window_length=2), | ||
}, | ||
) | ||
sessions = self.nyse_calendar.all_sessions | ||
start_date = sessions[sessions.get_loc(self.START_DATE) + 2] | ||
|
||
pipeline_result = self.pipeline_engine.run_pipeline( | ||
pipe, | ||
start_date=start_date, | ||
end_date=self.END_DATE, | ||
) | ||
chunked_result = run_chunked_pipeline( | ||
engine=self.pipeline_engine, | ||
pipeline=pipe, | ||
start_date=start_date, | ||
end_date=self.END_DATE, | ||
chunksize=22 | ||
) | ||
self.assertTrue(chunked_result.equals(pipeline_result)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
from pandas import Timestamp | ||
|
||
from nose_parameterized import parameterized | ||
|
||
from zipline.testing import ZiplineTestCase | ||
from zipline.utils.calendars import get_calendar | ||
from zipline.utils.date_utils import ( | ||
compute_date_range_chunks, | ||
roll_dates_to_previous_session | ||
) | ||
|
||
|
||
class TestDateUtils(ZiplineTestCase): | ||
|
||
@classmethod | ||
def init_class_fixtures(cls): | ||
super(TestDateUtils, cls).init_class_fixtures() | ||
cls.calendar = get_calendar('NYSE') | ||
|
||
@parameterized.expand([ | ||
( | ||
Timestamp('05-19-2017', tz='UTC'), # actual trading date | ||
Timestamp('05-19-2017', tz='UTC'), | ||
), | ||
( | ||
Timestamp('07-04-2015', tz='UTC'), # weekend nyse holiday | ||
Timestamp('07-02-2015', tz='UTC'), | ||
), | ||
( | ||
Timestamp('01-16-2017', tz='UTC'), # weeknight nyse holiday | ||
Timestamp('01-13-2017', tz='UTC'), | ||
), | ||
]) | ||
def test_roll_dates_to_previous_session(self, date, expected_rolled_date): | ||
self.assertEqual( | ||
roll_dates_to_previous_session(self.calendar, date)[0], | ||
expected_rolled_date | ||
) | ||
|
||
@parameterized.expand([ | ||
( | ||
None, | ||
[ | ||
( | ||
Timestamp('01-03-2017', tz='UTC'), | ||
Timestamp('01-31-2017', tz='UTC') | ||
) | ||
] | ||
), | ||
( | ||
10, | ||
[ | ||
( | ||
Timestamp('01-03-2017', tz='UTC'), | ||
Timestamp('01-17-2017', tz='UTC') | ||
), | ||
( | ||
Timestamp('01-18-2017', tz='UTC'), | ||
Timestamp('01-31-2017', tz='UTC') | ||
) | ||
] | ||
), | ||
( | ||
15, | ||
[ | ||
( | ||
Timestamp('01-03-2017', tz='UTC'), | ||
Timestamp('01-24-2017', tz='UTC') | ||
), | ||
( | ||
Timestamp('01-25-2017', tz='UTC'), | ||
Timestamp('01-31-2017', tz='UTC') | ||
) | ||
] | ||
), | ||
]) | ||
def test_compute_date_range_chunks(self, chunksize, expected): | ||
# These date ranges result in 20 business days | ||
start_date = Timestamp('01-03-2017') | ||
end_date = Timestamp('01-31-2017') | ||
|
||
date_ranges = compute_date_range_chunks( | ||
self.calendar, | ||
start_date, | ||
end_date, | ||
chunksize | ||
) | ||
|
||
self.assertListEqual(list(date_ranges), expected) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
from toolz import partition_all | ||
|
||
|
||
def roll_dates_to_previous_session(calendar, *dates): | ||
""" | ||
Roll ``dates`` to the next session of ``calendar``. | ||
Parameters | ||
---------- | ||
calendar : zipline.utils.calendars.trading_calendar.TradingCalendar | ||
The calendar to use as a reference. | ||
*dates : pd.Timestamp | ||
The dates for which the last trading date is needed. | ||
Returns | ||
------- | ||
rolled_dates: (np.datetime64, np.datetime64) | ||
The last trading date of the input dates, inclusive. | ||
""" | ||
all_sessions = calendar.all_sessions | ||
|
||
locs = [all_sessions.get_loc(dt, method='ffill') for dt in dates] | ||
return all_sessions[locs].tolist() | ||
|
||
|
||
def compute_date_range_chunks(calendar, start_date, end_date, chunksize): | ||
"""Compute the start and end dates to run a pipeline for. | ||
Parameters | ||
---------- | ||
calendar : TradingCalendar | ||
The trading calendar to align the dates with. | ||
start_date : pd.Timestamp | ||
The first date in the pipeline. | ||
end_date : pd.Timestamp | ||
The last date in the pipeline. | ||
chunksize : int or None | ||
The size of the chunks to run. | ||
Returns | ||
------- | ||
ranges : iterable[(np.datetime64, np.datetime64)] | ||
A sequence of start and end dates to run the pipeline for. | ||
""" | ||
if chunksize is None: | ||
dates = roll_dates_to_previous_session(calendar, start_date, end_date) | ||
|
||
return [(dates[0], dates[1])] | ||
|
||
all_sessions = calendar.all_sessions | ||
start_ix, end_ix = all_sessions.slice_locs(start_date, end_date) | ||
return ( | ||
(r[0], r[-1]) for r in partition_all( | ||
chunksize, all_sessions[start_ix:end_ix] | ||
) | ||
) |
Oops, something went wrong.