def fin_ratios_api(tick): """ reach out to the fin_ratios API on FMP """ # get fin ratios url = DATA_MAP['fin_ratios'][1][0].format(tick) raw = requests.get(url).content data = json.loads(raw)['ratios'] data_rows = [] for report in data: data_dict = {} data_dict['date'] = report['date'] # loop through sections for section in list(report.keys())[1:]: for item in report[section].keys(): data_dict[item] = report[section][item] data_rows.append(data_dict) data = pd.DataFrame.from_dict(data_rows, orient='columns') # data = data.set_index('Ratios').transpose().reset_index() data['month'] = data['date'].str.slice(5, 7) data['year'] = data['date'].str.slice(0, 4) data['tick'] = tick data = data.drop('date', axis=1) data = data.set_index(['tick', 'year', 'month']) data.columns = map_columns("fin_ratios", list(data.columns.values)) try: url = DATA_MAP['fin_ratios'][1][1].format(tick) raw = requests.get(url).content data_cm = pd.DataFrame(json.loads(raw)['metrics']) data_cm['year'] = data_cm['date'].str.slice(0, 4) data_cm['month'] = data_cm['date'].str.slice(5, 7) data_cm['tick'] = tick data_cm = data_cm.drop('date', axis=1) data_cm = data_cm.set_index(['tick', 'year', 'month']) data_cm.columns = map_columns("fin_ratios", list(data_cm.columns.values)) except Exception as exc: print(exc) print("May be an etf where this isnt applicable") return data = data.drop([ 'curr_ratio', 'roe', 'debt_to_equity', 'pb_ratio', 'fcf_per_share', 'pfcf_ratio', 'pe_ratio', 'div_yield', 'cash_per_share', 'receivables_turnover', 'days_of_inv_on_hand', 'payout_ratio', 'days_payables_outstanding', 'inv_turnover', 'ps_ratio', 'days_sales_outstanding', 'oper_cf_per_share', 'pocf_ratio', 'payables_turnover', 'receivables_turnover' ], axis=1) data = pd.merge(data, data_cm, left_index=True, right_index=True) send_to_db(data, 'fin_ratios', ['tick', 'year', 'month'])
def cf_statement_api(tick): """ reach out to the Cash Flow statement API on FMP """ url = DATA_MAP['cf_statement'][1].format(tick) try: raw = requests.get(url).content while True: if "Bad Gateway" in str(raw): print("{} Bad Gateway - sleeping for 1 second".format(tick)) time.sleep(1) raw = requests.get(url).content else: break data = json.loads(raw)['financials'] data = pd.DataFrame(data) if data.empty: print("No data from API for {}".format(tick)) return except pd.errors.EmptyDataError as exc: print(exc) print("May be an etf where this isnt applicable") raise data['month'] = data['date'].str.slice(5, 7) data['year'] = data['date'].str.slice(0, 4) data = data.drop('date', axis=1) data['tick'] = tick data = data.set_index(['tick', 'year', 'month']) data.columns = map_columns("cf_statement", list(data.columns.values)) send_to_db(data, 'cf_statement', ['tick', 'year', 'month'])
def inc_statement_api(tick): """ reach out to the incone statement API on FMP """ url = DATA_MAP['inc_statement'][1].format(tick) try: raw = requests.get(url).content data = json.loads(raw)['financials'] data = pd.DataFrame(data) except pd.errors.EmptyDataError as exc: print(exc) print("May be an etf where this isnt applicable") raise data['month'] = data['date'].str.slice(5, 7) data['year'] = data['date'].str.slice(0, 4) data = data.drop('date', axis=1) data['tick'] = tick data = data.set_index(['tick', 'year', 'month']) data.columns = map_columns("inc_statement", list(data.columns.values)) send_to_db(data, 'inc_statement', ['tick', 'year', 'month'])