Spaces:
Build error
Build error
File size: 2,494 Bytes
37d460f 878dd56 37d460f 878dd56 37d460f 65c8966 878dd56 37d460f 878dd56 37d460f 878dd56 37d460f 878dd56 37d460f 65c8966 37d460f |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# tools.py
import yfinance as yf
def get_price(ticker):
"""Get the current price of a stock by its ticker symbol."""
try:
# Clean the ticker symbol
ticker = ticker.strip().upper()
print(f"[Debug] Fetching price for ticker: {ticker}")
stock = yf.Ticker(ticker)
print(f"[Debug] Created yfinance Ticker object for {ticker}")
# Get the stock info
info = stock.info
print(f"[Debug] Retrieved stock info: {info}")
if 'regularMarketPrice' not in info or info['regularMarketPrice'] is None:
print(f"[Debug] No regularMarketPrice found for {ticker}")
return f"Could not get the current price for {ticker}. Please verify the ticker symbol."
price = info['regularMarketPrice']
company_name = info.get('longName', ticker)
print(f"[Debug] Found price for {company_name} ({ticker}): ${price}")
return f"The current price of {company_name} ({ticker}) is ${price:.2f}"
except Exception as e:
print(f"[Debug] Error in get_price for {ticker}: {str(e)}")
return f"Error getting price for {ticker}: {str(e)}"
def buying_power_tool(query):
"""Calculate how many shares you can buy with a specified dollar amount."""
try:
# Parse input
if ',' not in query:
return "Please provide input in the format: TICKER,AMOUNT (e.g., 'AAPL,20000')"
ticker, amount = query.split(',', 1)
ticker = ticker.strip().upper()
try:
amount = float(amount.strip())
except ValueError:
return f"Invalid amount provided: {amount}. Please provide a valid number."
if amount <= 0:
return "Please provide a positive dollar amount."
# Get stock price
stock = yf.Ticker(ticker)
info = stock.info
if 'regularMarketPrice' not in info or info['regularMarketPrice'] is None:
return f"Could not get the current price for {ticker}. Please verify the ticker symbol."
price = info['regularMarketPrice']
company_name = info.get('longName', ticker)
shares = int(amount // price)
return f"With ${amount:,.2f}, you can buy {shares:,} shares of {company_name} ({ticker}) at ${price:.2f} per share."
except Exception as e:
return f"Error calculating buying power: {str(e)}"
|