def calc_ebita(tickers): print('Fetching EBITA Data from Yahoo for 100 Tickers...') total_data = {} for index, ticker in enumerate(tickers): if index % 1 == 0: print('Fetching EBITA for %d out of %d' % (index, len(tickers))) try: yahoo_financials = YahooFinancials(ticker) total_data[ticker] = yahoo_financials.get_ebit() except KeyError: print('Could not calc. PE for %s' % ticker) total_data[ticker] = None continue return total_data
def fin_metrics(symbols): """symbols = tickers of the universe of required number of stocks""" #sort the list of the symbols and assign it to tickers tickers = sorted(symbols) #create the YahooFinancials object as records records = YahooFinancials(tickers) #get the financial statements statements = records.get_financial_stmts('annual', ['income', 'cash', 'balance']) #get the ebit for each ticker from the financials ebit = records.get_ebit() #get the previous stock prices for each stock share_pr = records.get_prev_close_price() #create an empty dataframe to hold the final result results_df = pd.DataFrame() #create empty lists to hold the metrics e_yield = [] ebita = [] fcfy = [] roce = [] b2m = [] #get the firt ticker in the list of tickers first_sym = tickers[0] #create the indexes for the dataframe of the various financial statments bs_index = statements['balanceSheetHistory'][first_sym][0].values( )[0].keys() is_index = statements['incomeStatementHistory'][first_sym][0].values( )[0].keys() cf_index = statements['cashflowStatementHistory'][first_sym][0].values( )[0].keys() #create the empty dataframes to hold the various financial statements bs_df = pd.DataFrame(index=bs_index) is_df = pd.DataFrame(index=is_index) cf_df = pd.DataFrame(index=cf_index) for s in symbols: #get the components of the various financial statements bs_result = pd.DataFrame( statements['balanceSheetHistory'][s][0].values()).T is_result = pd.DataFrame( statements['incomeStatementHistory'][s][0].values()).T cf_result = pd.DataFrame( statements['cashflowStatementHistory'][s][0].values()).T #put the components of the various financial statement in the empty dataframes created for them is_df = pd.concat([is_df, is_result], axis=1, join='outer') bs_df = pd.concat([bs_df, bs_result], axis=1, join='outer') cf_df = pd.concat([cf_df, cf_result], axis=1, join='outer') #create a list of the dataframes of the various financial statemenmts frames = [bs_df, is_df, cf_df] #concatenate the dataframe of the various financial statements into a single dataframe all_fins = pd.concat(frames).fillna(0) #Assign the list of tickers as the label of the single dataframe columns all_fins.columns = tickers for t in all_fins: #compute the required metrics and assign them to the dataframe created for them ev = all_fins.loc['commonStock'][t] * share_pr[t] + all_fins.loc[ 'shortLongTermDebt'][t] - all_fins.loc['cash'][t] y = ebit[t] / float(ev) fcff = all_fins.loc['totalCashFromOperatingActivities'][ t] - all_fins.loc['capitalExpenditures'][t] fcfs = float(fcff) / all_fins.loc['commonStock'][t] fcf_yield = float(fcfs) / share_pr[t] ce = all_fins.loc['totalAssets'][t] - all_fins.loc[ 'totalCurrentLiabilities'][t] roc_employed = float(ebit[t]) / ce cse = all_fins.loc['totalStockholderEquity'][t] - all_fins.loc[ 'otherStockholderEquity'][t] b2_market = float(cse) / all_fins.loc['commonStock'][t] * share_pr[t] fcfy.append(fcf_yield) e_yield.append(y) ebita.append(ebit[t]) roce.append(roc_employed) b2m.append(b2_market) results_df['EBITA'] = ebita results_df['Earnings Yield'] = e_yield results_df['Free Cash Flow Yield'] = fcfy results_df['Return on Capital Employed'] = roce results_df['Book to Market'] = b2m results_df.index = tickers return results_df
import yahoo-finance from yahoo_finance import Share yahoo = Share('YHOO') yahoo.get_price() >>> print yahoo.get_open() yahoo_finance.Share("SBI.NSE") symbol = yahoo_finance.Share("GOOG") google_data = symbol.get_historical("2019-06-01", "2019-06-30") google_df = pd.DataFrame(google_data) # Output data into CSV google_df.to_csv("/home/username/google_stock_data.csv") # pip install yahoofinancials from yahoofinancials import YahooFinancials tech_stocks = ['AAPL', 'MSFT', 'INTC'] bank_stocks = ['WFC', 'BAC', 'C'] yahoo_financials_tech = YahooFinancials(tech_stocks) yahoo_financials_banks = YahooFinancials(bank_stocks) yahoo_financials_tech ech_cash_flow_data_an = yahoo_financials_tech.get_financial_stmts('annual', 'cash') bank_cash_flow_data_an = yahoo_financials_banks.get_financial_stmts('annual', 'cash') banks_net_ebit = yahoo_financials_banks.get_ebit() tech_stock_price_data = yahoo_financials_tech.get_stock_price_data() daily_bank_stock_prices = yahoo_financials_banks.get_historical_price_data('2008-09-15', '2018-09-15', 'daily') daily_bank_stock_prices #https://pypi.org/project/yahoofinancials/