Picking Stocks with a Quantitative Momentum Strategy in Python
A simple yet useful method to optimize the process of choosing stocks
Disclaimer: This article is strictly for educational purposes and should not be taken as an investment tip.
While I was an amateur trader, the process of choosing the right stocks to trade was a nightmare. News on stocks, uncertainty, and emotions adds to the bitterness of this process. A long way ahead, today, I found my own solution using my best companion Python. In this article, we are going to build a simple quantitative momentum strategy in python that filters and picks out the best intraday stocks. But wait, what is a quantitative momentum strategy?
A Quantitative Momentum strategy is a strategy implemented to choose stocks that have increased in price the most. Simply speaking, it is the process of identifying stocks with a great uptrend. Now that we have built some understanding of what quantitative momentum strategy is and how it can be used to pick stocks. Let’s implement the strategy in python!
The steps involved in this article are:
- Importing the required packages- Extracting the list of all S&P 500 stock's symbols- Pulling Intraday data of all the stocks in the S&P 500- Calculating percentage change and momentum of all stocks- Finding stocks with greater momentum- Backtesting with a equal-weight portfolio
Step-1: Importing Packages
The primary packages of this article are the Pandas package to deal with data, the NumPy package to work with arrays, and the Requests package to pull data using an API. The secondary packages are going to be the Math package for mathematical functions, the SciPy package for complex functions, and the Statistics package for statistical functions.
Now that we have imported all the required packages into our python environment. Let’s proceed in extracting the list of all S&P 500 stock’s symbols.
Step-2: Extracting the List of all S&P 500 Stock’s Symbols
I’ve seen tutorials building complex web scraping functions to make this particular task get done. But, that’s not the way we are going to follow and our code is going to be much simpler and efficient.
Code Explanation: As I said before, we are not going to complicate our code or use any web scraping techniques to extract the symbols of the stocks in the S&P 500 but, we used a highly efficient function named ‘read_html’ provided by the Pandas package. This function will search for tables in the given URL and will return the data in a list format. Coming back to our code, we first extracted the symbols of the stocks in the S&P 500 using the ‘read_html’ function and stored it into the ‘sp500’ variable. Then we defined a variable ‘sp500_list’ to store the extracted data into a NumPy array. Now that we have collected all the S&P 500 stock’s symbols. Let’s pull some intraday data!
Step-3: Pulling Intraday Data using IEX Cloud API
In this step, we are going to pull the intraday data of all the S&P 500 stocks using IEX Cloud API. If you don’t know how to pull stock data using IEX Cloud API, I suggest you read my article on it here.
Code Explanation: This piece of code can be classified into three parts: Defining a function to pull intraday data, pulling intraday data for all the stocks in the S&P 500 using the defined function, saving and importing the extracted data.
The first part comprises the code from lines 1 to 36. In the first part, we are first defining a function named ‘get_intraday_prices’ which takes a stock’s symbol as the parameter. Inside the function, we are first defining two variables to store the API key and the URL. Using the ‘get’ function provided by the Requests package, we are extracting the intraday data in a JSON format. After doing some data processing and manipulations, we are returning the data in the form of a Pandas dataframe.
The second part comprises the code from lines 38 to 44. In this part, we are iterating over the symbols of the stocks to pull intraday data of each stock. We are storing the intraday data of each stock into the ‘df’ variable.
The third part comprises the code from lines 46 to 48. The main goal of this part is to save and import the extracted intraday data of all stocks. This step is optional but highly recommended as you can save time from running the iteration process when you're reopening the script. We are first saving the extracted intraday data in the name of ‘sp500’ using the ‘to_csv’ function provided by the Pandas package. Then, with the help of the ‘read_csv’ function, we are importing the saved dataframe.
Step-4: Calculating Percentage Change and Momentum
In this step, we are going to calculate the day change and the momentum of each stock.
Code Explanation: First, we are creating an empty list named ‘pc’ to store the percentage change of each stock. Utilizing a for-loop, we are iterating over the symbols of all the stocks in the S&P 500 and appended the calculated percentage change using the ‘pct_change’ function provided by the Pandas package into the ‘dc’ list. We are then creating a dataframe named ‘sp500_momentum’ to store the symbol of the stock, percentage change, and the momentum which we are going to calculate.
Coming to the code to calculate the momentum, we are first creating a column named ‘momentum’ in the ‘sp500_momentum’ dataframe and filled it with null values. Then we are passing on a for-loop to fill the null values with the actual momentum values.
Step-5: Finding Stocks having Greater Momentum
In this step, we are going to sort the stocks based on the momentum value we calculated and find the top 10 stocks having the highest momentum.
Code Explanation: I always follow the principle of keeping the code simple and efficient. I did it here too. We are first creating a dataframe named ‘top_picks’ which stores the top 10 stocks having greater momentum than the rest. In order to sort and find the top 10 stocks, we used the ‘nlargest’ function provided by the Pandas package.
In this step, we are going to backtest by investing an equal amount of money in the top 10 stocks with the highest momentum, and let’s see the results.
Code Explanation: Firstly, we are defining two variables to store the total investment value, and investment value per stock respectively. Next, we are passing a for-loop to find the day’s close price of all the top 10 stocks and appended those values into the ‘day_close’ variable. In further lines of code, we created a dataframe named ‘backtest_df’ which comprises the calculated number of stocks to buy in each of them, the returns we got by investing in those top 10 stocks, and finally the return percentage of our investment. As you can see, we got some nice returns. That’s it!
In this article, we learned how to implement a simple quantitative momentum strategy to pick tradable stocks. You can improve this article in two ways:
- Picking stocks for long-term investment: In this article, we picked stocks for intraday trading but, you can also implement the strategy to pick long-term investable stocks. By doing this, you will learn how to tune a quantitative momentum strategy accordingly and to use several metrics to build a strategy.
- More stocks: This whole article is based only on the stocks listed in the S&P 500 market index but, you can consider stocks listed on many more exchanges. The advantage of performing this task is you will be able to perform research on a large scale given the amount of data being pulled and you can master the art of extracting data using stocks APIs along with data processing as this task has an extensive amount of it.
That’s it! You have reached the end of this article. Also, if you forgot to follow any of the coding parts, don’t worry! I’ve provided the full source code at the end. Hope you found something useful in this article. If you’re having any suggestions to improve this article, kindly leave a comment.