#!/usr/bin/env python # coding: utf-8 # # Alphalens and Pyfolio integration # Alphalens can simulate the performance of a portfolio where the factor values are use to weight stocks. Once the portfolio is built, it can be analyzed by Pyfolio. For details on how this portfolio is built see: # - alphalens.performance.factor_returns # - alphalens.performance.cumulative_returns # - alphalens.performance.create_pyfolio_input # In[ ]: get_ipython().run_line_magic('pylab', 'inline --no-import-all') import alphalens import pyfolio import pandas as pd import numpy as np import datetime # First load some stocks data # In[2]: tickers = [ 'ACN', 'ATVI', 'ADBE', 'AMD', 'AKAM', 'ADS', 'GOOGL', 'GOOG', 'APH', 'ADI', 'ANSS', 'AAPL', 'AVGO', 'CA', 'CDNS', 'CSCO', 'CTXS', 'CTSH', 'GLW', 'CSRA', 'DXC', 'EBAY', 'EA', 'FFIV', 'FB', 'FLIR', 'IT', 'GPN', 'HRS', 'HPE', 'HPQ', 'INTC', 'IBM', 'INTU', 'JNPR', 'KLAC', 'LRCX', 'MA', 'MCHP', 'MSFT', 'MSI', 'NTAP', 'NFLX', 'NVDA', 'ORCL', 'PAYX', 'PYPL', 'QRVO', 'QCOM', 'RHT', 'CRM', 'STX', 'AMG', 'AFL', 'ALL', 'AXP', 'AIG', 'AMP', 'AON', 'AJG', 'AIZ', 'BAC', 'BK', 'BBT', 'BRK.B', 'BLK', 'HRB', 'BHF', 'COF', 'CBOE', 'SCHW', 'CB', 'CINF', 'C', 'CFG', 'CME', 'CMA', 'DFS', 'ETFC', 'RE', 'FITB', 'BEN', 'GS', 'HIG', 'HBAN', 'ICE', 'IVZ', 'JPM', 'KEY', 'LUK', 'LNC', 'L', 'MTB', 'MMC', 'MET', 'MCO', 'MS', 'NDAQ', 'NAVI', 'NTRS', 'PBCT', 'PNC', 'PFG', 'PGR', 'PRU', 'RJF', 'RF', 'SPGI', 'STT', 'STI', 'SYF', 'TROW', 'ABT', 'ABBV', 'AET', 'A', 'ALXN', 'ALGN', 'AGN', 'ABC', 'AMGN', 'ANTM', 'BCR', 'BAX', 'BDX', 'BIIB', 'BSX', 'BMY', 'CAH', 'CELG', 'CNC', 'CERN', 'CI', 'COO', 'DHR', 'DVA', 'XRAY', 'EW', 'EVHC', 'ESRX', 'GILD', 'HCA', 'HSIC', 'HOLX', 'HUM', 'IDXX', 'ILMN', 'INCY', 'ISRG', 'IQV', 'JNJ', 'LH', 'LLY', 'MCK', 'MDT', 'MRK', 'MTD', 'MYL', 'PDCO', 'PKI', 'PRGO', 'PFE', 'DGX', 'REGN', 'RMD', 'SYK', 'TMO', 'UNH', 'UHS', 'VAR', 'VRTX', 'WAT', 'MMM', 'AYI', 'ALK', 'ALLE', 'AAL', 'AME', 'AOS', 'ARNC', 'BA', 'CHRW', 'CAT', 'CTAS', 'CSX', 'CMI', 'DE', 'DAL', 'DOV', 'ETN', 'EMR', 'EFX', 'EXPD', 'FAST', 'FDX', 'FLS', 'FLR', 'FTV', 'FBHS', 'GD', 'GE', 'GWW', 'HON', 'INFO', 'ITW', 'IR', 'JEC', 'JBHT', 'JCI', 'KSU', 'LLL', 'LMT', 'MAS', 'NLSN', 'NSC', 'NOC', 'PCAR', 'PH', 'PNR', 'PWR', 'RTN', 'RSG', 'RHI', 'ROK', 'COL', 'ROP', 'LUV', 'SRCL', 'TXT', 'TDG', 'UNP', 'UAL', 'AES', 'LNT', 'AEE', 'AEP', 'AWK', 'CNP', 'CMS', 'ED', 'D', 'DTE', 'DUK', 'EIX', 'ETR', 'ES', 'EXC'] # In[ ]: import pandas_datareader.data as web pan = web.DataReader(tickers, "google", datetime.datetime(2015, 1, 1), datetime.datetime(2017, 1, 1)) # In[4]: pan = pan.transpose(2,1,0) # We'll compute a simple mean reversion factor looking at recent stocks performance: stocks that performed well in the last 5 days will have high rank and vice versa. # In[5]: factor = pan.loc[:,:,'Open'] factor = -factor.pct_change(5) factor = factor.stack() factor.index = factor.index.set_names(['date', 'asset']) # The pricing data passed to alphalens should contain the entry price for the assets so it must reflect the next available price after a factor value was observed at a given timestamp. Those prices must not be used in the calculation of the factor values for that time. Always double check to ensure you are not introducing lookahead bias to your study. # # The pricing data must also contain the exit price for the assets, for period 1 the price at the next timestamp will be used, for period 2 the price after 2 timestats will be used and so on. # # There are no restrinctions/assumptions on the time frequencies a factor should be computed at and neither on the specific time a factor should be traded (trading at the open vs trading at the close vs intraday trading), it is only required that factor and price DataFrames are properly aligned given the rules above. # # In our example, before the trading starts every day, we observe yesterday factor values. The price we pass to alphalens is the next available price after that factor observation: the daily open price that will be used as assets entry price. Also, we are not adding additional prices so the assets exit price will be the following days open prices (how many days depends on 'periods' argument). The retuns computed by Alphalens will therefore based on assets open prices. # In[6]: pricing = pan.loc[:,:,'Open'].iloc[1:] # # Prepare data and run Alphalens # Pyfolio wants timezone set to UTC otherwise it refuses to work # In[7]: pricing.index = pricing.index.tz_localize('UTC') # In[8]: factor = factor.unstack() factor.index = factor.index.tz_localize('UTC') factor = factor.stack() # In[9]: factor_data = alphalens.utils.get_clean_factor_and_forward_returns(factor, pricing, periods=(1, 3, 5), quantiles=5, bins=None) # In[10]: alphalens.tears.create_summary_tear_sheet(factor_data) # # Prepare data for Pyfolio # We can see in Alphalens analysis that quantiles 1 and 5 are the most predictive so we'll build a portfolio data using only those quantiles. # In[11]: pf_returns, pf_positions, pf_benchmark = \ alphalens.performance.create_pyfolio_input(factor_data, period='1D', capital=100000, long_short=True, group_neutral=False, equal_weight=True, quantiles=[1,5], groups=None, benchmark_period='1D') # Now that we have prepared the data we can run Pyfolio functions # In[12]: pyfolio.tears.create_full_tear_sheet(pf_returns, positions=pf_positions, benchmark_rets=pf_benchmark) # ## Analyzing subsets of data # Sometimes it might be useful to analyze subets of your factor data, for example it could be interesting to see the comparison of your factor in different days of the week. Below we'll see how to select and analyze factor data corresponding to Mondays, the positions will be held the for a period of 5 days # In[13]: monday_factor_data = factor_data[ factor_data.index.get_level_values('date').weekday == 0 ] # In[14]: pf_returns, pf_positions, pf_benchmark = \ alphalens.performance.create_pyfolio_input(monday_factor_data, period='5D', capital=100000, long_short=True, group_neutral=False, equal_weight=True, quantiles=[1,5], groups=None, benchmark_period='1D') # In[15]: pyfolio.tears.create_full_tear_sheet(pf_returns, positions=pf_positions, benchmark_rets=pf_benchmark)