{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "2a7b4695",
   "metadata": {},
   "source": [
    "<div style=\"background-color:#000;\"><img src=\"pqn.png\"></img></div>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4dde6243",
   "metadata": {},
   "source": [
    "This code processes historical stock data for financial analysis and option strategy backtesting. It reads multiple CSV files containing end-of-day data, concatenates them, and saves the result in a single file. The script then loads this combined data and uses the Optopsy library to analyze option chains. It evaluates various option strategies such as short calls, long straddles, and short strangles, providing a practical tool for financial professionals to assess historical performance and strategy efficacy."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5d5d8246",
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "import glob\n",
    "import pandas as pd\n",
    "import optopsy as op"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9337e673",
   "metadata": {},
   "source": [
    "Collect all CSV files in the \"rut-eod\" directory"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "25707d5a",
   "metadata": {},
   "outputs": [],
   "source": [
    "files = glob.glob(os.path.join(\"rut-eod\", \"*.csv\"))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "da83ac55",
   "metadata": {},
   "source": [
    "Initialize an empty list for storing dataframes"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "20abd76b",
   "metadata": {},
   "outputs": [],
   "source": [
    "dfs = []\n",
    "for fl in files:\n",
    "    # Read each CSV file and append its dataframe to the list\n",
    "    df = pd.read_csv(fl)\n",
    "    dfs.append(df)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "106998bc",
   "metadata": {},
   "source": [
    "Concatenate all dataframes and save the combined data to a new CSV file"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "47998816",
   "metadata": {},
   "outputs": [],
   "source": [
    "pd.concat(dfs).to_csv(\"rut_historic.csv\", index=False)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3d36b3a7",
   "metadata": {},
   "source": [
    "Load the combined historical data from the new CSV file"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e504c82f",
   "metadata": {},
   "outputs": [],
   "source": [
    "rut = pd.read_csv(\"rut_historic.csv\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0d6640b5",
   "metadata": {},
   "source": [
    "Print the most recent date in the dataset"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "67b0527c",
   "metadata": {},
   "outputs": [],
   "source": [
    "rut.date.max()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4c9823ce",
   "metadata": {},
   "source": [
    "Load the option chains from the combined historical data using specific column indices"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8240913f",
   "metadata": {},
   "outputs": [],
   "source": [
    "rut_chains = op.csv_data(\n",
    "    \"rut_historic.csv\",\n",
    "    underlying_symbol=1,\n",
    "    underlying_price=4,\n",
    "    option_type=8,\n",
    "    expiration=6,\n",
    "    quote_date=0,\n",
    "    strike=7,\n",
    "    bid=14,\n",
    "    ask=15\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e21cd76d",
   "metadata": {},
   "source": [
    "Display the first few rows of the option chains dataframe"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5850a16f",
   "metadata": {},
   "outputs": [],
   "source": [
    "rut_chains.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "219a8e53",
   "metadata": {},
   "source": [
    "Evaluate and round the results of the short calls strategy"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6f7e6cd7",
   "metadata": {},
   "outputs": [],
   "source": [
    "op.short_calls(rut_chains).round(2)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ad327b31",
   "metadata": {},
   "source": [
    "Evaluate and round the results of the long straddles strategy"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "57f48082",
   "metadata": {},
   "outputs": [],
   "source": [
    "op.long_straddles(rut_chains).round(2)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1d4573d2",
   "metadata": {},
   "source": [
    "Evaluate and round the results of the short strangles strategy with specified parameters"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "bb9d8953",
   "metadata": {},
   "outputs": [],
   "source": [
    "op.short_strangles(\n",
    "    rut_chains, \n",
    "    dte_interval=60, \n",
    "    max_entry_dte=70, \n",
    "    exit_dte=10,\n",
    "    otm_pct_interval=0.01,\n",
    "    max_otm_pct=0.10\n",
    ").round(2)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5baa0691",
   "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
}
