ranaroussi / yfinance

Download market data from Yahoo! Finance's API
https://aroussi.com/post/python-yahoo-finance
Apache License 2.0
14.96k stars 2.45k forks source link

Failed to download indices. #1285

Closed ChunxiaoYang closed 1 year ago

ChunxiaoYang commented 1 year ago

Hi! I got into trouble when I tried to download S&P500 Index history. The code for S&P500 in Yahoo Finance is ^GSPC, am I right? However I could download stocks like Apple, Amazon successfully. I don't know what is going on. Could you tell me how to fix it? Thanks a lot! aapl spx

ValueRaider commented 1 year ago

Run this, what happens?

from yfinance import utils as yfu

tz1 = yf.Ticker("^GSPC")._fetch_ticker_tz(debug_mode=True, proxy=proxy, timeout=10)
print("tz1 =", tz1)

tz2 = yfu.get_tz_cache().lookup("^GSPC")
print("tz2 =", tz2)

print("tz2 valid =", yfu.is_valid_timezone(tz2)
tz3 = yf.Ticker("^GSPC")._get_ticker_tz(debug_mode=True, proxy=proxy, timeout=10)
chrisvil commented 1 year ago

I made an update to newest version today and I have the same issue. All ticker data can be loaded but ^GSPC and ^NDX return an empty data frame. Everything worked fine until yesterday (until I made the update today).

chrisvil commented 1 year ago

I tried the code above (set proxy to any). tz1 returns none, tz2 returns 'America/New_York'

image
ChunxiaoYang commented 1 year ago

@ValueRaider Thanks for your help! Though it seems that it doesn't work for me, LOL. I couldn't download any stock data using your code "tz1" and "tz3"(Apple also failed, but I could download Apple using yf.download() so I think it's not the proxy's fault). Due to the internet policy, I couldn't connect to Yahoo without using proxy, so I don't know whether the code "tz2" works or not. : ) image

ChunxiaoYang commented 1 year ago

@ValueRaider And if I change the stock code into Apple(AAPL), code "tz3" returns "America/New_York". : ) image

ValueRaider commented 1 year ago

@chrisvil Interesting, _fetch_ticker_tz() should always return timezone if ticker valid.

  1. I edited my code, can you run 3rd print?
  2. Debug _fetch_ticker_tz() and see what Yahoo returns, specifically this line: data["chart"]["result"][0]["meta"]["exchangeTimezoneName"]
chrisvil commented 1 year ago

Mhh, now I get error messages

image
chrisvil commented 1 year ago

ValueRaider, can you receive valid data when you run simple code to download ^GSPC data? I'm doing a very simple call index_df = pdr.get_data_yahoo(index_name, start_date, end_date) and that worked until yesterday. All other ticker, except ^GSPC and ^NDX works.

ValueRaider commented 1 year ago

Yes works fine for me. Which makes debugging this tricky, because you need to help investigate.

ChunxiaoYang commented 1 year ago

@chrisvil Can you download other indices like "^DJI" or "^HSI" successfully? I couldn't download these two indices either. : (

chrisvil commented 1 year ago

Just tried, now nothing works (= no data) since I tried your debug code (I was able to download every S&P500 ticker an hour ago). I will restart my Mac completely and try again.

ChunxiaoYang commented 1 year ago

@ValueRaider Maybe the version of your yfinance package is an older one? Since @chrisvil said that yesterday an older version worked. My version of yfinance is 0.2.3.

chrisvil commented 1 year ago

Machine is back. Version 0.2.3 All S&P500 ticker can be downloaded. ^DJI and ^HSI cannot be downloaded (same issue as ^GSPC and ^NDX). I use the same function call for all.

chrisvil commented 1 year ago

I also changed my IP address to different country via VPN ... no difference.

chrisvil commented 1 year ago

I found a workaround ... I'm calling the function again when the data frame is empty and that seems to work. index_df = pdr.get_data_yahoo(index_name, start_date, end_date) if len(index_df) < 20: index_df = pdr.get_data_yahoo(index_name, start_date, end_date)

ChunxiaoYang commented 1 year ago

@chrisvil Wow! It really works!!! Many thanks for your warm help! Also many thanks to @ValueRaider ! You saved my final project! :)👍 So maybe pandas_datareader is necessary? I just installed the package pandas_datareader. It seems that I couldn't download indices without it. Anyway, I got the data.

The following is my code that works:

import yfinance as yf from pandas_datareader import data as pdr yf.pdr_override() index_df = pdr.get_data_yahoo('^GSPC', start="2021-12-31", end="2022-12-31") if len(index_df) < 20: index_df = pdr.get_data_yahoo("GSPC", start="2021-12-31", end="2022-12-31") print(index_df)

ValueRaider commented 1 year ago

get_data_yahoo doesn't do anything except call yfinance.download directly. Proof: pdr.get_data_yahoo == yf.download = True

So try with yf.download()

Also if either of you can dig into the yfinance source code and investigate what exactly is breaking, then we can fix.

ChunxiaoYang commented 1 year ago

@ValueRaider You are right! pdr.get_data_yahoo equals yf.download. Now my code yf.download() works too! So it's my fault, I didn't installed pandas_datareader at the beginning. After I installed pdr, everything works!

However a new issue appears: There is missing data when downloading index "^HSCI". I set the date range(start="2021-12-31", end="2022-12-31"), but the dataframe only includes data from 20130430 to 20210930, and 20210903-20210930 also disappears. I tried several times the results are the same. :( image

ValueRaider commented 1 year ago
df6 = pdr.get_data_yahoo(...

pandas-datareader is an entirely different project