Created
October 25, 2018 12:27
-
-
Save dhke/738fbd55b9984107e2cd67f5d47e519d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- encoding=utf-8 -*- | |
import requests | |
import csv | |
from datetime import datetime | |
import pandas as pd | |
import json | |
SYMBOLS_FILE = 'nasdaq-listed-symbols.csv' | |
IEX_URL_TEMPLATE = 'https://api.iextrading.com/1.0/stock/{symbol}/chart/5y' | |
DATE_FORMAT = '%Y-%m-%d' | |
with open(SYMBOLS_FILE, 'r') as symbols_fd: | |
sample = symbols_fd.read(8192) | |
symbols_fd.seek(0) | |
sniffer = csv.Sniffer() | |
dialect = sniffer.sniff(sample) | |
csv_reader = csv.DictReader(symbols_fd, dialect=dialect) | |
stocks = { | |
row['Symbol']: row['Company Name'].strip() | |
for row in csv_reader | |
} | |
df = pd.DataFrame() | |
symbols = stocks.keys() | |
for i, symbol in enumerate(symbols, start=1): | |
resp = requests.get(IEX_URL_TEMPLATE.format(symbol=symbol)) | |
resp_data = json.loads(resp.content) | |
dates = [ | |
datetime.strptime(item['date'], DATE_FORMAT) | |
for item in resp_data | |
] | |
# check https://iextrading.com/developer/docs/#chart | |
# for other possible values | |
values = [ | |
item['close'] | |
for item in resp_data | |
] | |
series = pd.Series(values, index=dates) | |
print("Loading {} ({} of {})...".format(symbol, i, len(symbols))) | |
df[symbol] = series | |
df.to_csv('stock_closing.csv') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment