# Import modules needed for pipeline
from quantopian.research import run_pipeline
from quantopian.pipeline import Pipeline
# Import any datasets we need
from quantopian.pipeline.data import USEquityPricing
# Import any built in factors we want to use
from quantopian.pipeline.factors import CustomFactor, Returns, AverageDollarVolume
# Import any built in filters we need
from quantopian.pipeline.filters import QTradableStocksUS, Q500US
# Import nupy and pandas because they rock
import numpy as np
import pandas as pd
class Previous_Strong(CustomFactor):
inputs = [USEquityPricing.low]
window_length = 22
def compute(self, today, asset_id, out, low):
# Make a pandas dataframe so we can use the rolling methods
low_df = pd.DataFrame(low)
# Set a value of True whenever the first value of the window minus the mean is less than a constant
MIN_DELTA = .15
first_lower_than_mean = low_df.rolling(3).apply(lambda group: (group[0]-group.mean() < MIN_DELTA))
# Sum the number of times the above condition is true
score = first_lower_than_mean.sum()
out[:] = score
def make_pipeline():
base_universe = Q500US()
# Define all the conditions for scoring
return_gt_1 = Returns(window_length=5) > .1
rerturns_top_50 = Returns(window_length=5).top(50, mask=base_universe)
price_gt_5 = EquityPricing.close.latest > 5
dollar_volume_gt_mean = AverageDollarVolume(window_length=10).demean(mask=(base_universe)) > 0
# Place the conditions into the pipeline output
return Pipeline(
columns={
'Weeklyreturn_gt_1': return_gt_1,
'rerturns_top_50': rerturns_top_50,
'price_gt_5': price_gt_5,
'dollar_volume_gt_mean': dollar_volume_gt_mean,
'previous_strong': Previous_Strong(mask=base_universe)
},
screen=base_universe
)
# Run pipeline to get the data into a dataframe
pipe_output = run_pipeline(make_pipeline(), '2013-01-01', '2014-05-01')
# Add a column for 'score' to the dataframe
# It will be the sum of the true conditions. Use 'sum' since True=1
pipe_output['score'] = pipe_output.sum(axis=1)
pipe_output.head(10)
# One can plot various things.
# Lets check the distribution of scores using the `hist` method
pipe_output.score.hist()
# Can also plot how the score of an asset changed over time
# The `xs` method will slice a single date or asset
pipe_output.xs(symbols('AAPL'), level=1).score.plot()