{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "d226535c",
   "metadata": {},
   "source": [
    "<div style=\"background-color:#000;\"><img src=\"pqn.png\"></img></div>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bbe7cc61",
   "metadata": {},
   "source": [
    "This code downloads historical stock data for a specified list of symbols using the yfinance library. It computes daily returns for each stock and then aggregates these returns to form a portfolio. The Calmar Ratio, a performance metric used to evaluate the risk-adjusted returns of the portfolio, is calculated using the empyrical library. Finally, it prints the Calmar Ratio and plots the portfolio's daily returns over the specified period."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "119bd5ed",
   "metadata": {},
   "outputs": [],
   "source": [
    "import empyrical as ep\n",
    "import yfinance as yf\n",
    "import warnings"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "d9553761",
   "metadata": {},
   "outputs": [],
   "source": [
    "warnings.filterwarnings(\"ignore\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "070d56f0",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Define a list of stock symbols to download historical data for\n",
    "symbols = [\n",
    "    \"AAPL\",\n",
    "    \"AMZN\",\n",
    "    \"GOOG\",\n",
    "    \"META\",\n",
    "    \"MSFT\",\n",
    "    \"NVDA\",\n",
    "    \"TSLA\",\n",
    "]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "c536d848",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "[*********************100%%**********************]  7 of 7 completed\n"
     ]
    }
   ],
   "source": [
    "# Download adjusted closing prices for the specified symbols from Yahoo Finance\n",
    "data = yf.download(symbols, start=\"2020-01-01\")[\"Adj Close\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "046c2ec7",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Calculate daily percentage returns for each stock and remove any missing values\n",
    "returns = data.pct_change().dropna()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "5aed80b2",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Aggregate the daily returns of all stocks to form a portfolio return series\n",
    "portfolio_returns = returns.sum(axis=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "874ff444",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Calmar Ratio: -0.3244445113808806\n"
     ]
    }
   ],
   "source": [
    "# Compute the Calmar Ratio for the portfolio using the empyrical library\n",
    "calmar_ratio = ep.calmar_ratio(portfolio_returns)\n",
    "print(f\"Calmar Ratio: {calmar_ratio}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "66c8e68a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Axes: xlabel='Date'>"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 48040 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# Plot the portfolio's daily returns over the specified period\n",
    "portfolio_returns.plot()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "79e49a51",
   "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
}
