{ "cells": [ { "cell_type": "code", "execution_count": 1, "id": "b8cef1f0", "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "import pandas as pd\n", "import yfinance as yf\n", "from datetime import datetime, timedelta\n", "\n", "\n", "def conf_int_ind(symb, target_time=None):\n", " ticker = yf.Ticker(symb)\n", " expirations = ticker.options\n", " df_time = pd.to_datetime(expirations)\n", " if target_time is None:\n", " target_time = df_time[0]\n", " target_time_r = df_time[df_time <= target_time][-1].strftime(\"%Y-%m-%d\")\n", "\n", " time_now = datetime.now().strftime(\"%Y-%m-%d\")\n", " T = (pd.to_datetime(target_time_r) - pd.to_datetime(time_now)).days + 1\n", "\n", " opt_chain = ticker.option_chain(target_time_r)\n", " current_price = ticker.fast_info[\"lastPrice\"]\n", "\n", " lop = opt_chain.puts.strike[~opt_chain.puts.inTheMoney].iloc[-1]\n", " hip = opt_chain.puts.strike[opt_chain.puts.inTheMoney].iloc[0]\n", " iv1p = opt_chain.puts.loc[opt_chain.puts.strike == lop, \"impliedVolatility\"].item()\n", " iv2p = opt_chain.puts.loc[opt_chain.puts.strike == hip, \"impliedVolatility\"].item()\n", " ivp = (iv1p + iv2p) / 2\n", " sdp = ivp * np.sqrt(T / 365)\n", "\n", " loca = opt_chain.calls.strike[opt_chain.calls.inTheMoney].iloc[-1]\n", " hica = opt_chain.calls.strike[~opt_chain.calls.inTheMoney].iloc[0]\n", " iv1c = opt_chain.calls.loc[opt_chain.calls.strike == loca, \"impliedVolatility\"].item()\n", " iv2c = opt_chain.calls.loc[opt_chain.calls.strike == hica, \"impliedVolatility\"].item()\n", " ivc = (iv1c + iv2c) / 2\n", " sdc = ivc * np.sqrt(T / 365)\n", "\n", " row = {\n", " \"symbol\": [f\"{ticker.info['symbol']}\"],\n", " \"days\": [f\"{target_time_r}: ({T}) days\"],\n", " \"-2.5%(p)\": [current_price * (1 - 2 * sdp)],\n", " \"-6.5%(p)\": [current_price * (1 - 1.5 * sdp)],\n", " \"-16.5%(p)\": [current_price * (1 - 1 * sdp)],\n", " \"current\": [current_price],\n", " \"+16.5%(c)\": [current_price * (1 + 1 * sdc)],\n", " \"+6.5%(c)\": [current_price * (1 + 1.5 * sdc)],\n", " \"+2.5%(c)\": [current_price * (1 + 2 * sdc)],\n", " }\n", "\n", " df = pd.DataFrame(row).set_index(\"symbol\")\n", " return df\n", "\n", "\n", "def conf_int_duo(symb, target_time=None):\n", " if target_time is None:\n", " expirations = yf.Ticker(symb).options\n", " df_time = pd.to_datetime(expirations)\n", " dlist = expirations[: (df_time - datetime.now() <= timedelta(days=14)).sum()]\n", " df = pd.concat([conf_int_ind(symb, d) for d in dlist])\n", " else:\n", " df = conf_int_ind(symb, target_time)\n", " return df\n", "\n", "\n", "def conf_int(symblist, target_time=None):\n", " if isinstance(symblist, str):\n", " df = conf_int_duo(symblist)\n", " elif isinstance(symblist, list):\n", " df = pd.concat([conf_int_duo(symb, target_time) for symb in symblist])\n", " return df\n" ] }, { "cell_type": "code", "execution_count": 2, "id": "587f982a", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
days-2.5%(p)-6.5%(p)-16.5%(p)current+16.5%(c)+6.5%(c)+2.5%(c)
symbol
SOXL2026-03-13: (5) days53.22236653.24677453.27118353.32000053.36881753.39322553.417633
SOXL2026-03-20: (12) days53.16874653.20656053.24437353.32000053.47115753.54673553.622313
MSFT2026-03-11: (3) days408.829314408.974487409.119659409.410004409.555362409.628041409.700720
MSFT2026-03-13: (5) days408.660337408.847754409.035170409.410004409.504071409.551105409.598139
MSFT2026-03-16: (8) days408.935267409.053951409.172635409.410004409.528991409.588485409.647978
MSFT2026-03-18: (10) days408.879232409.011925409.144618409.410004409.543036409.609551409.676067
MSFT2026-03-20: (12) days408.828572408.973930409.119288409.410004409.483239409.519857409.556475
\n", "
" ], "text/plain": [ " days -2.5%(p) -6.5%(p) -16.5%(p) current \\\n", "symbol \n", "SOXL 2026-03-13: (5) days 53.222366 53.246774 53.271183 53.320000 \n", "SOXL 2026-03-20: (12) days 53.168746 53.206560 53.244373 53.320000 \n", "MSFT 2026-03-11: (3) days 408.829314 408.974487 409.119659 409.410004 \n", "MSFT 2026-03-13: (5) days 408.660337 408.847754 409.035170 409.410004 \n", "MSFT 2026-03-16: (8) days 408.935267 409.053951 409.172635 409.410004 \n", "MSFT 2026-03-18: (10) days 408.879232 409.011925 409.144618 409.410004 \n", "MSFT 2026-03-20: (12) days 408.828572 408.973930 409.119288 409.410004 \n", "\n", " +16.5%(c) +6.5%(c) +2.5%(c) \n", "symbol \n", "SOXL 53.368817 53.393225 53.417633 \n", "SOXL 53.471157 53.546735 53.622313 \n", "MSFT 409.555362 409.628041 409.700720 \n", "MSFT 409.504071 409.551105 409.598139 \n", "MSFT 409.528991 409.588485 409.647978 \n", "MSFT 409.543036 409.609551 409.676067 \n", "MSFT 409.483239 409.519857 409.556475 " ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "symbs = [\"SOXL\", \"MSFT\"]\n", "\n", "df = conf_int(symbs)\n", "df" ] }, { "cell_type": "code", "execution_count": null, "id": "428526cd", "metadata": {}, "outputs": [], "source": [ "# symb = \"SOXL\"\n", "\n", "# ticker = yf.Ticker(symb)\n", "# expirations = ticker.options\n", "# target_time = expirations[0]\n", "\n", "\n", "# time_now = datetime.now().strftime(\"%Y-%m-%d\")\n", "# T = (pd.to_datetime(target_time) - pd.to_datetime(time_now)).days + 1\n", "\n", "# opt_chain = ticker.option_chain(target_time)\n", "# current_price = ticker.fast_info[\"lastPrice\"]\n" ] } ], "metadata": { "kernelspec": { "display_name": "env", "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.13.9" } }, "nbformat": 4, "nbformat_minor": 5 }