Last active
June 14, 2017 14:30
-
-
Save adxrgh/f130fc9bf30134614f617d027ebad772 to your computer and use it in GitHub Desktop.
[wind添加当天的a股收盘数据]#tags:wind,IO
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.start() | |
today=dt.datetime.now().strftime('%Y%m%d') | |
a=w.wset("sectorconstituent","date=%s;sectorid=%s" % (today,'a001010100000000')) | |
all_A_code=a.Data[1] | |
#获取当天的股票收盘价 | |
ohlc=w.wss(all_A_code, "open,high,low,close,volume,amt","tradeDate=%s;priceAdj=F;cycle=D" % today) | |
ohlc | |
a=DataFrame(np.matrix(ohlc.Data)) | |
a.columns=ohlc.Codes | |
b=a.transpose() | |
b.columns=ohlc.Fields | |
b.index.name='secid' | |
b['tradedate']=today | |
b | |
from sqlalchemy import create_engine | |
e=create_engine('mysql+pymysql://'+'root:1235@localhost/wind?charset=utf8') | |
b.to_sql('stockprice',e,if_exists='append') |
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
import pymysql #python2要使用其他接口 | |
conn = pymysql.connect(host='localhost',user='root',password='1235', db='wind1',charset="utf8") | |
cursor = conn.cursor() | |
sql = "INSERT INTO stockprice VALUES (%s, %s, %s, %s, %s, %s, %s, %s)" | |
a=w.wset("sectorconstituent","date=%s;sectorid=%s" % (today,'0305030100000000')) | |
all_A_code=a.Data[1] | |
for j in range(0,len(all_A_code)):#有3000多只个股的长度 | |
#获取当天的股票收盘价 | |
ohlc=w.wss(all_A_code[j], "open,high,low,close,volume,amt","tradeDate=%s;priceAdj=F;cycle=D" % today) | |
for i in range(0,len(ohlc.Data[0])):#i为提取指标中的个股只数 | |
sqllist=[]#位置相当重要 不可移动到前方 | |
sqltuple=() | |
sqllist.append(str(all_A_code[j]))#添加个股代码 | |
sqllist.append(ohlc.Times[0].strftime('%Y%m%d'))#添加当天的时间 | |
for k in range(0, len(ohlc.Fields)): | |
sqllist.append(str(ohlc.Data[k][i])) | |
sqltuple=tuple(sqllist) | |
sqltuple | |
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