refactor: Update get_current_prices to use fast_info for reliable price fetching
This commit is contained in:
parent
3a985aa1f5
commit
933141618b
@ -404,28 +404,14 @@ def get_open_trades():
|
||||
|
||||
def get_current_prices(tickers: list) -> dict:
|
||||
"""Get current prices for multiple tickers using yfinance"""
|
||||
try:
|
||||
# Create a space-separated string of tickers
|
||||
ticker_str = " ".join(tickers)
|
||||
# Get data for all tickers at once
|
||||
data = yf.download(ticker_str, period="1d", interval="1m", group_by='ticker')
|
||||
|
||||
prices = {}
|
||||
if len(tickers) == 1:
|
||||
# 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 {}
|
||||
prices = {}
|
||||
for ticker in tickers:
|
||||
try:
|
||||
stock = yf.Ticker(ticker)
|
||||
prices[ticker] = stock.fast_info["last_price"]
|
||||
except Exception as e:
|
||||
print(f"Error getting price for {ticker}: {e}")
|
||||
return prices
|
||||
|
||||
def get_trade_history(limit: int = 50):
|
||||
with create_client() as client:
|
||||
|
||||
Loading…
Reference in New Issue
Block a user