Maximum Sharpe Ratio
Calculate the weights of the maximum Sharpe ratio portfolio: the fully invested portfolio with the highest expected return per unit of volatility above the risk free rate. It is the tangency portfolio, where a line from the risk free rate touches the efficient frontier.
The weights maximize (w’μ - r_f) / sqrt(w’Σw) subject to the weights summing to one and lying between the minimum and maximum weight, with μ the annualized expected returns, Σ the annualized covariance matrix and r_f the risk free rate.
For more information, see the following papers:
- Tobin, J. (1958). “Liquidity Preference as Behavior Towards Risk.” The Review of Economic Studies, 25(2), 65-86.
- Sharpe, W.F. (1966). “Mutual Fund Performance.” The Journal of Business, 39(1), 119-138.
Also known as: tangency portfolio, optimal risky portfolio, max Sharpe.
Maximum Sharpe Ratio in Python or via the MCP
Install the Finance Toolkit with:
pip install financetoolkit -U
Then call get_maximum_sharpe_weights as shown below.
from financetoolkit import Toolkit
toolkit = Toolkit(
["AAPL", "MSFT", "JNJ", "XOM"],
api_key="FINANCIAL_MODELING_PREP_KEY",
start_date="2021-01-01",
end_date="2025-12-31",
)
toolkit.portfolio.get_maximum_sharpe_weights(maximum_weight=0.4)
Or, after setting up the Finance Toolkit MCP, ask:
Which weights in Apple, Microsoft, Johnson & Johnson and ExxonMobil, with at most 40% in any one stock, gave the highest Sharpe ratio based on 2021 to 2025?
Which returns:
| Weight | Expected Return | Volatility | Sharpe Ratio | Risk Contribution | |
|---|---|---|---|---|---|
| AAPL | 0.0293 | 0.1933 | 0.2786 | 0.5727 | 0.0287 |
| MSFT | 0.3129 | 0.2017 | 0.2572 | 0.6532 | 0.3207 |
| JNJ | 0.2578 | 0.0984 | 0.1673 | 0.3867 | 0.1137 |
| XOM | 0.4 | 0.291 | 0.2702 | 0.9522 | 0.5369 |
| Portfolio | 1 | 0.2105 | 0.1568 | 1.1275 | 1 |
Parameters
get_maximum_sharpe_weights accepts the following parameters.
| Parameter | Type | Description | Example |
|---|---|---|---|
period | str | The frequency of the returns (daily, weekly, monthly, quarterly or yearly). Defaults to “daily”. | "weekly" |
minimum_weight | float | The lowest weight an asset may have; a negative value allows short positions. Defaults to 0. | 0 |
maximum_weight | float | The highest weight an asset may have. Defaults to 1. | 1 |
include_benchmark | bool | Whether to include the benchmark as an asset. Defaults to False. | True |
rounding | int | None | The number of decimals to round the results to. Defaults to None, which uses the Toolkit’s rounding. | 2 |