pilwon / node-yahoo-finance

Yahoo Finance historical quotes and snapshot data downloader written in Node.js
495 stars 123 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 #85

Closed kk0186 closed 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.

gadicc commented 2 years ago

Hi @kk0186, I think you're reporting this against the wrong project, as you're writing in and importing a python library whereas this project is for node / javascript :sweat_smile:

So you can try check with the right project but I wonder if maybe your issue is exceeding Yahoo's rate limit and them blocking you. You could try insert a small delay (try 1s first to check, but probably 200ms might be ok) between requests and make sure that you don't execute more than say 4 simultaneous requests to Yahoo at a time. But yeah I don't know anything about the library you're using so YMMV :)

kk0186 commented 2 years ago

@gadicc apologize for my mistake, but appreciate your comment , I will tried to see if adding the delay would work. thank you very much

gadicc commented 2 years ago

:ok_hand::grin::pray: