247 lines
6.1 KiB
Plaintext
247 lines
6.1 KiB
Plaintext
{
|
|
"cells": [
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "1ed30b1e",
|
|
"metadata": {},
|
|
"source": [
|
|
"<div style=\"background-color:#000;\"><img src=\"pqn.png\"></img></div>"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "550cc5f3",
|
|
"metadata": {},
|
|
"source": [
|
|
"This code downloads historical price data for SPY and AAPL from Yahoo Finance, calculates daily returns, and computes the Sharpe ratio for these returns. It includes a function to determine the Sharpe ratio, adjusting for a daily benchmark return. The code then plots the rolling 30-day Sharpe ratio for AAPL and visualizes the histogram of these Sharpe ratios. Additionally, it compares the rolling 30-day Sharpe ratio of AAPL against SPY and plots the histogram of the differences."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "dcabe4c7",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"import yfinance as yf\n",
|
|
"import numpy as np"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "46f6a31c",
|
|
"metadata": {},
|
|
"source": [
|
|
"Download historical price data for SPY and AAPL from Yahoo Finance"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "2a83f550",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"data = yf.download([\"SPY\", \"AAPL\"], start=\"2020-01-01\", end=\"2022-07-31\")"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "2018e200",
|
|
"metadata": {},
|
|
"source": [
|
|
"Extract adjusted closing prices for SPY and AAPL"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "c977a8bc",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"closes = data['Adj Close']\n",
|
|
"spy_returns = closes.SPY.pct_change().dropna()\n",
|
|
"aapl_returns = closes.AAPL.pct_change().dropna()"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "9741f742",
|
|
"metadata": {},
|
|
"source": [
|
|
"Define a function to calculate the Sharpe ratio of a strategy"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "f618f290",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"def sharpe_ratio(returns, adjustment_factor=0.0):\n",
|
|
" \"\"\"\n",
|
|
" Determines the Sharpe ratio of a strategy.\n",
|
|
" \n",
|
|
" Parameters\n",
|
|
" ----------\n",
|
|
" returns : pd.Series or np.ndarray\n",
|
|
" Daily returns of the strategy, noncumulative.\n",
|
|
" adjustment_factor : int, float\n",
|
|
" Constant daily benchmark return throughout the period.\n",
|
|
"\n",
|
|
" Returns\n",
|
|
" -------\n",
|
|
" sharpe_ratio : float\n",
|
|
"\n",
|
|
" Note\n",
|
|
" -----\n",
|
|
" See https://en.wikipedia.org/wiki/Sharpe_ratio for more details.\n",
|
|
" \"\"\"\n",
|
|
"\n",
|
|
" # Adjust returns by subtracting the benchmark return\n",
|
|
"\n",
|
|
" returns_risk_adj = returns - adjustment_factor\n",
|
|
"\n",
|
|
" # Print the annualized standard deviation of the risk-adjusted returns\n",
|
|
"\n",
|
|
" print(returns_risk_adj.std() * np.sqrt(252))\n",
|
|
"\n",
|
|
" # Return the annualized Sharpe ratio\n",
|
|
"\n",
|
|
" return (\n",
|
|
" returns_risk_adj.mean() / returns_risk_adj.std()\n",
|
|
" ) * np.sqrt(252)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "035cea96",
|
|
"metadata": {},
|
|
"source": [
|
|
"Calculate the Sharpe ratio for SPY daily returns"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "23dfb39c",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"sharpe_ratio(spy_returns)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "19aebd83",
|
|
"metadata": {},
|
|
"source": [
|
|
"Calculate the Sharpe ratio for AAPL daily returns"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "12292244",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"sharpe_ratio(aapl_returns)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "77252a24",
|
|
"metadata": {},
|
|
"source": [
|
|
"Plot the rolling 30-day Sharpe ratio for AAPL"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "75215b9c",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"aapl_returns.rolling(30).apply(sharpe_ratio).plot()"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "5ac119f9",
|
|
"metadata": {},
|
|
"source": [
|
|
"Plot the histogram of the rolling 30-day Sharpe ratios for AAPL"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "bae6269a",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"aapl_returns.rolling(30).apply(sharpe_ratio).hist(bins=50)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "3b70d6ff",
|
|
"metadata": {},
|
|
"source": [
|
|
"Compare the rolling 30-day Sharpe ratio of AAPL against SPY and plot the histogram of the differences"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "0796ca2e",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"(\n",
|
|
" aapl_returns.rolling(30).apply(sharpe_ratio)\n",
|
|
" - spy_returns.rolling(30).apply(sharpe_ratio)\n",
|
|
").hist(bins=50)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "cef5bf53",
|
|
"metadata": {},
|
|
"source": [
|
|
"<a href=\"https://pyquantnews.com/\">PyQuant News</a> is where finance practitioners level up with Python for quant finance, algorithmic trading, and market data analysis. Looking to get started? Check out the fastest growing, top-selling course to <a href=\"https://gettingstartedwithpythonforquantfinance.com/\">get started with Python for quant finance</a>. For educational purposes. Not investment advise. Use at your own risk."
|
|
]
|
|
}
|
|
],
|
|
"metadata": {
|
|
"jupytext": {
|
|
"cell_metadata_filter": "-all",
|
|
"main_language": "python",
|
|
"notebook_metadata_filter": "-all"
|
|
},
|
|
"kernelspec": {
|
|
"display_name": "Python 3 (ipykernel)",
|
|
"language": "python",
|
|
"name": "python3"
|
|
},
|
|
"language_info": {
|
|
"codemirror_mode": {
|
|
"name": "ipython",
|
|
"version": 3
|
|
},
|
|
"file_extension": ".py",
|
|
"mimetype": "text/x-python",
|
|
"name": "python",
|
|
"nbconvert_exporter": "python",
|
|
"pygments_lexer": "ipython3",
|
|
"version": "3.10.13"
|
|
}
|
|
},
|
|
"nbformat": 4,
|
|
"nbformat_minor": 5
|
|
}
|