cgoldberg / ystockquote

Fetch stock quote data from Yahoo Finance
534 stars 181 forks source link

Why my python (Spyder) got stuck (i.e. did not return result) when I was trying to run the code of retrieving stock data from yahoo finance #60

Open kk0186 opened 2 years ago

kk0186 commented 2 years ago

I have a list of US stocks ( around 3000 stocks) in excel and I want to get the stock price and other related daily stock info on each stock in certain time period. So I wrote a for loop to get all related stock info on each stock ( and I have tried both yahoo_fin.stock_info and yfinance packages). But then it just got stuck at certain stock and never return the result. I am using spyder for my python coding. below is the code:

import pandas as pd
import numpy as np
import yahoo_fin.stock_info as si

ticker_list = pd.read_excel(r'C:\Users\user\Desktop\ticker_list.xlsx',engine="openpyxl")

n=[]
et=[] 
df_empty=[]
for i in range(len(ticker_list)): 
    try:
        df = df_empty[0:0]
        df=si.get_data(ticker_list.iloc[i]['ticker'], start_date='2021-09-30', end_date='2022-04-25') 
# I used yf.download for using yfinance package
        mkt_cap=si.get_quote_table(ticker_list.iloc[i]['ticker'])["Market Cap"]
        df.insert (7,"mkt_cap",mkt_cap) 
        print(ticker_list.iloc[i]['ticker'])
        df.replace(np.inf, np.nan)        
        df = df.dropna()
        df.reset_index(inplace=True)   
        n.append(df)   
    except:
        et.append(ticker_list.iloc[i]['ticker'])
        print('error: ' + ticker_list.iloc[i]['ticker'])
        pass
print(n)

when I run the code below what it shows in my console:

Python 3.8.13 (default, Mar 28 2022, 06:59:08) [MSC v.1916 64 bit (AMD64)]
Type "copyright", "credits" or "license" for more information.

IPython 7.31.1 -- An enhanced Interactive Python.

runfile('C:/Users/USER/get_all_stock_data.py', wdir='C:/Users/USER')
BYDDY
CHTR
JD
KYCCF
RY-PT
GE
BP
MO
VLKPF
SMAWF
AMAT

I tired many times already , everytime it just stuck on different ticker everytime. Also I have also tried to leave the code running for a day but it just stop it right there and never give me return. when I tried to debug, it also stop running at either the si.get.data line or si.get_quote_table line. I actually use this code since last year and it work fine, it just start not working when I run this code again last week. I also tried several other packages ( e.g. pandas_datareader.data) still not able to get the stock info. Appreciate if anyone can let me know what is the problem.