yfinance icon indicating copy to clipboard operation
yfinance copied to clipboard

stock.shares returns None type sometimes

Open realJogicodes opened this issue 3 years ago • 2 comments

Hi

I am looping over a long list of tickers to scrape data and every so often, the shares method returns a None type.

If I catch that and keep trying to run the method later in time (a minute, 2 minutes even), it keeps returning the None type over and over. If I interrupt manually and restart the script instead, odds are very high to getting a dataframe with the share info returned.

Here's part of my code:

import yfinance as yf
from pathlib import Path
import csv
import time
import json
from datetime import datetime

def make_path(ticker):
    subfolder = f'data/{ticker}'
    Path(subfolder).mkdir(parents=True, exist_ok=True)
    return subfolder

def get_shares(stock):
    return stock.shares

def safe_shares_data(ticker, stock):
    folder = make_path(ticker)
    time.sleep(0.5)
    shares = get_shares(stock)
    while shares is None:
        log(ticker, 'Trying to get share data again')
        time.sleep(20)
        shares = get_shares(stock)
    shares.to_csv(path_or_buf=f'{folder}/shares_out_{ticker}.csv')

def log(ticker, msg):
    now = datetime.now()
    message = f'{now.strftime("%Y-%m-%d %H:%M:%S")}: {ticker} - {msg}\n'
    with open('log.txt', 'a') as flog:
        flog.write(message)

tickers = ['AAPL', 'V', 'MA', 'HEI'] #EXAMPLE. tickers is a long list with thousands of stocks in alphabetical order

for ticker in tickers:
    time.sleep(1)
    stock = initialize_yf(ticker)
    info = stock.info
    print("Length of info:", len(info))
    if len(info) < 10:  # test for delisting.
        continue
    sector = info.get('sector')  # exclude 'Financial Services'
    time.sleep(0.5)
    if sector != "Financial Services":
        safe_shares_data(ticker, stock)

I have no idea what to make of it and why that happens. The shares method works well manually so I doubt that it's a bug. Is it that yahoo is kicking me off? But if that's the case, why can I re-take the script if i start it again?

realJogicodes avatar Oct 15 '22 15:10 realJogicodes

Hi everyone! I'm having the same issue here. Running it in a Google Colab notebook.

I also tried to use "time.sleep(0.5)" because I thought I was being kicked out of yahoo.

Can anyone help us?

vinicius-vargas avatar Oct 28 '22 22:10 vinicius-vargas

The solution to not get blocked by spam detection is to stop spamming. shares changes once a year, why can't you cache it somewhere? requests session is a basic dumb way to cache, works for most (all?) financial data, it's documented in the README.

ValueRaider avatar Oct 28 '22 23:10 ValueRaider