Original author is Magnus Erik Hvass Pedersen
This is a collection of research papers on long-term investing, portfolio optimization, etc. They are written as Python Notebooks so they can easily be modified and run again.
The InvestOps Python package contains the main results and algorithms from this research, making it very easy to use in your own Python projects.
The following Python Notebooks produce the plots and statistics for some of my "normal" research papers which can be downloaded from SSRN and GitHub.
-
Long-Term Stock Forecasting (PDF) (Video) (Notebook) (Google Colab)
-
Simple Portfolio Optimization That Works! (PDF) (Video) (Notebook) (Google Colab)
-
Fast Portfolio Diversification (PDF) (Video) (Notebook) (Google Colab)
-
Portfolio Group Constraints (PDF) (Notebook) (Google Colab)
-
Does Volatility Harvesting Really Work? (PDF) (Video) (Notebook) (Google Colab)
The following Python Notebooks contain stand-alone research without "normal" papers.
- Basic Long-Term Stock Forecasting (Notebook) (Google Colab)
1-B. Better Long-Term Stock Forecasts (Notebook) (Google Colab)
1-C. Theory of Long-Term Stock Forecasting (Notebook) (Google Colab)
1-D. Testing the Stock Forecasting Model (Notebook) (Google Colab)
1-E. Forecasting U.S. Stock Indices (Notebook) (Google Colab)
1-F. Forecasting International Stock Indices (Notebook) (Google Colab)
1-G. Forecasting House Price Index (Notebook) (Google Colab)
-
Comparing Stock Indices (Notebook) (Google Colab)
-
Portfolio Optimization Using Signals (Notebook) (Google Colab)
-
Multi-Objective Portfolio Optimization (Notebook) (Google Colab)
-
Forecasting the P/Sales Ratio (Notebook) (Google Colab)
-
Forecasting Sales Growth (Notebook) (Google Colab)
-
Forecasting Dividends (Notebook) (Google Colab)
There is a YouTube video for each research paper.
The Python Notebooks use source-code located in different files to allow for easy re-use across multiple Notebooks. It is therefore recommended that you download the whole repository from GitHub, instead of just downloading the individual Python Notebooks.
The easiest way to download and install this is by using git from the command-line:
git clone https://github.com/Hvass-Labs/FinanceOps.git
This creates the directory FinanceOps
and downloads all the files to it.
This also makes it easy to update the files, simply by executing this command inside that directory:
git pull
You can also download the contents of the GitHub repository as a Zip-file and extract it manually.
If you want to run these tutorials on your own computer, then it is best to use a virtual environment when installing the required packages, so you can easily delete the environment again.
The following command creates a virtual environment named financeops
:
virtualenv financeops
Or you can use Anaconda instead of a virtualenv:
conda create --name financeops python=3
Then you switch to the virtual environment and install the required packages:
source activate financeops
pip install -r requirements.txt
When you are done working on the project you can deactivate the virtualenv:
source deactivate
Once you have installed the required Python packages in a virtual environment,
you run the following commands from the FinanceOps
directory to view, edit
and run the Notebooks:
source activate financeops
jupyter notebook
If you want to edit the other source-code then you may use the free version of PyCharm.
If you do not want to install anything on your own computer, then the Notebooks can be viewed, edited and run entirely on the internet by using Google Colab.
You click the "Google Colab"-link next to the research papers listed above. You can view the Notebook on Colab but in order to run it you need to login using your Google account.
Then you need to execute the following commands at the top of the Notebook, which clones FinanceOps to your work-directory on Colab, and installs all the required Python packages:
# Clone the repository from GitHub to Google Colab's temporary drive.
import os
work_dir = "/content/FinanceOps/"
if not os.path.exists(work_dir):
!git clone https://github.com/Hvass-Labs/FinanceOps.git
os.chdir(work_dir)
# Install the required Python packages.
!pip install -r requirements.txt
Note that you will need to run this every time you login to Google Colab.
Because many of these Notebooks and some of the Python packages also read/write data on the local disk, you would need to create so-called Docker volumes to enable persistent data-storage on your local disk. The instructions for setting this up would be complicated, and it seems much easier to run the Notebooks using one of the other methods above.
- Recent share-price and fundamental data from SimFin.
- Older share-price data from Yahoo Finance.
- Intraday share-price data from Alpha Vantage
- Financial data for some individual stocks collected manually by the author from the 10-K Forms filed with the U.S. SEC.
- Newer S&P 500 data from the S&P Earnings & Estimates Report and older data from the research staff at S&P and Compustat (some older data is approximated by their research staff).
- Financial data for Exchange Traded Funds (ETF) from Morningstar Direct.
- U.S. Government Bond yield for 1-year constant maturity. From the U.S. Federal Reserve.
- The inflation index is: All Items Consumer Price Index for All Urban Consumers (CPI-U), U.S. City Average. Data from the US Department of Labor, Bureau of Labor Statistics.
These Python Notebooks and source-code are published under the MIT License which allows very broad use for both academic and commercial purposes.
You are very welcome to modify and use the source-code in your own project. Please keep a link to the original repository.
The financial data is not covered by the MIT license and may have limitations on commercial redistribution, etc.