1 Star 1 Fork 3

Jaco2016/python_stock

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
stockKFirstFetch.py 2.65 KB
一键复制 编辑 原始数据 按行查看 历史
Jaco2016 提交于 2018-01-22 01:38 . 111
#!/usr/bin/python
# -*- coding: UTF-8 -*-
from sqlalchemy import create_engine
import tushare as ts
import pandas as pd
import time as threadTime
from datetime import time,timedelta,datetime
# df = ts.get_tick_data('600377', date='2017-05-17')
# df.head(10)
# print(df)
# ts.get_hist_data('600848',start='2015-01-05',end='2015-01-09')
# 格力 000651 康美药业 600518 宁沪高速 600377 中国建筑 601668
# 50ETF 510050 农业银行 601288 招商银行 600036 中国银行 601988 宝钢包装 601968
# 上证指数 000001
# 初始化mysql引擎
engine = create_engine('mysql://root:@127.0.0.1/stock?charset=utf8')
# # 获取上市日期
# df = ts.get_stock_basics()
# print(df)
# #存入数据库
# df.to_sql('ipo_date',engine,if_exists='append')
# stockCodeList = ['000651', '600518', '600377', '601668', '601288', '600036', '601988', '601968','000001', '002300']
# stockCodeList = ['000651']
# stockCodeList = [ '600732', '000998', '002191', '002250', '002465', '002518']
# stockCodeList = [ '601668','601288','000333', '169105', '000651', '600036','601688', '600030', '601766', '510050']
stockCodeList = [ '601766']
for stock_code in stockCodeList:
# stock_code='000651'
sqlStr="select t.`code`,t.timeToMarket from ipo_date t where t.code=%s" % (stock_code)
print sqlStr
df=pd.io.sql.read_sql_query(sqlStr,engine,index_col='code')
ipoDate = df.ix[stock_code]['timeToMarket'] #上市日期YYYYMMDD
ipoStr=str(ipoDate)
ipoDT= datetime.strptime(ipoStr, '%Y%m%d')
oneYear = timedelta(days=365)
oneDay=timedelta(days=1)
dStartDate=ipoDT
startDate=dStartDate.strftime('%Y-%m-%d')
dEndDate=dStartDate+oneYear
endDate=dEndDate.strftime('%Y-%m-%d')
dNow=datetime.now()
while dNow>dEndDate:
print "start to get %s : %s -- %s " % (stock_code,startDate,endDate)
df=ts.get_h_data(stock_code,start=startDate,end=endDate)
#存入数据库
df.to_sql(stock_code,engine,if_exists='append',index_label='date')
print "\n get %s date:%s - %s successful!" % (stock_code,startDate,endDate)
threadTime.sleep(5)
dStartDate=dEndDate+oneDay
startDate=dStartDate.strftime('%Y-%m-%d')
dEndDate=dStartDate+oneYear
endDate=dEndDate.strftime('%Y-%m-%d')
endDate=dNow.strftime('%Y-%m-%d')
print "start to get %s : %s -- %s " % (stock_code,startDate,endDate)
df=ts.get_h_data(stock_code,start=startDate,end=endDate)
#存入数据库
df.to_sql(stock_code,engine,if_exists='append',index_label='date')
print "\n get %s date:%s - %s successful!" % (stock_code,startDate,endDate)
threadTime.sleep(5)
print "%s day info all done!" % (stock_code)
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Python
1
https://gitee.com/jaco2016/python_stock.git
git@gitee.com:jaco2016/python_stock.git
jaco2016
python_stock
python_stock
master

搜索帮助