stocktobuy = pd.DataFrame(columns=['year','instrument','publish_date','tm_date','fs_net_profit','fs_deducted_profit','open0','open1','open2','open3','bench_open0','bench_open1','bench_open2','bench_open3'])
# 分别为:年份,股票,财报日,脱帽日(若没脱帽:NAN),净利润,扣非后净利润,2月1日(或之后最近交易日)开盘价,财报日开盘价,摘帽日开盘价,摘帽后第11个交易日开盘价,基准的2月1日开盘价,基准的财报日开盘价,基准的摘帽日开盘价,基准的摘帽后第10个交易日开盘价
for yy in range(2014,2018):
tradeday = D.trading_days(market='CN', start_date='%d-02-01' %yy, end_date='%d-02-15' %yy)
day0 = tradeday.iloc[0]['date'] # 获得该年份第一个交易日日期
instruments = D.instruments(start_date='%d-02-01' %yy, end_date='%d-02-01' %yy, market='CN_STOCK_A') # 获得股票列表
alldata = D.history_data(instruments, start_date=day0, end_date=day0, fields=['st_status'], frequency='daily')
ststock = alldata[alldata['st_status']==2]['instrument'] # 获得st股票列表
for instrument0 in ststock:
fData = D.financial_statements(instrument0, start_date='%d-01-01' %yy, end_date='%d-12-01' %yy, \
fields=['fs_quarter','fs_publish_date','fs_operating_revenue','fs_net_profit','fs_deducted_profit'])
if len(fData)>0 and fData.iloc[0]['fs_net_profit']>0 and fData.iloc[0]['fs_deducted_profit']>0 and fData.iloc[0]['fs_operating_revenue']>1e7: # 有望摘帽
tmtime = D.history_data(instrument0, start_date='%d-02-01' %yy, end_date='%d-07-30' %yy, fields=['open','st_status','suspended'], frequency='daily') # 获取开盘价及ST状态
benchdata = D.history_data(instruments='000300.SHA', start_date='%d-02-01' %yy, end_date='%d-07-30' %yy, fields=['open'], frequency='daily') # 获取基准数据
tmprow = {'year':(int)(yy),'instrument':instrument0,'publish_date':fData.iloc[0]['fs_publish_date'], \
'fs_net_profit':fData.iloc[0]['fs_net_profit'], \
'fs_deducted_profit':fData.iloc[0]['fs_deducted_profit'], \
'open1':tmtime[tmtime['date']>=fData.iloc[0]['fs_publish_date']].iloc[0]['open'], \
'bench_open1':benchdata[benchdata['date']>=fData.iloc[0]['fs_publish_date']].iloc[0]['open']}
open0dates = tmtime[(tmtime['date']>='%d-02-01' %yy) & (tmtime['date']<fData.iloc[0]['fs_publish_date']) & (tmtime['suspended']==False)]
if len(open0dates)>0:
tmprow['open0'] = tmtime[tmtime['date']==open0dates.iloc[0]['date']].iloc[0]['open']
tmprow['bench_open0'] = benchdata[benchdata['date']==open0dates.iloc[0]['date']].iloc[0]['open']
else:
tmprow['open0'] = np.nan
tmprow['bench_open0'] = np.nan
if len(tmtime[tmtime['st_status']==0])>10: # 成功摘帽
tmprow['tm_date'] = tmtime[tmtime['st_status']==0].iloc[0]['date']
tmprow['open2'] = tmtime[tmtime['st_status']==0].iloc[0]['open']
tmprow['open3'] = tmtime[tmtime['st_status']==0].iloc[10]['open']
tmprow['bench_open2'] = benchdata[benchdata['date']>=tmtime[tmtime['st_status']==0].iloc[0]['date']].iloc[0]['open']
tmprow['bench_open3'] = benchdata[benchdata['date']>=tmtime[tmtime['st_status']==0].iloc[10]['date']].iloc[0]['open']
else: # 未摘帽
tmprow['tm_date'] = np.nan
tmprow['open2'] = tmtime[tmtime['date']>='%d-06-01' %yy].iloc[0]['open']
tmprow['bench_open2'] = benchdata[benchdata['date']>='%d-06-01' %yy].iloc[0]['open']
#print(tmprow)
stocktobuy=stocktobuy.append(tmprow,ignore_index=True)