Created
June 18, 2018 17:18
-
-
Save kevashcraft/6204744c79b909365b9d3d77d77d2cfb to your computer and use it in GitHub Desktop.
Retrieve Stock Market Data from the AlphaVantage API
This file contains 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
from urllib.request import urlopen | |
import yaml | |
def retrieve(symbol=None): | |
with open('config.yml') as f: | |
config = yaml.load(f) | |
params = { | |
'function': 'TIME_SERIES_DAILY', | |
'symbol': symbol, | |
'outputsize': 'full', | |
'apikey': config['alphavantage']['apikey'] | |
} | |
param_str = '&'.join(['{}={}'.format(key, value) for key, value in params.items()]) | |
url = "https://www.alphavantage.co/query?{}".format(param_str) | |
print('Retrieving {}'.format(url)) | |
csv_data = urlopen(url).read().decode('utf-8') | |
print('Saving to {}.csv'.format(symbol)) | |
with open('{}.csv'.format(symbol), 'w') as f: | |
f.write(csv_data) | |
print('Done') | |
if __name__ == '__main__': | |
import argparse | |
parser = argparse.ArgumentParser(description="Data Retriever") | |
parser.add_argument('--symbol', help="The symbol to retrieve", default='GOOGL') | |
args = parser.parse_args() | |
retrieve(symbol=args.symbol) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment