def comparable_roe(tickers): all_roes = [] for t in tickers: yahoo_financials = YahooFinancials(t) b_s = yahoo_financials.get_financial_stmts('quarterly', 'balance') b_s = json_normalize(b_s) i_s = yahoo_financials.get_financial_stmts('quarterly', 'income') i_s = json_normalize(i_s) roes = [] for i in range(4): balance = pd.DataFrame.from_dict(b_s.loc[0][0][i]) income_s = pd.DataFrame.from_dict(i_s.loc[0][0][i]) roe = income_s.loc['netIncome'] / balance.loc[ 'totalStockholderEquity'] roes.append(float(roe)) dates = list(pd.DataFrame.from_dict(i_s.loc[0][0]).columns) frame = pd.DataFrame({'ticker': [t] * 4, 'roe': roes, 'date': dates}) all_roes.append(frame) frame = pd.concat(all_roes) frame['date'] = pd.to_datetime(frame['date']) ax = sns.lineplot(x="date", y="roe", hue="ticker", data=frame)
def update_graph_interactive_image(value): value, label, stock = int(value.split("$$$$$")[1]), value.split( "$$$$$")[0], value.split("$$$$$")[2] if value == 0: yahoo_financials = YahooFinancials(stock) input = yahoo_financials.get_financial_stmts('annual', 'income') htmls = json2html.convert( json=input, table_attributes="id=\"info-table\" class=\"table-inverse\"") uc = dash_dangerously_set_inner_html.DangerouslySetInnerHTML( str(htmls)) elif value == 1: yahoo_financials = YahooFinancials(stock) input = yahoo_financials.get_financial_stmts('annual', 'balance') htmls = json2html.convert( json=input, table_attributes="id=\"info-table\" class=\"table-inverse\"") uc = dash_dangerously_set_inner_html.DangerouslySetInnerHTML( str(htmls)) elif value == 2: yahoo_financials = YahooFinancials(stock) input = yahoo_financials.get_financial_stmts('quarterly', 'cash') htmls = json2html.convert( json=input, table_attributes="id=\"info-table\" class=\"table-inverse\"") uc = dash_dangerously_set_inner_html.DangerouslySetInnerHTML( str(htmls)) elif value == 3: yahoo_financials = YahooFinancials(stock) input = yahoo_financials.get_stock_quote_type_data() htmls = json2html.convert( json=input, table_attributes="id=\"info-table\" class=\"table-inverse\"") uc = dash_dangerously_set_inner_html.DangerouslySetInnerHTML( str(htmls)) print(uc) return [ html.Div([ html.Div(html.H5(label + " Report of " + stock), style={"padding": "2%"}), html.Div([uc], style={ "overflow-x": "auto", "overflow-y": "auto", "height": "750px" }) ]) ]
def get_incomeStatement(self): companyData = YahooFinancials(self.ticker) stmts = companyData.get_financial_stmts('annual', 'income') stmtsValues = stmts[list(stmts.keys())[0]][self.ticker] tableName = list(stmts.keys())[0] allStmtsConverted = self._convert(stmtsValues) return (allStmtsConverted[::-1], tableName)
def get_cashFlowQ(self): companyData = YahooFinancials(self.ticker) stmts = companyData.get_financial_stmts('quarterly', 'cash') stmtsValues = stmts[list(stmts.keys())[0]][self.ticker] tableName = list(stmts.keys())[0] allStmtsConverted = self._convert(stmtsValues) return (allStmtsConverted[::-1], tableName)
def fundamentals(tickers, begin="2020-05-26", end="2020-06-26",): format_header = '{:>6} {:>6} {:>6} {:>6} {:>6} {:>6} {:>6} {:>6} {:>6} {:>6} {:>10} {:>10}' format_numbers = '{:>6} {:>6} {:>6} {:>6} {:>6} {:>6} {:>6} {:>6} {:>6} {:>6} {:>10.2e} {:>10.2E}' print(format_header.format('ticker', 'P/E', 'EARNSH', 'BK/PR', 'DY', 'DR', 'VAR', 'PEG', 'PS', 'PCI', 'VOLPR', 'CM',)) for ticker in tickers: yf = YF(ticker) try: pe = get_number_for_None(get_number_for_None(yf.get_pe_ratio())) prices = yf.get_historical_price_data(begin, end, "daily")[ticker]['prices'] stat_pr = calc_statistics(prices) var_pr = get_number_for_None(100 * stat_pr[1] / stat_pr[0]) volume = get_number_for_None(stat_pr[4]) es = get_number_for_None(yf.get_earnings_per_share()) sh = get_number_for_None(yf.get_num_shares_outstanding(price_type='current')) ''' "pegRatio" "priceToBook" "heldPercentInstitutions" ''' statistics = yf.get_key_statistics_data()[ticker] summary = yf.get_summary_data()[ticker] peg = get_number_for_None(statistics["pegRatio"]) PCI = get_number_for_None(statistics["heldPercentInstitutions"]) bv = yf.get_key_statistics_data()[ticker]['bookValue'] pr = yf.get_current_price() if pr is not None and bv is not None: pr_bv = get_number_for_None(pr/bv) else: pr_bv = '-99.99' f_s = yf.get_financial_stmts('annual', 'income')['incomeStatementHistory'][ticker][0] f_s_key = list(f_s.keys())[0] totalRevenue = f_s[f_s_key]['totalRevenue'] outstanding = statistics["sharesOutstanding"] rev_per_share = totalRevenue / outstanding if pr is not None and es is not None: p_s = get_number_for_None(rev_per_share/float(es)) else: p_s = '99' dy = get_number_for_None(yf.get_dividend_yield()) dr = get_number_for_None(yf.get_dividend_rate()) volume10days = summary['averageVolume10days'] marketCap = summary['marketCap'] # float(volume)*pr # float(sh)*pr) print(format_numbers.format(ticker, pe, es, pr_bv, dy, dr, var_pr, peg, p_s, PCI, volume10days, marketCap)) except Exception as e: print(ticker, e)
def create_retrieve_thread(tickers, metric, file_name, data_dict, batch_no): start_loop = time.time() print( f"Batch/thread {batch_no + 1}: Tickers to be retrieved are: {tickers}") yahoo_financials = YahooFinancials(tickers) if metric == "balance": print(f"Retrieving annual balance sheets from Yahoo Finance...") financial_statement = yahoo_financials.get_financial_stmts( 'annual', 'balance')['balanceSheetHistory'] elif metric == "income": print( f"Retrieving annual income statement history from Yahoo Finance..." ) financial_statement = yahoo_financials.get_financial_stmts( 'annual', 'income')['incomeStatementHistory'] elif metric == "cap": print(f"Retrieving market cap information from Yahoo Finance...") financial_statement = yahoo_financials.get_market_cap() else: print("Metric entered is not recognized.") financial_statement = {} dict_lock.acquire() data_dict.update(financial_statement) end_loop = time.time() print(f"Saving batch {batch_no + 1} to JSON file...") json.dump(data_dict, open(file_name + '.json', 'w')) dict_lock.release() print( f"Time elapsed for batch {batch_no + 1}: {end_loop - start_loop}, metric: {metric}" ) print()
def get_balance_sheets(tickers): """ Get yearly balance sheets :param tickers: List of tickers :return: """ print('Fetching Yearly Balance Data from Yahoo for 100 Tickers...') total_data = {} for index, ticker in enumerate(tickers): if index % 1 == 0: print('Fetching Yearly Balance Data for %d out of %d' % (index, len(tickers))) yahoo_financials = YahooFinancials(ticker) total_data[ticker] = \ yahoo_financials.get_financial_stmts('quaterly', 'balance')['balanceSheetHistoryQuarterly'][ticker] return total_data
def calc_fcf(tickers): print('Fetching Free Cash Flow Yield Data from Yahoo for 100 Tickers...') total_data = {} for index, ticker in enumerate(tickers): if index % 1 == 0: print('Fetching Free Cash Flow Yield Data for %d out of %d' % (index, len(tickers))) try: yahoo_financials = YahooFinancials(ticker) total_data[ticker] = yahoo_financials.get_financial_stmts( 'quarterly', 'balance') except KeyError: print('Could not fetch Free Cash Flow Yield for %s' % ticker) total_data[ticker] = None continue return total_data
def stock_statements(tickers, frequency, statement_type): # data_list = [] # for ticker in tickers: yahoo_financials = YahooFinancials(ticker) ass = yahoo_financials.get_financial_stmts(frequency, statement_type, reformat=True) if statement_type is 'cash' and frequency is 'quarterly': data = ass['cashflowStatementHistoryQuarterly'] if statement_type is 'cash' and frequency is 'annual': data = ass['cashflowStatementHistory'] if statement_type is 'income' and frequency is 'quarterly': data = ass['incomeStatementHistoryQuarterly'] if statement_type is 'income' and frequency is 'annual': data = ass['incomeStatementHistory'] if statement_type is 'balance' and frequency is 'quarterly': data = ass['balanceSheetHistoryQuarterly'] if statement_type is 'balance' and frequency is 'annual': data = ass['balanceSheetHistory'] return data
def financials(): # financeform = CheckFinancials() # return render_template('finance.html', title='Finance', form=financeform) ticker = 'AMZN' yahoo_financials = YahooFinancials(ticker) balance_sheet_data_qt = yahoo_financials.get_financial_stmts( 'quarterly', 'balance') appended_data = [] indices = [] # for item in balance_sheet_data_qt['balanceSheetHistoryQuarterly']['AMZN']: # # print(item.keys()) # col = next(iter(item)) # indices = item[col].keys() # values = item[col].values() # interim = {col: values} # print(interim) # appended_data.append(interim) #interim = pd.DataFrame({col: values}, index=indices) #pd.concat([d, interim], axis=1, sort=False) #d.append(pd.DataFrame(item)) #print(d) #d = pd.DataFrame(data=appended_data, index=indices) #print(d) #d = pd.DataFrame(data=balance_sheet_data_qt['balanceSheetHistoryQuarterly']['AMZN']) d = {'col1': [1, 2], 'col2': [3, 4]} df = pd.DataFrame(data=d) return render_template("dataframe.html", name="dataframe", data=df.to_html())
from app import app from yahoofinancials import YahooFinancials ticker = 'AMZN' yahoo_financials = YahooFinancials(ticker) balance_sheet_data_qt = yahoo_financials.get_financial_stmts('quarterly', 'balance') for item in balance_sheet_data_qt['balanceSheetHistoryQuarterly']['AMZN']: print(item.keys())
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/
'''Work in progress''' #TO DO: # Check what method to call # pool the record of dividends # perform operation to detemine range. (current div - ave 20 years) from yahoofinancials import YahooFinancials #data2 = d._scrape_data('https://finance.yahoo.com/quote/appl','','financials') THIS MAY BE A WAY TO ACCESS DIVIDENS company = 'AAPL' yahoo_financials = YahooFinancials(company) data = yahoo_financials.get_financial_stmts('annual', 'income') print(f'{data} \n\n') # uncoment to see how data is structured # Organizing Data. # Note to self: You will use this data to evaluate if the company is an OPM Addict annual_statements = data["incomeStatementHistory"][ company] #a list containing dictionaries print(f'4 YEARS OF INCOME STATEMETS FOR {company}\n') for index in range( len(annual_statements)): #looping over each yearly statement for date in annual_statements[ index]: # this loops over each key in my dictionary print(date) for accounting_item, value in annual_statements[index][date].items( ): #loops over each of the accounting item of the Financial Statement (each item is the key of a dictionary) print(f"\t {accounting_item}: {value}")
def get_financials(self, symbol): ssl._create_default_https_context = ssl._create_unverified_context yahoo_financials = YahooFinancials(symbol) print(yahoo_financials.get_financial_stmts('quarterly', 'income'))
class TestModule(TestCase): def setUp(self): self.test_yf_stock_single = YahooFinancials('C') self.test_yf_stock_multi = YahooFinancials(stocks) self.test_yf_treasuries_single = YahooFinancials('^IRX') self.test_yf_treasuries_multi = YahooFinancials(us_treasuries) self.test_yf_currencies = YahooFinancials(currencies) # Fundamentals Test def test_yf_fundamentals(self): # Single stock test single_balance_sheet_data_qt = self.test_yf_stock_single.get_financial_stmts( 'quarterly', 'balance') single_income_statement_data_qt = self.test_yf_stock_single.get_financial_stmts( 'quarterly', 'income') single_all_statement_data_qt = self.test_yf_stock_single.get_financial_stmts( 'quarterly', ['income', 'cash', 'balance']) # Multi stock test multi_balance_sheet_data_qt = self.test_yf_stock_multi.get_financial_stmts( 'quarterly', 'balance') multi_income_statement_data_qt = self.test_yf_stock_multi.get_financial_stmts( 'quarterly', 'income') multi_all_statement_data_qt = self.test_yf_stock_multi.get_financial_stmts( 'quarterly', ['income', 'cash', 'balance']) # Single stock check result = check_fundamental(single_balance_sheet_data_qt, 'bal') self.assertEqual(result, True) result = check_fundamental(single_income_statement_data_qt, 'inc') self.assertEqual(result, True) result = check_fundamental(single_all_statement_data_qt, 'all') self.assertEqual(result, True) # Multi stock check result = check_fundamental(multi_balance_sheet_data_qt, 'bal') self.assertEqual(result, True) result = check_fundamental(multi_income_statement_data_qt, 'inc') self.assertEqual(result, True) result = check_fundamental(multi_all_statement_data_qt, 'all') self.assertEqual(result, True) # Historical Price Test def test_yf_historical_price(self): single_stock_prices = self.test_yf_stock_single.get_historical_price_data( '2015-01-15', '2017-10-15', 'weekly') expect_dict = { 'high': 49.099998474121094, 'volume': 125737200, 'formatted_date': '2015-01-12', 'low': 46.599998474121094, 'adjclose': 45.669029235839844, 'date': 1421038800, 'close': 47.61000061035156, 'open': 48.959999084472656 } self.assertDictEqual(single_stock_prices['C']['prices'][0], expect_dict) # Extra Module Methods Test def test_yf_module_methods(self): # Stocks if isinstance(self.test_yf_stock_single.get_current_price(), float): self.assertEqual(True, True) else: self.assertEqual(False, True) if isinstance(self.test_yf_stock_single.get_net_income(), int): self.assertEqual(True, True) else: self.assertEqual(False, True) # Treasuries if isinstance(self.test_yf_treasuries_single.get_current_price(), float): self.assertEqual(True, True) else: self.assertEqual(False, True)
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
''' ticker = 'AAPL' yahoo_financials = YahooFinancials(ticker) stock_info={ 'marketCap': 0 , 'price_c':0, 'i_rate':0, 'long_debt':0, 'short_debt':0, 'ROE':0, 'num_shares':0, } stock_summary = yahoo_financials.get_stock_summary_data() stock_info['num_shares']=stock_summary[ticker]['marketCap']/stock_summary[ticker]['previousClose'] stock_info['price_c']=172.23 stock_info['marketCap']=stock_info['price_c']*stock_info['num_shares'] balance_sheet_data_qt = yahoo_financials.get_financial_stmts('quarterly', 'balance') income_statement_data_qt = yahoo_financials.get_financial_stmts('quarterly', 'income') all_statement_data_qt = yahoo_financials.get_financial_stmts('quarterly', ['income', 'cash', 'balance']) earnings_data = yahoo_financials.get_stock_earnings_data() net_income = yahoo_financials.get_net_income() historical_stock_prices = yahoo_financials.get_historical_stock_data('2018-01-01', '2018-04-20', 'daily')
except Exception as e: print(e) plt.close('all') if __name__ == '__main__': # print(yahoo_financials) # # tech_stocks = ['AAPL', 'MSFT', 'INTC'] # yahoo_financials_tech = YahooFinancials(tech_stocks) # tech_cash_flow_data_an = yahoo_financials_tech.get_financial_stmts('annual', 'cash') # for s in tech_cash_flow_data_an["cashflowStatementHistory"]: # for date in tech_cash_flow_data_an["cashflowStatementHistory"][s][0]: # print(s, date, tech_cash_flow_data_an["cashflowStatementHistory"][s][0][date]) # make_USDKRW_graph() # make_USDKRWKOSPI_graph() yahoo_financials = YahooFinancials('AACG') bs1 = yahoo_financials.get_financial_stmts('quarter', 'balance') bs2 = yahoo_financials.get_financial_stmts('annual', 'balance') cf1 = yahoo_financials.get_financial_stmts('quarter', 'cash') cf2 = yahoo_financials.get_financial_stmts('annual', 'cash') # print(yahoo_financials.get_financial_stmts('quarter', 'balance')) # for k1 in bs1["balanceSheetHistoryQuarterly"]["AACG"]: # print(k1) # for k2 in bs2["balanceSheetHistory"]["AACG"]: # print(k2) for k1 in cf1["cashflowStatementHistoryQuarterly"]["AACG"]: print(k1) for k2 in cf2["cashflowStatementHistory"]["AACG"]: print(k2)
class Company(object): def __init__(self, ticker): self.ticker = ticker.upper() self.financials = YahooFinancials(ticker) self.timeframe = 'annual' self.balance_sheet = None self.income_statement = None self.cash_flow_statement = None self.key_statistics = None self.historical_eps = None self.price_data = None self.new_corporate_tax_rate = 0.21 self.old_corporate_tax_rate = 0.35 self.risk_free_ror = 0.025 self.market_ror = 0.098 def __set_balance_sheet(self): if str(type(self.balance_sheet)) == "<class 'NoneType'>": try: self.balance_sheet = self.financials.get_financial_stmts( self.timeframe, 'balance')['balanceSheetHistory'][self.ticker] self.balance_sheet = self.__clean_statement_timestamp( self.balance_sheet) except: return None def __set_income_statement(self): if str(type(self.income_statement)) == "<class 'NoneType'>": try: self.income_statement = self.financials.get_financial_stmts( self.timeframe, 'income')['incomeStatementHistory'][self.ticker] self.income_statement = self.__clean_statement_timestamp( self.income_statement) except: return None def __set_cash_flow_statement(self): if str(type(self.cash_flow_statement)) == "<class 'NoneType'>": try: self.cash_flow_statement = self.financials.get_financial_stmts( self.timeframe, 'cash')['cashflowStatementHistory'][self.ticker] self.cash_flow_statement = self.__clean_statement_timestamp( self.cash_flow_statement) except: return None def __set_key_statistics(self): if str(type(self.key_statistics)) == "<class 'NoneType'>": try: self.key_statistics = self.financials.get_key_statistics_data() except: return None def __set_historical_eps(self): if str(type(self.historical_eps)) == "<class 'NoneType'>": url = 'https://www.nasdaq.com/earnings/report/{}'.format( self.ticker) page = requests.get(url) if page.status_code == 200: soup = BeautifulSoup(page.text, 'html.parser') eps_list = [] td = soup.find_all('td') counter = 0 for i in td[2::5]: try: test = float(i.get_text()) counter += 1 except: break for i in range(counter * 5): eps_list.append(list(soup.find_all('td'))[i].get_text()) self.historical_eps = pd.DataFrame({ 'textdate': eps_list[0::5], 'timestamp': eps_list[1::5], 'eps': eps_list[2::5], 'consensus_eps': eps_list[3::5], 'surprise': eps_list[4::5] }) self.historical_eps = self.historical_eps.iloc[::-1] self.historical_eps.reset_index(inplace=True, drop=True) else: return None def __set_price_data(self): if str(type(self.price_data)) == "<class 'NoneType'>": try: self.price_data = pd.DataFrame( self.financials.get_historical_price_data( '1800-01-01', str(datetime.now())[:10], 'daily')[self.ticker]['prices']) except: pass def __clean_statement_timestamp(self, statement): for i in statement: for j in i.keys(): i[j[:4]] = i.pop(j) return statement def __get_specific_year(self, statement, year): for i in statement: if str(list(i.keys())[0]) == str(year): return i['{}'.format(year)] def __json_to_dataframe(self, data): columns = [] for i in data: for j in i.keys(): columns.append(j) df = pd.DataFrame(data[0]) for i in range(len(columns)): df[columns[i]] = pd.DataFrame(data[i]) df = df[df.columns[::-1]] return df def set_risk_free_ror(self, risk_free_ror): self.risk_free_ror = risk_free_ror def set_market_ror(self, market_ror): self.market_ror = market_ror def check_risk_free_ror(self): return self.risk_free_ror def check_market_ror(self): return self.market_ror def check_old_corporate_tax_rate(self): return self.old_corporate_tax_rate def check_new_corporate_tax_rate(self): return self.new_corporate_tax_rate def get_balance_sheet(self): self.__set_balance_sheet() df = self.__json_to_dataframe(self.balance_sheet) return df def get_income_statement(self): self.__set_income_statement() df = self.__json_to_dataframe(self.income_statement) return df def get_cash_flow_statement(self): self.__set_cash_flow_statement() df = self.__json_to_dataframe(self.cash_flow_statement) return df def get_key_statistics(self): self.__set_key_statistics() ser = pd.Series(self.key_statistics[self.ticker]) return ser def get_historical_eps(self): self.__set_historical_eps() return self.historical_eps def get_price_data(self): self.__set_price_data() return self.price_data def check_ticker(self): return self.ticker def revenue(self, year): self.__set_income_statement() try: return self.__get_specific_year(self.income_statement, year)['totalRevenue'] except: return None def total_expenses(self, year): self.__set_income_statement() self.__set_cash_flow_statement() try: operating_expenses = self.__get_specific_year( self.income_statement, year)['totalOperatingExpenses'] interest = self.__get_specific_year(self.income_statement, year)['interestExpense'] tax = self.__get_specific_year(self.income_statement, year)['incomeTaxExpense'] depreciation = self.__get_specific_year(self.cash_flow_statement, year)['depreciation'] return abs(operating_expenses) + abs(interest) + abs(tax) + abs( depreciation) except: return None def operating_expenses(self, year): self.__set_income_statement() try: return self.__get_specific_year(self.income_statement, year)['totalOperatingExpenses'] except: return None def outstanding_shares(self): self.__set_key_statistics() try: return self.get_key_statistics()['sharesOutstanding'] except: return None def share_price(self): self.__set_price_data() try: return float(self.price_data[-1:]['close']) except: return None def market_value(self): try: return self.share_price() * self.outstanding_shares() except: return None def liquid_assets(self, year): self.__set_balance_sheet() try: return self.__get_specific_year( self.balance_sheet, year)['cash'] + self.__get_specific_year( self.balance_sheet, year)['shortTermInvestments'] except: return None def total_debt(self, year): self.__set_balance_sheet() try: return self.__get_specific_year( self.balance_sheet, year)['shortLongTermDebt'] + self.__get_specific_year( self.balance_sheet, year)['longTermDebt'] except: return None def tax(self, year): self.__set_income_statement() try: return abs( self.__get_specific_year(self.income_statement, year)['incomeTaxExpense']) except: return None def interest(self, year): self.__set_income_statement() try: return self.__get_specific_year(self.income_statement, year)['interestExpense'] except: return None def depreciation(self, year): self.__set_cash_flow_statement() try: return self.__get_specific_year(self.cash_flow_statement, year)['depreciation'] except: return None def cost_of_revenue(self, year): self.__set_income_statement() try: return self.__get_specific_year(self.income_statement, year)['costOfRevenue'] except: return None def total_receivables(self, year): self.__set_balance_sheet() try: return self.__get_specific_year(self.balance_sheet, year)['netReceivables'] except: return None def total_liabilities(self, year): self.__set_balance_sheet() try: return self.__get_specific_year(self.balance_sheet, year)['totalLiab'] except: return None def total_assets(self, year): self.__set_balance_sheet() try: return self.__get_specific_year(self.balance_sheet, year)['totalAssets'] except: return None def total_capital(self, year): self.__set_balance_sheet() try: return self.__get_specific_year( self.balance_sheet, year)['totalStockholderEquity'] + self.total_debt(year) except: return None def total_equity(self, year): try: return self.total_assets(year) - self.total_liabilities(year) except: return None def capital_expenditures(self, year): self.__set_cash_flow_statement() try: return self.__get_specific_year(self.cash_flow_statement, year)['capitalExpenditures'] except: return None def net_income(self, year): self.__set_income_statement() try: return self.__get_specific_year(self.income_statement, year)['netIncome'] except: return None def gross_profit(self, year): self.__set_income_statement() try: return self.__get_specific_year(self.income_statement, year)['grossProfit'] except: return None def earnings_per_share(self): try: return self.financials.get_earnings_per_share() except: return None def pe_ratio(self): try: return self.financials.get_pe_ratio() except: return None def enterprise_value(self): try: return self.get_key_statistics()['enterpriseValue'] except: return None def ebit(self, year): self.__set_income_statement() try: return self.__get_specific_year(self.income_statement, year)['ebit'] except: return None def ebitda(self, year): try: temp_net_income = abs(self.net_income(year)) temp_tax = abs(self.tax(year)) temp_interest = abs(self.interest(year)) temp_depreciation = abs(self.depreciation(year)) return temp_net_income + temp_tax + temp_interest + temp_depreciation except: return None def quick_ratio(self, year): try: temp_liquid_assets = abs(self.liquid_assets(year)) temp_receivables = abs(self.total_receivables(year)) temp_liabilities = abs(self.total_liabilities(year)) return (temp_liquid_assets + temp_receivables) / temp_liabilities except: return None def income_continuing_operations_margin(self, year): try: temp_ebit = self.ebit(year) temp_revenue = abs(self.revenue(year)) return temp_ebit / temp_revenue except: return None def net_margin(self, year): try: temp_net_income = self.net_income(year) temp_revenue = self.revenue(year) return temp_net_income / temp_revenue except: return None def return_on_assets(self, year): try: ending_year = year beginning_year = year - 1 temp_net_income = self.net_income(year) beginning_total_assets = abs(self.total_assets(beginning_year)) ending_total_assets = abs(self.total_assets(ending_year)) return temp_net_income / ( (beginning_total_assets + ending_total_assets) / 2) except: return None def return_on_capital(self, year): try: ending_year = year beginning_year = year - 1 if ending_year >= 2018: temp_ebit = self.ebit(year) temp_tax_rate = self.new_corporate_tax_rate beginning_total_capital = abs( self.total_capital(beginning_year)) ending_total_capital = abs(self.total_capital(ending_year)) return (temp_ebit * (1 - temp_tax_rate)) / ( (beginning_total_capital + ending_total_capital) / 2) elif ending_year < 2018: temp_ebit = self.ebit(year) temp_tax_rate = self.old_corporate_tax_rate beginning_total_capital = abs( self.total_capital(beginning_year)) ending_total_capital = abs(self.total_capital(ending_year)) return (temp_ebit * (1 - temp_tax_rate)) / ( (beginning_total_capital + ending_total_capital) / 2) except: return None def return_on_equity(self, year): try: temp_ebit = self.ebit(year) temp_total_equity = abs(self.total_equity(year)) return temp_ebit / temp_total_equity except: return None def cash_flow_operations(self, year): try: temp_revenue = abs(self.revenue(year)) temp_operating_expenses = abs(self.operating_expenses(year)) return temp_revenue - temp_operating_expenses except: return None def free_cash_flow(self, year): try: if year >= 2018: temp_cfo = self.cash_flow_operations(year) temp_interest = self.interest(year) temp_tax_rate = self.new_corporate_tax_rate temp_capex = self.capital_expenditures(year) return temp_cfo + (temp_interest * (1 - temp_tax_rate)) - temp_capex elif year < 2018: temp_cfo = self.cash_flow_operations(year) temp_interest = self.interest(year) temp_tax_rate = self.old_corporate_tax_rate temp_capex = self.capital_expenditures(year) return temp_cfo + (temp_interest * (1 - temp_tax_rate)) - temp_capex except: return None def beta(self): try: return self.financials.get_beta() except: return None def cost_of_equity(self): try: return self.risk_free_ror + ( self.beta() * (self.market_ror - self.risk_free_ror)) except: return None def gross_profit_margin(self, year): try: return (self.revenue(year) - self.cost_of_revenue(year)) / self.revenue(year) except: return None def operating_profit_margin(self, year): try: return self.ebit(year) / self.revenue(year) except: return None def net_profit_margin(self, year): try: return self.net_income(year) / self.revenue(year) except: return None def short_ratio(self): try: return self.get_key_statistics()['shortRatio'] except: return None
from yahoofinancials import YahooFinancials import pandas as pd # Prepare yahoo finance call tickers = input("Enter tickers to track: (seperate by comma and space) ").split(", ") print("Loading tickers: ", tickers) data = YahooFinancials(tickers) income = data.get_financial_stmts('annualy', 'income') balance = data.get_financial_stmts('annualy', 'balance') cash = data.get_financial_stmts('annualy', 'cash') df = pd.DataFrame() def put_in(thing, what): temp = pd.DataFrame() for (ticker, value) in list(what.values())[0].items(): for x in value: for (date, data) in x.items(): print(date) temp = temp.append(pd.Series(data, name=ticker)) # We only want most recent data so the rest we don't care about break break # Rotate
class TestModule(TestCase): def setUp(self): self.test_yf_stock_single = YahooFinancials('C') self.test_yf_stock_multi = YahooFinancials(stocks) self.test_yf_treasuries_single = YahooFinancials('^IRX') self.test_yf_treasuries_multi = YahooFinancials(us_treasuries) self.test_yf_currencies = YahooFinancials(currencies) # Fundamentals Test def test_yf_fundamentals(self): # Single stock test single_balance_sheet_data_qt = self.test_yf_stock_single.get_financial_stmts( 'quarterly', 'balance') single_income_statement_data_qt = self.test_yf_stock_single.get_financial_stmts( 'quarterly', 'income') single_all_statement_data_qt = self.test_yf_stock_single.get_financial_stmts( 'quarterly', ['income', 'cash', 'balance']) # Multi stock test multi_balance_sheet_data_qt = self.test_yf_stock_multi.get_financial_stmts( 'quarterly', 'balance') multi_income_statement_data_qt = self.test_yf_stock_multi.get_financial_stmts( 'quarterly', 'income') multi_all_statement_data_qt = self.test_yf_stock_multi.get_financial_stmts( 'quarterly', ['income', 'cash', 'balance']) # Single stock check result = check_fundamental(single_balance_sheet_data_qt, 'bal') self.assertEqual(result, True) result = check_fundamental(single_income_statement_data_qt, 'inc') self.assertEqual(result, True) result = check_fundamental(single_all_statement_data_qt, 'all') self.assertEqual(result, True) # Multi stock check result = check_fundamental(multi_balance_sheet_data_qt, 'bal') self.assertEqual(result, True) result = check_fundamental(multi_income_statement_data_qt, 'inc') self.assertEqual(result, True) result = check_fundamental(multi_all_statement_data_qt, 'all') self.assertEqual(result, True) # Historical Price Test def test_yf_historical_price(self): single_stock_prices = self.test_yf_stock_single.get_historical_price_data( '2015-01-15', '2017-10-15', 'weekly') expect_dict = { 'high': 49.099998474121094, 'volume': 125737200, 'formatted_date': '2015-01-12', 'low': 46.599998474121094, 'adjclose': 45.35684585571289, 'date': 1421038800, 'close': 47.61000061035156, 'open': 48.959999084472656 } self.assertDictEqual(single_stock_prices['C']['prices'][0], expect_dict) # Historical Stock Daily Dividend Test def test_yf_dividend_price(self): single_stock_dividend = self.test_yf_stock_single.get_daily_dividend_data( '1986-09-15', '1987-09-15') expect_dict = { "C": [{ "date": 533313000, "formatted_date": "1986-11-25", "amount": 0.02999 }, { "date": 541348200, "formatted_date": "1987-02-26", "amount": 0.02999 }, { "date": 544714200, "formatted_date": "1987-04-06", "amount": 0.332 }, { "date": 549120600, "formatted_date": "1987-05-27", "amount": 0.02999 }, { "date": 552576600, "formatted_date": "1987-07-06", "amount": 0.332 }, { "date": 557501400, "formatted_date": "1987-09-01", "amount": 0.02999 }] } self.assertDictEqual(single_stock_dividend, expect_dict) # Extra Module Methods Test def test_yf_module_methods(self): # Stocks if isinstance(self.test_yf_stock_single.get_current_price(), float): self.assertEqual(True, True) else: self.assertEqual(False, True) if isinstance(self.test_yf_stock_single.get_net_income(), int): self.assertEqual(True, True) else: self.assertEqual(False, True) # Treasuries if isinstance(self.test_yf_treasuries_single.get_current_price(), float): self.assertEqual(True, True) else: self.assertEqual(False, True)
ohlc.columns = ohlc.columns.swaplevel(0, 1) ohlc.sort_index(level=0, axis=1, inplace=True) prices = ohlc["Adj Close"] dividends = ohlc["Dividends"] prices.to_csv(f"data/{project}/prices_daily.csv") dividends.to_csv(f"data/{project}/dividends.csv") statements = {} tickers_done = [] for ticker in tqdm(tickers): # Get statements if ticker in tickers_done: continue yahoo_financials = YahooFinancials(ticker) stmts_codes = ['income', 'cash', 'balance'] all_statement_data = yahoo_financials.get_financial_stmts( 'annual', stmts_codes) # build statements dictionnary for a in all_statement_data.keys(): if a not in statements: statements[a] = list() for b in all_statement_data[a]: try: for result in all_statement_data[a][b]: extracted_date = list(result)[0] dataframe_row = list(result.values())[0] dataframe_row['date'] = extracted_date dataframe_row['symbol'] = b statements[a].append(dataframe_row) except Exception as e: print("Error on " + ticker + " : " + a) tickers_done.append(ticker)
def balance_sheet_provider(comp_name, comp_code): yahoo_finance_object = YahooFinancials(comp_code) balance_sheet_raw = yahoo_finance_object.get_financial_stmts( 'annual', 'balance') income_statement_raw = yahoo_finance_object.get_financial_stmts( 'annual', 'income') e_p_s = None pe_rat = None try: e_p_s = yahoo_finance_object.get_earnings_per_share() pe_rat = yahoo_finance_object.get_pe_ratio() except KeyError: pass balance_sheet = balance_sheet_raw["balanceSheetHistory"][comp_code] income_statement = income_statement_raw["incomeStatementHistory"][ comp_code] company_data_provider_object = StockDataExtractor(comp_code, comp_name, balance_sheet, income_statement, e_p_s, pe_rat) ''' balance_sheet_raw = {"balanceSheetHistory": {"KO": [{"2017-12-31": {"intangibleAssets": 7235000000, "capitalSurplus": 15864000000, "totalLiab": 68919000000, "totalStockholderEquity": 17072000000, "minorityInterest": 1905000000, "otherCurrentLiab": 2169000000, "totalAssets": 87896000000, "commonStock": 1760000000, "otherCurrentAssets": 7993000000, "retainedEarnings": 60430000000, "otherLiab": 10504000000, "goodWill": 9401000000, "treasuryStock": -60982000000, "otherAssets": 3231000000, "cash": 6006000000, "totalCurrentLiabilities": 27194000000, "deferredLongTermAssetCharges": 331000000, "shortLongTermDebt": 47754000000, "otherStockholderEquity": -10305000000, "propertyPlantEquipment": 8203000000, "totalCurrentAssets": 36545000000, "longTermInvestments": 23281000000, "netTangibleAssets": 436000000, "shortTermInvestments": 14669000000, "netReceivables": 3667000000, "longTermDebt": 31221000000, "inventory": 2655000000, "accountsPayable": 2288000000}}, {"2016-12-31": {"intangibleAssets": 10499000000, "capitalSurplus": 14993000000, "totalLiab": 64050000000, "totalStockholderEquity": 23062000000, "minorityInterest": 158000000, "otherCurrentLiab": 1936000000, "totalAssets": 87270000000, "commonStock": 1760000000, "otherCurrentAssets": 3592000000, "retainedEarnings": 65502000000, "otherLiab": 7786000000, "goodWill": 10629000000, "treasuryStock": -59193000000, "otherAssets": 2928000000, "cash": 8555000000, "totalCurrentLiabilities": 26532000000, "deferredLongTermAssetCharges": 326000000, "shortLongTermDebt": 45801000000, "otherStockholderEquity": -11205000000, "propertyPlantEquipment": 10635000000, "totalCurrentAssets": 34010000000, "longTermInvestments": 18569000000, "netTangibleAssets": 1934000000, "shortTermInvestments": 13646000000, "netReceivables": 3856000000, "longTermDebt": 29732000000, "inventory": 2675000000, "accountsPayable": 2682000000}}, {"2015-12-31": {"intangibleAssets": 12843000000, "capitalSurplus": 14016000000, "totalLiab": 64232000000, "totalStockholderEquity": 25554000000, "minorityInterest": 210000000, "otherCurrentLiab": 2333000000, "totalAssets": 89996000000, "commonStock": 1760000000, "otherCurrentAssets": 4748000000, "retainedEarnings": 65018000000, "otherLiab": 8760000000, "goodWill": 11289000000, "treasuryStock": -55240000000, "otherAssets": 3030000000, "cash": 7309000000, "totalCurrentLiabilities": 26929000000, "deferredLongTermAssetCharges": 360000000, "shortLongTermDebt": 44401000000, "otherStockholderEquity": -10174000000, "propertyPlantEquipment": 12571000000, "totalCurrentAssets": 33395000000, "longTermInvestments": 16868000000, "netTangibleAssets": 1422000000, "shortTermInvestments": 12591000000, "netReceivables": 4466000000, "longTermDebt": 19100000000, "inventory": 3100000000, "accountsPayable": 2089000000}}]}} income_statement_raw = {"incomeStatementHistory": {"KO": [{"2017-12-31": {"researchDevelopment": None, "effectOfAccountingCharges": None, "incomeBeforeTax": 6742000000, "minorityInterest": 1905000000, "netIncome": 1248000000, "sellingGeneralAdministrative": 14653000000, "grossProfit": 22154000000, "ebit": 7583000000, "operatingIncome": 7501000000, "otherOperatingExpenses": None, "interestExpense": 841000000, "extraordinaryItems": None, "nonRecurring": None, "otherItems": None, "incomeTaxExpense": 5560000000, "totalRevenue": 35410000000, "totalOperatingExpenses": 0, "costOfRevenue": 13256000000, "totalOtherIncomeExpenseNet": -989000000, "discontinuedOperations": 101000000, "netIncomeFromContinuingOps": 1182000000, "netIncomeApplicableToCommonShares": 1248000000}}, {"2016-12-31": {"researchDevelopment": None, "effectOfAccountingCharges": None, "incomeBeforeTax": 8136000000, "minorityInterest": 158000000, "netIncome": 6527000000, "sellingGeneralAdministrative": 16772000000, "grossProfit": 25398000000, "ebit": 8869000000, "operatingIncome": 8626000000, "otherOperatingExpenses": None, "interestExpense": 733000000, "extraordinaryItems": None, "nonRecurring": None, "otherItems": None, "incomeTaxExpense": 1586000000, "totalRevenue": 41863000000, "totalOperatingExpenses": 0, "costOfRevenue": 16465000000, "totalOtherIncomeExpenseNet": -592000000, "discontinuedOperations": 101000000, "netIncomeFromContinuingOps": 6550000000, "netIncomeApplicableToCommonShares": 6527000000}}, {"2015-12-31": {"researchDevelopment": None, "effectOfAccountingCharges": None, "incomeBeforeTax": 9605000000, "minorityInterest": 210000000, "netIncome": 7351000000, "sellingGeneralAdministrative": 18084000000, "grossProfit": 26812000000, "ebit": 10461000000, "operatingIncome": 8728000000, "otherOperatingExpenses": None, "interestExpense": 856000000, "extraordinaryItems": None, "nonRecurring": None, "otherItems": None, "incomeTaxExpense": 2239000000, "totalRevenue": 44294000000, "totalOperatingExpenses": 0, "costOfRevenue": 17482000000, "totalOtherIncomeExpenseNet": 1244000000, "discontinuedOperations": 101000000, "netIncomeFromContinuingOps": 7366000000, "netIncomeApplicableToCommonShares": 7351000000}}]}} ''' comp_ratio_data_dict = json.dumps( company_data_provider_object.return_data_for_stock()) return comp_ratio_data_dict
from yahoofinancials import YahooFinancials import pandas as pd import datetime as datetime df = pd.read_excel('C:/Users/Jacob Steenhuysen/Downloads/Data Center REITs Tickers.xlsx', sheet_name='Sheet1') tickers_list = df['Ticker'].tolist() data = pd.DataFrame(columns=tickers_list) yahoo_financials_ecommerce = YahooFinancials(data) ecommerce_income_statement_data = yahoo_financials_ecommerce.get_financial_stmts('quarterly', 'balance') data = ecommerce_income_statement_data['balanceSheetHistoryQuarterly'] df_dict = dict() for ticker in tickers_list: df_dict[ticker] = pd.concat([pd.DataFrame(data[ticker][x]) for x in range(len(data[ticker]))], sort=False, join='outer', axis=1) df = pd.concat(df_dict, sort=True) df_l = pd.DataFrame(df.stack()) df_l.reset_index(inplace=True) df_l.columns = ['ticker', 'financials', 'date', 'value'] df = df_l.pivot_table(index=['date', 'financials'], columns='ticker', values='value') #df_2.index['date'] = pd.to_datetime(df_2.index['date'])
from yahoofinancials import YahooFinancials import pandas as pd import datetime as datetime df = pd.read_excel('C:/Users/Jacob Steenhuysen/Downloads/REIT Tickers1.xlsx', sheet_name='Sheet1') tickers_list = df['Ticker'].tolist() data = pd.DataFrame(columns=tickers_list) yahoo_financials_ecommerce = YahooFinancials(data) ecommerce_income_statement_data = yahoo_financials_ecommerce.get_financial_stmts( 'annual', 'income') data = ecommerce_income_statement_data['incomeStatementHistory'] df_dict = dict() for ticker in tickers_list: df_dict[ticker] = pd.concat( [pd.DataFrame(data[ticker][x]) for x in range(len(data[ticker]))], sort=False, join='outer', axis=1) df = pd.concat(df_dict, sort=True) df_l = pd.DataFrame(df.stack()) df_l.reset_index(inplace=True)
def get_fundamentals(symbol): check_existence() yahoo_financials = YahooFinancials(symbol) stock_type = yahoo_financials.get_stock_quote_type_data() annual_stmt = yahoo_financials.get_financial_stmts('annual', ['balance', 'income', 'cash']) return stock_type, annual_stmt
def get_stock_data(stock_data): symbol = stock_data['symbol'] start = time.time() print('start: {}'.format(symbol)) Stock = YahooFinancials(symbol) stock_data['pe_ratio'] = Stock.get_pe_ratio() if stock_data['pe_ratio'] != None and stock_data['pe_ratio'] < 15: key_stock_data = Stock.get_key_statistics_data() stock_data['pb_ratio'] = key_stock_data[symbol]['priceToBook'] if stock_data['pb_ratio'] != None and stock_data['pb_ratio'] < 1.5: balance_sheet_data = Stock.get_financial_stmts('annual', 'balance') income_sheet_data = Stock.get_financial_stmts('annual', 'income') bal1 = balance_sheet_data['balanceSheetHistory'][symbol][0] bal2 = balance_sheet_data['balanceSheetHistory'][symbol][1] bal3 = balance_sheet_data['balanceSheetHistory'][symbol][2] bal4 = balance_sheet_data['balanceSheetHistory'][symbol][3] bal1_key1 = next(iter(bal1)) bal2_key1 = next(iter(bal2)) bal3_key1 = next(iter(bal3)) bal4_key1 = next(iter(bal4)) inc1 = income_sheet_data['incomeStatementHistory'][symbol][0] inc2 = income_sheet_data['incomeStatementHistory'][symbol][1] inc3 = income_sheet_data['incomeStatementHistory'][symbol][2] inc4 = income_sheet_data['incomeStatementHistory'][symbol][3] inc1_key1 = next(iter(inc1)) inc2_key1 = next(iter(inc2)) inc3_key1 = next(iter(inc3)) inc4_key1 = next(iter(inc4)) stock_data['liabilities'] = bal1[bal1_key1][ 'totalCurrentLiabilities'] stock_data['assets'] = bal1[bal1_key1]['totalCurrentAssets'] stock_data['equity1'] = bal1[bal1_key1]['totalStockholderEquity'] stock_data['equity2'] = bal2[bal2_key1]['totalStockholderEquity'] stock_data['equity3'] = bal3[bal3_key1]['totalStockholderEquity'] stock_data['equity4'] = bal4[bal4_key1]['totalStockholderEquity'] stock_data['net_income1'] = inc1[inc1_key1]['netIncome'] stock_data['net_income2'] = inc2[inc2_key1]['netIncome'] stock_data['net_income3'] = inc3[inc3_key1]['netIncome'] stock_data['net_income4'] = inc4[inc4_key1]['netIncome'] db = sqlite3.connect('stocks.db') c = db.cursor() c.execute( '''INSERT INTO stocks ( symbol, company_name, pb_ratio, pe_ratio, assets, liabilities, net_income1, net_income2, net_income3, net_income4, equity1, equity2, equity3, equity4 ) VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?)''', (stock_data['symbol'], stock_data['company_name'], stock_data['pb_ratio'], stock_data['pe_ratio'], stock_data['assets'], stock_data['liabilities'], stock_data['net_income1'], stock_data['net_income2'], stock_data['net_income3'], stock_data['net_income4'], stock_data['equity1'], stock_data['equity2'], stock_data['equity3'], stock_data['equity4'])) db.commit() del stock_data end = time.time() print('got: {} - {}'.format(symbol, round(end - start))) return None
class Ticker: def __init__(self, ticker, begin=begin, end=end): self.ticker = ticker self.YF = YahooFinancials(ticker) self.historical_data = None self.historical_price_data = None self.begin = begin self.end = end # https://github.com/JECSand/yahoofinancials self.all_statement_data_qt = self.YF.get_financial_stmts( 'quarterly', ['income', 'cash', 'balance']) def get_historical_price_data(self): self.historical_data = self.YF.get_historical_price_data( self.begin, self.end, 'daily')[self.ticker] self.historical_price_data = self.historical_data['prices'] def get_close(self): if self.historical_data == None: self.get_historical_price_data() close = np.zeros(len(self.historical_price_data)) for i, price in enumerate(self.historical_price_data): close[i] = (price['high'] + price['open'] + price['close']) / 3.0 return close def get_volume(self): if self.historical_data == None: self.get_historical_price_data() close = np.zeros(len(self.historical_price_data)) for i, price in enumerate(self.historical_price_data): close[i] = price['volume'] return close def get_ma_close(self, len_ma=20): close = self.get_close() return Ticker.get_ma(close, len_ma) def get_ma_volume(self, len_ma=20): close = self.get_volume() return Ticker.get_ma(close, len_ma) @staticmethod def get_ma(close, len_ma=20): len_close = close.size ma_close = np.zeros(len_close) if len_close > 2.0 * len_ma: for i in range(len_close - len_ma): ma_close[i + len_ma] = close[i:i + len_ma].mean() return ma_close def get_boll(self, len_ma=20): close = self.get_close() len_close = close.size ma_close = np.zeros(len_close) std_close = np.zeros(len_close) if len_close > 2.0 * len_ma: for i in range(len_close - len_ma): ma_close[i + len_ma] = close[i:i + len_ma].mean() std_close[i + len_ma] = close[i:i + len_ma].std() return ma_close + 2 * std_close, ma_close, ma_close - 2 * std_close def return_n(self, len_ma=20): close = self.get_close() len_close = close.size ma_close = np.zeros(len_close) if len_close > 2.0 * len_ma: for i in range(len_close - len_ma): ma_close[i + len_ma] = np.log(close[i + len_ma] / close[i]) return ma_close # https://www.wikihow.com/Calculate-Historical-Stock-Volatility def get_volatility(self, len_ma=20): ret_urn = self.return_n(len_ma) mean_return = ret_urn.mean() std_return = np.sqrt((ret_urn - mean_return)**2 / (ret_urn.size - 1)) return std_return def get_status_ma(self, len_ma=20, short_period=3): ma_short = self.get_ma_close(short_period) ma_close = self.get_ma_close(len_ma) close = ma_short criterio = (close[close.size - 1] - ma_close[ma_close.size - 1]) / close[close.size - 1] return criterio > 0, criterio < 0, 100 * criterio ''' https://www.investopedia.com/articles/technical/02/082702.asp It is important to note that an increasing price, together with declining volume, is always, without exception, bearish. When the market is at the top, one would, therefore, expect to see an oversold volume chart. Another important point: rising volume, together with declining prices, is also bearish. ''' def get_vol_osc(self, len_long=20, len_short=5): v_short = self.get_ma_volume(len_short) v_long = self.get_ma_volume(len_long) return v_short - v_long def get_lineal_macd(self, sh=10, lon=20, fil=7): close = self.get_close() sh_close = self.get_ma(close, sh) lon_close = self.get_ma(close, lon) macd = sh_close - lon_close macd_signal_line = Ticker.get_ma(macd, len_ma=fil) macd_hist = macd - macd_signal_line return macd_hist, macd_signal_line, macd @staticmethod def lineal_macd(close, sh=10, lon=20, fil=7): sh_close = Ticker.get_ma(close, sh) lon_close = Ticker.get_ma(close, lon) macd = sh_close - lon_close macd_signal_line = Ticker.get_ma(macd, len_ma=fil) macd_hist = macd - macd_signal_line decay = 0.75 macd_hist = Ticker.iir(macd_hist, decay) return macd_hist, macd_signal_line, macd @staticmethod def iir(signal, decay): b = 1 - decay y = np.zeros(signal.size) for i in range(1, signal.size): y[i] = y[i - 1] + b * (signal[i] - y[i - 1]) return y # iir low pass filter # https://tomroelandts.com/articles/low-pass-single-pole-iir-filter @staticmethod def lineal_macd_iir(close, sh=.7, lon=.9, fil=.7): sh_close = Ticker.iir(close, sh) lon_close = Ticker.iir(close, lon) macd = sh_close - lon_close macd_signal_line = Ticker.iir(macd, fil) macd_hist = macd - macd_signal_line decay = 0.7 macd_hist = Ticker.iir(macd_hist, decay) return macd_hist, macd_signal_line, macd
from yahoofinancials import YahooFinancials ticker = "AAPL" yahoo_financials = YahooFinancials(ticker) balance_sheet_data_qt = yahoo_financials.get_financial_stmts( "quarterly", "balance") income_statement_data_qt = yahoo_financials.get_financial_stmts( "quarterly", "income") all_statement_data_qt = yahoo_financials.get_financial_stmts( "quarterly", ["income", "cash", "balance"]) apple_earnings_data = yahoo_financials.get_stock_earnings_data() apple_net_income = yahoo_financials.get_net_income() historical_stock_prices = yahoo_financials.get_historical_price_data( "2008-09-15", "2018-09-15", "weekly")
# -*- coding: utf-8 -*- """ Created on Fri May 17 18:46:54 2019 @author: skd """ import json import pandas as pd from yahoofinancials import YahooFinancials from pandas.io.json import json_normalize stmnt_typ = 'income' yahoo_financials = YahooFinancials('PCAR') jsons = yahoo_financials.get_financial_stmts('annual', 'stmnt_typ') json_ids = [] frames = [] for json_id, d in jsons.items(): json_ids.append(json_id) frames.append(pd.DataFrame.from_dict(d, orient='index')) #try def flatten_json(b, delim): val = {} for i in b.keys(): if isinstance(b[i], dict): get = flatten_json(b[i], delim) for j in get.keys():