Quant’s Guide: Finding Key Metrics & Ratios Using Python | by Posey | Towards Data Science

We’ll use Yahoo Finance for this example. You can use your data source of choice. First, let’s import the libraries and grab the data used in this article...
The Efficient Frontier is a common subject of portfolio theory. It involves finding the portfolio(s) with the highest expected return with fixed risk OR the lowest risk for a fixed expected return.
Let’s walk through the above code piece by piece...
data = f.get(stocks, start='2019-01-01', end='2019-09-01')        mu = expected_returns.mean_historical_return(data)        S = risk_models.sample_cov(data)         ef = EfficientFrontier(mu, S)        raw_weights = ef.max_sharpe()        cleaned_weights = ef.clean_weights()        perf = ef.portfolio_performance(verbose=True)        ef.__dict__
First, we gather our data passing a list of stocks and a timeframe to ffn.
Next, we calculate mu and S. mu is calculated from the mean of historical returns for stocks from the set timeframe. S is the covariance matrix.
Using mu and S we can find the efficient frontier. From here, we can plot risk vs return and begin to curve fit the optimal portfolios.
From there, we can do some fun things like maximize for various ratios like Sharpe, Calmar, Sortino, or whatever we desire and get the weights of individual equities. It will only find the efficient frontier and distribute weights from the list of stocks we pass and get returns from Yahoo Finance.
The stats and metrics the following script explores include:
Returns
CAGR
Max Drawdown
Sharpe Ratio
Calmar Ratio
Keep in mind that calc_stats() gives us a lot more data, but we’re cherrypicking some relevant metrics.
Immediately you see us calculate cumulative returns using ffn. We round and find the mean of the cumulative returns in an effort to clean up our data a bit. From there, I found that the function for calculating the Sharpe Ratio directly doesn’t work for me so I grab it from calc_stats. To do this, I iterate over every stock in my desired list of stocks and add their Sharpe Ratios to a list. I can then take the mean of that list of ratios to find the mean Sharpe Ratio or do whatever else I desire...
Now in the above section, I gather Sharpe, max drawdown, CAGR, and Calmar for each individual equity. In the end, you might append it to a list, JSON to send someplace else, maybe a dataframe. That’s totally up to you!
At Spawner we’re tackling these problems and our API can solve most of the herein discussed problems.
I hope this made it clear to you that some powerful libraries are out there to be explored. I’ve received a lot of emails asking for resources. Here’s a list of resources directly mentioned or tangential to this article:
ffn
pyportfolioopt
Quantopian
IEX Cloud
pynance
Zipline
TA-Lib
bt
pyfolio
Github link
Let’s continue the convo on Twitter.
To support my writing and get full access to all articles on Medium, visit https://posey.medium.com/membership
Note from Towards Data Science’s editors: While we allow independent authors to publish articles in accordance with our rules and guidelines, we do not endorse each author’s contribution. You should not rely on an author’s works without seeking professional advice. See our Reader Terms for details.