MichaelJMath
7/16/2017 - 11:58 PM

## Quantopian Alphalens snippets

Quantopian Alphalens snippets

``````def get_al_prices(result, periods=(1,5,21)):
assets = result.index.get_level_values(1).unique()
start_date = result.index.get_level_values(0)[0]
end_date = result.index.get_level_values(0)[-1]  + max(periods) * pd.tseries.offsets.BDay()
pricing = get_pricing(assets, start_date, end_date, fields="open_price")
return pricing

def get_factor_data(result,
factor_col,
periods=(1,5,21),
quantiles=5,
bins=None,
groupby=None,
binning_by_group=False,
groupby_labels=None,
max_loss=0.35):

pricing = get_al_prices(result, periods)

factor_data = al.utils.get_clean_factor_and_forward_returns(factor=result[factor_col],
prices=pricing,
groupby=groupby,
binning_by_group=binning_by_group,
groupby_labels=groupby_labels,
quantiles=quantiles,
bins=bins,
periods=periods,
max_loss=max_loss)

return pricing, factor_data``````
``````al.tears.create_full_tear_sheet(factor_data,
by_group=True,
long_short=True)``````
``````def get_al_prices(result, periods=(1,5,21)):
assets = result.index.levels[1].unique()
start_date = result.index.get_level_values(0)[0]
end_date = result.index.get_level_values(0)[-1]  + max(periods) * pd.tseries.offsets.BDay()
pricing = get_pricing(assets, start_date, end_date, fields="open_price")
return pricing

``````
``````factor_data = al.utils.get_clean_factor_and_forward_returns(factor=result['Factor_Column_Name'],
prices=pricing,
groupby=result['sector_code'],
by_group=False,
groupby_labels=MORNINGSTAR_SECTOR_CODES,
quantiles=5,
periods=(1,5,10))``````
``````# Create Event Tearsheet to Evaluate Alpha decay
al.tears.create_event_returns_tear_sheet(factor_data,
pricing,
avgretplot=(0,65),
long_short=True,
by_group=False)``````