Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathbasic-example-simfin+.py
More file actions
Latest commit
55 lines (44 loc) · 1.8 KB
/
Copy pathbasic-example-simfin+.py
File metadata and controls
55 lines (44 loc) · 1.8 KB
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
# if you haven't installed requests, get it via 'pip install requests'
importrequests
# if you haven't installed pandas, get it via 'pip install pandas'
importpandasaspd
# here you have to enter your actual API key from SimFin
# note: for this example, you need a SimFin+ subscription, otherwise the API will return an error.
api_key="YOUR_API_KEY"
# list of tickers we want to get data for
tickers= ["AAPL", "NVDA", "WMT"]
# define the periods that we want to retrieve
periods= ["q1", "q2", "q3", "q4"]
year_start=2012
year_end=2020
# request url for all financial statements
request_url='https://simfin.com/api/v2/companies/statements'
# variable to store the names of the columns
columns= []
# variable to store our data
output= []
# with simfin+, we can retrieve all data in just one call; this is much faster than making individual requests
# define the parameters for the query
parameters= {"statement": "pl", "ticker": ",".join(tickers), "period": "quarters", "fyear": ",".join([str(x) forxinlist(range(year_start,year_end+1))]), "api-key": api_key}
# make the request
request=requests.get(request_url, parameters)
# convert response to json
all_data=request.json()
# in the data that is returned, each index represents one of the requested tickers
forresponse_index, datainenumerate(all_data):
# make sure that data was found
ifdata['found'] andlen(data['data']) >0:
# add the column descriptions once only
iflen(columns) ==0:
columns=data['columns']
# add the data
output+=data['data']
# make dataframe from output
df=pd.DataFrame(output, columns=columns)
# save to XLSX
# set up the XLSX writer
writer=pd.ExcelWriter("simfin_data.xlsx", engine='xlsxwriter')
# write data and close file
df.to_excel(writer)
writer.save()
writer.close()