Skip to content

Instantly share code, notes, and snippets.

@kevashcraft
Created June 18, 2018 17:18
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kevashcraft/6204744c79b909365b9d3d77d77d2cfb to your computer and use it in GitHub Desktop.
Save kevashcraft/6204744c79b909365b9d3d77d77d2cfb to your computer and use it in GitHub Desktop.
Retrieve Stock Market Data from the AlphaVantage API
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