This project demonstrates two quantitative investing strategies: a momentum strategy and a value strategy. Each strategy involves selecting 50 stocks from the S&P 500 based on specific criteria and calculating recommended trades for an equal-weight portfolio of these stocks, as well as more advanced strategies.
- Introduction
- Library Imports
- Importing Stock Data
- Making API Calls
- Quantitative Momentum Strategy
- Quantitative Value Strategy
- Calculating Shares to Buy
- Saving to Excel
- Calculating Returns
- Investment Strategy using 80-20 Principle
- Visualizing Returns
- Acknowledgements
This project builds two separate investing strategies:
- Momentum Investing: Investing in stocks that have increased in price the most.
- Value Investing: Investing in stocks that are cheapest relative to common measures of business value (like earnings or assets).
Each strategy selects the 50 best stocks based on their criteria and calculates recommended trades for an equal-weight portfolio of these stocks, as well as more advanced strategies.
The project utilizes the following libraries:
numpy
pandas
requests
math
scipy.stats
xlsxwriter
We import a list of S&P 500 stocks from a CSV file.
The project uses the IEX Cloud API to fetch the necessary data for the two strategies.
The top 50 stocks by one-year price return are selected and sorted.
The strategy is refined by considering high-quality momentum, selecting stocks from the highest percentiles of:
- 1-month price returns
- 3-month price returns
- 6-month price returns
- 1-year price returns
Each stock is assigned a percentile rank for each timeframe, and the average of these percentiles (HQM score) determines the top momentum stocks.
The top 50 stocks by combined value metrics are selected and sorted. These metrics include:
- Price-to-earnings ratio (P/E ratio)
- Price-to-book ratio (P/B ratio)
- Price-to-sales ratio (P/S ratio)
- Enterprise value divided by EBITDA (EV/EBITDA)
- Enterprise value divided by gross profit (EV/GP)
The strategy is refined by considering multiple value metrics, selecting stocks from the lowest percentiles of:
- Price-to-earnings ratio
- Price-to-book ratio
- Price-to-sales ratio
- Enterprise value divided by EBITDA (EV/EBITDA)
- Enterprise value divided by gross profit (EV/GP)
Each stock is assigned a percentile rank for each metric, and the average of these percentiles (RV score) determines the top value stocks.
A function calculates the number of shares to buy for each selected stock based on the given portfolio size.
The results are saved to an Excel file using xlsxwriter
, with formatted columns for better readability.
The returns for the selected stocks are calculated over different timeframes, using the following steps:
- Fetch Historical Price Data: Retrieve the historical prices for each stock.
- Calculate Returns: Compute the returns for each selected stock over the specified timeframes (e.g., 1 month, 3 months, 6 months, and 1 year).
- Aggregate Returns: Aggregate the returns for the entire portfolio.
An alternative investment strategy based on the 80-20 principle is implemented for the two strategies. In this strategy, 80% of the portfolio size is allocated to the top 20% of stocks based on the HQM or RV Score, and the remaining 20% of the portfolio size is allocated to the rest of the stocks.
The code iterates through the stocks in hqm_dataframe
or rv_dataframe
and calculates returns for each timeframe (one month, three months, six months, and one year) using the 80-20 allocation principle. For the top 20% of stocks, 80% of the portfolio size is allocated, and for the remaining 80% of stocks, 20% of the portfolio size is allocated.
Ensure to adjust the portfolio_size
variable as per your portfolio setup before running the code.
The project includes a section to visualize and compare the returns of two different investment strategies:
- Equal Weighting: Allocating an equal amount of capital to all selected stocks.
- Unequal Weighting (80-20 Principle): Allocating 80% of the portfolio to the top 20% of stocks based on the HQM or RV Score, and 20% to the remaining 80% of stocks.
To visualize the returns, the project uses matplotlib
to generate a bar graph showing the returns for different timeframes (1 month, 3 months, 6 months, and 1 year).
The steps to generate the visualization include:
- Calculate Returns: Compute the returns for each selected stock over the specified timeframes.
- Aggregate Returns: Aggregate the returns for the entire portfolio under the two strategies.
- Plot the Graph: Use
matplotlib
to create a bar graph comparing the returns of the two strategies.
By visualizing the returns, you can easily compare the performance of the equal-weight and 80-20 principle strategies across different timeframes.
The following graph shows the comparison of returns over different timeframes using the 80-20 investment strategy:
- Returns Comparison for Quant Momentum Strategy
- Returns Comparison for Quant Value Strategy
This project was developed using the following resources and libraries:
- IEX Cloud API
- pandas library
- numpy library
- scipy library
- xlsxwriter library
- matplotlib library
- Special thanks to the creators of the open-source libraries used in this project.