Bshraman commited on
Commit
20d7dc7
·
verified ·
1 Parent(s): 5ac9c87

Update app.py

Browse files

Error Handling and more data

Files changed (1) hide show
  1. app.py +28 -9
app.py CHANGED
@@ -5,6 +5,10 @@ import pytz
5
  import yaml
6
  from tools.final_answer import FinalAnswerTool
7
  import yfinance as yf
 
 
 
 
8
 
9
  from Gradio_UI import GradioUI
10
 
@@ -35,25 +39,40 @@ def get_current_time_in_timezone(timezone: str) -> str:
35
  return f"Error fetching time for timezone '{timezone}': {str(e)}"
36
 
37
  @tool
38
- def get_current_stock_price(ticker_symbol: str) -> float:
39
  """
40
- Fetches the latest stock price for the given ticker symbol.
 
 
41
  Args:
42
  ticker_symbol: The stock ticker symbol to fetch the price for.
 
43
  Returns:
44
- float: The current stock price of the given ticker symbol.
 
 
45
  """
46
  try:
 
47
  stock = yf.Ticker(ticker_symbol)
48
- stock_data = stock.history(period="1d")
49
 
50
  if stock_data.empty:
51
- logger.warning(f"No data available for ticker symbol: {ticker_symbol}")
52
- return None
53
-
54
- current_price = stock_data['Close'].iloc[-1] # Get the most recent close price
 
 
 
 
55
 
56
- return float(current_price)
 
 
 
 
 
57
 
58
  except yf.exceptions.YFinanceException as yf_err:
59
  logger.error(f"YFinance error for {ticker_symbol}: {str(yf_err)}")
 
5
  import yaml
6
  from tools.final_answer import FinalAnswerTool
7
  import yfinance as yf
8
+ import logging
9
+
10
+ # Set up logging
11
+ logging.basicConfig(level=logging.INFO)
12
 
13
  from Gradio_UI import GradioUI
14
 
 
39
  return f"Error fetching time for timezone '{timezone}': {str(e)}"
40
 
41
  @tool
42
+ def get_current_price(ticker_symbol: str) -> Optional[dict]:
43
  """
44
+ Fetches the latest stock price for the given ticker symbol and calculates
45
+ the price change over the last 5 days.
46
+
47
  Args:
48
  ticker_symbol: The stock ticker symbol to fetch the price for.
49
+
50
  Returns:
51
+ dict: A dictionary containing the current stock price, the price change
52
+ over the last 5 days, and the percentage change.
53
+ Returns None if there's an error or no data is available.
54
  """
55
  try:
56
+ # Fetch stock data
57
  stock = yf.Ticker(ticker_symbol)
58
+ stock_data = stock.history(period="5d") # Fetch data for the last 5 days
59
 
60
  if stock_data.empty:
61
+ return {"error": f"No historical data available for {ticker_symbol}"}
62
+
63
+ # Get the current price (last close price)
64
+ current_price = stock_data['Close'].iloc[-1]
65
+
66
+ # Calculate the price change over the last 5 days
67
+ price_change = current_price - stock_data['Close'].iloc[0]
68
+ price_change_percent = (price_change / stock_data['Close'].iloc[0]) * 100
69
 
70
+ # Round all values to 2 decimal places
71
+ return {
72
+ "price": round(current_price, 2),
73
+ "price_change": round(price_change, 2),
74
+ "price_change_percent": round(price_change_percent, 2)
75
+ }
76
 
77
  except yf.exceptions.YFinanceException as yf_err:
78
  logger.error(f"YFinance error for {ticker_symbol}: {str(yf_err)}")