The fundamental data source I use has a different format than other stock price data, so I create a different object to get the information. The coding logic is similar to the one of getting stock price data but I use enum class to differentiate three different statements. Please check, and of course, we will add database connection later. One of the advantages of using object to get data is that we can modify them easily without affecting other parts of the code.
In [1]:
import pandas as pd
from datetime import datetime, timedelta
import requests as re
import json
import urllib as ur
from enum import Enum
In [21]:
class fundamentals(Enum):
income = "income-statement"
cash = "cash-flow-statement"
balance = "balance-sheet-statement"
class get_fundamentals():
def __init__(self, ticker, fun, output ="table"):
if not isinstance(fun, fundamentals):
raise "should input a instance of fundamentals"
self.ticker = ticker.upper()
self.fun = fun
self.output = "table"
self.result = {}
def data_bulk_output(self):
if self.output == "table":
return self.result
def get_fundamentals_data(self):
trial=0
while trial < 3:
try:
profile="https://financialmodelingprep.com/api/financials/{}/{}".format(self.fun.value,self.ticker)
temp = re.get(profile,verify=False).text
temp=temp.replace("\n","")
temp = temp.replace("<pre>","")
temp= json.loads(temp)
temp = pd.DataFrame(temp[self.ticker])
self.result[self.ticker] =temp
return temp
except Exception as e:
print e
trial+=1
In [31]:
my = fundamentals.cash
foo = get_fundamentals("AAPL",my)
foo.get_fundamentals_data()
Out[31]: