import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from quantopian.research import prices, symbols, returns
from quantopian.research import run_pipeline
from quantopian.pipeline import Pipeline
from quantopian.pipeline.factors import Returns
from quantopian.pipeline.data import EquityPricing
from quantopian.pipeline.filters import QTradableStocksUS
import alphalens as al
from quantopian.pipeline.filters.morningstar import Q500US
from quantopian.pipeline.factors import AverageDollarVolume
from alphalens.tears import create_full_tear_sheet
from alphalens.utils import get_clean_factor_and_forward_returns
from alphalens.tears import create_full_tear_sheet
from alphalens.tears import create_information_tear_sheet
from alphalens.tears import create_returns_tear_sheet
from alphalens.performance import mean_information_coefficient
def make_pipeline():
base_universe = QTradableStocksUS()
weekly_return = Returns(
inputs=[EquityPricing.close],
window_length=7
)
weekly_returns_decile = Returns(window_length=10).deciles()
top_decile = (weekly_returns_decile.isnull())
return Pipeline(
columns={
'Weekly Returns': weekly_return,
# 'Weekly Returns Decile': weekly_returns_decile
},
screen=QTradableStocksUS()
)
my_pipe = make_pipeline()
result = run_pipeline(make_pipeline(), '2003-01-01', '2004-05-01')
result.head()
print('Number of securities that passed the filter: %d' % len(result))
pricing_data = get_pricing(
symbols=result.index.levels[1], # Finds all assets that appear at least once in "factor_data"
start_date='2003-01-01',
end_date='2004-05-01', # must be after run_pipeline()'s end date. Explained more in lesson 4
fields='open_price' # Generally, you should use open pricing. Explained more in lesson 4
)
pricing_data
merged_data = get_clean_factor_and_forward_returns(
factor=result,
prices=pricing_data
)
merged_data
create_full_tear_sheet(merged_data)
create_information_tear_sheet(merged_data)
create_returns_tear_sheet(merged_data)
mean_information_coefficient(merged_data).plot(title="IC Decay");