Getting Peak Value and Closing for a Stock
This is helpful while filing IT returns.
from datetime import datetime
import yfinance as yf
for year in range(2016, 2022):
start_date = datetime(year, 1, 1)
end_date = datetime(year, 12, 31)
data = yf.download('<Stock Ticker>', start=start_date, end=end_date)print('Year %s, Peak %s, Close %s' % (year, max(data.High), data.Close[-1]))
Getting the closing on a particular date
from datetime import datetime
import yfinance as yf
dates = ["2016-03-01",
"2016-06-01",
"2018-02-28",
"2018-08-31",
"2019-02-28",
"2017-07-31",
"2017-01-31",
"2017-01-31",
"2018-02-28",
"2016-07-31",
"2016-01-31",]
# Obtain all the data for X years.
start_date = datetime(2015, 1, 1)
end_date = datetime(2022, 11, 1)
# In the example below we're getting values for USD/INR
data = yf.download('INR=X', start=start_date, end=end_date)
# Reset Index so that Date can be queried.
df = data.reset_index()
for d in df.values:
if str(d[0].date()) in dates:print(d[0], d[4])
Labels: yfinance stocks peak close value