refactor: Update get_current_prices to use fast_info for reliable price fetching

This commit is contained in:
Bobby (aider) 2025-02-10 11:21:23 -08:00
parent 3a985aa1f5
commit 933141618b

View File

@ -404,28 +404,14 @@ def get_open_trades():
def get_current_prices(tickers: list) -> dict: def get_current_prices(tickers: list) -> dict:
"""Get current prices for multiple tickers using yfinance""" """Get current prices for multiple tickers using yfinance"""
try: prices = {}
# Create a space-separated string of tickers for ticker in tickers:
ticker_str = " ".join(tickers) try:
# Get data for all tickers at once stock = yf.Ticker(ticker)
data = yf.download(ticker_str, period="1d", interval="1m", group_by='ticker') prices[ticker] = stock.fast_info["last_price"]
except Exception as e:
prices = {} print(f"Error getting price for {ticker}: {e}")
if len(tickers) == 1: return prices
# Handle single ticker case
ticker = tickers[0]
if not data.empty:
prices[ticker] = data['Close'].iloc[-1]
else:
# Handle multiple tickers
for ticker in tickers:
if (ticker, 'Close') in data:
prices[ticker] = data[ticker]['Close'].iloc[-1]
return prices
except Exception as e:
print(f"Error fetching current prices: {e}")
return {}
def get_trade_history(limit: int = 50): def get_trade_history(limit: int = 50):
with create_client() as client: with create_client() as client: