Python提取Wind数据
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
python使用用Wind接口获取全部A股历史交易数据14人已关注
果然是中文编码的问题。搞定了,新鲜出炉一个小脚本。谢谢各位大神的解答。
"""
@Author: wenrongli
@Date: 2014-11-18
@Version: V1.0
"""
#!/user/python
# -*- coding: gb2312 -*-
import os
import sys
import getopt
from WindPy import *
from datetime import *
class LoadHisInfo:
def __init__(self):
self.OutputDir=r'E:'
self.StockCodeList=[]
def StartWind(self):
w.start()
def LoadStockList(self):
res=w.wset("SectorConstituent","date=;sector=全部A股")
if res.ErrorCode != 0:
print('Error['+str(res.ErrorCode)+'][load stockcode list fail]\n') sys.exit()
for i in range(0,len(res.Data[0])):
for k in range(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)
def LoadHisInfo(self):
for code in self.StockCodeList:
outputPath=self.OutputDir+code+r'.txt'
data=w.wsd(code, "open,high,low,close", "19900101", "",showblank=0)
if data.ErrorCode != 0:
print('Error['+str(data.ErrorCode)+'][load history info fail]\n')
sys.exit()
fOutputObj=open(outputPath.encode('gbk'),'a')
for i in range(0,len(data.Data[0])):
date=str(data.Times[i])
for k in range(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"):
closePrice=data.Data[k][i]
if (openPrice==0)or (lowPrice==0)or(highPrice==0)or(closePrice==0):
continue
result=date[0:10]+","+str(openPrice)+","+str(highPrice)+"," +str(lowPrice)+","+str (closePrice)+"\n"
fOutputObj.write(result)
fOutputObj.close()
def Run(self):
self.StartWind()
self.LoadStockList()
self.LoadHisInfo()
if __name__=='__main__':
loadInfoObj=LoadHisInfo()
if len(sys.argv) < 3 :
print("python LoadWindInfo.py -o outPutDir")
sys.exit()
opts, args = getopt.getopt(sys.argv[1:], "o:")
for op, value in opts:
if op == "-o":
loadInfoObj.OutputDir = value
else:
print("python LoadWindInfo.py -o outPutDir")
sys.exit()
loadInfoObj.Run()
---------------------------------------------------------------------------------------------------------------------
本来想获取A股股票代码列表,通过文档例子,返回了错误码-103,没找到这个错误码的说明,w.wset("SectorConstituent",u"date=20130608;sector=全部 A 股"),现在只能通过同花顺导出股票代码列表,然后通过配置文件的形式来拉数据,哪位大神知道怎么使用这个接口的还请赐教,不胜感激~~
这里分享一个python获取A股历史交易数据的例子。
#!/user/python
# -*- coding: utf-8 -*-
import math
import os
import sys
import getopt
from WindPy import *
from decimal import *
from datetime import *
WorkSpace='E:\\WorkSpace'
dataFilesDir=WorkSpace+'\\data'
configFilePath=WorkSpace+'\\config\\StockConfig.txt'
url=''
fConfigObj=open(configFilePath, "r")
count=0
for line in fConfigObj.readlines():
if len(line) <8:
continue
code=line[2:8]
codeName=line[2:8]+"."+line[0:2]
outputPath=r'E:/WorkSpace/data/'+code+r'.txt'
w.start()
data=w.wsd(codeName, "open,high,low,close", "19900101", "",showblank=0)
if data.ErrorCode != 0:
print('error code:'+str(data.ErrorCode)+'\n')
sys.exit()
fOutputObj=open(outputPath.encode('gbk'),'a')