ranaroussi / yfinance

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

ConnectionError #2024

Open BeachGuy007 opened 3 months ago

BeachGuy007 commented 3 months ago

Describe bug

1 Failed download: ['UPRO']: ConnectionError(MaxRetryError('HTTPSConnectionPool(host=\'fc.yahoo.com\', port=443): Max retries exceeded with url: / (Caused by NameResolutionError("<urllib3.connection.HTTPSConnection object at 0x000001AD4DB41FD0>: Failed to resolve \'fc.yahoo.com\' ([Errno 11001] getaddrinfo failed)"))'))

This happens for all tickers. UPRO is just an example.

Simple code that reproduces your problem

import yfinance as yf import pandas as pd

def fetch_data(tickers): ticker_data = {} for ticker in tickers: try: data = yf.download(ticker, 5d, 1m, threads=True) if not data.empty: ticker_data[ticker] = data['Close'] else: ticker_data[ticker] = None except Exception as e: ticker_data[ticker] = None return ticker_data

def update_data(tickers): ticker_data = fetch_data(tickers)

for ticker, data in ticker_data.items():
    if data is not None and len(data) > 0:
        latest_price = data.iloc[-1]
    else:
        latest_price = 'N/A'

df = pd.DataFrame(data_rows, columns=["Ticker", "Price"])

def run_script(): tickers = ["UPRO"] update_data(tickers)

if name == "main": run_script()

Debug log

1 Failed download: ['UPRO']: ConnectionError(MaxRetryError('HTTPSConnectionPool(host=\'fc.yahoo.com\', port=443): Max retries exceeded with url: / (Caused by NameResolutionError("<urllib3.connection.HTTPSConnection object at 0x000001AD4DB41FD0>: Failed to resolve \'fc.yahoo.com\' ([Errno 11001] getaddrinfo failed)"))'))

[*****100%%**] 1 of 1 completed

1 Failed download: ['UPRO']: ConnectionError(MaxRetryError('HTTPSConnectionPool(host=\'fc.yahoo.com\', port=443): Max retries exceeded with url: / (Caused by NameResolutionError("<urllib3.connection.HTTPSConnection object at 0x000001AD4DC65A10>: Failed to resolve \'fc.yahoo.com\' ([Errno 11001] getaddrinfo failed)"))'))

Bad data proof

No response

yfinance version

0.2.41

Python version

3.11

Operating system

WIN11

BeachGuy007 commented 3 months ago

Mysteriously working now

BeachGuy007 commented 3 months ago

Reappeared again today. Was problematic before open yesterday (9:30am EDT), then mysteriously resolved and worked the rest of the day. Today worked before 9:30am but then after 9:30am get error.

BeachGuy007 commented 3 months ago

Happened again this morning. All quotes N/A

BeachGuy007 commented 3 months ago

Working now. I guess intermittent issue? Could be Yahoo servers?

BeachGuy007 commented 3 months ago

Happening again (5am). Issue with Yahoo servers? Throttling?

ValueRaider commented 3 months ago

Are you spamming? https://github.com/ranaroussi/yfinance/discussions/1513

BeachGuy007 commented 3 months ago

Ah no. Real issue. Worked fine all day yesterday. Worked this morning for a while then N/A now.

BeachGuy007 commented 3 months ago

Now some quotes coming through. I think it's yahoo servers(?).

ValueRaider commented 3 months ago

yfinance stores cookie here: https://github.com/ranaroussi/yfinance#persistent-cache-store - does deleting it help?

BeachGuy007 commented 3 months ago

I am on WIN11 and there is no directory called py-yfinance here: Windows = C:/Users/<USER>/AppData/Local/py-yfinance

I also don't have yf.set_tz_cache_location("custom/cache/location")

BeachGuy007 commented 3 months ago

All quotes were N/A a second ago and just now flashed real quote.

ValueRaider commented 3 months ago

I also don't have yf.set_tz_cache_location

... how did you check yfinance version?

BeachGuy007 commented 3 months ago

PS C:\Users\Bill\Documents\Python> pip show yfinance Name: yfinance Version: 0.2.41 Summary: Download market data from Yahoo! Finance API Home-page: https://github.com/ranaroussi/yfinance Author: Ran Aroussi Author-email: ran@aroussi.com License: Apache Location: C:\Users\Bill\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages Requires: beautifulsoup4, frozendict, html5lib, lxml, multitasking, numpy, pandas, peewee, platformdirs, pytz, requests Required-by: PS C:\Users\Bill\Documents\Python>

ValueRaider commented 3 months ago

PIP can be wrong. Use the method in the new issue form.

BeachGuy007 commented 3 months ago

Back to N/As again

BeachGuy007 commented 3 months ago

import yfinance as yf ; print(yf.version) 0.2.41

ValueRaider commented 3 months ago

Regardless, if you don't have yf.set_tz_cache_location then your version must be almost 1-year old or corrupt. https://github.com/ranaroussi/yfinance/commit/59af19d84c73e026730ac2c48310316011fe7efd

BeachGuy007 commented 3 months ago

Ok I uninstalled yfinance rebooted and reinstalled yfinance. Quotes seem to be working now. Do you think that should clear the issue? I'll let you know if something happens. Thanks for all your help!

pip uninstall yfinance reboot pip install yfinance

BeachGuy007 commented 3 months ago

New error even after uninstalling/reinstalling.

BeachGuy007 commented 3 months ago

1 Failed download: 2024-08-21 08:17:14,639:ERROR:['UPRO']: YFPricesMissingError('$%ticker%: possibly delisted; No price data found (period=1d)')

Happens to any/all tickers

BeachGuy007 commented 3 months ago

Maybe it's their severs? Right at 9:30am all quotes worked today.

BeachGuy007 commented 3 months ago

1/2 an hour into the trading day and all quotes working fine. Must be something sporadic with Yahoo servers during ETH.

BeachGuy007 commented 1 month ago

I wrote some reconnection python code and it seems to be working with that.

def handle_connection_error(self):
    if not self.connection_active:
        if self.reconnect_attempts < self.max_reconnect_attempts:
            self.reconnect_attempts += 1
            print(f"Connection lost. Attempting to reconnect (Attempt {self.reconnect_attempts}/{self.max_reconnect_attempts})...")
            self.display_reconnecting()
            self.root.after(self.reconnect_delay, self.update_data)  # Wait before retrying
        else:
            print("Max reconnection attempts reached. Please check your internet connection.")
            self.display_connection_error()
    else:
        self.reconnect_attempts = 0

def display_reconnecting(self):
    self.ax.clear()
    self.ax.text(0.5, 0.5, f"Reconnecting...\nAttempt {self.reconnect_attempts}/{self.max_reconnect_attempts}",
                 ha='center', va='center', color='yellow')
    self.canvas.draw()
    self.equity_label.configure(text="Reconnecting...", fg='yellow')
    self.change_from_purchase_label.configure(text="", fg='yellow')
    self.change_label_today.configure(text="", fg='yellow')

def display_connection_error(self):
    self.ax.clear()
    self.ax.text(0.5, 0.5, "Connection lost.\nUnable to fetch data.\nPlease check your internet connection.",
                 ha='center', va='center', color='red')
    self.canvas.draw()
    self.equity_label.configure(text="Connection Error", fg='red')
    self.change_from_purchase_label.configure(text="", fg='red')
    self.change_label_today.configure(text="", fg='red')
bastienjalbert commented 2 days ago

I don't think my root cause was the same as your but if you use custom DNS server (for example pihole) fc.yahoo.com could be blocked. So ensure you can open and connect to the url from the device and you may check your dns.

BeachGuy007 commented 2 days ago

Yes, I use unbound on router and when I connect to fc.yahoo.com it redirects to https://www.yahoo.com/. I tested with the script below (see response). But I don't think unbound is the problem as my scripts work fine on Linux OS.

import requests

def check_connection(url): try:

Send a GET request to the URL

    response = requests.get(url, timeout=10)  # Set a timeout to prevent hanging
    # Check if the response status code is 200 (OK)
    if response.status_code == 200:
        print(f"Successfully connected to {url}.")
    else:
        print(f"Failed to connect to {url}. HTTP Status Code: {response.status_code}")
except requests.exceptions.Timeout:
    print(f"Connection to {url} timed out.")
except requests.exceptions.RequestException as e:
    print(f"Failed to connect to {url}. Error: {e}")

URL to check

url_to_check = "fc.yahoo.com"

Check the connection to the URL

check_connection(url_to_check)

Failed to connect to https://fc.yahoo.com. HTTP Status Code: 404