Search code examples
pythonstockyahoo-finance

Get last price of stock data in python


I have searched for this topic and I found some packages that are useful. All what I am trying to get is the last price of any specific ticker such as "MSFT" Here's a code that I found and it is good

import pandas_datareader as pdr
from datetime import datetime

ibm = pdr.get_data_yahoo(symbols='MSFT', start=datetime(2021, 3, 1), end=datetime(2021, 3, 12))
print(ibm['Adj Close'])

This works for range of dates. How can I get the last price only without hard-coding the start date or end date?


Solution

  • Just use tail keyword.

    from datetime import datetime, date
    
    ibm = pdr.get_data_yahoo(symbols='MSFT', start = date.today(), end = date.today())
    print(ibm['Adj Close'].tail(1))