Spaces:
Sleeping
Sleeping
Update tools/stock_price_tool.py
Browse files- tools/stock_price_tool.py +36 -2
tools/stock_price_tool.py
CHANGED
|
@@ -40,6 +40,40 @@ class StockPriceTool(Tool):
|
|
| 40 |
# If there's an error, log it and return None
|
| 41 |
print(f"Error fetching price for {ticker_symbol}: {str(e)}")
|
| 42 |
return None
|
| 43 |
-
|
| 44 |
def __init__(self, *args, **kwargs):
|
| 45 |
-
self.is_initialized = False
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
# If there's an error, log it and return None
|
| 41 |
print(f"Error fetching price for {ticker_symbol}: {str(e)}")
|
| 42 |
return None
|
| 43 |
+
|
| 44 |
def __init__(self, *args, **kwargs):
|
| 45 |
+
self.is_initialized = False
|
| 46 |
+
|
| 47 |
+
def forward_stage(self, ticker_symbol: str) -> Optional[Any]:
|
| 48 |
+
"""
|
| 49 |
+
Forward stage to handle further analysis or additional steps after fetching the stock price.
|
| 50 |
+
Args:
|
| 51 |
+
ticker_symbol (str): The stock ticker symbol.
|
| 52 |
+
Returns:
|
| 53 |
+
Any: Additional stock analysis or further actions based on the stock data.
|
| 54 |
+
Example:
|
| 55 |
+
>>> StockPriceTool().forward_stage("AAPL")
|
| 56 |
+
{"price": 150.25, "price_change": -0.5}
|
| 57 |
+
"""
|
| 58 |
+
# Fetch the current stock price
|
| 59 |
+
current_price = self._call(ticker_symbol)
|
| 60 |
+
|
| 61 |
+
if current_price is None:
|
| 62 |
+
return {"error": f"Unable to fetch data for {ticker_symbol}"}
|
| 63 |
+
|
| 64 |
+
# For forward analysis, we can fetch historical data for further insights
|
| 65 |
+
stock = yf.Ticker(ticker_symbol)
|
| 66 |
+
stock_data = stock.history(period="5d") # Fetch last 5 days of stock data
|
| 67 |
+
|
| 68 |
+
if stock_data.empty:
|
| 69 |
+
return {"error": f"No historical data available for {ticker_symbol}"}
|
| 70 |
+
|
| 71 |
+
# Calculate the price change over the last 5 days
|
| 72 |
+
price_change = current_price - stock_data['Close'].iloc[-2]
|
| 73 |
+
price_change_percent = (price_change / stock_data['Close'].iloc[-2]) * 100
|
| 74 |
+
|
| 75 |
+
return {
|
| 76 |
+
"price": current_price,
|
| 77 |
+
"price_change": price_change,
|
| 78 |
+
"price_change_percent": price_change_percent
|
| 79 |
+
}
|