Data Science Portfolio

Scrape Historical OHLC (Open-High-Low-Close) Stock Prices

Overview

Obtain a unique API key here: https://www.alphavantage.co/support/#api-key

Import libraries

import io
import os
import sys
import math
import csv
import time
import itertools
import bisect
import string
import requests
import random
import requests
import json
import numpy
import pandas as pd
import alpha_vantage
import bs4
from bs4 import BeautifulSoup

Define stock scraper method

def ohlc_stock_price_scraper(symbol, interval, api_key):
    # Use GET request to access data through API, using custom API key and desired symbol/interval options
    data = requests.get('https://www.alphavantage.co/query?function=TIME_SERIES_' + interval + '&symbol=' + symbol + '&apikey=' + api_key + '=csv')
    
    return data

Test scraper method

data = ohlc_stock_price_scraper('AAPL', 'DAILY', 'WTX6IKTWWR57LOIQ&datatype')

Store raw data in new variable

raw_data = pd.read_csv(io.StringIO(data.content.decode('utf-8')))

View the first 10 rows of data

raw_data.head(10)
timestamp open high low close volume
0 2019-04-05 196.450 197.100 195.93 197.00 18472107
1 2019-04-04 194.790 196.370 193.14 195.69 19114275
2 2019-04-03 193.250 196.500 193.15 195.35 23271830
3 2019-04-02 191.090 194.460 191.05 194.02 22765732
4 2019-04-01 191.640 191.680 188.38 191.24 27861964
5 2019-03-29 189.830 190.080 188.54 189.95 23563961
6 2019-03-28 188.950 189.559 187.53 188.72 20780363
7 2019-03-27 188.750 189.760 186.55 188.47 29848427
8 2019-03-26 191.664 192.880 184.58 186.79 49800538
9 2019-03-25 191.510 191.980 186.60 188.74 43845293