{ "cells": [ { "cell_type": "markdown", "metadata": { "pycharm": {} }, "source": [ "# Obtaining Historical Stock Data\n", "\n", "Keywords: stock price data\n", "\n", "The purpose of this notebook is to download historical trading data for a selected group of the stocks from Alpha Vantage for use with other notebooks. Use of this notebook requires you so enter your personal Alpha Vantage api key into a file `data/api_key.txt`. The trading data is stored as individual `.csv` files in a designated directory. Subsequent notebooks read and consolidate that data into a singe file. \n", "\n", "You only need to run this notebook if you wish to analyze a different set of stocks, if you wish to update data for the existing set." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Imports" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import os\n", "import pandas as pd\n", "import requests\n", "import time" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Select Stocks to Download" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "pycharm": {} }, "outputs": [], "source": [ "djia = ['AXP','BA','CAT','CSCO','CVX','DD','DIS','GE', \\\n", " 'GS','HD','IBM','INTC','JNJ','JPM','KO','MCD', \\\n", " 'MMM','MRK','MSFT','NKE','PFE','PG','T','TRV', \\\n", " 'UNH','UTX','V','VZ','WMT','XOM']\n", "\n", "favs = ['AAPL']\n", "\n", "stocks = favs + djia\n", "\n", "data_dir = os.path.join('data', 'stocks')\n", "os.makedirs(data_dir, exist_ok=True)" ] }, { "cell_type": "markdown", "metadata": { "pycharm": {} }, "source": [ "## Alpha Vantage\n", "\n", "The following cells retrieve a history of daily trading data for a specified set of stock ticker symbols. These functions use the free [Alpha Vantage](https://www.alphavantage.co/) data service. The free service tier provides up to 5 queries per minute.\n", "\n", "The service requires an personal api key which can be claimed [here](https://www.alphavantage.co/support/#api-key) in just a few seconds. Place the key as a string in a file `data/api_key.txt` in the data directory as this notebook (note: api_key.txt is not distributed with the github repository). The function `api_key()` returns the key stored in `api_key.txt`." ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "pycharm": {} }, "outputs": [], "source": [ "def api_key():\n", " \"Read api_key.txt and return api_key\"\n", " try:\n", " with open('data/api_key.txt') as fp:\n", " line = fp.readline()\n", " except:\n", " raise RuntimeError('Error while attempting to read data/api_key.txt')\n", " return line.strip()" ] }, { "cell_type": "markdown", "metadata": { "pycharm": {} }, "source": [ "The function `alphavantage(s)` returns a pandas dataframe holding historical trading data for a stocker ticker symbol specified by `s`." ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "pycharm": {} }, "outputs": [ { "data": { "text/html": [ "
\n", " | open | \n", "high | \n", "low | \n", "close | \n", "adjusted close | \n", "volume | \n", "dividend amount | \n", "split coefficient | \n", "
---|---|---|---|---|---|---|---|---|
2022-11-07 | \n", "137.110 | \n", "139.145 | \n", "135.671 | \n", "138.92 | \n", "138.920000 | \n", "83374628 | \n", "0.00 | \n", "1.0 | \n", "
2022-11-04 | \n", "142.090 | \n", "142.670 | \n", "134.380 | \n", "138.38 | \n", "138.380000 | \n", "140814796 | \n", "0.23 | \n", "1.0 | \n", "
2022-11-03 | \n", "142.060 | \n", "142.800 | \n", "138.750 | \n", "138.88 | \n", "138.649552 | \n", "97918516 | \n", "0.00 | \n", "1.0 | \n", "
2022-11-02 | \n", "148.945 | \n", "152.170 | \n", "145.000 | \n", "145.03 | \n", "144.789347 | \n", "93604623 | \n", "0.00 | \n", "1.0 | \n", "
2022-11-01 | \n", "155.080 | \n", "155.450 | \n", "149.130 | \n", "150.65 | \n", "150.400022 | \n", "80379345 | \n", "0.00 | \n", "1.0 | \n", "