def re_limit_up(start_date, end_date): rawdata = read_data('daily', start_date=start_date, end_date=end_date) # list_days=basic().list_days(rawdata) limit = read_data('stk_limit', start_date=start_date, end_date=end_date) data = rawdata.merge(limit[['ts_code', 'trade_date', 'up_limit']], on=['ts_code', 'trade_date']) print(data.shape) data['red_line'] = data.apply(lambda x: 1 if x['low'] == x['up_limit'] else 0, axis=1) data['reback_limit'] = data.apply( lambda x: 1 if (x['open'] == x['close']) & (x['open'] == x['up_limit']) & (x['low'] < x['close']) else 0, axis=1) print(data.shape) pre = basic().pre_data(data, label=['red_line'], pre_days=2) data = data.merge(pre[['ts_code', 'trade_date', 'pre_2_red_line']], on=['ts_code', 'trade_date']) pre = basic().pre_data(data, label=['reback_limit'], pre_days=1) data = data.merge(pre[['ts_code', 'trade_date', 'pre_1_reback_limit']], on=['ts_code', 'trade_date']) print(data.shape) data = data.loc[(data['pre_2_red_line'] == 1) & (data['pre_1_reback_limit'] == 1)] print(data.shape) # save_data('破板后回封表现.csv') # data=data.loc[(data['up_limit']==data['close'])&(data['open']==data['close'])&(data['low']<data['up_limit'])] # print(data.shape) data = data.merge(basic().list_days(data, list_days=15)) print(data.shape) wool = sheep.wool(data, rawdata) return wool
def verify(up,data): t1 = datetime.datetime.now() s1=sheep.wool(up,data) t2= datetime.datetime.now() s2=sheep.wool2(up,data) t3 = datetime.datetime.now() s1.to_csv('s1.csv') s2.to_csv('s2.csv') print()
def first_up(raw_data,start_date='20200201', end_date='20200215', PRICEB='open', NEWPRICEB='price_buy', PRICES='close', sell_date=1, buy_date=2, CUTS=2,CHANGE=['open', 'pre_close'], model=None, model3_pct=1.01): if buy_date != 1: price_buy = basic.basic().pre_data( raw_data[['ts_code', 'trade_date', PRICEB, '%s/%s' % (CHANGE[0], CHANGE[1])]], label=[PRICEB, '%s/%s' % (CHANGE[0], CHANGE[1])], new_label=[NEWPRICEB, 'open_pre_close_change', ], pre_days=-buy_date + 1) raw_data = raw_data.merge(price_buy[['ts_code', 'trade_date', NEWPRICEB, 'open_pre_close_change']], on=['ts_code', 'trade_date']) # 筛选收盘涨停数据 data = raw_data.loc[:, ['ts_code', 'trade_date', 'close', 'pre_close', 'up_limit', 'open_pre_close_change']] print(data.shape) data = data.merge( basic.basic().pre_data(data[['ts_code', 'trade_date', 'up_limit']], label=['up_limit'], new_label=['pre_up_limit'])[ ['ts_code', 'trade_date', 'pre_up_limit']], on=['ts_code', 'trade_date']) print(data.shape) data.dropna(inplace=True) print(data.shape) data = data[data['close'] == data['up_limit']] # print(data.shape) # raw_data=raw_data[raw_data["ts_code"].isin(data['ts_code'].unique()) == True] # print('不在涨停df中的股票out',raw_data.shape) # print(raw_data['ts_code'].unique().shape) # buy_data = raw_data.loc[:, ['ts_code', 'trade_date', PRICES]] # z1=sheep.wool(data, raw_data, PRICEB='price_buy') # print(z1.iloc[-1, -1]) # 全市场,首板,涨停回溯 # z2 = sheep.wool(data, raw_data) # print(z2.iloc[-1, -1]) # # t1=sheep.wool(data[data['pre_close'] != data['pre_up_limit']], # # raw_data, PRICEB='price_buy') # # print(t1.iloc[-1, -1]) # t2 = sheep.wool(data[data['pre_close'] != data['pre_up_limit']], # raw_data) # print(t2.iloc[-1, -1]) # data.to_csv('%s~%slimit_stock_list.csv' % (start_date, end_date)) # res_all = pd.DataFrame() if model: PRICES = 'price_sell' if model == 1: raw_data[PRICES] = raw_data.apply( lambda x: x['open'] if x['open'] >= x['pre_close'] else x['pre_close'] if x['pre_close'] <= x[ 'high'] else x['close'],axis=1) if model == 2: raw_data[PRICES] = 10 * raw_data['amount'] / raw_data['vol'] if model==3: raw_data[PRICES]=raw_data.apply(lambda x:x['open'] if x['open'] >(x['pre_close']*1.01) else x['close'],axis=1) if model==4: raw_data[PRICES]=raw_data.apply(lambda x:x['open'] if x['open']>=(1.03*x['pre_close']) else x['open']*1.01 if x['high']>(x['open']*1.01) else x['close'] ,axis=1) # temp = basic.basic().pre_data(raw_data[['ts_code', 'trade_date', 'ma']], label=['ma'], new_label=[PRICES]) # raw_data = raw_data.merge(temp[['ts_code', 'trade_date', PRICES]],on=['ts_code', 'trade_date']) res_some = pd.DataFrame() for cut in range(-12, 11, CUTS): if cut < -10: buy_data = data[data['open_pre_close_change'] == -9999] print('open down') elif cut > 8: buy_data = data[data['open_pre_close_change'] == 9999] print('open up') else: buy_data = data[(data['open_pre_close_change'] < cut + 2) & (data['open_pre_close_change'] >= cut)] print('%s<=x<%s' % (cut, cut + CUTS)) if buy_data.empty: # res_all.loc['%s~%s' % (cut, cut + CUTS), 'res'] = None res_some.loc['%s~%s' % (cut, cut + CUTS), 'res'] = None print(buy_data['open_pre_close_change'].max(), buy_data['open_pre_close_change'].min(), buy_data['open_pre_close_change'].mean()) continue # wool_all = sheep.wool(buy_data, raw_data, PRICEB='price_buy', PRICES=PRICES, days=sell_date) # if wool_all.iloc[-1, -1]: # wool_all.to_csv('pct%swool_all%s~%s.csv' % (cut, start_date, end_date)) # print('all%s' % cut, wool_all.iloc[-1, -1]) # res_all.loc['%s~%s' % (cut, cut + CUTS), 'res'] = wool_all.iloc[-1, -1] # res_all.loc['%s~%s' % (cut, cut + CUTS), 'n_mean'] = wool_all['n'].mean() # res_all.loc['%s~%s' % (cut, cut + CUTS), 'days'] = wool_all.shape[0] wool_some = sheep.wool(buy_data[buy_data['pre_close'] != buy_data['pre_up_limit']], raw_data, PRICEB='price_buy', PRICES=PRICES, days=sell_date) if wool_some.empty: continue if wool_some.iloc[-1, -1]: wool_some.to_csv('pct%swool_some%s~%s-%s.csv' % (cut, start_date, end_date,sell_date)) print('some', wool_some.iloc[-1, -1]) res_some.loc['%s~%s' % (cut, cut + CUTS), 'res'] = wool_some.iloc[-1, -1] res_some.loc['%s~%s' % (cut, cut + CUTS), 'n_mean'] = wool_some['n'].mean() res_some.loc['%s~%s' % (cut, cut + CUTS), 'days'] = wool_some.shape[0] # buy_data.to_csv('pct%slimit_stock_list%s~%s.csv' % (cut, start_date, end_date)) # res_all.to_csv('cut%s-of-all(%s,%s)%s~%s.csv' % (CUTS, PRICEB, PRICES, start_date, end_date)) # res_some.to_csv('cut%s-of-first(%s,%s)%s~%s.csv' % (CUTS, PRICEB, PRICES, start_date, end_date)) return res_some
def idea7(XISHU=0.998, UPPER=False, PERIOD=5, TIMES=5, CHGPCT=[1, 2]): FORMAT = lambda x: '%.4f' % x label = ['low_ma5'] path = 'D:\\workgit\\stock\\util\\stockdata\\' # pct=list(range(-11,11)) today = datetime.datetime.today().date() tool = basic.basic() pro = ts.pro_api() while (not os.path.isfile(path + str(today) + '\data.csv')) or ( not os.path.isfile(path + str(today) + '\daily-basic.csv')): today = today - datetime.timedelta(1) # 基础数据,市值信息, data = pd.read_csv(path + str(today) + '\data.csv', index_col=0, dtype={'trade_date': object})[[ 'ts_code', 'trade_date', 'close', 'pct_chg', 'low', 'ma5' ]] # data=data[data['trade_date']>='20191201'] data['lowma5'] = data.apply(lambda x: 1 if x['low'] > (XISHU * x['ma5']) else 0, axis=1) if UPPER: data['lowma5'] = data.apply(lambda x: 1 if (x['lowma5'] == 1) & (x['low'] <= (XISHU + UPPER) * x['ma5']) else 0, axis=1) stock_baisc = pd.read_csv( path + str(today) + '\daily-basic.csv', index_col=0, dtype={'trade_date': object})[['ts_code', 'trade_date', 'turnover_rate', 'total_mv']] history = pd.read_csv(path + str(today) + '\history_name.csv', index_col=0, dtype={'trade_date': object}) history['name'] = 'st' path = path + str(today) + 'idea7\\' if not os.path.isdir(path): os.makedirs(path) print(path) today = today.strftime('%Y%m%d') df = data.copy() df.sort_values(by='trade_date', inplace=True) # df.to_csv(path+'2019datalowma5.csv') c_times = df.groupby('ts_code')['lowma5'].rolling(PERIOD).sum() c_times.index = c_times.index.droplevel() c_times = pd.DataFrame(c_times) c_times.rename(columns={'lowma5': 'count'}, inplace=True) # count_times=count_times[count_times['%s_%s_uptimes'%(label,period)]>=up_period] df = df.join(c_times) df = df.merge(history, on=['ts_code', 'trade_date'], how='left') print(df.shape) df = df[df['name'].isna()] df.drop(columns='name', inplace=True) print(df.shape) df.dropna(inplace=True) # # df.to_csv(path + 'data.csv') if CHGPCT: df = df[(df['count'] >= TIMES) & (df['pct_chg'] >= CHGPCT[0]) & (df['pct_chg'] <= CHGPCT[1])][['ts_code', 'trade_date']] if df.empty: return pd.DataFrame() else: df.to_csv(path + 'low%sma%stimes%s(%s-%s).csv' % (XISHU, PERIOD, TIMES, CHGPCT[0], CHGPCT[1])) wool = sheep.wool(df, data) if not wool.empty: wool.to_csv( path + 'low%s-%sma%stimes%s(%s-%s)huisuxiaoguo.csv' % (XISHU, XISHU + UPPER, PERIOD, TIMES, CHGPCT[0], CHGPCT[1])) res = df[df['trade_date'] == today][['ts_code']] if res.empty: print('%s无数据' % today) else: res.to_csv(path + 'low%sma%stimes%s(%s-%s)%s.txt' % (XISHU, PERIOD, TIMES, CHGPCT[0], CHGPCT[1], today), index=False) return wool
# median_list=filter.StockFilter().stock_basic(name="st|ST|药",market="科创板",industry='生物制药|医药商业|医疗保健|中成药|化学制药') # print(median_list) # df[df["ts_code"].isin(median_list['ts_code']) == False][['ts_code']].to_csv('0203非跌停非医药.txt',index=False) # df[['ts_code']].to_csv('0203非跌停.txt',index=False) df = df[df['ts_code'].isin(up_data['ts_code']) == False] print(df.shape) res = pd.DataFrame() for days in range(1, NDAY + 1): for pct in np.arange(-10, 11, t): data = df[(df['pct_chg'] >= pct) & (df['pct_chg'] < (pct + t))] # if pct==9: # print(data) data_res = sheep.wool(data, raw_data, days=days) if data_res.empty: continue res.loc[pct, 'huisu%s' % days] = data_res.iloc[-1, 0] # if 'count' not in res.columns: if data_res.iloc[-1, 1]: res.loc[pct, 'count'] = data_res.iloc[-1, 1] down_res = sheep.wool(down_data, raw_data, days=days) if down_res.empty: continue up_res = sheep.wool(up_data, raw_data, days=days) res.loc['down', 'huisu%s' % days] = down_res.iloc[-1, 0] res.loc['down', 'count'] = down_res.iloc[-1, 1] res.loc['up', 'huisu%s' % days] = up_res.iloc[-1, 0] res.loc['up', 'count'] = up_res.iloc[-1, 1] others_res = sheep.wool(df, raw_data, days=days)
# 去除st history = tool.history_name(start_date=df['trade_date'].min()) history['name'] = 'st' df = df.merge(history, on=['ts_code', 'trade_date'], how='left') # print(df.shape) df = df[df['name'].isna()] df.drop(columns=['name'], inplace=True) # print(daily_basic.shape) # print(df.shape) # df.to_csv(path+'data.csv') # 1.10天上涨大于等于7次的股票 # 2.所有股票筛选100*(ma1/ma10-1)在[1,10]之间 # 3.筛选换手率,市值 df = df[df['count_%s' % LABEL] >= TIMES] # print(df.shape) df = df[(df['ma1/ma10pct'] >= up_n_pct[0]) & (df['ma1/ma10pct'] <= up_n_pct[1])] # print(df.shape) df = df.merge(daily_basic[['ts_code', 'trade_date']], on=['ts_code', 'trade_date']) # print(df.shape) df.to_csv(path + '%daysup%stimes.csv' % (PERIOD, TIMES)) sheep.wool(df, data).to_csv(path + '%daysup%stimeshuisu.csv' % (PERIOD, TIMES)) # df[df['trade_date']==(str(today).replace('-',''))][['ts_code']].reset_index(drop=True).to_csv('%sdaysup%stimes%snew.txt'%(PERIOD,TIMES,str(today)),index=False) print('10day up 6', df[df['trade_date'] == (str(today).replace('-', ''))].shape[0])
on=['ts_code', 'trade_date'], how='left') stock_data1 = stock_data1[stock_data1['name'].isna()] stock_data1.drop(columns='name', inplace=True) df = pd.DataFrame() for day in stock_data1['trade_date'].unique(): # df=pd.concat([data[data['trade_date']==day].sort_values(by='trade_date',ascending=False).head(30),df]) df = pd.concat([ stock_data1[stock_data1['trade_date'] == day].sort_values( by='score', ascending=False).head(30), df ]) # stock.to_csv(path + str((mv_bins[i], mv_bins[i + 1])) + "30ofall_marks.csv") df.to_csv(path + str((mv_bins[i], mv_bins[i + 1])) + "-30-of-bins.csv") stock = sheep.wool(df, data, days=days) if stock.iloc[-1, -1] > 1: print(stock.iloc[-1, -1]) stock.to_csv(path + str((mv_bins[i], mv_bins[i + 1])) + "pct-wool.csv") # print(stock) #所有股票排名回溯 top_n = [50] for it in top_n: for switch in [False]: df = pd.DataFrame() stock_mark = stock_mark.merge(history_name, on=['ts_code', 'trade_date'], how='left') stock_mark = stock_mark[stock_mark['name'].isna()]
def first_up( start_date='20200201', end_date='20200215', PRICEB='open', PRICES='close', sell_date=1, buy_date=2, CUTS=2, CHANGE=['open', 'pre_close'], ): NOTCONTAIN = sfilter.StockFilter().stock_basic(end_date, name="st|ST", market="科创板") raw_data = pd.read_sql_query( 'select * from daily where (trade_date>=%(start)s and trade_date<=%(end)s)', params={ 'start': start_date, 'end': end_date }, con=engine) stk_limit = pd.read_sql_query( 'select * from stk_limit where (trade_date>=%(start)s and trade_date<=%(end)s)', params={ 'start': start_date, 'end': end_date }, con=engine) raw_data.drop_duplicates(inplace=True) stk_limit.drop_duplicates(inplace=True) print(raw_data.shape, stk_limit.shape) raw_data = raw_data[raw_data["ts_code"].isin(NOTCONTAIN['ts_code']) == False] print('科创,st,out', raw_data.shape) raw_data = raw_data.merge( stk_limit.loc[:, ['ts_code', 'trade_date', 'up_limit', 'down_limit']], on=['ts_code', 'trade_date']) raw_data['%s/%s' % (CHANGE[0], CHANGE[1])] = raw_data.apply( lambda x: 99 if x[CHANGE[0]] == x['up_limit'] else -99 if x[CHANGE[0]] == x['down_limit'] else x[CHANGE[0]] / x[CHANGE[1]], axis=1) if buy_date != 1: price_buy = basic.basic().pre_data( raw_data[[ 'ts_code', 'trade_date', PRICEB, '%s/%s' % (CHANGE[0], CHANGE[1]) ]], label=[PRICEB, '%s/%s' % (CHANGE[0], CHANGE[1])], new_label=[ 'price_buy', 'xishu_pct', ], pre_days=-buy_date + 1) raw_data = raw_data.merge( price_buy[['ts_code', 'trade_date', 'price_buy', 'xishu_pct']], on=['ts_code', 'trade_date']) print(raw_data.shape) raw_data.dropna(inplace=True) print(raw_data.shape) # 筛选收盘涨停数据 data = raw_data.loc[:, [ 'ts_code', 'trade_date', 'close', 'pre_close', 'up_limit', 'xishu_pct' ]] print(data.shape) data = data.merge(basic.basic().pre_data( data[['ts_code', 'trade_date', 'up_limit']], label=['up_limit'], new_label=['pre_up_limit'])[['ts_code', 'trade_date', 'pre_up_limit']], on=['ts_code', 'trade_date']) print(data.shape) data = data[data['close'] == data['up_limit']] # print(data.shape) # raw_data=raw_data[raw_data["ts_code"].isin(data['ts_code'].unique()) == True] # print('不在涨停df中的股票out',raw_data.shape) # print(raw_data['ts_code'].unique().shape) # buy_data = raw_data.loc[:, ['ts_code', 'trade_date', PRICES]] # z1=sheep.wool(data, raw_data, PRICEB='price_buy') # print(z1.iloc[-1, -1]) z2 = sheep.wool(data, raw_data) print(z2.iloc[-1, -1]) # t1=sheep.wool(data[data['pre_close'] != data['pre_up_limit']], # raw_data, PRICEB='price_buy') # print(t1.iloc[-1, -1]) t2 = sheep.wool(data[data['pre_close'] != data['pre_up_limit']], raw_data) print(t2.iloc[-1, -1]) res_all = pd.DataFrame() res_some = pd.DataFrame() # data.to_csv('%s-%slimit_stock_list.csv' % (start_date, end_date)) for cut in range(-10, 12, CUTS): if cut < -9: buy_data = data[data['xishu_pct'] == -99] print('open down') elif cut > 10: buy_data = data[data['xishu_pct'] == 99] print('open up') else: buy_data = data[(data['xishu_pct'] < (1 + 0.01 * cut)) & (data['xishu_pct'] >= (0.98 + 0.01 * cut))] print('%s<=x<%s' % ((0.98 + 0.01 * cut), (1 + 0.01 * cut))) if buy_data.empty: res_all.loc[cut, 'res'] = None res_some.loc[cut, 'res'] = None print(cut, buy_data['xishu_pct'].max(), buy_data['xishu_pct'].min(), buy_data['xishu_pct'].mean()) continue wool_all = sheep.wool(buy_data, raw_data, PRICEB='price_buy', days=sell_date) if wool_all.iloc[-1, -1]: # wool_all.to_csv('%s-%spct%swool_all.csv' % (start_date, end_date, cut)) print('all%s' % cut, wool_all.iloc[-1, -1]) res_all.loc[cut, 'res'] = wool_all.iloc[-1, -1] res_all.loc[cut, 'n_mean'] = wool_all['n'].mean() res_all.loc[cut, 'days'] = wool_all.shape[0] wool_some = sheep.wool( buy_data[buy_data['pre_close'] != buy_data['pre_up_limit']], raw_data, PRICEB='price_buy', days=sell_date) if wool_some.iloc[-1, -1]: # wool_some.to_csv('%s-%spct%swool_some.csv' % (start_date, end_date, cut)) print('some', wool_some.iloc[-1, -1]) res_some.loc[cut, 'res'] = wool_some.iloc[-1, -1] res_some.loc[cut, 'n_mean'] = wool_some['n'].mean() res_some.loc[cut, 'days'] = wool_some.shape[0]