您的当前位置:首页正文

python使用用Wind接口获取全部A股历史交易数据

2022-10-20 来源:好走旅游网


python使用用Wind接口获取全部A股历史交易数据

果然是中文编码的问题。搞定了,新鲜出炉一个小脚本。谢谢各位大神的解答。

\"\"\"

@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\全部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\

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(lowPrice)+\

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\全部 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\

if data.ErrorCode != 0:

print('error code:'+str(data.ErrorCode)+'\\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(lowPrice)+\

fOutputObj.write(result)

fOutputObj.close()fConfigObj.close()

因篇幅问题不能全部显示,请点此查看更多更全内容