-
-
Save adxrgh/b14dc1c63d2ee915e9f19e6f57dcca24 to your computer and use it in GitHub Desktop.
[wind获取行业所有股票历史数据]#tags:wind
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from WindPy import w | |
w.start() | |
from datetime import datetime | |
dt=datetime.now() | |
#连接WIND数据库 execute内要用三个引号 | |
import pymysql #python2要使用其他接口 | |
conn = pymysql.connect(host='localhost',user='root',password='1235', db='wind',charset="utf8") | |
cursor = conn.cursor() | |
cursor.execute(""" | |
CREATE TABLE stockprice | |
(secid VARCHAR(20) NOT NULL, | |
tradedate VARCHAR(50), | |
openprice VARCHAR(50), | |
highprice VARCHAR(50), | |
lowprice VARCHAR(50), | |
closeprice VARCHAR(50), | |
volume VARCHAR(50), | |
amt VARCHAR(50) | |
); | |
""") | |
sql = "INSERT INTO stockprice VALUES (%s, %s, %s, %s, %s, %s, %s, %s)"#格式一定要全设置为string 否则会报错 | |
# 通过wset来取数据集数据 | |
print('\n\n'+'-----通过wset来取数据集数据,获取全部A股代码列表-----'+'\n') | |
a=w.wset("sectorconstituent","date=2017-05-25;sectorid=0305030100000000")#a.Data[1]是所有A股代码 | |
print(a) | |
for j in range(0,len(a.Data[1])):#j为所有A股数量 | |
# 通过wsd来提取时间序列数据,比如取开高低收成交量,成交额数据 | |
ipo=w.wss(str(a.Data[1][j]),'ipo_date') | |
print(ipo) | |
ohlc=w.wsd(str(a.Data[1][j]), "open,high,low,close,volume,amt",ipo.Data[0][0],dt,"PriceAdj=F" ,"Fill=Previous") | |
print(ohlc) | |
if ohlc.ErrorCode!=0: | |
continue | |
for i in range(0,len(ohlc.Data[0])):#i为个股的日期长短 | |
sqllist=[] | |
sqltuple=() | |
sqllist.append(str(a.Data[1][j])) | |
if len(ohlc.Times)>1: | |
sqllist.append(ohlc.Times[i].strftime('%Y%m%d'))#添加时间 | |
for k in range(0, len(ohlc.Fields)):#k为提取个股的Ohlc等指标的数量 | |
sqllist.append(ohlc.Data[k][i])#提取k个数据的i时间长度的数据 | |
sqltuple=tuple(sqllist) | |
print('成功取得并添加%d的数据' %(j)) | |
cursor.execute(sql,sqltuple) | |
conn.commit() | |
conn.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment