Skip to content

Instantly share code, notes, and snippets.

@angwrk
Created July 19, 2023 14:24
Show Gist options
  • Save angwrk/5e0c173f3630ca34ad3e261fd6ffd5e5 to your computer and use it in GitHub Desktop.
Save angwrk/5e0c173f3630ca34ad3e261fd6ffd5e5 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"nbformat": 4,
"nbformat_minor": 5,
"metadata": {
"noteable-chatgpt": {
"create_notebook": {
"openai_conversation_id": "183294bd-9ba7-5a7b-8264-5a585fee08ab",
"openai_ephemeral_user_id": "f9559fd3-f4c2-5169-b7a2-679f1e6eda54"
}
},
"noteable": {
"last_transaction_id": "1a139c99-56b5-47b5-9e61-e55feb0ba102"
},
"selected_hardware_size": "small"
},
"cells": [
{
"id": "8071cea4-df7e-432b-9787-9348cf3b3e8c",
"cell_type": "code",
"metadata": {
"noteable": {
"cell_type": "code",
"output_collection_id": "5bede853-5ff4-47fb-ae1a-33b66d9ac1bf"
},
"ExecuteTime": {
"end_time": "2023-07-19T10:45:07.231776+00:00",
"start_time": "2023-07-19T10:45:02.329640+00:00"
}
},
"execution_count": null,
"source": "!pip install -q yfinance\nimport yfinance as yf\nimport numpy as np\nimport matplotlib.pyplot as plt\nfrom datetime import datetime, timedelta",
"outputs": []
},
{
"id": "087d4a21-ef31-496e-8ab6-453e4e07a18e",
"cell_type": "code",
"metadata": {
"noteable": {
"cell_type": "code",
"output_collection_id": "19602a19-f373-49a1-b51a-552c9f32fad4"
},
"ExecuteTime": {
"end_time": "2023-07-19T10:45:10.456565+00:00",
"start_time": "2023-07-19T10:45:08.754492+00:00"
}
},
"execution_count": null,
"source": "# Define the ticker symbols\ntickers = ['AAPL', 'MSFT', 'GOOGL', 'AMZN', 'FB', 'TSLA', 'NVDA', 'JPM', 'V', 'JNJ']\n# Define the start and end dates\nend_date = datetime.now()\nstart_date = end_date - timedelta(days=5*365)\n# Download the data\ndata = yf.download(tickers, start=start_date, end=end_date)['Adj Close']\n# Normalize the data\nnormalized_data = data / data.iloc[0]\n# Plot the normalized data\nplt.figure(figsize=(14, 7))\nfor i in normalized_data.columns.values:\n plt.plot(normalized_data.index, normalized_data[i], lw=2, label=i)\nplt.legend(loc='upper left', fontsize=12)\nplt.ylabel('Normalized price')\nplt.title('Normalized prices of 10 US stocks over the last 5 years')\nplt.grid(True)\nplt.show()",
"outputs": []
},
{
"id": "5b167631-9578-4ab9-bfdc-6496c94c52db",
"cell_type": "code",
"metadata": {
"noteable": {
"cell_type": "code",
"output_collection_id": "8910d555-8d8d-43f7-bdd4-68e55557e31e"
},
"ExecuteTime": {
"end_time": "2023-07-19T10:45:24.427403+00:00",
"start_time": "2023-07-19T10:45:22.319908+00:00"
}
},
"execution_count": null,
"source": "from matplotlib.lines import Line2D\n# Define colors for the plots\ncolors = plt.rcParams['axes.prop_cycle'].by_key()['color']\n# Plot the real cumulative returns\nplt.figure(figsize=(14, 7))\nfor i, ticker in enumerate(real_returns.columns):\n plt.plot(real_returns.index, real_returns[ticker], lw=2, label=ticker, color=colors[i % len(colors)])\nplt.ylabel('Cumulative returns')\nplt.title('Real Cumulative Returns over the last 5 years')\nplt.legend()\nplt.grid(True)\nplt.show()\n# Plot the simulations\nplt.figure(figsize=(14, 7))\nfor t, ticker in enumerate(normalized_data.columns):\n for i in range(num_simulations):\n plt.plot(normalized_data.index, simulated_returns_array[i, :, t], lw=2, alpha=0.4, color=colors[t % len(colors)])\nplt.ylabel('Cumulative returns')\nplt.title('Monte Carlo Random Walk Simulations of Cumulative Returns over the last 5 years')\nhandles = [Line2D([0], [0], color=colors[i % len(colors)], lw=2) for i in range(len(normalized_data.columns))]\nplt.legend(handles, normalized_data.columns)\nplt.grid(True)\nplt.show()",
"outputs": []
},
{
"id": "d004afb9-eaf5-4821-a708-31b3f0fbff67",
"cell_type": "code",
"metadata": {
"noteable": {
"cell_type": "code",
"output_collection_id": "4325227d-9b2b-44e4-8b85-9df93e6e3cd0"
},
"ExecuteTime": {
"end_time": "2023-07-19T10:45:33.318383+00:00",
"start_time": "2023-07-19T10:45:29.619452+00:00"
}
},
"execution_count": null,
"source": "# Normalize the simulated returns\nsimulated_returns_random_normalized = simulated_returns_random.groupby(level='Simulation').apply(lambda x: x - x.iloc[0])\n# Plot the normalized simulations\nplt.figure(figsize=(14, 7))\nfor t, ticker in enumerate(normalized_data.columns):\n for i in range(num_simulations):\n plt.plot(normalized_data.index, simulated_returns_random_normalized.loc[i, ticker], lw=2, alpha=0.4, color=colors[t % len(colors)])\nplt.ylabel('Cumulative returns')\nplt.title('Normalized Random Walk Simulations of Cumulative Returns over the last 5 years (No Trend)')\nhandles = [Line2D([0], [0], color=colors[i % len(colors)], lw=2) for i in range(len(normalized_data.columns))]\nplt.legend(handles, normalized_data.columns)\nplt.grid(True)\nplt.show()",
"outputs": []
},
{
"id": "1b669076-2048-423d-84d4-dab0fa6224e1",
"cell_type": "code",
"metadata": {
"jupyter": {
"outputs_hidden": false,
"source_hidden": false
},
"noteable": {
"cell_type": "code"
}
},
"execution_count": null,
"source": "",
"outputs": []
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment