How to get actual stock prices with yfinance?

Viewed 29984
import yfinance as yf

stock = yf.Ticker("ABEV3.SA")

data1= stock.info


print(data1)

There is "bid" and "ask", but no actual stock price.

11 Answers

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)

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'))

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

Try this:

import datetime
import yfinance as yf
now = datetime.datetime.now().strftime("%Y-%m-%d")
data = yf.Ticker("ABEV3.SA")
data = data.history(start="2010-01-01",  end=now)
print(df)

this is what looks like realtime quote to me:

import yfinance as yf
yca = yf.Ticker("YCA.L").history(interval="1m", period = "1d")
yca['Close'][-1]

yfinance has download function which let's you download the stock price data for a specified period. for e.g. I'll use the same stock that you wanted data for.

import yfinance as yf
data = yf.download("ABEV3.SA", start="2020-03-01", end="2020-03-30")

above line downloads data for march month as the specified date is that.

the data will be a pandas dataframe so you can directly use that for operation.

hope this helps.

The bid and ask prices are actually what are quoted on the exchange. A bid price is what a market maker is prepared to pay to buy shares, an ask is the price market makers require before selling. The spread is the difference between bid and ask.

What is usually referred to as the stock price is an average of the bid and ask prices. How the average is calculated depends on the exchange. If your feed does not offer a mid price provided by the exchange, then for many purposes it is sufficient to take the mean of the bid and ask.

Opening and closing prices are also determined by the exchange and may not be the first or last trades, but an average of the first or last 15 minutes trading, or may include after-hours prices.

Some details of how the LSE specifies ticker data: LSE ticker data

And, if you want to get into the nitty-gritty, a bit more detail on how orders are matched and generate price data:

Idiots Guide to the London Stock Exchange's SETSmm

Below code will get current price for list of symbols and add all result in dict.

import yfinance as yf

symbols = ["TSLA", "NIO"]
result = {}
for symbol in symbols:
    data = yf.Ticker(symbol)
    today_data = data.history(period='1d')
    result[symbol] = round((today_data['Close'][0]),2)
print(result)

Try this to get current price for multiples stocks:

stocks = ['PETR4.SA', 'ELET3.SA', 'VALE3.SA']

df = yf.download(' '.join(stocks), period='1d', progress=False)
df = df['Close']

for stock in stocks:
    if stock not in df or len(df[stock]) == 0: # this verification is important if trading session is closed
        continue
    quote = df[stock][0]
    print('%s = %.2f'%(stock, quote))

Okay, so you want to obtain the current (latest) value.
That's relatively simple, just one single line that gets the history of the stock of 1 day.

symbol = "AAPL"
stock = yf.Ticker(symbol)
latest_price = stock.history(period='1d')['Close'][0]

# Completely optional but I recommend having some sort of round(er?).
# Dealing with 148.60000610351562 is a pain.
estimate = round(latest_price, 2) 

print (estimate)

You should also put this in a function to make it more universal.

NOTE: My previous answer recommended the use of AlphaAdvantage, still an option on the table but it's limited to 5 reqs per minute. I changed my answer but you can get a TL;DR of it here:
Use requests and json, pull data, format, list comprehension(?)

I know theres better answers than this and probably very similar ones to this, this is just a personal method I prefer.

Related