Notebook
In [104]:
from quantopian.pipeline import CustomFactor, Pipeline
from quantopian.research import run_pipeline
from quantopian.pipeline.data.builtin import USEquityPricing

import numpy
In [105]:
class StdDev(CustomFactor):
    def compute(self, today, asset_ids, out, values):
        # Calculates the column-wise standard deviation, ignoring NaNs
        out[:] = numpy.nanstd(values, axis=0)
In [106]:
def make_pipeline():
    mean_close_10 = SimpleMovingAverage(inputs=[USEquityPricing.close], window_length=10)
    volume_filter = (USEquityPricing.volume.latest > 1000000)
    std_dev = StdDev(inputs=[USEquityPricing.close], window_length=5)
    std_filter = (std_dev > 2)
    price_filter = (USEquityPricing.close.latest > 5) and (USEquityPricing.close.latest < 50)
    return Pipeline(
        columns={
            
            '10 MA of closing price' : mean_close_10,
            'Volume': USEquityPricing.volume.latest,
            'std_dev': std_dev
        } , screen=(std_filter & price_filter & volume_filter)
    )
    
   
In [107]:
result = run_pipeline(make_pipeline(), '2016-12-15', '2016-12-15')
print len(result)
10
In [103]:
result.sort_values([''], ascending=[1,0])
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-103-73f4f3bd8200> in <module>()
----> 1 result.sort_values([''], ascending=[1,0])

/usr/local/lib/python2.7/dist-packages/pandas/core/frame.pyc in sort_values(self, by, axis, ascending, inplace, kind, na_position)
   3126         if com.is_sequence(ascending) and len(by) != len(ascending):
   3127             raise ValueError('Length of ascending (%d) != length of by (%d)' %
-> 3128                              (len(ascending), len(by)))
   3129         if len(by) > 1:
   3130             from pandas.core.groupby import _lexsort_indexer

ValueError: Length of ascending (2) != length of by (1)
In [16]:
class TenDayMeanDifference(CustomFactor):
    # Default inputs.
    inputs = [USEquityPricing.close, USEquityPricing.open]
    window_length = 10
    def compute(self, today, asset_ids, out, close, open):
        # Calculates the column-wise mean difference, ignoring NaNs
        out[:] = numpy.nanmean(close - open, axis=0)
In [17]:
class Momentum(CustomFactor):
    # Default inputs
    inputs = [USEquityPricing.close]

    # Compute momentum
    def compute(self, today, assets, out, close):
        out[:] = close[-1] / close[0]
In [ ]: