pythonyfinance

yfinance not fetching the entire range of stock I'm asking it to


I'm new to the yfinance library, so I may just be being completely stupid here, but I'm trying tpo fetch 365 days worth of information, and when I request that from yfinance, it gives me 251 days. Similarly, when I ask for 10 years of information, or 3650 days, it only gives me 2516 days. Does anyone know how to fix this?

EDIT: I do know that msft is not the correct ticker for AT&T, but I am going straight off of a template and it's not going to cause any errors, I'll fix when I have the time

import yfinance as yf
msft = yf.Ticker("T")
# get all stock info
msft.info
historical_market_data = []
# get historical market data
hist = msft.history(period="1y")
days = 0
for i in range(len(hist)):
  days +=1
  historical_market_data.append([hist.High.iloc[i], hist.Low.iloc[i], hist.Open.iloc[i], hist.Close.iloc[i], days])
'''
Item 0: High of the day
Item 1: Low of the day
Item 2: Price at opening
Item 3: Price at closing
Item 4: Day number
'''
for i in historical_market_data:
  print(f"High: {i[0]}, Low: {i[1]}, Open: {i[2]}, Close: {i[3]}, Day: {i[4]}")

Solution

  • Stock data encompasses Monday-Friday, and excludes holidays, as that is when markets are open; holiday and weekend data would be duplicates of the previous days. Your data for a full year will not be 365 days, since the typical week is 5 days for the stock market.

    You are receiving 251 records; 251/365 is roughly 5/7, and if you include the ~10 days that the stock market is on holiday, you will land at the proper proportion.

    If you want 365 days of data just for testing, pick a crypto coin ticker (i.e. "BTC-USD") as they have 24/7 markets.