资源描述:
《python使用用Wind接口获取全部A股历史交易数据.doc》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、python使用用Wind接口获取全部A股历史交易数据果然是中文编码的问题。搞定了,新鲜出炉一个小脚本。谢谢各位大神的解答。"""@Author:wenrongli@Date:2014-11-18@Version:V1.0"""#!/user/python#-*-coding:gb2312-*-importosimportsysimportgetoptfromWindPyimport*fromdatetimeimport*classLoadHisInfo:def__init__(self):self.Out
2、putDir=r'E:'self.StockCodeList=[]defStartWind(self):w.start()defLoadStockList(self):res=w.wset("SectorConstituent","date=;sector=全部A股")ifres.ErrorCode!=0:print('Error['+str(res.ErrorCode)+'][loadstockcodelistfail]')sys.exit()foriinrange(0,len(res.Data[0]
3、)):forkinrange(0,len(res.Fields)):if(res.Fields[k]=='date'):date=res.Data[k][i]if(res.Fields[k]=="wind_code"):code=res.Data[k][i]if(res.Fields[k]=="sec_name"):name=res.Data[k][i]self.StockCodeList.append(code)defLoadHisInfo(self):forcodeinself.StockCodeLis
4、t:outputPath=self.OutputDir+code+r'.txt'data=w.wsd(code,"open,high,low,close","","",showblank=0)ifdata.ErrorCode!=0:print('Error['+str(data.ErrorCode)+'][loadhistoryinfofail]')sys.exit()fOutputObj=open(outputPath.encode('gbk'),'a')foriinrange(0,len(data.
5、Data[0])):date=str(data.Times[i])forkinrange(0,len(data.Fields)):if(data.Fields[k]=="OPEN"):openPrice=data.Data[k][i]if(data.Fields[k]=="LOW"):lowPrice=data.Data[k][i]if(data.Fields[k]=="HIGH"):highPrice=data.Data[k][i]if(data.Fields[k]=="CLOSE"):closePric
6、e=data.Data[k][i]if(openPrice==0)or(lowPrice==0)or(highPrice==0)or(closePrice==0):continueresult=date[0:10]+","+str(openPrice)+","+str(highPrice)+","+str(lowPrice)+","+str(closePrice)+""fOutputObj.write(result)fOutputObj.close()defRun(self):self.StartWin
7、d()self.LoadStockList()self.LoadHisInfo()if__name__=='__main__':loadInfoObj=LoadHisInfo()iflen(sys.argv)<3:print("pythonLoadWindInfo.py-ooutPutDir")sys.exit()opts,args=getopt.getopt(sys.argv[1:],"o:")forop,valueinopts:ifop=="-o":loadInfoObj.OutputDir=va
8、lueelse:print("pythonLoadWindInfo.py-ooutPutDir")sys.exit()loadInfoObj.Run()---------------------------------------------------------------------------------------------------------------------本来想获取A股股票代码列表,通