def add_stock(market, ticker): try: stock = Stock(market, ticker) stock.generate_metrics() if stock.data and stock.data['price']: stock_list_entry = StockListEntry(market.upper(), ticker.upper()) db.session.add(stock_list_entry) db.session.commit() return stock except Exception as e: logger.error(traceback.format_exc())
def get_stocks_from_google(): try: stocks = [] stock_list_entries = StockListEntry.query.all() for stock_list_entry in stock_list_entries: stock = Stock(stock_list_entry.market, stock_list_entry.ticker) stock.generate_metrics() for key, value in stock.data.items(): setattr(stock_list_entry, key, value) db.session.commit() stocks.append(object_to_dict(stock_list_entry)) return stocks except Exception as e: logger.error(traceback.format_exc())
def test_generate_metrics(self): with self.client: stock = Stock('NASDAQ', 'AAPL') stock.generate_metrics() self.assertTrue(stock.data['price']) self.assertTrue(stock.data['current_ratio']) self.assertTrue(stock.data['quick_ratio']) self.assertTrue(stock.data['return_on_equity']) self.assertTrue(stock.data['debt_equity_ratio']) self.assertTrue(stock.data['net_profit_margin']) self.assertTrue(stock.data['free_cash_flow']) self.assertTrue(stock.data['price_to_earnings_ratio']) stock = Stock('NASDAQ', 'AAPLL') stock.generate_metrics() self.assertFalse(stock.data)
def test_price_to_earnings_ratio_success(self): with self.client: stock = Stock('NASDAQ', 'AAPL') self.assertTrue(stock._get_price_to_earnings_ratio())
def test_free_cash_flow_fail(self): with self.client: stock = Stock('NASDAQ', 'TSLAA') self.assertFalse(stock._compute_free_cash_flow())
def test_free_cash_flow_success(self): with self.client: stock = Stock('NASDAQ', 'TSLA') self.assertTrue(stock._compute_free_cash_flow())
def test_net_profit_margin_fail(self): with self.client: stock = Stock('NASDAQ', 'TSLAA') self.assertFalse(stock._compute_net_profit_margin())
def test_net_profit_margin_success(self): with self.client: stock = Stock('NASDAQ', 'TSLA') self.assertTrue(stock._compute_net_profit_margin())
def test_debt_equity_ratio_success(self): with self.client: stock = Stock('NASDAQ', 'TSLA') self.assertTrue(stock._compute_debt_equity_ratio())
def test_debt_equity_ratio_fail(self): with self.client: stock = Stock('NASDAQ', 'TSLAA') self.assertFalse(stock._compute_debt_equity_ratio())
def test_return_on_equity_fail(self): with self.client: stock = Stock('NASDAQ', 'TSLAA') self.assertFalse(stock._compute_return_on_equity())
def test_return_on_equity_success(self): with self.client: stock = Stock('NASDAQ', 'TSLA') self.assertTrue(stock._compute_return_on_equity())
def test_shareholders_equity_fail(self): with self.client: stock = Stock('NASDAQ', 'TSLAA') self.assertFalse(stock._compute_shareholders_equity())
def test_shareholders_equity_success(self): with self.client: stock = Stock('NASDAQ', 'TSLA') self.assertTrue(stock._compute_shareholders_equity())
def test_price_to_earnings_ratio_fail(self): with self.client: stock = Stock('NASDAQ', 'TSLAA') self.assertFalse(stock._get_price_to_earnings_ratio())