From 72887f0065971c174f505237aeda6091cf18d54c Mon Sep 17 00:00:00 2001 From: Scott Sanderson Date: Mon, 21 Dec 2015 12:14:01 -0500 Subject: [PATCH] ENH: Change DollarVolume to AverageDollarVolume. --- docs/source/appendix.rst | 3 ++- docs/source/whatsnew/0.8.4.txt | 2 +- tests/pipeline/test_engine.py | 21 +++++++++++++++------ zipline/pipeline/factors/__init__.py | 4 ++-- zipline/pipeline/factors/technical.py | 14 ++++++++++---- 5 files changed, 30 insertions(+), 14 deletions(-) diff --git a/docs/source/appendix.rst b/docs/source/appendix.rst index 575aa5cf..84139a8d 100644 --- a/docs/source/appendix.rst +++ b/docs/source/appendix.rst @@ -66,7 +66,8 @@ Pipeline API .. autoclass:: zipline.pipeline.factors.ExponentialWeightedMovingStdDev :members: -.. autofunction:: zipline.pipeline.factors.DollarVolume +.. autoclass:: zipline.pipeline.factors.AverageDollarVolume + :members: .. autoclass:: zipline.pipeline.filters.Filter :members: __and__, __or__ diff --git a/docs/source/whatsnew/0.8.4.txt b/docs/source/whatsnew/0.8.4.txt index 8eaa3522..5944d37b 100644 --- a/docs/source/whatsnew/0.8.4.txt +++ b/docs/source/whatsnew/0.8.4.txt @@ -53,7 +53,7 @@ Enhancements window_length. (:issue:`884`). * Added a new built-in factor: - :class:`~zipline.pipeline.factors.DollarVolume`. (:issue:`910`). + :class:`~zipline.pipeline.factors.AverageDollarVolume`. (:issue:`927`). * Added :class:`~zipline.pipeline.factors.ExponentialWeightedMovingAverage` and :class:`~zipline.pipeline.factors.ExponentialWeightedMovingStdDev` diff --git a/tests/pipeline/test_engine.py b/tests/pipeline/test_engine.py index 6174bf68..438b5dbb 100644 --- a/tests/pipeline/test_engine.py +++ b/tests/pipeline/test_engine.py @@ -54,7 +54,7 @@ from zipline.pipeline.loaders.equity_pricing_loader import ( from zipline.pipeline.engine import SimplePipelineEngine from zipline.pipeline import CustomFactor from zipline.pipeline.factors import ( - DollarVolume, + AverageDollarVolume, EWMA, EWMSTD, ExponentialWeightedMovingAverage, @@ -951,9 +951,18 @@ class ParameterizedFactorTestCase(TestCase): def test_dollar_volume(self): results = self.engine.run_pipeline( - Pipeline(columns={'dv': DollarVolume()}), - self.dates[0], + Pipeline( + columns={ + 'dv1': AverageDollarVolume(window_length=1), + 'dv5': AverageDollarVolume(window_length=5), + } + ), + self.dates[5], self.dates[-1], - )['dv'].unstack() - expected = (self.raw_data ** 2) * 2 - assert_frame_equal(results, expected) + ) + + expected_1 = (self.raw_data[5:] ** 2) * 2 + assert_frame_equal(results['dv1'].unstack(), expected_1) + + expected_5 = rolling_mean((self.raw_data ** 2) * 2, window=5)[5:] + assert_frame_equal(results['dv5'].unstack(), expected_5) diff --git a/zipline/pipeline/factors/__init__.py b/zipline/pipeline/factors/__init__.py index fabfa497..edd928b1 100644 --- a/zipline/pipeline/factors/__init__.py +++ b/zipline/pipeline/factors/__init__.py @@ -8,7 +8,7 @@ from .events import ( BusinessDaysUntilNextEarnings, ) from .technical import ( - DollarVolume, + AverageDollarVolume, EWMA, EWMSTD, ExponentialWeightedMovingAverage, @@ -25,7 +25,7 @@ __all__ = [ 'BusinessDaysSincePreviousEarnings', 'BusinessDaysUntilNextEarnings', 'CustomFactor', - 'DollarVolume', + 'AverageDollarVolume', 'EWMA', 'EWMSTD', 'ExponentialWeightedMovingAverage', diff --git a/zipline/pipeline/factors/technical.py b/zipline/pipeline/factors/technical.py index 273618f2..fe08b9c0 100644 --- a/zipline/pipeline/factors/technical.py +++ b/zipline/pipeline/factors/technical.py @@ -130,12 +130,18 @@ class MaxDrawdown(CustomFactor, SingleInputMixin): out[i] = (peak - data[end, i]) / data[end, i] -def DollarVolume(): +class AverageDollarVolume(CustomFactor): """ - Returns a Factor computing the product of most recent close price and - volume. + Average Daily Dollar Volume + + **Default Inputs:** [USEquityPricing.close, USEquityPricing.volume] + + **Default Window Length:** None """ - return USEquityPricing.close.latest * USEquityPricing.volume.latest + inputs = [USEquityPricing.close, USEquityPricing.volume] + + def compute(self, today, assets, out, close, volume): + out[:] = nanmean(close * volume, axis=0) class _ExponentialWeightedFactor(SingleInputMixin, CustomFactor):