Last active
May 29, 2017 06:23
-
-
Save adxrgh/a83acadc9117b670a0a5d187e19c294d to your computer and use it in GitHub Desktop.
[寻找一组股票的历史最低价 并和现价比较]#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
#连接数据库 | |
conn = pymysql.connect(host='localhost',user='root',password='1235', db='wind',charset="utf8") | |
#从数据库查询数据,构建dataframe | |
cur = conn.cursor() | |
cur.execute("select tradedate,secid,closeprice,volume from stockprice;") #调整sql字段顺序即可获得不同顺序的【】 | |
cur.scroll(0,"absolute") | |
ohlc=[] | |
time=[] | |
for line in cur.fetchall(): | |
ohlc.append(line[-3:]) | |
time.append(line[0]) | |
df=DataFrame(ohlc,index=time,columns=['windcode','close','volume']) | |
#对格式进行整理 | |
df.index=df.index.to_datetime() | |
df[['close','volume']]=df[['close','volume']].astype(float) | |
#获取近三年收盘价的最低值 | |
df['2015':'2017'].groupby('windcode')['close'].min() | |
#与现价进行比较 | |
df.groupby('windcode')['close'].last()/df['2015':'2017'].groupby('windcode')['close'].min()-1 |
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 | |
#连接数据库 | |
conn = pymysql.connect(host='localhost',user='root',password='1235', db='wind',charset="utf8") | |
df=pd.read_sql('select tradedate,secid,closeprice,volume from stockprice;',con=conn) | |
print('正在读取:',len(df)) | |
conn.colse() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment