- install anaconda
- create & activate a virtual environment
- install quantstats lib:
pip install quantstats --upgrade --no-cache-dir
- open this repo with your favourite python IDE, I use pycharm
- in your python IDE, set up the project interpreter as your created virtual environment, which is where you installed quantstats
- open main.py, go to the main function
- set three global variables:
risk_free_return
,year
,quarter
risk_free_return
: 3-month treasury bill yield. It's updated daily. To get the latest yield, click hereyear
: how many years back do you want to get the data. max is 10quarter
: a boolean value to toggle quarter or annual report for company fundamentals
- adjust the
number
variable below the global variables to run what you need. - You will need to read on to know what each number indicates.
- analyze a company's fundamentals
- set
number = 1.1
, fill in the function with a company stock ticker - run
main.py
- it will print historical financials including balance sheet, income statements, & cash flow statements
- the console also shows the following analysis:
- profitability:
- Gross Margin
- Cost Margin
- Net Profit Margin
- Expenses Margin
- ROE
- ROA
- operating:
- Receivables Turnover Days
- Inventories Turnover Days
- Total Assets Turnover Days
- solvency:
- Liability/Asset Ratio
- Current Ratio
- Acid-test Ratio
- growth:
- Revenue Growth
- Net Income Growth
- Operating Income Growth
- Free Cash Flow Growth
- investing:
- wacc
- roic
- excess return
- economic profit
- stockholders equity growth
- dividendYield
- dividendPayoutRatio
- Number of Shares
- Market Capitalization
- Enterprise Value
- profitability:
- set
- set
number = 1.2
- either prepare a .txt file containing company tickers at each line, or specify the company tickers in
ticker_list
variable - run
main.py
- it will produce a table comparing different companies profitability, operating capabilities, solvency and investment value
- the comparison provides both mean and latest values
- below picture shows an example
- set
number = 3.1
- fill in tickers of interested in
asset_tickers
variable - run main.py
- it will generate an image describing the pair-wise assets correlations
- below picture shows an example
-
My slides on Modern Portfolio Theory: click me
- this slide introduces in great details and a friendly way to help understand & apply MPT optimization
-
- when optimizing risk, the optimizer will find the optimal weights for the assets to minimize the portfolio risk
- set
number = 3.2
- set
asset_tickers
to be a list of tickers of interest - run main.py
- in the console, optimal weights for each assets will be provided
- the efficient frontier will also be plotted (will only plot efficient frontier for less than 4 assets)
- below pictures show an example
-
- when optimizing sharpe ratio, the optimizer will find the optimal weights for assets to maximize the portfolio sharpe ratio
- set
number = 3.3
- set
asset_tickers
to be a list of tickers of interest - run main.py
- in the console, optimal weights for each assets will be provided
- It will also provide the risk return of portfolios in different ratios of risk-free asset and risky assets
- the efficient frontier, and the capital market line will be plotted
- below pictures show an example
- why do evaluation?
- modern portfolio theory did change how people invest, by focusing on diversification, risk and return, etc
- however, modern portfolio theory does has its assumptions (which is too ideal), and limitations due to its assumptions
- since we used MPT to optimize our portfolio, we need to understand how well MPT works on the selected assets
- that's the need to do the evaluation
- I suggest you to do evaluation for every assets combo you would like to invest
- evaluation methods explained
- or a year yi (i >= 2) in a series of years: [y1, y2, y3, …, yn]
- get prediction:
- use history data [y1, y2, …, yi-1] to find the optimal weights
- apply these optimal weights to the data in year yi, so as to get predicted risk, return, & sharpe
- get ground truth:
- use only data in year yi to find the optimal weights in that year
- apply these weights to the data in year yi, so as to get the best-possible portfolio in that year
- we compare prediction and ground truth
- how to run evaluation
- set
number = 3.5
- fill in
asset_ticers
with ticker strings of interest - run main.py
- this will generate 4 pictures
- risk optimization
- a figure shows to provide evidence on how well the MPT tracks risk-optimized ground truth.
- another figure plots the return of the portfolio if using MPT risk-optimized weights, and ground truth weights
- sharpe ration optimization
- a figure shows to provide evidence on how well the MPT tracks sharpe ratio optimized ground truth.
- another figure plots the return of the portfolio if using MPT sharpe ratio optimized weights, and ground truth weights
- risk optimization
- set
- what to observe from the 4 pictures:
- if optimize portfolio by minimizing the risk, the predicted portfolio risk should be higher than ground truth
- if optimize portfolio by maximizing sharpe ratio, predicted portfolio sharpe should be lower than ground truth
- the closer the prediction tracks the ground truth, the more evidently that MPT works in terms of predicting the best portfolio
- below pictures show an example
Enjoy! :-)
RY