I've been looking for a few more tools to automate stock analysis, which is how i found this link to the code below. The author says he posted the whole code but I've not seen it so I am reconstructing it and can't quite get it running. Link below.
Requests, web scraping and pandas are areas where I'm not as proficent so I figure the code Jedi's on SO could help untangle or update this code.
https://medium.com/swlh/automating-your-stock-portfolio-research-with-python-for-beginners-912dc02bf1c2
Long term I'm trying to learn python by updating or building more features into tools that others have created so this is also a learning experience. So I would love you to fix it but I would more prefer you give hints and lead me towards possible solutions.
# FILENAME financial_analysis.py
# SOURCE https://medium.com/swlh/automating-your-stock-portfolio-research-with-python-for-beginners-912dc02bf1c2
import requests
import pandas as pd
def getdata(stock):
"""Company Quote Group of Items"""
company_quote = requests.get(f"https://financialmodelingprep.com/api/v3/quote/{stock}")
company_quote = company_quote.json()
share_price = float("{0:.2f}".format(company_quote[0]['price']))
# Balance Sheet Group of Items
BS = requests.get(f"https://financialmodelingprep.com/api/v3/financials/balance-sheet-statement/{stock}?period=quarter")
BS = BS.json()
# print_data = getdata(aapl)
#Total Debt
debt = float("{0:.2f}".format(float(BS['financials'][0]['Total debt'])/10**9))#Total Cash
cash = float("{0:.2f}".format(float(BS['financials'][0]['Cash and short-term investments'])/10**9))
# Income Statement Group of Items
IS = requests.get(f"https://financialmodelingprep.com/api/v3/financials/income-statement/{stock}?period=quarter")
IS = IS.json()
# Most Recent Quarterly Revenue
qRev = float("{0:.2f}".format(float(IS['financials'][0]['Revenue'])/10**9))
# Company Profile Group of Items
company_info = requests.get(f"https://financialmodelingprep.com/api/v3/company/profile/{stock}")
company_info = company_info.json()# Chief Executive Officer
ceo = company_info['profile']['ceo']
return(share_price, cash, debt, qRev, ceo)
tickers = {'AAPL', 'MSFT', 'GOOG', 'T', 'CSCO', 'INTC', 'ORCL', 'AMZN', 'FB', 'TSLA', 'NVDA'}
data = map(getdata, tickers)
df = pd.DataFrame(data,
columns=['Total Cash', 'Total Debt', 'Q3 2019 Revenue', 'CEO'],
index=tickers), print(df)
generates this error
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/IPython/core/interactiveshell.py", line 3296, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File "<ipython-input-2-d9759a746769>", line 1, in <module>
runfile('/Users/owner/sbox/Jamesmk6_3/toolbox/financial_analysis.py', wdir='/Users/owner/sbox/Jamesmk6_3/toolbox')
File "/Users/owner/Library/Application Support/JetBrains/Toolbox/apps/PyCharm-P/ch-1/193.7288.30/PyCharm.app/Contents/plugins/python/helpers/pydev/_pydev_bundle/pydev_umd.py", line 197, in runfile
pydev_imports.execfile(filename, global_vars, local_vars) # execute the script
File "/Users/owner/Library/Application Support/JetBrains/Toolbox/apps/PyCharm-P/ch-1/193.7288.30/PyCharm.app/Contents/plugins/python/helpers/pydev/_pydev_imps/_pydev_execfile.py", line 18, in execfile
exec(compile(contents+"
", file, 'exec'), glob, loc)
File "/Users/owner/sbox/Jamesmk6_3/toolbox/financial_analysis.py", line 44, in <module>
index=tickers), print(df)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/pandas/core/frame.py", line 469, in __init__
data = list(data)
File "/Users/owner/sbox/Jamesmk6_3/toolbox/financial_analysis.py", line 12, in getdata
share_price = float("{0:.2f}".format(company_quote[0]['price']))
KeyError: 0
I've dug deeper and found the dev pages but there seems to be a complication between what the author did and their docs show.