{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "f0b34223",
   "metadata": {},
   "source": [
    "<div style=\"background-color:#000;\"><img src=\"pqn.png\"></img></div>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "faa221b8",
   "metadata": {},
   "source": [
    "This code retrieves stock and options data for the ETF \"SPY\" and futures data for the symbol \"ES\". It uses the OpenBB Terminal SDK to load the data, including historical prices and options chains. The retrieved data is then stored in HDF5 files for easy access and persistence. This is useful for financial analysis and modeling, enabling efficient data storage and manipulation. The code concludes by reading the stored data and printing it for verification."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1e4509d9",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "from openbb_terminal.sdk import openbb"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0d2e2e11",
   "metadata": {},
   "outputs": [],
   "source": [
    "STOCKS_DATA_STORE = \"stocks.h5\"\n",
    "FUTURES_DATA_STORE = \"futures.h5\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2fe2335b",
   "metadata": {},
   "outputs": [],
   "source": [
    "ticker = \"SPY\"\n",
    "root = \"ES\""
   ]
  },
  {
   "cell_type": "markdown",
   "id": "24c85387",
   "metadata": {},
   "source": [
    "Retrieve stock price data for the ticker \"SPY\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b650ad85",
   "metadata": {},
   "outputs": [],
   "source": [
    "spy_equity = openbb.stocks.load(ticker)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8e1952ce",
   "metadata": {},
   "source": [
    "Retrieve options expiration dates for the ticker \"SPY\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3f1867fa",
   "metadata": {},
   "outputs": [],
   "source": [
    "spy_expirations = openbb.stocks.options.expirations(ticker)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "473c96d9",
   "metadata": {},
   "source": [
    "Retrieve historical options data for a specific expiration date"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "88dd5dff",
   "metadata": {},
   "outputs": [],
   "source": [
    "spy_historic = openbb.stocks.options.hist(\n",
    "    ticker, \n",
    "    spy_expirations[1], \n",
    "    440\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a15b5e11",
   "metadata": {},
   "source": [
    "Retrieve options chains for the ticker \"SPY\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "22932258",
   "metadata": {},
   "outputs": [],
   "source": [
    "spy_chains = openbb.stocks.options.chains(ticker)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2b653927",
   "metadata": {},
   "source": [
    "Store the retrieved stock and options data in an HDF5 file"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "87844c25",
   "metadata": {},
   "outputs": [],
   "source": [
    "with pd.HDFStore(STOCKS_DATA_STORE) as store:\n",
    "    store.put(\"equities/spy/stock_prices\", spy_equity)\n",
    "    store.put(\"equities/spy/options_prices\", spy_historic)\n",
    "    store.put(\"equities/spy/chains\", spy_chains)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f19a14bb",
   "metadata": {},
   "source": [
    "Retrieve and store futures data for the symbol \"ES\" for specific expiration dates"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a3596058",
   "metadata": {},
   "outputs": [],
   "source": [
    "with pd.HDFStore(FUTURES_DATA_STORE) as store:\n",
    "    for i in range(23, 31):\n",
    "        expiry = f\"20{i}-12\"\n",
    "        df = openbb.futures.historical(\n",
    "            symbols=[root],\n",
    "            expiry=expiry,\n",
    "            start_date=\"2020-01-01\",\n",
    "            end_date=\"2022-12-31\"\n",
    "        )\n",
    "        df.rename(\n",
    "            columns={\n",
    "                \"Adj Close\": expiry\n",
    "            },\n",
    "            inplace=True\n",
    "        )\n",
    "        prices = df[expiry]\n",
    "        store.put(f'futures/{root}/{expiry}', prices)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e2d26e16",
   "metadata": {},
   "source": [
    "Load stored stock prices, options prices, and options chains from the HDF5 file"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1f6f38e8",
   "metadata": {},
   "outputs": [],
   "source": [
    "with pd.HDFStore(STOCKS_DATA_STORE) as store:\n",
    "    spy_prices = store[\"equities/spy/stock_prices\"]\n",
    "    spy_options = store[\"equities/spy/options_prices\"]\n",
    "    spy_chains = store[\"equities/spy/chains\"]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "eaef333c",
   "metadata": {},
   "source": [
    "Load stored futures prices for a specific expiration date from the HDF5 file"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "718f2b5e",
   "metadata": {},
   "outputs": [],
   "source": [
    "with pd.HDFStore(FUTURES_DATA_STORE) as store:\n",
    "    es_prices = store[f\"futures/{root}/2023-12\"]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6e8de69d",
   "metadata": {},
   "source": [
    "Print the loaded stock prices for verification"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "be69f51a",
   "metadata": {},
   "outputs": [],
   "source": [
    "print(spy_prices)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "738952d1",
   "metadata": {},
   "source": [
    "Print the loaded options prices for verification"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a6ce0462",
   "metadata": {},
   "outputs": [],
   "source": [
    "print(spy_options)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "36b43858",
   "metadata": {},
   "source": [
    "Print the loaded options prices for verification"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d23ab3b5",
   "metadata": {},
   "outputs": [],
   "source": [
    "print(spy_options)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5b9da3de",
   "metadata": {},
   "source": [
    "Print the loaded futures prices for verification"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c3afda92",
   "metadata": {},
   "outputs": [],
   "source": [
    "print(es_prices)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3c427529",
   "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
}
