def getMarketData(): tickers = 'AAPL' financials = YahooFinancials(tickers) # company_stock_price = financials.get_stock_price_data() #gets stock price information historical_stock_prices_data = financials.get_historical_price_data( '2015-11-21', '2020-11-21', 'daily') #gets historical daily stock price of company # get_Div_data(historical_stock_prices_data[tickers]) get_stock_price_data(historical_stock_prices_data[tickers]) # company_balance_sheet_data_qt = financials.get_financial_stmts('quarterly', 'balance') #get balance sheet # company_income_statement_data_qt = financials.get_financial_stmts('quarterly', 'income') #get income statement company_key_statistics_data = financials.get_key_statistics_data( ) #includes profit margins, forward eps, yearly change etc. # get_forward_pe(company_key_statistics_data[tickers]) # get_trailing_eps(company_key_statistics_data[tickers]) # get_foward_eps(company_key_statistics_data[tickers]) # get_ytdReturn(company_key_statistics_data[tickers]) company_earnings_data = financials.get_stock_earnings_data( ) #historical eps only for 1 year span # get_earnings_data(company_earnings_data[tickers]) company_dividend_yield = financials.get_dividend_yield( ) #current dividends yield company_dividend = financials.get_dividend_rate() #current dividends rate company_avg_div_yield_1year = financials.get_annual_avg_div_yield( ) #average 1 year div yield company_avg_div_yield_5year = financials.get_five_yr_avg_div_yield( ) #average 5 year div yield company_eps = financials.get_earnings_per_share() #current eps company_pe = financials.get_pe_ratio() #current pe ratio company_beta = financials.get_beta() #current beta company_current_stock_price = financials.get_current_price( ) #current stock price company_revenue = financials.get_total_revenue() #current company revenue company_operating_income = financials.get_operating_income( ) #current company operating income company_net_income = financials.get_net_income() #current net income company_yearly_high = financials.get_yearly_high() #get yearly high company_yearly_low = financials.get_yearly_low() #get yearly low company_moving_50 = financials.get_50day_moving_avg( ) #50 day moving average of stock company_moving_200 = financials.get_200day_moving_avg( ) #200 day moving average of stock
async def stock(ctx, ticker, info): yahoo_financials = YahooFinancials(ticker) if (info == "current"): await ctx.send(ticker.upper() + " current share price: $" + str(yahoo_financials.get_current_price())) if (info == "open"): await ctx.send(ticker.upper() + " share price at opening: $" + str(yahoo_financials.get_open_price())) if (info == "prevclose"): await ctx.send(ticker.upper() + " share priced at previous close: $" + str(yahoo_financials.get_prev_close_price())) if (info == "cap"): await ctx.send(ticker.upper() + " market cap: $" + str("{:,}".format(yahoo_financials.get_market_cap()))) if (info == "dailylow"): await ctx.send(ticker.upper() + " daily low: $" + str(yahoo_financials.get_daily_low())) if (info == "dailyhigh"): await ctx.send(ticker.upper() + " daily high: $" + str(yahoo_financials.get_daily_high())) if (info == "yearlow"): await ctx.send(ticker.upper() + " yearly low: $" + str(yahoo_financials.get_yearly_low())) if (info == "yearhigh"): await ctx.send(ticker.upper() + " yearly high: $" + str(yahoo_financials.get_yearly_high())) if (info == "rev"): await ctx.send(ticker.upper() + " total revenue: $" + str("{:,}".format(yahoo_financials.get_total_revenue())) ) if (info == "net"): await ctx.send(ticker.upper() + " net income: $" + str("{:,}".format(yahoo_financials.get_net_income()))) if (info == "op"): await ctx.send( ticker.upper() + " operating income: $" + str("{:,}".format(yahoo_financials.get_operating_income()))) if (info == "profit"): await ctx.send(ticker.upper() + " gross profit: $" + str("{:,}".format(yahoo_financials.get_gross_profit())))
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")
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)
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)