def generate_header(dd_input2): global q_display if dd_input2 is None: raise PreventUpdate sym = Stock(dd_input2, token=token) q_display = sym.get_quote() #stock_header_dict[dd_input2] = {k: v for k, v in q_display.items() if k in ['symbol', 'open', 'close', 'high', 'low', 'latestPrice', 'latestVolume', 'companyName']} return """{0} ({1})""".format( q_display['companyName'], q_display['symbol']), """Latest Price Sold: {0}""".format( get_data_points( q_display['symbol'], "QUOTE-LATESTPRICE")), "Open Price: {0}".format( get_data_points( q_display['symbol'], "QUOTE-OPEN")), """Close Price: {0}""".format( get_data_points(q_display['symbol'], "QUOTE-CLOSE") ), """High Price: {0}""".format( get_data_points( q_display['symbol'], "QUOTE-HIGH")), """Low Price: {0}""".format( get_data_points(q_display['symbol'], "QUOTE-LOW") ), """Latest Volume Traded: {0}""".format( get_data_points(q_display['symbol'], "QUOTE-LATESTVOLUME"))
def get_cash_flow(self, stock=None) -> int: """ Get cash flow for a stock Parameters: stock (str): ticker string of the stock Returns: (int): the cash flow of the stock """ if self.pre_built: return self.cash_flow self._check_args(stock) return get_data_points(stock, SC.CASH_FLOW)
def get_price_to_sales(self, stock=None) -> float: """ Get price to sales ratio of a given stock Parameters: stock (str): ticker string of the stock Returns: (float): price to sales of the stock """ if self.pre_built: return self.price_to_sales self._check_args(stock) return get_data_points(stock, SC.PRICE_TO_SALES)
def get_price_to_book(self, stock=None) -> float: """ Get the price to book value ratio of a given stock Parameters: stock (str): ticker string of the stock Returns: (float): the price of the stock divided by the book value per share of the stock """ if self.pre_built: return self.price_to_book self._check_args(stock) return get_data_points(stock, SC.PRICE_TO_BOOK)
def get_debt_to_equity(self, stock=None) -> float: """ Get the debt to equity ratio of a given stock Parameters: stock (str): ticker string of the stock Returns: (float): the total liabilities of the stock divided by shareholder equity """ if self.pre_built: return self.debt_to_equity self._check_args(stock) return get_data_points(stock, SC.DEBT_TO_EQUITY)
def get_beta(self, stock=None) -> float: """ Get the beta value of a given stock Refers to the volatility of a stock beta > 1: more volatile beta < 1: less volatile Parameters: stock (str): ticker string of the stock Returns: (float): the beta value of the stock """ if self.pre_built: return self.beta self._check_args(stock) return get_data_points(stock, SC.BETA)
def build(self, stock): """ Preload all important metrics to avoid making multiple api calls for the same information Parameters: stock (str): ticker string of the stock """ self.stock = Stock(stock) self.pe_ratio = self.stock.get_quote()[SC.PE_RATIO] self.price_to_book = get_data_points(stock, SC.PRICE_TO_BOOK) self.debt_to_equity = get_data_points(stock, SC.DEBT_TO_EQUITY) self.peg = get_data_points(stock, SC.PEG) self.beta = get_data_points(stock, SC.BETA) self.cash_flow = get_data_points(stock, SC.CASH_FLOW) self.price_to_sales = get_data_points(stock, SC.PRICE_TO_SALES) self.pre_built = True
def get_info(message): if message in [ 'address', 'ZIP', 'EMPLOYEES', 'EXCHANGE', 'WEBSITE', 'PHONE', 'WEEK52HIGH', 'WEEK52LOW' ]: return get_data_points(suggestions[0], key=message, token="sk_9520be7c9fdb4ae5ad5055d91ce95721", output_format='pandas') else: stock = Stock(suggestions[0], token="sk_9520be7c9fdb4ae5ad5055d91ce95721") quote = stock.get_quote() if message == 'companyName': return quote['companyName'] elif message == 'logo': return 'url: ' + stock.get_logo()['url'] elif message == 'latestPrice': return quote['latestPrice'] elif message == 'avgTotalVolume': return quote['avgTotalVolume'] elif message == 'news': news = stock.get_news() return '\n'.join([ 'title: ' + n['headline'] + '\nurl: ' + n['url'] for n in news[:5] ]) elif message == 'history_price': history_price = stock.get_historical_prices() return '\n'.join([ 'date: ' + p['date'] + '\nopen: ' + str(p['open']) + '\nclose: ' + str(p['close']) + '\nhigh: ' + str(p['high']) + '\nlow: ' + str(p['low']) for p in history_price[-5:] ]) else: # sharesOutstanding return stock.get_key_stats()['sharesOutstanding']
def test_no_symbol_fails(self): with pytest.raises(TypeError): get_data_points()
def test_get_one_data_point2(self): data = get_data_points("AAPL", "ZIP", output_format="pandas") assert isinstance(data, str)
def test_get_one_data_point(self): data = get_data_points("AAPL", "ZIP") assert isinstance(data, str)
def test_all_data_points(self): data = get_data_points("AAPL") assert isinstance(data, pd.DataFrame) assert "key" in data.columns
def test_get_one_data_point_pandas(self): # pandas should also be str data = get_data_points("AAPL", "ZIP", output_format='pandas') assert isinstance(data, str)
def test_all_data_points_pandas(self): data = get_data_points("AAPL", output_format='pandas') assert isinstance(data, pd.DataFrame) assert "ZIP" in data.columns
def test_all_data_points(self): data = get_data_points("AAPL") assert isinstance(data, list) assert isinstance(data[0], dict) assert "weight" in data[1]