首页 > 解决方案 > 即使有股票数据,如何用 pandas 和 excel 解决关键错误 5?

问题描述

我在 python 中有以下代码作为股票筛选器。

import pandas as pd
import numpy as np
import yfinance as yf
import datetime as dt
from pandas_datareader import data as pdr
import os 
from pandas import ExcelWriter


yf.pdr_override()#takes new changes in python into consideration
start = dt.datetime(2015,1,1)
now = dt.datetime.now()
ftypes = [(".xlsm","*.xlsx",".xls")]
filePath = r'C:\Users\Yatharth K\Desktop\n.xlsx'
stocklist = pd.read_excel(filePath)
stocklist = stocklist.head()
stocklist=stocklist.sort_index()

for i in range(0,50):
    
    try:
        stock = str(stocklist["Symbol"][i])
        df = pdr.get_data_yahoo(stock,start,now)
        dfmonth = df.groupby(pd.Grouper(freq = "M"))["High"].max()
        glDate = 0
        lastGLV = 0
        currentDate = ""
        currentGLV = 0
        
        for index, value in dfmonth.items():
            if(value>currentGLV):
                currentGLV = value
                currentDate = index
                counter = 0
            if(value<currentGLV):
                counter = counter + 1
                if(counter == 3 and ((index.month != now.month) or (index.year != now.year))):
                    glDate = currentDate
                    lastGLV = currentGLV
                    counter = 0
        over = now - glDate
        if(over.days <= 35 and lastGLV != 0):
            final.append("Ticker: " + str(stock + " " + str(lastGLV) + ": " + str(glDate)))
    except:
        print("No data")

我的 excel 文件在这里:https ://www.nasdaq.com/market-activity/stocks/screener 。我已经多次检查链接并且它有效。但我不断收到一个关键错误 5。有人说可能没有任何可用的股票数据,但我检查了 yahoo Finance pandas 数据阅读器,即使它们产生错误,我也可以访问大部分股票。我添加了一个 except 来消除错误,但它仍然没有解决问题。请帮忙。谢谢。

标签: pandas

解决方案


推荐阅读