def portfolio(request, portfolio_pk): portfolio = Portfolio.objects.get(pk=portfolio_pk) check_ownership(request, portfolio) stock_not_found = False quote_name = request.POST.get('quote', None) if quote_name: quote_name = quote_name.upper() yf = YahooFinancials(quote_name) exchange = yf.get_stock_exchange() try: stock = Stock.objects.get(name=quote_name) except Stock.DoesNotExist: stock = Stock.objects.create( name=quote_name, stock_exchange=yf.get_stock_exchange(), market_cap=convert_number(yf.get_market_cap()), last_price=convert_number(yf.get_current_price()) ) PortfolioStock.objects.get_or_create(stock=stock, portfolio=portfolio) stocks = PortfolioStock.objects.filter(portfolio=portfolio).order_by("-stock__value_score") operations = Operation.objects.filter(portfolio_stock__portfolio=portfolio)[0:10] total_value, total_spent, profit = portfolio.balance_sheet() if total_spent != 0: profit_purcent = round(profit / total_spent * 100, 2) else: profit_purcent = 0 total_cash_value = total_value + portfolio.cash c = { 'quote_name': quote_name, 'stock_not_found': stock_not_found, 'portfolio':portfolio, 'stocks': stocks, 'operations':operations, 'total_value':total_value, 'total_spent':total_spent, 'profit':profit, 'profit_purcent':profit_purcent, 'total_cash_value':total_cash_value, } c.update(csrf(request)) return render_to_response('portfolio.html', c)
class YahooFinancialEngine(AbstractEngine): """description of class""" StockName: str PeRatio: float FromDate: date FromDateStr: str ToDate: date ToDateStr: str Financial: YahooFinancials HistoricalPriceDic: dict FinancialTypeDic: dict def __init__(self, yahoo_ticker: YahooTicker): self.__dateFormat: str = '%Y-%m-%d' self.__ticker = yahoo_ticker self.__setFrom(yahoo_ticker.DateTimeFrom) self.__setTo(yahoo_ticker.DateTimeTo) self.Financial = YahooFinancials(self.__ticker.TickerName) self.HistoricalPriceDic = self.Financial.get_historical_price_data(self.FromDateStr, self.ToDateStr, 'daily')[self.__ticker.TickerName] self.__setStockName() self.__setPeRatio() self.FinancialTypeDic = self.Financial.YAHOO_FINANCIAL_TYPES def __setStockName(self): self.StockName = ( 'NA' if self.Financial.get_stock_exchange() is None else self.Financial.get_stock_exchange() ) def __setPeRatio(self): self.PeRatio = ( 0.0 if self.Financial.get_pe_ratio() is None else self.Financial.get_pe_ratio() ) def __setFrom(self, from_date: date): self.FromDate = from_date self.FromDateStr = from_date.strftime(self.__dateFormat) def __setTo(self, to_date: date): self.ToDate = to_date self.ToDateStr = to_date.strftime(self.__dateFormat) def GetDailyHistoricalDataPrices(self, from_date: date, to_date: date): self.__setFrom(from_date) self.__setTo(to_date) return self.HistoricalPriceDic['prices']
def fetch_and_handle_index_info(thread_id, db, indices): for index in indices: log.info(log.FETCHER_SCRIPT_LOG_FILE, 'Thread ' + thread_id + ' fetching info for index: ' + index) index_info = YahooFinancials(index) stock_exchange = '' try: stock_exchange = index_info.get_stock_exchange() except Exception as e: log.error(log.FETCHER_SCRIPT_LOG_FILE, 'Thread ' + thread_id + ' was unable to store index information for: ' + index + ', failed to resolve index exchange') log.error(log.FETCHER_SCRIPT_LOG_FILE, 'Exception: ' + str(e)) if (stock_exchange != ''): handle_index_info(db, index, index_info)
def fetch_and_handle_stock_info(thread_id, db, stocks): for stock in stocks: log.info(log.FETCHER_SCRIPT_LOG_FILE, 'Thread ' + thread_id + ' fetching stock info for ticker: ' + stock) stock_info = YahooFinancials(stock) stock_exchange = '' try: stock_exchange = stock_info.get_stock_exchange() except Exception as e: log.error(log.FETCHER_SCRIPT_LOG_FILE, 'Thread ' + thread_id + ' was unable to store stock information for ticker: ' + stock + ', failed to resolve stock exchange') log.error(log.FETCHER_SCRIPT_LOG_FILE, 'Exception: ' + str(e)) if (stock_exchange != ''): handle_stock_info(db, stock, stock_info)