Getting Fundamental Data

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.




Get Fundamental Data







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]:
Basic Cost of revenue Diluted EBITDA Gross profit Income before taxes Interest Expense Net income Net income available to common shareholders Net income from continuing operations Operating income Other income (expense) Provision for income taxes Research and development Revenue Sales, General and administrative Total operating expenses
2013-09 6477 106606 6522 57048 64304 50155 136 37037 37037 37037 48999 1292 13118 4475 170910 10830 15305
2014-09 6086 112258 6123 61813 70537 53483 384 39510 39510 39510 52503 1364 13973 6041 182795 11993 18034
2015-09 5753 140089 5793 84505 93626 72515 733 53394 53394 53394 71230 2018 19121 8067 233715 14329 22396
2016-09 5471 131376 5500 73333 84263 61372 1456 45687 45687 45687 60024 2804 15685 10045 215639 14194 24239
2017-09 5217 141048 5252 76569 88186 64089 2323 48351 48351 48351 61344 5068 15738 11581 229234 15261 26842
TTM 5171 147254 5209 79386 91922 66939 2532 50525 50525 50525 64259 5212 16414 12117 239176 15546 27663