def get_data_from_one_year(company_name, year): stock = Stock(company_name) data = [] for i in range(1, 5): filing = stock.get_filing(period='quarterly', year=year, quarter=i) try: income_statement = filing.get_income_statements().reports[0].map #balance_sheet = filing.get_balance_sheets().reports[0].map #cash_flow = filing.get_cash_flows().reports[0].map except: continue one_quarter_data = income_statement if filing.get_balance_sheets(): balance_sheet = filing.get_balance_sheets().reports[0].map one_quarter_data.update(balance_sheet) if filing.get_cash_flows(): cash_flow = filing.get_cash_flows().reports[0].map one_quarter_data.update(cash_flow) #one_quarter_data.update(balance_sheet) #one_quarter_data.update(cash_flow) # trick to maintain the same structure of the data my_date_obj = namedtuple('MyStruct', 'value') date = my_date_obj(value=f"{year}_{i}") one_quarter_data["date"] = date data.append(one_quarter_data) return data
def test_get_cash_flows(): stock = Stock(symbol='SPWR') filing = stock.get_filing(period='quarterly', year=2018, quarter=4) result = filing.get_cash_flows() print(FinancialReportEncoder().encode(result)) # for easy QA using JSON # ensure certain data points are correct profit_loss = result.reports[0].map['us-gaap_ProfitLoss'].value assert profit_loss == -745351000.0
def test_get_statement_of_financial_position(): # synonymous with balance sheet stock = Stock(symbol='IBM') filing = stock.get_filing(period='annual', year=2018, quarter=1) result = filing.get_balance_sheets() print(FinancialReportEncoder().encode(result)) # for easy QA using JSON # ensure certain data points are correct assets = result.reports[0].map['us-gaap_Assets'].value assert assets == 125356000000.0
def test_get_balance_sheets(): stock = Stock(symbol='SPWR') filing = stock.get_filing(period='quarterly', year=2018, quarter=4) result = filing.get_balance_sheets() print(FinancialReportEncoder().encode(result)) # for easy QA using JSON # ensure certain data points are correct assets = result.reports[0].map['us-gaap_Assets'].value assert assets == 3126117000.0
def test_get_statement_of_earnings(): # synonymous with income statement stock = Stock(symbol='IBM') filing = stock.get_filing(period='annual', year=2018, quarter=1) result = filing.get_income_statements() print(FinancialReportEncoder().encode(result)) # for easy QA using JSON # ensure certain data points are correct revenue = result.reports[0].map['us-gaap_Revenues'].value assert revenue == 79139000000.0
def test_get_income_statements(): stock = Stock(symbol='AAPL') filing = stock.get_filing(period='quarterly', year=2016, quarter=1) result = filing.get_income_statements() print(FinancialReportEncoder().encode(result)) # for easy QA using JSON # ensure certain data points are correct revenue = result.reports[0].map['us-gaap_SalesRevenueNet'].value assert revenue == 75872000000.0
def test_get_filing_no_filing_found_exception(): stock = Stock(symbol='FB') try: # IPO was in 2012 filing = stock.get_filing(period='quarterly', year=2011, quarter=1) assert False except NoFilingInfoException: assert True
def get_data_from_one_year(company_name, year): stock = Stock(company_name) one_year_income_statements = [] one_year_balance_sheets = [] one_year_cash_flows = [] for i in range(1, 5): filing = stock.get_filing(period='quarterly', year=year, quarter=i) income_statements = filing.get_income_statements().reports balance_sheets = filing.get_balance_sheets().reports cash_flows = filing.get_cash_flows().reports income_statement = None for report in income_statements: # choose the report from the correct year, covering only 3 months (not cumulative) report_year = int(report.date.year) report_period = int(report.months) # this condition can be relaxed later if report_year == year and report_period == 3: income_statement = report.map income_statement['exact_date'] = report.date income_statement['period'] = 3 break one_year_income_statements.append(income_statement) balance_sheet = None for report in balance_sheets: report_year = int(report.date.year) if report_year == year: balance_sheet = report.map balance_sheet['exact_date'] = report.date balance_sheet['period'] = None break one_year_balance_sheets.append(balance_sheet) cash_flow = None for report in cash_flows: report_year = int(report.date.year) report_period = int(report.months) if report_year == year: cash_flow = report.map cash_flow['exact_date'] = report.date cash_flow['period'] = report_period break one_year_cash_flows.append(cash_flow) return { "income_statements": one_year_income_statements, "balance_sheets": one_year_balance_sheets, "cash_flows": one_year_cash_flows }
def getData(ticket, ty, ignored_cmp, period='annual', year=2018, quarter=0): # period = 'annual' # or 'quarterly', which is the default # year = 2018 # can use default of 0 to get the latest # quarter = 1 # 1, 2, 3, 4, or default value of 0 to get the latest try: stock = Stock(ticket) filing = stock.get_filing(period, year, quarter) # financial reports (contain data for multiple years) if ty == 'income_statements': statements = filing.get_income_statements() elif ty == "balance_sheets": statements = filing.get_balance_sheets() elif ty == "cash_flows": statements = filing.get_cash_flows() jsonstr = FinancialReportEncoder().encode(statements) data = json.loads(jsonstr) # print(data.keys()) # dict_keys(['company', 'date_filed', 'reports']) listreports = data['reports'] columns = [] data = [] map_keys = [] for report in listreports: # print(report.keys()) # dict_keys(['date', 'months', 'map']): string, int, dict for report_key in report.keys(): if report_key == 'map': for map_key in report['map'].keys(): map_keys.append(map_key) # print(map_key, report['map'][map_key].keys()) # dict_keys(['label', 'value']) for key in report['map'][map_key].keys(): # print(key, report['map'][map_key][key]) if key == 'label': columns.append(report['map'][map_key][key]) else: data.append(report['map'][map_key][key]) sec_data = pd.DataFrame([data], columns=columns) sec_data = sec_data.transpose() sec_data.reset_index(inplace=True) sec_data['ind'] = np.arange(len(sec_data)) sec_data.set_index("ind", inplace=True) map_data = pd.Series(map_keys) return sec_data, True, ignored_cmp except: print('Invalid company input', cmp, ty, period, year, quarter) ignored_cmp.append(cmp) return None, False, ignored_cmp
# # # # In[18]: # # cik_not_found = [] year_not_found = [] income_statements_error = [] balance_sheets_error = [] cash_flows_error = [] for y in range(2017, 2018): for k in stock_list: try: stock = Stock(k) except: cik_not_found.append(k) continue period = 'quarterly' try: filing = stock.get_filing('annual', y, 1) except: year_not_found.append([k, y]) try: income_statements = filing.get_income_statements() newdict_income_statements = income_statements.reports[0] except: income_statements_error.append(k)
def filing(cikList): # This is inspired from examples # This uses the full version of the library, not the lite version, therefore not using it from edgar.stock import Stock import datetime tickers = [] '''with open('../data/spy500 - test.csv') as csv_file: csv_reader = csv.reader(csv_file, delimiter=',') for row in csv_reader: tickers.append(row[0])''' tickers = ['XYL'] print(tickers) for tuple in cikList: print('********************************') print('CIK: %s' % tuple[0]) print('URL: %s' % tuple[1]) try: # stock = Stock(ticker) stock = Stock(cik=tuple[0].lstrip('0')) except IndexError as e: print(e) continue period = 'quarterly' # or 'annual', which is the default year = 0 # can use default of 0 to get the latest quarter = 0 # 1, 2, 3, 4, or default value of 0 to get the latest # using defaults to get the latest annual, can simplify to stock.get_filing() # Get CIK from .csv, get 10-K / 10-Q / file link from master.idx lookup, process .txt try: filing = stock.get_filing(period, year, quarter, filing_url=tuple[1]) except Exception as e: print(e) continue # financial reports (contain data for multiple years) try: income_statements = filing.get_income_statements() except Exception as e: print(e) if str(e) == 'list index out of range': continue # balance_sheets = filing.get_balance_sheets() # cash_flows = filing.get_cash_flows() labelDict = { 'revenue': [ 'us-gaap_RevenueFromContractWithCustomerExcludingAssessedTax', 'us-gaap_RevenueFromContractWithCustomerIncludingAssessedTax', 'us-gaap_Revenues' ], 'netIncome': [ 'us-gaap_NetIncomeLoss', 'us-gaap_NetIncomeLossAvailableToCommonStockholdersBasicAbstract', 'us-gaap_NetIncomeLossAvailableToCommonStockholdersBasic', '' ], 'epsBasic': [ 'us-gaap_EarningsPerShareBasic', 'us-gaap_EarningsPerShareBasicAndDiluted', 'us-gaap_IncomeLossFromContinuingOperationsPerBasicShare' ], 'epsDiluted': [ 'us-gaap_EarningsPerShareDiluted', 'us-gaap_EarningsPerShareBasicAndDiluted', 'us-gaap_IncomeLossFromContinuingOperationsPerDilutedShare' ] } for key in labelDict: for label in labelDict[key][:]: if label not in income_statements.reports[0].map and ('defref_' + label) not in \ income_statements.reports[ 0].map: labelDict[key].remove(label) for report in income_statements.reports: print(report.date) print("Last %d months" % report.months) for key in labelDict: print(key + ':') for label in labelDict[key]: print(label) try: print(report.map[label]) except KeyError as e: print(e) continue
def test_init_unknown_symbol(): try: Stock(symbol='ZZZZZZZZZZZZZZZ') assert False except IndexError: assert True
def test_get_filing(): stock = Stock(symbol='AAPL') filing = stock.get_filing(period='quarterly', year=2016, quarter=1) assert True
def test_init(): stock = Stock(symbol='AAPL') assert stock.symbol == 'AAPL' assert stock.cik == '320193'
from edgar.stock import Stock stockAlexion = Stock('ALXN') stockRegeneron = Stock('REN') stockVertex = Stock('VRTX') stockBiomarin = Stock('BMRN') stockIncyte = Stock('INCY') period = 'quarterly' filingAlexion2018Q4 = stockAlexion.get_filing(period, 2018, 4) filingAlexion2019Q1 = stockAlexion.get_filing(period, 2019, 1) filingAlexion2019Q2 = stockAlexion.get_filing(period, 2019, 2) filingAlexion2019Q3 = stockAlexion.get_filing(period, 2019, 3) filingAlexion2019Q4 = stockAlexion.get_filing(period, 2019, 4) filingAlexion2020Q1 = stockAlexion.get_filing(period, 2020, 1) bSheetAlexion2018Q4 = filingAlexion2018Q4.get_balance_sheets() bSheetAlexion2019Q1 = filingAlexion2019Q1.get_balance_sheets() bSheetAlexion2019Q2 = filingAlexion2019Q2.get_balance_sheets() bSheetAlexion2019Q3 = filingAlexion2019Q3.get_balance_sheets() bSheetAlexion2019Q4 = filingAlexion2019Q4.get_balance_sheets() bSheetAlexion2020Q1 = filingAlexion2020Q1.get_balance_sheets() filingRegeneron2018Q4 = stockRegeneron.get_filing(period, 2018, 4) filingRegeneron2019Q1 = stockRegeneron.get_filing(period, 2019, 1) filingRegeneron2019Q2 = stockRegeneron.get_filing(period, 2019, 2) filingRegeneron2019Q3 = stockRegeneron.get_filing(period, 2019, 3) filingRegeneron2019Q4 = stockRegeneron.get_filing(period, 2019, 4) filingRegeneron2020Q1 = stockRegeneron.get_filing(period, 2020, 1)