{ "cells": [ { "cell_type": "markdown", "id": "f8371bff", "metadata": {}, "source": [ "# Market Performance Visualization\n", "---" ] }, { "cell_type": "code", "execution_count": 1, "id": "c624dfe1", "metadata": { "ExecuteTime": { "end_time": "2022-07-10T18:17:15.685041Z", "start_time": "2022-07-10T18:17:15.680967Z" } }, "outputs": [], "source": [ "# # Install libraries - uncomment if running on a new environment (e.g., Colab)\n", "# !pip install --upgrade capon themes" ] }, { "cell_type": "code", "execution_count": 2, "id": "171daa8e", "metadata": { "ExecuteTime": { "end_time": "2022-07-10T18:17:16.259856Z", "start_time": "2022-07-10T18:17:15.686901Z" } }, "outputs": [], "source": [ "import capon\n", "import themes\n", "\n", "from capon.datasets import load_stock_indexes\n", "\n", "\n", "themes.register(enable=\"capon\")" ] }, { "cell_type": "markdown", "id": "096fee1c", "metadata": {}, "source": [ "## The Data" ] }, { "cell_type": "code", "execution_count": 3, "id": "d400ab82", "metadata": { "ExecuteTime": { "end_time": "2022-07-10T18:17:16.263869Z", "start_time": "2022-07-10T18:17:16.260715Z" } }, "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", "
symbolnamedescriptioncountry_codecity
0^GSPCS&P 500S&P 500 Index (SPX)USNew York
1^DJIDow 30Dow Jones Industrial Average (DJIA)USNew York
2^IXICNasdaqNASDAQ Composite Index (COMP)USNew York
3^RUTRussell 2000Russell 2000 Index (RUT)USNew York
\n", "
" ], "text/plain": [ " symbol name description country_code \\\n", "0 ^GSPC S&P 500 S&P 500 Index (SPX) US \n", "1 ^DJI Dow 30 Dow Jones Industrial Average (DJIA) US \n", "2 ^IXIC Nasdaq NASDAQ Composite Index (COMP) US \n", "3 ^RUT Russell 2000 Russell 2000 Index (RUT) US \n", "\n", " city \n", "0 New York \n", "1 New York \n", "2 New York \n", "3 New York " ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "indices_tickers = load_stock_indexes().query(\"country_code=='US'\")\n", "indices_tickers" ] }, { "cell_type": "code", "execution_count": 4, "id": "33ad7a62", "metadata": { "ExecuteTime": { "end_time": "2022-07-10T18:17:16.325353Z", "start_time": "2022-07-10T18:17:16.265282Z" } }, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "796d1f769ecf4baaad7831363a72daad", "version_major": 2, "version_minor": 0 }, "text/plain": [ " 0%| | 0/4 [00:00\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", " \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", "
timestampsymbolcurrencyopenlowhighclosevolumeadjclose
02019-09-30 09:30:00-04:00^GSPCUSD2967.0700682967.0700682983.8500982976.73999032491300002976.739990
12019-10-01 09:30:00-04:00^GSPCUSD2983.6899412938.6999512992.5300292940.25000035607500002940.250000
22019-10-02 09:30:00-04:00^GSPCUSD2924.7800292874.9299322924.7800292887.61010739141800002887.610107
32019-10-03 09:30:00-04:00^GSPCUSD2885.3798832855.9399412911.1298832910.62988335151300002910.629883
42019-10-04 09:30:00-04:00^GSPCUSD2918.5600592918.5600592953.7399902952.01001030056000002952.010010
..............................
49392024-08-21 09:30:00-04:00^RUTUSD2146.6101072146.6101072171.2700202170.56005931196700002170.560059
49402024-08-22 09:30:00-04:00^RUTUSD2170.7299802147.8701172174.8798832150.03002929815300002150.030029
49412024-08-23 09:30:00-04:00^RUTUSD2159.9799802159.9799802222.7500002218.69995132919400002218.699951
49422024-08-26 09:30:00-04:00^RUTUSD2230.2099612216.9399412237.6899412217.91992229385700002217.919922
49432024-08-27 09:30:00-04:00^RUTUSD2210.8798832195.0700682210.8798832203.00000027989900002203.000000
\n", "

4944 rows × 9 columns

\n", "" ], "text/plain": [ " timestamp symbol currency open low \\\n", "0 2019-09-30 09:30:00-04:00 ^GSPC USD 2967.070068 2967.070068 \n", "1 2019-10-01 09:30:00-04:00 ^GSPC USD 2983.689941 2938.699951 \n", "2 2019-10-02 09:30:00-04:00 ^GSPC USD 2924.780029 2874.929932 \n", "3 2019-10-03 09:30:00-04:00 ^GSPC USD 2885.379883 2855.939941 \n", "4 2019-10-04 09:30:00-04:00 ^GSPC USD 2918.560059 2918.560059 \n", "... ... ... ... ... ... \n", "4939 2024-08-21 09:30:00-04:00 ^RUT USD 2146.610107 2146.610107 \n", "4940 2024-08-22 09:30:00-04:00 ^RUT USD 2170.729980 2147.870117 \n", "4941 2024-08-23 09:30:00-04:00 ^RUT USD 2159.979980 2159.979980 \n", "4942 2024-08-26 09:30:00-04:00 ^RUT USD 2230.209961 2216.939941 \n", "4943 2024-08-27 09:30:00-04:00 ^RUT USD 2210.879883 2195.070068 \n", "\n", " high close volume adjclose \n", "0 2983.850098 2976.739990 3249130000 2976.739990 \n", "1 2992.530029 2940.250000 3560750000 2940.250000 \n", "2 2924.780029 2887.610107 3914180000 2887.610107 \n", "3 2911.129883 2910.629883 3515130000 2910.629883 \n", "4 2953.739990 2952.010010 3005600000 2952.010010 \n", "... ... ... ... ... \n", "4939 2171.270020 2170.560059 3119670000 2170.560059 \n", "4940 2174.879883 2150.030029 2981530000 2150.030029 \n", "4941 2222.750000 2218.699951 3291940000 2218.699951 \n", "4942 2237.689941 2217.919922 2938570000 2217.919922 \n", "4943 2210.879883 2203.000000 2798990000 2203.000000 \n", "\n", "[4944 rows x 9 columns]" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "indices = capon.stocks(\n", " indices_tickers[\"symbol\"],\n", " start=\"2019-10-01\",\n", " end=\"2025-01-01\",\n", " interval=\"1d\",\n", " n_jobs=-1,\n", ")\n", "indices" ] }, { "cell_type": "markdown", "id": "1402e09f", "metadata": {}, "source": [ "## Plot" ] }, { "cell_type": "code", "execution_count": 5, "id": "74b44103", "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "/opt/miniconda3/envs/capon/lib/python3.10/site-packages/altair/utils/deprecation.py:65: AltairDeprecationWarning: 'selection' is deprecated.\n", " Use 'selection_point()' or 'selection_interval()' instead; these functions also include more helpful docstrings.\n", " warnings.warn(message, AltairDeprecationWarning, stacklevel=1)\n", "/opt/miniconda3/envs/capon/lib/python3.10/site-packages/altair/vegalite/v5/api.py:469: AltairDeprecationWarning: The types 'single' and 'multi' are now\n", " combined and should be specified using \"selection_point()\".\n", " warnings.warn(\n", "/opt/miniconda3/envs/capon/lib/python3.10/site-packages/altair/utils/deprecation.py:65: AltairDeprecationWarning: 'add_selection' is deprecated. Use 'add_params' instead.\n", " warnings.warn(message, AltairDeprecationWarning, stacklevel=1)\n" ] }, { "data": { "text/html": [ "\n", "\n", "
\n", "" ], "text/plain": [ "alt.LayerChart(...)" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "capon.plot(indices)" ] }, { "cell_type": "code", "execution_count": 6, "id": "ef3dab45", "metadata": { "ExecuteTime": { "end_time": "2022-07-10T18:17:16.400381Z", "start_time": "2022-07-10T18:17:16.328404Z" } }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "/opt/miniconda3/envs/capon/lib/python3.10/site-packages/altair/utils/deprecation.py:65: AltairDeprecationWarning: 'selection' is deprecated.\n", " Use 'selection_point()' or 'selection_interval()' instead; these functions also include more helpful docstrings.\n", " warnings.warn(message, AltairDeprecationWarning, stacklevel=1)\n", "/opt/miniconda3/envs/capon/lib/python3.10/site-packages/altair/vegalite/v5/api.py:469: AltairDeprecationWarning: The types 'single' and 'multi' are now\n", " combined and should be specified using \"selection_point()\".\n", " warnings.warn(\n", "/opt/miniconda3/envs/capon/lib/python3.10/site-packages/altair/utils/deprecation.py:65: AltairDeprecationWarning: 'add_selection' is deprecated. Use 'add_params' instead.\n", " warnings.warn(message, AltairDeprecationWarning, stacklevel=1)\n" ] }, { "data": { "text/html": [ "\n", "\n", "
\n", "" ], "text/plain": [ "alt.LayerChart(...)" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "capon.plot(indices, normalize=\"2020-01-01\", title=\"Market Indices Change\")" ] }, { "cell_type": "markdown", "id": "32282873", "metadata": {}, "source": [ "(Under the hood, [`capon`](https://github.com/gialdetti/capon) used the [`altair`](https://altair-viz.github.io) visualization library here)" ] } ], "metadata": { "hide_input": false, "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.14" }, "toc": { "base_numbering": 1, "nav_menu": {}, "number_sections": true, "sideBar": true, "skip_h1_title": true, "title_cell": "Table of Contents", "title_sidebar": "Contents", "toc_cell": false, "toc_position": {}, "toc_section_display": true, "toc_window_display": true } }, "nbformat": 4, "nbformat_minor": 5 }