A previous post showcased a pre-release version of Alphalens
Algorithms rely on predictive factors for success. We call these factors alphas. Alphas express a predictive relationship between some given set of information and future returns. By applying this relationship to multiple stocks we can hope to generate an alpha signal and trade off of it. Alphalens is a Python package for performance analysis of alpha factors which can be used to create factor models and cross-sectional equity algos.
Analyzing your factors in Research first allows you to spend less time writing, running and analyzing backtests with Pyfolio. Consequently, this allows for faster iteration of ideas, and a final algorithm that you can be confident in. Building a rigorous workflow with Alphalens will make your strategies more robust and less prone to overfitting - things we look for when evaluating algorithms.
Alphalens is only one part of the algorithm creation process. The main function of Alphalens is to surface the most relevant statistics and plots about a single alpha factor. This information can tell you if the alpha factor you found is predictive -- whether you have found an "edge." These statistics cover:
- Returns Analysis
- Information Analysis
- Turnover Analysis
- Group Analysis
- Event-style Analysis
Using Alphalens in Quantopian Research is pretty simple:
1. Define your pipeline alpha factors
class Momentum(CustomFactor):
inputs = [USEquityPricing.close]
window_length = 252
def compute(self, today, assets, out, close):
out[:] = close[-20] / close[0]
2. Create and run your pipeline
alpha_pipe = Pipeline(columns={'my_factor': Momentum()})
alphas = run_pipeline(alpha_pipe, start_date=start, end_date=end)
3. Get pricing data
assets = alphas.index.levels[1].unique()
pricing = get_pricing(assets, start, end + pd.Timedelta(days=30), fields="open_price")
4. Run the Alphalens factor tear sheet.
# Ingest and format data
factor_data = alphalens.utils.get_clean_factor_and_forward_returns(alphas['my_factor'], pricing)
# Run analysis
alphalens.tears.create_full_tear_sheet(factor_data)
Get started with the Factor Analysis Lecture
Here are some places to check out too:
- Alphalens Docs for an analysis of a professional alpha factor.
- Alphalens Github repo
- Example notebook from the repo to use anywhere
- Alphalens pre-release post
Thanks to Luca and other community members for your contributions!