资源描述
python使用用Wind接口获取全部A股历史交易数据果然是中文编码的问题。搞定了,新鲜出炉一个小脚本。谢谢各位大神的解答。Author:wenrongliDate:2014-11-18Version:V1.0#!/user/python# -*-coding:gb2312-*-importosimportsysimportgetoptfromWindPyimport*fromdatetimeimport*classLoadHisInfo:def_init_(self):self.OutputDir=rE:self.StockCodeList=defStartWind(self):w.start()defLoadStockList(self):res=w.wset(SectorConstituent,date=;sector=全部A股)ifres.ErrorCode!=0:print(Error+str(res.ErrorCode)+loadstockcodelistfailn)sys.exit()foriinrange(0,len(res.Data0):forkinrange(0,len(res.Fields):if(res.Fieldsk=date):date=res.Datakiif(res.Fieldsk=wind_code):code=res.Datakiif(res.Fieldsk=sec_name):name=res.Datakiself.StockCodeList.append(code)defLoadHisInfo(self):forcodeinself.StockCodeList:outputPath=self.OutputDir+code+r.txtdata=w.wsd(code,open,high,low,close,19900101,showblank=0)ifdata.ErrorCode!=0:print(Error+str(data.ErrorCode)+loadhistoryinfofailn)sys.exit()fOutputObj=open(outputPath.encode(gbk),a)foriinrange(0,len(data.Data0):date=str(data.Timesi)forkinrange(0,len(data.Fields):if(data.Fieldsk=OPEN):openPrice=data.Datakiif(data.Fieldsk=LOW):lowPrice=data.Datakiif(data.Fieldsk=HIGH):highPrice=data.Datakiif(data.Fieldsk=CLOSE):closePrice=data.Datakiif(openPrice=0)or(lowPrice=0)or(highPrice=0)or(closePrice=0):continueresult=date0:10+,+str(openPrice)+,+str(highPrice)+,+str(lowPrice)+,+str(closePrice)+nfOutputObj.write(result)fOutputObj.close()defRun(self):self.StartWind()self.LoadStockList()self.LoadHisInfo()if_name_=_main_:loadInfoObj=LoadHisInfo()iflen(sys.argv)<3:print(pythonLoadWindInfo.py-ooutPutDir)sys.exit()opts,args=getopt.getopt(sys.argv1:,o:)forop,valueinopts:ifop=-o:loadInfoObj.OutputDir=valueelse:print(pythonLoadWindInfo.py-ooutPutDir)sys.exit()loadInfoObj.Run()本来想获取A股股票代码列表,通过文档例子,返回了错误码-103,没找到这个错误码的说明,w.wset(SectorConstituent,udate=20130608;sector=全部A股),现在只能通过同花顺导出股票代码列表,然后通过配置文件的形式来拉数据,哪位大神知道怎么使用这个接口的还请赐教,不胜感激这里分享一个python获取A股历史交易数据的例子。#!/user/python# -*-coding:utf-8-*-importmathimportosimportsysimportgetoptfromWindPyimport*fromdecimalimport*fromdatetimeimport*WorkSpace=E:WorkSpacedatadataconfigconfigStockConfig.txturl=fConfigObj=open(config,r)count=0forlineinfConfigObj.readlines():iflen(line)<8:continuecode=line2:8codeName=line2:8+.+line0:2outputPath=rE:/WorkSpace/data/+code+r.txtw.start()data=w.wsd(codeName,open,high,low,close,19900101,showblank=0)ifdata.ErrorCode!=0:print(errorcode:+str(data.ErrorCode)+n)sys.exit()fOutputObj=open(outputPath.encode(gbk),a)foriinrange(0,len(data.Data0):date=str(data.Timesi)forkinrange(0,len(data.Fields):if(data.Fieldsk=OPEN):openPrice=data.Datakiif(data.Fieldsk=LOW):lowPrice=data.Datakiif(data.Fieldsk=HIGH):highPrice=data.Datakiif(data.Fieldsk=CLOSE):closePrice=data.Datakiif(openPrice=0)or(lowPrice=0)or(highPrice=0)or(closePrice=0):continueresult=date0:10+,+str(openPrice)+,+str(highPrice)+,+str(lowPrice)+,+str(closePrice)+nfOutputObj.write(result)fOutputObj.close()fConfigObj.close()
展开阅读全文