Skip to content

Commit

Permalink
Merge pull request quantopian#1324 from quantopian/rate-of-change-per…
Browse files Browse the repository at this point in the history
…centage

Rate of change percentage
  • Loading branch information
richafrank authored Jul 14, 2016
2 parents 4a0629b + c9b0e40 commit 8b9a9dd
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 0 deletions.
26 changes: 26 additions & 0 deletions tests/pipeline/test_technical.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from __future__ import division

from nose_parameterized import parameterized
from six.moves import range
import numpy as np
import pandas as pd
import talib
Expand All @@ -15,6 +16,7 @@
Aroon,
FastStochasticOscillator,
IchimokuKinkoHyo,
RateOfChangePercentage,
)
from zipline.testing import ExplodingObject, parameter_space
from zipline.testing.fixtures import WithAssetFinder, ZiplineTestCase
Expand Down Expand Up @@ -354,3 +356,27 @@ def test_input_validation(self, arg):
str(e.exception),
'%s must be <= the window_length: 53 > 52' % arg,
)


class TestRateOfChangePercentage(ZiplineTestCase):
@parameterized.expand([
('constant', [2.] * 10, 0.0),
('step', [2.] + [1.] * 9, -50.0),
('linear', [2. + x for x in range(10)], 450.0),
('quadratic', [2. + x**2 for x in range(10)], 4050.0),
])
def test_rate_of_change_percentage(self, test_name, data, expected):
window_length = len(data)

rocp = RateOfChangePercentage(
inputs=(USEquityPricing.close,),
window_length=window_length,
)
today = pd.Timestamp('2014')
assets = np.arange(5, dtype=np.int64)
# broadcast data across assets
data = np.array(data)[:, np.newaxis] * np.ones(len(assets))

out = np.zeros(len(assets))
rocp.compute(today, assets, out, data)
assert_equal(out, np.full((len(assets),), expected))
2 changes: 2 additions & 0 deletions zipline/pipeline/factors/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
FastStochasticOscillator,
IchimokuKinkoHyo,
MaxDrawdown,
RateOfChangePercentage,
Returns,
RSI,
SimpleMovingAverage,
Expand All @@ -47,6 +48,7 @@
'IchimokuKinkoHyo',
'Latest',
'MaxDrawdown',
'RateOfChangePercentage',
'RecarrayField',
'Returns',
'RollingLinearRegressionOfReturns',
Expand Down
23 changes: 23 additions & 0 deletions zipline/pipeline/factors/technical.py
Original file line number Diff line number Diff line change
Expand Up @@ -590,3 +590,26 @@ def compute(self,
out.senkou_span_a = (tenkan_sen + kijun_sen) / 2
out.senkou_span_b = (high.max(axis=0) + low.min(axis=0)) / 2
out.chikou_span = close[chikou_span_length]


class RateOfChangePercentage(CustomFactor):
"""
Rate of change Percentage
ROC measures the percentage change in price from one period to the next.
The ROC calculation compares the current price with the price `n`
periods ago.
Formula for calculation: ((price - prevPrice) / prevPrice) * 100
price - the current price
prevPrice - the price n days ago, equals window length
"""
def compute(self, today, assets, out, close):
today_close = close[-1]
prev_close = close[0]
evaluate('((tc - pc) / pc) * 100',
local_dict={
'tc': today_close,
'pc': prev_close
},
global_dict={},
out=out,
)

0 comments on commit 8b9a9dd

Please sign in to comment.