|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "code", |
| 5 | + "execution_count": null, |
| 6 | + "metadata": {}, |
| 7 | + "outputs": [], |
| 8 | + "source": [ |
| 9 | + "# Imports\n", |
| 10 | + "import yfinance as yf\n", |
| 11 | + "import datetime as dt\n", |
| 12 | + "import pandas as pd\n", |
| 13 | + "import requests\n", |
| 14 | + "import time\n", |
| 15 | + "import io\n", |
| 16 | + "\n", |
| 17 | + "# Read data from URL containing NASDAQ listings\n", |
| 18 | + "url = \"https://pkgstore.datahub.io/core/nasdaq-listings/nasdaq-listed_csv/data/7665719fb51081ba0bd834fde71ce822/nasdaq-listed_csv.csv\"\n", |
| 19 | + "data = requests.get(url).content\n", |
| 20 | + "df = pd.read_csv(io.StringIO(data.decode(\"utf-8\")))\n", |
| 21 | + "df.head()" |
| 22 | + ] |
| 23 | + }, |
| 24 | + { |
| 25 | + "cell_type": "code", |
| 26 | + "execution_count": null, |
| 27 | + "metadata": {}, |
| 28 | + "outputs": [], |
| 29 | + "source": [ |
| 30 | + "symbols = df[\"Symbol\"].to_list()" |
| 31 | + ] |
| 32 | + }, |
| 33 | + { |
| 34 | + "cell_type": "code", |
| 35 | + "execution_count": null, |
| 36 | + "metadata": {}, |
| 37 | + "outputs": [], |
| 38 | + "source": [ |
| 39 | + "stock_id = input(\"Enter Stock ID:\")\n", |
| 40 | + "stock_id = stock_id.upper()\n", |
| 41 | + "\n", |
| 42 | + "if stock_id in symbols:\n", |
| 43 | + " df_stock = yf.download(stock_id, start=\"1950-01-01\", period=\"1d\")\n", |
| 44 | + " print(df_stock)\n", |
| 45 | + "else:\n", |
| 46 | + " print(\"Incorrect Stock Symbol. Please Enter Valid Symbols\")" |
| 47 | + ] |
| 48 | + }, |
| 49 | + { |
| 50 | + "cell_type": "code", |
| 51 | + "execution_count": null, |
| 52 | + "metadata": {}, |
| 53 | + "outputs": [], |
| 54 | + "source": [ |
| 55 | + "df_stock.drop(\"Adj Close\", axis=\"columns\", inplace=True)\n", |
| 56 | + "df_stock.head()" |
| 57 | + ] |
| 58 | + }, |
| 59 | + { |
| 60 | + "cell_type": "code", |
| 61 | + "execution_count": null, |
| 62 | + "metadata": {}, |
| 63 | + "outputs": [], |
| 64 | + "source": [ |
| 65 | + "# Reset index makes sure the dataframe has indexing of its own and converts the date index to a column\n", |
| 66 | + "df_stock.reset_index(inplace=True)\n", |
| 67 | + "df_stock.head()" |
| 68 | + ] |
| 69 | + }, |
| 70 | + { |
| 71 | + "cell_type": "code", |
| 72 | + "execution_count": null, |
| 73 | + "metadata": {}, |
| 74 | + "outputs": [], |
| 75 | + "source": [ |
| 76 | + "# Convert the date to a datetime object (gets converted to a specialised type of datetime object)\n", |
| 77 | + "df_stock[\"Date\"] = pd.to_datetime(df_stock[\"Date\"])\n", |
| 78 | + "df_stock.Date.dtype" |
| 79 | + ] |
| 80 | + }, |
| 81 | + { |
| 82 | + "cell_type": "code", |
| 83 | + "execution_count": null, |
| 84 | + "metadata": {}, |
| 85 | + "outputs": [], |
| 86 | + "source": [ |
| 87 | + "# Convert date to epoch datetime format\n", |
| 88 | + "df_stock[\"Date\"] = (df_stock[\"Date\"] - dt.datetime(1970, 1, 1)).dt.total_seconds()\n", |
| 89 | + "df_stock.head()" |
| 90 | + ] |
| 91 | + }, |
| 92 | + { |
| 93 | + "cell_type": "code", |
| 94 | + "execution_count": null, |
| 95 | + "metadata": {}, |
| 96 | + "outputs": [], |
| 97 | + "source": [ |
| 98 | + "df_stock.Date.dtype" |
| 99 | + ] |
| 100 | + }, |
| 101 | + { |
| 102 | + "cell_type": "code", |
| 103 | + "execution_count": null, |
| 104 | + "metadata": {}, |
| 105 | + "outputs": [], |
| 106 | + "source": [ |
| 107 | + "# Format for plotting requires specific size for date so multiply by 1000\n", |
| 108 | + "df_stock[\"Date\"] = df_stock[\"Date\"] * 1000\n", |
| 109 | + "df_stock.head()" |
| 110 | + ] |
| 111 | + }, |
| 112 | + { |
| 113 | + "cell_type": "code", |
| 114 | + "execution_count": null, |
| 115 | + "metadata": {}, |
| 116 | + "outputs": [], |
| 117 | + "source": [ |
| 118 | + "# Convert to json format and make sure its converted as json with arrays thus orient = values\n", |
| 119 | + "df_stock.to_json(\"data/\" + stock_id + \"_mod.json\", orient=\"values\")" |
| 120 | + ] |
| 121 | + } |
| 122 | + ], |
| 123 | + "metadata": { |
| 124 | + "kernelspec": { |
| 125 | + "display_name": "Python 3", |
| 126 | + "language": "python", |
| 127 | + "name": "python3" |
| 128 | + }, |
| 129 | + "language_info": { |
| 130 | + "codemirror_mode": { |
| 131 | + "name": "ipython", |
| 132 | + "version": 3 |
| 133 | + }, |
| 134 | + "file_extension": ".py", |
| 135 | + "mimetype": "text/x-python", |
| 136 | + "name": "python", |
| 137 | + "nbconvert_exporter": "python", |
| 138 | + "pygments_lexer": "ipython3", |
| 139 | + "version": "3.8.5" |
| 140 | + } |
| 141 | + }, |
| 142 | + "nbformat": 4, |
| 143 | + "nbformat_minor": 4 |
| 144 | +} |
0 commit comments