In this post, we will demonstrate how to create a simple pipeline that uses Linear Regression to identify stock momentum, and filters stocks with the strongest momentum indicator. Then, analyzes going long and short on stocks from this signal. We will use Alphalens to analyze the quality of the Factor, then we will use pyfolio to analyze the returns from this factor. Eventually, we will create a simple zipline-trader algorithm that trades based on that signal and backtest it.
All the data used in this post is from the Alpaca data API which could be obtained with a free account.
Disclaimer: This is not a profitable strategy that you could deploy to live markets. It's written as an instructional post, showing the strengths of this framework and what you could do with it.
Now, let's get things started :)
Loading the Data Bundle
We use the Alpaca data service to create a data bundle that we feed into the zipline-trader's engine. For a detailed explanation on how to connect the data to zipline-trader go to the zipline-trader docs: https://zipline-trader.readthedocs.io/en/latest/
Imports and Definitions:
Calculating the Linear Regression Factor
This is a factor that runs a linear regression over one year of stock log returns and calculates a "slope" as our factor. It is based on the Alphalens example library.
Creating the Pipeline
Let's create a pipeline that:
Starts from our entire universe (S&P 500)
Calculates AverageDollarVolume for the past 30 days, and selects the top 20 stocks.
Calculate MyFactor for the 20 stocks selected in the previous step.
Plot the pipeline
We can plot our pipeline to get a visual sense of what the process does
Now we want to check if our factor has the potential for alpha generation. We will use Alphalens.
Data preparation
Alphalens input consists of two types of information: the factor values for the time period under analysis and the historical assets prices (or returns). Alphalens doesn't need to know how the factor was computed, the historical factor values are enough. This is interesting because we can use the tool to evaluate factors for which we have the data but not the implementation details.
Alphalens requires that factor and price data follow a specific format and it provides a utility function, get_clean_factor_and_forward_returns, that accepts factor data, price data, and optionally group information (for example the sector groups, useful to perform sector specific analysis) and returns the data suitably formatted for Alphalens.
Running Alphalens
Once the factor data is ready, running Alphalens analysis is pretty simple and it consists of one function call that generates the factor report (statistical information and plots). Please remember that it is possible to use the help python built-in function to view the details of a function.
Let's now create a simple algorithm that wraps our pipeline and backtest it against our data bundle as if we run it in a live market. This is more realistic than what we just did with pyfolio since we do not run a factor in live trading. There are a lot of moving parts and we need to wrap it in a logic that works under the market conditions.
All and all we got pretty good results with positive returns. We did better than our benchmark (SPY) for the year 2020. So it could be a basis for creating something more robust.
What next?
We had significant drawdowns, one could minimize these.
One can backtest during a much longer period.
One can optimize the pipeline. The above example is just a simple setup, definitely not the optimized setup so different parameters would create different results.
One could implement this in paper trading. While backtests are good, but it is important to see what happens in real time.
The wrapping algorithm is extremely simplified, much more work could be done there.
One could make the algorithm sector neutral, or maybe even find better responding sectors.
Technology and services are offered by AlpacaDB, Inc. Brokerage services are provided by Alpaca Securities LLC (alpaca.markets), member FINRA/SIPC. Alpaca Securities LLC is a wholly-owned subsidiary of AlpacaDB, Inc.