-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPolygon-API-Connector2.1
118 lines (82 loc) · 3.14 KB
/
Polygon-API-Connector2.1
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
import requests
import pandas as pd
import datetime
import pytz
import time
import concurrent.futures
api = 'API HERE'
def tickerlistcreator():
storagelist = []
status = 1
totalpages = 1
#Set URL for the initial request
url = 'https://api.polygon.io/v3/reference/tickers?type=CS&market=stocks&active=true&sort=ticker&order=asc&limit=1000' + api
while status == 1:
response = requests.get(url)
for item in response.json()['results']:
storagelist.append(item['ticker'])
if "next_url" in response.json():
url = response.json()['next_url'] + api
totalpages += 1
else:
status = 0
print('total pages of ticker symbols: ', totalpages)
return(storagelist)
def stockdownloader(stock, startdate, enddate):
url = ('https://api.polygon.io/v2/aggs/ticker/%s/range/1/day/%s/%s?adjusted=true&sort=asc&limit=1000' % (
stock, startdate, enddate)) + api
status = 1
totalpages = 1
storage = []
futures = []
while status == 1:
response = requests.get(url)
try:
# Run with threading
for item in response.json()['results']:
with concurrent.futures.ThreadPoolExecutor(max_workers=20) as executor:
futures.append(executor.submit(innerfunction, response, stock, item))
# future = executor.submit(innerfunction,response,stock)
# print(future.result())
# Run without threading
# innerfunction(response, stock)
except:
print("Error with stock:", stock)
if "next_url" in response.json():
url = response.json()['next_url'] + api
totalpages += 1
else:
status = 0
for future in concurrent.futures.as_completed(futures):
storage.append(future.result())
# print(future.result())
df = pd.DataFrame(storage)
print("dataframe her: ", df)
# print("df lavet ud fra 'futures' listen: ", df)
# print('total pages of stock data: ', totalpages)
def innerfunction(response, stock, item):
date = datetime.datetime.fromtimestamp(item['t'] / 1000, tz=pytz.timezone('America/New_York')).date()
#date = '2021-01-01'
ticker = response.json()['ticker']
marketcapresponse = requests.get('https://api.polygon.io/vX/reference/tickers/%s?date=%s' % (stock, date) + api)
marketcap = marketcapresponse.json()['results']['market_cap']
json_element = {
'date': date,
'ticker': ticker,
'open': item['o'],
'high': item['h'],
'low': item['l'],
'close': item['c'],
'marketcap': marketcap}
return (json_element)
#------- RUNNING FUNCTIONS -------
#Define test universe
tickerlist = tickerlistcreator()
print("First 10 tickers: ", tickerlist[0:10])
print("Total number of tickers is: ", len(tickerlist))
starttime = time.perf_counter()
#Download data - the main function - set currently to just use the first ticker
for stock in tickerlist[0:1]:
stockdownloader(stock, '2021-11-01','2021-12-31')
endtime = time.perf_counter()
print("Total runtime: ", endtime-starttime)