Skip to content

Instantly share code, notes, and snippets.

@quantitativo
Last active May 29, 2024 19:36
Show Gist options
  • Save quantitativo/3b42c9f92e6c7e9785e6affe46050b18 to your computer and use it in GitHub Desktop.
Save quantitativo/3b42c9f92e6c7e9785e6affe46050b18 to your computer and use it in GitHub Desktop.
Checking QQQ drawdown
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"id": "22fbd4d5-8b90-4bb0-bb5f-285e48d94a99",
"metadata": {},
"outputs": [],
"source": [
"import pandas as pd\n",
"import yfinance as yf"
]
},
{
"cell_type": "code",
"execution_count": 13,
"id": "2a03f94b-b242-4467-a425-6398f529d3d0",
"metadata": {},
"outputs": [],
"source": [
"df = yf.download('QQQ', progress=False)\n",
"\n",
"closing_prices = df['Close']\n",
"running_max = closing_prices.cummax()\n",
"drawdowns_closing_prices = (closing_prices - running_max) / running_max\n",
"\n",
"adj_closing_prices = df['Adj Close']\n",
"running_max = adj_closing_prices.cummax()\n",
"drawdowns_adj_closing_prices = (adj_closing_prices - running_max) / running_max"
]
},
{
"cell_type": "code",
"execution_count": 14,
"id": "b9e090a0-73f6-4df3-8c24-ee94f4406586",
"metadata": {},
"outputs": [],
"source": [
"def generate_drawdowns(drawdown_series: pd.Series) -> pd.DataFrame:\n",
" def split_nonzero_subseries(series):\n",
" non_zero_groups = []\n",
" current_group = []\n",
"\n",
" for index, value in series.items():\n",
" if value != 0:\n",
" current_group.append(index)\n",
" else:\n",
" if current_group:\n",
" non_zero_groups.append(series[current_group])\n",
" current_group = []\n",
"\n",
" # Append the last group if it exists\n",
" if current_group:\n",
" non_zero_groups.append(series[current_group])\n",
"\n",
" return non_zero_groups\n",
"\n",
" dds = split_nonzero_subseries(drawdown_series)\n",
" drawdowns = pd.DataFrame(columns=['start', 'end', 'duration', 'value'])\n",
" for dd in dds:\n",
" drawdowns.loc[len(drawdowns)] = [dd.index[0], dd.index[-1], len(dd), dd.min()]\n",
"\n",
" return drawdowns"
]
},
{
"cell_type": "code",
"execution_count": 15,
"id": "fed404f0-f45a-4142-9f6b-017c3255865d",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>start</th>\n",
" <th>end</th>\n",
" <th>duration</th>\n",
" <th>value</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>35</th>\n",
" <td>2000-03-28</td>\n",
" <td>2015-02-19</td>\n",
" <td>3747</td>\n",
" <td>-0.829639</td>\n",
" </tr>\n",
" <tr>\n",
" <th>161</th>\n",
" <td>2021-12-28</td>\n",
" <td>2023-12-12</td>\n",
" <td>493</td>\n",
" <td>-0.351187</td>\n",
" </tr>\n",
" <tr>\n",
" <th>125</th>\n",
" <td>2020-02-20</td>\n",
" <td>2020-06-02</td>\n",
" <td>72</td>\n",
" <td>-0.285594</td>\n",
" </tr>\n",
" <tr>\n",
" <th>102</th>\n",
" <td>2018-08-30</td>\n",
" <td>2019-04-11</td>\n",
" <td>154</td>\n",
" <td>-0.227966</td>\n",
" </tr>\n",
" <tr>\n",
" <th>46</th>\n",
" <td>2015-12-02</td>\n",
" <td>2016-07-26</td>\n",
" <td>163</td>\n",
" <td>-0.161044</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" start end duration value\n",
"35 2000-03-28 2015-02-19 3747 -0.829639\n",
"161 2021-12-28 2023-12-12 493 -0.351187\n",
"125 2020-02-20 2020-06-02 72 -0.285594\n",
"102 2018-08-30 2019-04-11 154 -0.227966\n",
"46 2015-12-02 2016-07-26 163 -0.161044"
]
},
"execution_count": 15,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"generate_drawdowns(drawdowns_adj_closing_prices).sort_values('value').head()"
]
},
{
"cell_type": "code",
"execution_count": 17,
"id": "fad23f7a-778a-47e0-b9e7-251e02cf4c39",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>start</th>\n",
" <th>end</th>\n",
" <th>duration</th>\n",
" <th>value</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>35</th>\n",
" <td>2000-03-28</td>\n",
" <td>2016-09-02</td>\n",
" <td>4136</td>\n",
" <td>-0.829639</td>\n",
" </tr>\n",
" <tr>\n",
" <th>142</th>\n",
" <td>2021-11-22</td>\n",
" <td>2023-12-14</td>\n",
" <td>519</td>\n",
" <td>-0.356172</td>\n",
" </tr>\n",
" <tr>\n",
" <th>108</th>\n",
" <td>2020-02-20</td>\n",
" <td>2020-06-04</td>\n",
" <td>74</td>\n",
" <td>-0.285594</td>\n",
" </tr>\n",
" <tr>\n",
" <th>85</th>\n",
" <td>2018-08-30</td>\n",
" <td>2019-04-16</td>\n",
" <td>157</td>\n",
" <td>-0.231552</td>\n",
" </tr>\n",
" <tr>\n",
" <th>27</th>\n",
" <td>2000-01-04</td>\n",
" <td>2000-01-18</td>\n",
" <td>10</td>\n",
" <td>-0.154683</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" start end duration value\n",
"35 2000-03-28 2016-09-02 4136 -0.829639\n",
"142 2021-11-22 2023-12-14 519 -0.356172\n",
"108 2020-02-20 2020-06-04 74 -0.285594\n",
"85 2018-08-30 2019-04-16 157 -0.231552\n",
"27 2000-01-04 2000-01-18 10 -0.154683"
]
},
"execution_count": 17,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"generate_drawdowns(drawdowns_closing_prices).sort_values('value').head()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "7f29d2a6-c6b2-473c-b436-9f2937a11d3e",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"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.12.1"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment