pimvic commited on
Commit
d10434e
·
verified ·
1 Parent(s): 8c5c24b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +46 -0
app.py CHANGED
@@ -36,6 +36,52 @@ def get_current_time_in_timezone(timezone: str) -> str:
36
 
37
  final_answer = FinalAnswerTool()
38
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
39
  # If the agent does not answer, the model is overloaded, please use another model or the following Hugging Face Endpoint that also contains qwen2.5 coder:
40
  # model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud'
41
 
 
36
 
37
  final_answer = FinalAnswerTool()
38
 
39
+
40
+ @tool
41
+ def get_current_stock_price(ticker_symbol: str) -> Optional[dict]:
42
+ """
43
+ Get the latest stock price for the given ticker symbol and calculates
44
+ the price change over the last 5 days and retrun current stock price ,
45
+ price change and percentage change.
46
+ Args:
47
+ ticker_symbol: The stock ticker symbol to fetch the price for.
48
+ Returns:
49
+ price -> dict: A dictionary containing the current stock price, the price change
50
+ over the last 5 days, and the percentage change.
51
+ Returns None if there's an error or no data is available.
52
+ """
53
+ try:
54
+ # Fetch stock data
55
+ stock = yf.Ticker(ticker_symbol)
56
+ stock_data = stock.history(period="5d") # Fetch data for the last 5 days
57
+
58
+ if stock_data.empty:
59
+ return {"error": f"No historical data available for {ticker_symbol}"}
60
+
61
+ # Get the current price (last close price)
62
+ current_price = stock_data['Close'].iloc[-1]
63
+
64
+ # Calculate the price change over the last 5 days
65
+ price_change = current_price - stock_data['Close'].iloc[0]
66
+ price_change_percent = (price_change / stock_data['Close'].iloc[0]) * 100
67
+
68
+ # Round all values to 2 decimal places
69
+ price = {
70
+ "price": round(current_price, 2),
71
+ "price_change": round(price_change, 2),
72
+ "price_change_percent": round(price_change_percent, 2)
73
+ }
74
+ return price
75
+
76
+ except yf.exceptions.YFinanceException as yf_err:
77
+ logger.error(f"YFinance error for {ticker_symbol}: {str(yf_err)}")
78
+ except Exception as e:
79
+ logger.error(f"Unexpected error fetching price for {ticker_symbol}: {str(e)}")
80
+ return None
81
+
82
+ final_answer = FinalAnswerTool()
83
+
84
+
85
  # If the agent does not answer, the model is overloaded, please use another model or the following Hugging Face Endpoint that also contains qwen2.5 coder:
86
  # model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud'
87