{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "0c0b824d",
   "metadata": {},
   "source": [
    "<div style=\"background-color:#000;\"><img src=\"pqn.png\"></img></div>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "14722e46",
   "metadata": {},
   "source": [
    "This notebook calculates the Treynor ratio for a given stock and its benchmark index over time. It fetches historical price data for the specified stock and benchmark, computes daily returns, and calculates rolling beta values. The Treynor ratio is then derived using these beta values. This ratio helps in assessing the risk-adjusted performance of the asset relative to the benchmark, aiding in investment decision-making."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "64fbd380",
   "metadata": {},
   "outputs": [],
   "source": [
    "from openbb_terminal.sdk import openbb\n",
    "from openbb_terminal.sdk import TerminalStyle"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f263a055",
   "metadata": {},
   "outputs": [],
   "source": [
    "theme = TerminalStyle(\"light\", \"light\", \"light\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6bd464bd",
   "metadata": {},
   "source": [
    "Load historical stock data for JPM and SPY over the specified date range"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4adcbef5",
   "metadata": {},
   "outputs": [],
   "source": [
    "data = openbb.stocks.load(\n",
    "    \"JPM, SPY\",\n",
    "    start_date=\"2014-01-01\",\n",
    "    end_date=\"2022-12-31\"\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "be35b95f",
   "metadata": {},
   "source": [
    "Extract adjusted closing prices for JPM and SPY from the loaded data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "84cbd927",
   "metadata": {},
   "outputs": [],
   "source": [
    "asset = data[\"Adj Close\"].JPM\n",
    "benchmark = data[\"Adj Close\"].SPY"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7325eefa",
   "metadata": {},
   "source": [
    "Calculate daily returns for JPM and SPY, dropping any missing values"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "201dc273",
   "metadata": {},
   "outputs": [],
   "source": [
    "asset_returns = asset.pct_change().dropna()\n",
    "benchmark_returns = benchmark.pct_change().dropna()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c5b1530c",
   "metadata": {},
   "source": [
    "Calculate the rolling variance of benchmark returns over a 30-day window"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "eb1cbc10",
   "metadata": {},
   "outputs": [],
   "source": [
    "bm_var = benchmark_returns.rolling(\n",
    "    window=30\n",
    ").var()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "af91c7ce",
   "metadata": {},
   "source": [
    "Calculate the rolling covariance between asset returns and benchmark returns over a 30-day window"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ebcee8bb",
   "metadata": {},
   "outputs": [],
   "source": [
    "bm_cov = benchmark_returns.rolling(\n",
    "    window=30\n",
    ").cov(asset_returns)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "44a33731",
   "metadata": {},
   "source": [
    "Compute the rolling beta values by dividing the rolling covariance by the rolling variance"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0d100612",
   "metadata": {},
   "outputs": [],
   "source": [
    "beta = bm_cov / bm_var"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fb8f6eee",
   "metadata": {},
   "source": [
    "Calculate the Treynor ratio by adjusting the returns of the asset against its beta"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "40d560cd",
   "metadata": {},
   "outputs": [],
   "source": [
    "treynor = (\n",
    "    asset_returns - benchmark_returns\n",
    ") / beta"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7e050c57",
   "metadata": {},
   "source": [
    "Display the calculated Treynor ratio"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a927dab4",
   "metadata": {},
   "outputs": [],
   "source": [
    "treynor"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cfc7731a",
   "metadata": {},
   "source": [
    "Plot the Treynor ratio over time to visualize its trend"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "863ccecf",
   "metadata": {},
   "outputs": [],
   "source": [
    "treynor.plot()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6f0482e8",
   "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"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
