from quantopian.pipeline import Pipeline
from quantopian.pipeline.data import factset
from quantopian.research import run_pipeline
from quantopian.pipeline.filters import QTradableStocksUS
from quantopian.pipeline.factors import CustomFactor, SimpleBeta, Returns, SimpleMovingAverage
from quantopian.pipeline.data.builtin import USEquityPricing
# class yesterday_gain(CustomFactor):
# inputs =[USEquityPricing.close];
# window_length=2
# window_safe =True
# def compute(self,today,assets,out,close):
# days_return = (close[-1]-close[0])/close[0]
# out[:] = close[-1]
def make_pipeline():
# Measures a company's asset growth rate.
asset_growth = factset.Fundamentals.assets_gr_qf.latest
close_price =USEquityPricing.close.latest
universe = QTradableStocksUS()
factor_maps= {'Asset Growth': asset_growth,'price':close_price}
return Pipeline(
columns = factor_maps,
screen = universe
)
pipeline_output = run_pipeline(pipeline=make_pipeline(), start_date='2016-01-04', end_date='2016-12-01')
pipeline_output.head(5)
# Show the first 5 rows of factor_data
pricing_data = get_pricing(
symbols=pipeline_output.index.levels[1], # Finds all assets that appear at least once in "factor_data"
start_date='2016-01-04', end_date='2016-12-01',
fields='close_price' # Generally, you should use open pricing.
)
pricing_data.head(5)
class yesterday_gain(CustomFactor):
inputs =[USEquityPricing.close];
window_length=2
window_safe =True
def compute(self,today,assets,out,close):
days_return = ((close[-1]-close[0])/close[0])*100
out[:] = days_return
def make_pipeline():
# Measures a company's asset growth rate.
asset_growth = factset.Fundamentals.assets_gr_qf.latest
# close_price =USEquityPricing.close.latest
universe = QTradableStocksUS()
factor_maps= {'Asset Growth': asset_growth,'yesterday_gain':yesterday_gain(mask = universe)}
return Pipeline(
columns = factor_maps,
screen = universe
)
pipeline_output = run_pipeline(pipeline=make_pipeline(), start_date='2016-01-04', end_date='2016-12-01')
new_pipe = pipeline_output.reset_index()
new_pipe.loc[new_pipe.level_0>'2016-01-04']
# Show the first 5 rows of factor_data
pricing_data = get_pricing(
symbols=pipeline_output.index.levels[1], # Finds all assets that appear at least once in "factor_data"
start_date='2016-01-04',
end_date='2016-12-01',
fields='open_price' # Generally, you should use open pricing.
)
# Show the first 5 rows of pricing_data
pricing_data.head(5)
from alphalens.utils import get_clean_factor_and_forward_returns
factor_data = get_clean_factor_and_forward_returns(
factor=-pipeline_output['yesterday_gain'],
prices=pricing_data
)
# Show the first 5 rows of merged_data
factor_data.head(5)
Finally, execute the following cell to pass the output of get_clean_factor_and_forward_returns()
to a function called create_full_tear_sheet()
. This will create whats known as a tear sheet.
from alphalens.tears import create_full_tear_sheet
create_full_tear_sheet(factor_data)
In the next lesson, we will show you how to interpret the charts produced by create_full_tear_sheet()
.