Spaces:
Sleeping
Sleeping
| import pandas as pd | |
| import yfinance as yf | |
| from datetime import datetime | |
| from config import Config | |
| def historical_return(company: yf.Ticker, history_years: int) -> float: | |
| history = company.history(period=f"{history_years}y") | |
| return (history["Close"].iloc[-1] - history["Close"].iloc[0]) / history["Close"].iloc[0] | |
| def get_financial_info(ticker: str, metrics: list) -> dict: | |
| try: | |
| company = yf.Ticker(ticker) | |
| current_price = company.info.get("currentPrice") | |
| if not current_price: | |
| return {metric: 'DELISTED' for metric in metrics} | |
| except: | |
| return {metric: 'DELISTED' for metric in metrics} | |
| financial_info = {} | |
| for metric in metrics: | |
| if company.info.get(metric, None): | |
| financial_info.update({metric: company.info.get(metric)}) | |
| continue | |
| if Config.FINANCE_HISTORICAL_RETURN in metric: | |
| financial_info.update({metric: historical_return(company, metric[-1])}) | |
| financial_info.update({Config.FINANCE_UPDATE_TIME: datetime.now().strftime("%d-%m-%Y")}) | |
| return financial_info # {price: 100, marketCap: 1000, update: 01-01-2021} | |