#该脚本可以提取沪深两市上市公司股票信息,并按以下信息分类:(1)当天股价创近10个交易日新高的股票;(2)停牌的股票;(3)复牌不超过一个交易日或者新发行的股票
2 #将分类后的股票及其信息(股价新高、当前状态等)存入mysql数据库
3 from time
import *
4 import pandas as pd
5 import tushare as ts
6 from datetime
import date
7 import datetime
8 import MySQLdb
9 import sys
10 reload(sys)
11 sys.setdefaultencoding(
‘utf8‘)
#将编码方式默认改为utf-8
12 #-------------------------------定义函数:计算当天往前推Deltadays个交易日所对应的日期--------------------------
13 def com_StartDate(Deltadays):
14 i=
0
15 da=
date.today()
16 while i!=
Deltadays:
17 da=da-datetime.timedelta(days=1
)
18 if da.isoweekday()==6
or da.isoweekday()==7:
#判断当前日期是否为周末(此处未考虑法定节假日,后续改进)
19 pass
20 else:
21 i+=1
22 return da.strftime(
"%Y-%m-%d")
#返回值数据类型为string
23
24 #--------------定义函数:判断某支股票处于什么状态,并返回股票代码、名字和股价(对于创新高的股票)等信息--------
25 def stock_info(stockID,startdate,index):
26 today=
date.today()
27 df=ts.get_hist_data(stockID,start=startdate,end=today.strftime(
"%Y-%m-%d"))
#获取股票信息
28 if type(df)!=type(None)
and len(df.index)>1:
#抓取时间周期内,有数据天数低于2天的,算作新股
29 df=df.reindex(index=index,method=
‘ffill‘,fill_value=int(-1
))
30 period_high=df[
‘high‘].max()
31 today_high=df.ix[today.strftime(
"%Y-%m-%d")][
‘high‘]
32 if today_high>=period_high
and today_high>
0 :
33 stock_status=int(0)
#非新股,未停牌,股价创新高
34 return stock_status,df
35 elif today_high==int(-1
):
36 stock_status=int(1)
#非新股,但停牌
37 return stock_status,df
38 else:
39 stock_status=int(2)
#非新股,未停牌,股价未创新高
40 return stock_status,df
41 else:
42 stock_status=int(3)
#新股或复牌不超过1天的股票
43 return stock_status,df
44
45 #-----------------------------------------------获取沪深两市所有 上市公司基本信息---------------------------
46 all_stocks_info=
ts.get_stock_basics()
47 Deltadays=10
48 startdate=
com_StartDate(Deltadays)
49 newRecStocksID=[]
#股票价格创新高的股票代码列表
50 newRecStocksPrice=[]
#创新高的股票价格列表
51 newRecStocksName=[]
#股票价格创新高的股票名称列表
52 suspendedStocksID=[]
#停牌股票代码列表
53 suspendedStocksName=[]
#停牌股票名称列表
54 newORresumeStocksID=[]
#新发行或者当天复牌的股票代码列表
55 newORresumeStocksName=[]
#新发行或者当天复牌的股票名称列表
56 today=
date.today()
57 index=ts.get_hist_data(
‘002625‘,start=startdate,end=today.strftime(
"%Y-%m-%d"),retry_count=10,pause=1
).index
58 for i
in all_stocks_info.index:
59 stock_status,stockinfo=
stock_info(str(i), startdate,index)
60 if stock_status==int(0):
#当日股价创Deltadays天内新高的股票
61 newRecStocksID.append(str(i).zfill(6
))
62 newRecStocksName.append(all_stocks_info.ix[str(i),
‘name‘])
63 newRecStocksPrice.append(stockinfo.ix[today.strftime(
"%Y-%m-%d"),
‘high‘])
64 elif stock_status==int(1):
#已停牌的股票
65 suspendedStocksID.append(str(i).zfill(6
))
66 suspendedStocksName.append(all_stocks_info.ix[str(i),
‘name‘])
67 elif stock_status==int(2):
#未停牌,且股价未创Deltadays天内新高的股票
68 pass
69 else:
#该股票为新发行或者复牌不超过一天的股票
70 newORresumeStocksID.append(str(i).zfill(6
))
71 newORresumeStocksName.append(all_stocks_info.ix[str(i),
‘name‘])
72
73 #-------------------------------------------数据写入DataFrame----------------------------------------------
74 newRecStocks=pd.DataFrame({
‘stockID‘:newRecStocksID,
‘stockname‘:newRecStocksName,
‘high_price‘:newRecStocksPrice,
‘status‘:[
‘NewRecord‘]*
len(newRecStocksID)})
75 suspendedStocks=pd.DataFrame({
‘stockID‘:suspendedStocksID,
‘stockname‘:suspendedStocksName,
‘status‘:[
‘Suspended‘]*
len(suspendedStocksID)})
76 newORresumeStocks=pd.DataFrame({
‘stockID‘:newORresumeStocksID,
‘stockname‘:newORresumeStocksName,
‘status‘:[
‘New or Resume‘]*
len(newORresumeStocksID)})
77
78 #-----------------------------------将数据存入MySQL数据库,数据库名称:stocks------------------------------
79 db=MySQLdb.connect(
‘localhost‘,
‘root‘,
‘900417‘,
‘stocks‘,charset=
‘utf8‘)
80 pd.io.sql.to_sql(frame=newRecStocks,con=db,name=today.strftime(
"%Y%m%d")+
‘newRecordStocks‘,flavor=
‘mysql‘,schema=None,if_exists=
‘replace‘,index=True,index_label=None,chunksize=1000,dtype=
None)
81 pd.io.sql.to_sql(frame=suspendedStocks,con=db,name=today.strftime(
"%Y%m%d")+
‘suspendedStocks‘,flavor=
‘mysql‘,schema=None,if_exists=
‘replace‘,index=True,index_label=None,chunksize=1000,dtype=
None)
82 pd.io.sql.to_sql(frame=newORresumeStocks,con=db,name=today.strftime(
"%Y%m%d")+
‘newORresumeStocks‘,flavor=
‘mysql‘,schema=None,if_exists=
‘replace‘,index=True,index_label=None,chunksize=1000,dtype=
None)
83 db.commit()
84 db.close()
用Python获取沪深两市上市公司股票信息,提取创近10天股价新高的、停牌的、复牌不超过一天或者新发行的股票,并存入mysql数据库
标签: