Adding a built in Returns factor to the pipeline API.

This commit is contained in:
Tim Shawver
2015-12-01 13:24:41 -05:00
parent efcb016f64
commit 631a1879a3
4 changed files with 43 additions and 1 deletions
+4
View File
@@ -58,3 +58,7 @@ benchmarks.db
# Vagrant temp folder
.vagrant
# Intellij IDE temp project files
.project
zipline.iml
+25 -1
View File
@@ -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)
+2
View File
@@ -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',
+12
View File
@@ -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