-
Notifications
You must be signed in to change notification settings - Fork 0
/
alphavantage_api.py
63 lines (56 loc) · 2.05 KB
/
alphavantage_api.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
"""
This is a simple Python3 api module/function for alphavantage, that retrieves data in csv format and saves it.
I'm new to programming so it may not be perfect, but does what I want.
I'll add features as I get time.
"""
#You need requests for this, see: http://docs.python-requests.org/en/master/
import requests
#For csv files
import csv
#Needed for timer to not exceed free downloads
import time
#For file paths
import os
#variables
#Replace /your/file/path.sqlite with the appropriate path
save_to = '/your/file/path/'
#Put your alphavantage api key here
api_key = 'Your API Key'
#needed for timer
start_time = time.time()
#not needed, but great way to feed stocks to the api
#I use a list generated from a sqlite database or a csv file
stock_list = []
#alphavantage api for reference
"""
https://www.alphavantage.co/query?
function=TIME_SERIES_DAILY_ADJUSTED
symbol=MSFT
datatype=csv
apikey=
outputsize=full
"""
#Function that does everything, it has two arguments
def get_ticker(save_to, stock_list):
#counter is so if something happens you know where you are in your list and can resume at that location(manually)
counter = 0
for each in stock_list:
print(counter)
print(each)
counter+=1
payload = {'function':'TIME_SERIES_DAILY_ADJUSTED', 'symbol': '{}'.format(each), 'datatype':'csv',\
'apikey': '{}'.format(api_key), 'outputsize':'full'}
r = requests.get('https://www.alphavantage.co/query?', params=payload)
if r.raise_for_status() is None:
#this will make a response object called r
#print(r.url)#for testing that is constructed correctly
#Saves each downloaded file as csv to save_to folder as stock.csv
file = open('{}{}.csv'.format(save_to, each), 'w+')
file.write(r.text)
file.close()
#Actual timer that limits stock downloads/minute(up to 5/min are free)
time.sleep(13 -((time.time() - start_time) % 13))
else:
print(r.raise_for_status)
if __name__=='__main__':
get_ticker(save_to, stock_list)