| from langchain.tools import tool | |
| import yfinance as yf | |
| import datetime as dt | |
| def fetch_market_snapshot_tool(ticker: str) -> str: | |
| """ | |
| Pulls basic market snapshot with yfinance: price, change, volume, valuation. | |
| Returns a compact textual snapshot. | |
| """ | |
| tk = yf.Ticker(ticker) | |
| info = {} | |
| try: | |
| price = tk.fast_info.last_price | |
| prev_close = tk.fast_info.previous_close | |
| change = None | |
| if price is not None and prev_close: | |
| change = (price - prev_close) / prev_close * 100 | |
| info.update({ | |
| "price": price, "prev_close": prev_close, "pct_change": change, | |
| "market_cap": tk.fast_info.market_cap, "volume": tk.fast_info.last_volume, | |
| "currency": tk.fast_info.currency | |
| }) | |
| except Exception as e: | |
| return f"Market snapshot failed: {e}" | |
| lines = [f"MARKET SNAPSHOT for {ticker.upper()}"] | |
| lines.append(f"Price: {info.get('price')} {info.get('currency')}") | |
| if info.get("pct_change") is not None: | |
| lines.append(f"Day change: {info['pct_change']:.2f}%") | |
| lines.append(f"Market Cap: {info.get('market_cap')}") | |
| lines.append(f"Volume: {info.get('volume')}") | |
| return "\n".join(lines) |