From 631a1879a3ab8009e17c4f13fd91a79bc467f26b Mon Sep 17 00:00:00 2001 From: Tim Shawver Date: Mon, 30 Nov 2015 15:45:15 -0500 Subject: [PATCH 1/3] Adding a built in Returns factor to the pipeline API. --- .gitignore | 4 ++++ tests/pipeline/test_factor.py | 26 +++++++++++++++++++++++++- zipline/pipeline/factors/__init__.py | 2 ++ zipline/pipeline/factors/technical.py | 12 ++++++++++++ 4 files changed, 43 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 6f7da1df..5ff97ba4 100644 --- a/.gitignore +++ b/.gitignore @@ -58,3 +58,7 @@ benchmarks.db # Vagrant temp folder .vagrant + +# Intellij IDE temp project files +.project +zipline.iml diff --git a/tests/pipeline/test_factor.py b/tests/pipeline/test_factor.py index 53de9839..80e155a4 100644 --- a/tests/pipeline/test_factor.py +++ b/tests/pipeline/test_factor.py @@ -8,7 +8,7 @@ from numpy.random import randn, seed from zipline.errors import UnknownRankMethod from zipline.pipeline import Factor, Filter, TermGraph -from zipline.pipeline.factors import RSI +from zipline.pipeline.factors import RSI, Returns from zipline.utils.test_utils import check_allclose, check_arrays from .base import BasePipelineTestCase @@ -222,3 +222,27 @@ class FactorTestCase(BasePipelineTestCase): rsi.compute(today, assets, out, test_data) check_allclose(expected, out) + + @parameterized.expand([ + (100, 15), + (101, 4), + (102, 100), + ]) + def test_returns(self, seed_value, window_length): + + returns = Returns(window_length=window_length) + + today = datetime64(1, 'ns') + assets = arange(3) + out = empty((3,), dtype=float) + + seed(seed_value) # Seed so we get deterministic results. + test_data = abs(randn(window_length, 3)) + + # Calculate the expected returns + expected = (test_data[-1] - test_data[0]) / test_data[0] + + out = empty((3,), dtype=float) + returns.compute(today, assets, out, test_data) + + check_allclose(expected, out) diff --git a/zipline/pipeline/factors/__init__.py b/zipline/pipeline/factors/__init__.py index 6eab0719..05347115 100644 --- a/zipline/pipeline/factors/__init__.py +++ b/zipline/pipeline/factors/__init__.py @@ -6,6 +6,7 @@ from .latest import Latest from .technical import ( MaxDrawdown, RSI, + Returns, SimpleMovingAverage, VWAP, WeightedAverageValue, @@ -17,6 +18,7 @@ __all__ = [ 'Latest', 'MaxDrawdown', 'RSI', + 'Returns', 'SimpleMovingAverage', 'VWAP', 'WeightedAverageValue', diff --git a/zipline/pipeline/factors/technical.py b/zipline/pipeline/factors/technical.py index ba58dd58..688c9b80 100644 --- a/zipline/pipeline/factors/technical.py +++ b/zipline/pipeline/factors/technical.py @@ -25,6 +25,18 @@ from zipline.utils.control_flow import ignore_nanwarnings from .factor import CustomFactor +class Returns(CustomFactor): + """ + Calculates the percent change in close price over the given window_length. + + **Default Inputs**: [USEquityPricing.close] + """ + inputs = [USEquityPricing.close] + + def compute(self, today, assets, out, close): + out[:] = (close[-1] - close[0]) / close[0] + + class RSI(CustomFactor, SingleInputMixin): """ Relative Strength Index From 026ab30ea5edf6b1f623208cc9c1d375d4a0a617 Mon Sep 17 00:00:00 2001 From: Tim Shawver Date: Tue, 1 Dec 2015 15:26:10 -0500 Subject: [PATCH 2/3] Added an entry to whatsnew for 0.8.4. --- docs/source/whatsnew/0.8.4.txt | 3 +++ 1 file changed, 3 insertions(+) diff --git a/docs/source/whatsnew/0.8.4.txt b/docs/source/whatsnew/0.8.4.txt index 700316cd..a55df6fe 100644 --- a/docs/source/whatsnew/0.8.4.txt +++ b/docs/source/whatsnew/0.8.4.txt @@ -28,6 +28,9 @@ Enhancements :meth:`~zipline.pipeline.factors.Factor.isfinite` methods to :class:`zipline.pipeline.factors.Factor` (:issue:`861`). +* Added :class:`zipline.pipeline.factors.Returns`, a built-in factor which + calculates the percent change in close price over the given window_length. + Experimental Features ~~~~~~~~~~~~~~~~~~~~~ From d576a9dd3a99c93f90b9a9b91b198d6ad516e2ae Mon Sep 17 00:00:00 2001 From: Tim Shawver Date: Tue, 1 Dec 2015 15:54:19 -0500 Subject: [PATCH 3/3] Add the built-in factors to the API docs for zipline.io, so that I can link to the Returns factor from the release notes. --- docs/source/appendix.rst | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/docs/source/appendix.rst b/docs/source/appendix.rst index e73d5102..9da7452f 100644 --- a/docs/source/appendix.rst +++ b/docs/source/appendix.rst @@ -33,6 +33,27 @@ Pipeline API :exclude-members: dtype :member-order: bysource +.. autoclass:: zipline.pipeline.factors.Latest + :members: + +.. autoclass:: zipline.pipeline.factors.MaxDrawdown + :members: + +.. autoclass:: zipline.pipeline.factors.Returns + :members: + +.. autoclass:: zipline.pipeline.factors.RSI + :members: + +.. autoclass:: zipline.pipeline.factors.SimpleMovingAverage + :members: + +.. autoclass:: zipline.pipeline.factors.VWAP + :members: + +.. autoclass:: zipline.pipeline.factors.WeightedAverageValue + :members: + .. autoclass:: zipline.pipeline.filters.Filter :members: __and__, __or__ :exclude-members: dtype