Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

stock.shares returns None type sometimes #1087

Closed
proofofjogi opened this issue Oct 15, 2022 · 2 comments
Closed

stock.shares returns None type sometimes #1087

proofofjogi opened this issue Oct 15, 2022 · 2 comments

Comments

@proofofjogi
Copy link

proofofjogi commented Oct 15, 2022

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?

@vinicius-vargas
Copy link

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?

@ValueRaider
Copy link
Collaborator

ValueRaider commented Oct 28, 2022

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 ValueRaider closed this as not planned Won't fix, can't repro, duplicate, stale Feb 2, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants