【发布时间】:2014-03-07 15:46:08
【问题描述】:
我的代码是关于循环 1200 多个股票代码并从 yahoo 提要中检索历史报价(160~200 天)。因为这需要时间,我将引号存储在 csv 中,只需下载时间增量的引号,即日期差异的任何引号或任何引号 csv 文件都丢失了......我使用了 pandas 函数调用 get_data_yahoo(stocknum ,开始,结束)。
但是,我发现整个过程所花的时间没有什么区别,似乎一天的 d/l 报价与 200 天的报价相同……熊猫是如何处理雅虎的股票报价的?还有其他更好的饲料吗?我可以提出任何建议或改进以加快该过程吗?
这里是我为检索股票报价所做的函数调用。
def readStockPrice(stock,period=200,delay=1):
#define the period for stock filtering
now=dt.date.today()
end=now.strftime("%Y-%m-%d")
#start=(now-dt.timedelta(days=period)).strftime("%Y-%m-%d")
if os.path.isfile('cache/'+stock+'.csv'):
df=pd.read_csv('cache/'+stock+'.csv',index_col=0,parse_dates=True)
lastrecorddate=df.index.to_pydatetime()[-1].date()
delta=(now-lastrecorddate).days
if delta>delay:
#print("retrieving "+stock+" quotes from the web")
start=(lastrecorddate+dt.timedelta(days=1)).strftime("%Y-%m-%d")
try:
df_delta=web.get_data_yahoo(stock,start,end)
df=df.append(df_delta)
df_delta.to_csv('cache/'+stock+'.csv',header=False,mode='a')
except IOError:
return pd.DataFrame()
else:
#print("retrieving "+stock+" quotes from the web")
start=(now-dt.timedelta(days=period)).strftime("%Y-%m-%d")
try:
df=web.get_data_yahoo(stock,start,end)
if not df.empty:
df.to_csv('cache/'+stock+'.csv')
except IOError:
return pd.DataFrame()
return df
【问题讨论】: