How to get actual stock prices with yfinance?
Try this:
import yfinance as yf
stock = yf.Ticker("ABEV3.SA")
price = stock.info['regularMarketPrice']
print(price)
This method returns the most updated value in my testing.
def get_current_price(symbol):
ticker = yf.Ticker(symbol)
todays_data = ticker.history(period='1d')
return todays_data['Close'][0]
print(get_current_price('TSLA'))
I used this filtering combination to get only the last quote.
import yfinance as yf
tickers = ['ABEV3.SA']
for ticker in tickers:
ticker_yahoo = yf.Ticker(ticker)
data = ticker_yahoo.history()
last_quote = data['Close'].iloc[-1]
print(ticker, last_quote)
To get the last closing price use this:
import yfinance as yf
tickerSymbol = 'AMD'
tickerData = yf.Ticker(tickerSymbol)
todayData = tickerData.history(period='1d')
todayData['Close'][0] #use print() in case you're testing outside a interactive session