def roi(self, df=None, days=1): if df is None: return sheep.wool2(self.data, self.raw_data, PRICEB=self.PRICEB, PRICES=self.PRICES, days=days) return sheep.wool2(df, self.raw_data, PRICEB=self.PRICEB, PRICES=self.PRICES, days=days)
def line_stock(): raw_data=read_data('daily',start_date=start_date,end_date=end_date) data=raw_data.loc[raw_data['low']==raw_data['high']] print(data.shape) list_days=basic().list_days(data) print(list_days.shape) data=data.merge(list_days,on=['ts_code','trade_date']) res1=sheep.wool2(data.loc[data['pct_chg']>0],raw_data,) res2=sheep.wool2(data.loc[data['pct_chg']<0],raw_data) print(res1.describe()) print(res2.describe()) print()
def buy_model(red_line,raw_data): res=[] # 单独处理涨跌停数据 # 做切分处理,如果满足rate*pre_close>low,卖出,否则delete pre_date=basic().pre_date(red_line[['trade_date']],days=-1) red_line=red_line.merge(pre_date,on=['trade_date']) red_line['trade_date']=red_line['pre_-1_date'] red_line.drop(['pre_-1_date'],axis=1,inplace=True) print(red_line.shape) red_line=red_line.iloc[:,:2].merge(raw_data.iloc[:,:11],on=['ts_code','trade_date']) red_line['low_pct']=100*(red_line['low']/red_line['pre_close']-1) for cut in (['down']+list(range(-8,10,2))+['up']): data=raw_data.iloc[:,:11].copy() if cut=='down': buy_data=red_line.loc[red_line['low']==red_line['down_limit']].copy() PB='down_limit' elif cut=='up': buy_data=red_line.loc[red_line['low']==red_line['up_limit']].copy() PB='up_limit' else: buy_data=red_line.loc[cut>red_line['low_pct']].copy() data['PB']=data['pre_close']*(1+0.01*cut) PB='PB' if buy_data.empty: continue print(cut) roi=sheep.wool2(buy_data,data,PRICEB=PB) res.append([cut,roi.shape[0],roi.iloc[-1,-1]]) res=pd.DataFrame(res,columns=['cut','n_days','pct']) return res
def roi(data,rawdata): summary=pd.DataFrame() print(data.shape[0]) all_data = rawdata.copy() for pctb in range(-10,11,2): if pctb==-10: all_data['price_buy']=all_data['down_limit'] elif pctb==10: all_data['price_buy']=all_data['up_limit'] else: all_data['price_buy']=all_data['pre_close']*(1+0.01*pctb) for pcts in range(-10,11,2): if pcts == -10: all_data['price_sell'] = all_data['down_limit'] elif pcts == 10: all_data['price_sell'] = all_data['up_limit'] else: all_data['price_sell'] = all_data['pre_close']*(1+0.01*pctb) all_data=all_data.loc[(all_data['price_buy']>=all_data['low'])&(all_data['price_sell']<=all_data['high'])] res=sheep.wool2(data,all_data,PRICEB='price_buy',PRICES='price_sell') detail=pd.DataFrame({'buy':{pctb},'sell':{pcts},'day':{res.shape[0]},'n':{res['n'].sum()},'pct_avg':{res['pct'].mean()},'pct_all':{res.iloc[-1,-1]}}) summary=pd.concat([summary,detail],ignore_index=True) print()
def fun1(limit_type='up'): res = pd.DataFrame() # data = read_data('daily', start_date=start_date, end_date=end_date) limit = read_data('stk_limit', start_date=start_date, end_date=end_date) data = data.merge(limit, on=['ts_code', 'trade_date']) data['is_roof'] = data.apply( lambda x: 99 if x['close'] == x['up_limit'] else 1 if x['close'] == x['down_limit'] else x['pct_chg'], axis=1) for rate in [-99] + list(range(-10, 10)) + [99]: print(rate) df = data.loc[(data['is_roof'] >= rate) & (data['is_roof'] < (rate + 1))].copy() if df.empty: continue # df['pct']=(df['close']/df['open']-1)*100 # res.loc[rate,'pct']=df['pct'].mean() wool = sheep.wool2(df[['ts_code', 'trade_date']], data, PRICEB=PRICEB, days=1) res.loc[rate, 'mean'] = wool.iloc[:, -3].mean() res.loc[rate, 'n'] = wool.iloc[-1, -2] res.loc[rate, 'all_pct'] = wool.iloc[-1, -1] save_data(res, 'pct_chg_cut_res%s-%s.csv' % (start_date, end_date))
def pro(start_date=None, end_date=None, limit_type='up', days=2): data = gls.first_limit(start_date=start_date, end_date=end_date, limit_type=limit_type, days=days) data = data.loc[(data['pct_chg'] >= -11) & (data['pct_chg'] <= 11)] fl = data.loc[(data['pre_1_is_roof'] == 1) & (data['pre_2_is_roof'] == 0)] return sheep.wool2(fl, fl, days=0, PRICEB='open', PRICES='close')
def next_performance(self,data,PRICEBUY='close',PRICESELL='close',days=1): res=pd.DataFrame() for d in range(1, days): wool=sheep.wool2(data,self.raw_data,PRICEB=PRICEBUY,PRICES=PRICESELL,days=d) res['%spct' % d] = wool['pct'] res['%sall_pct' % d] = wool['all_pct'] res['n']=wool['n'] return res.iloc[:-days,:]
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 roi(red_line, raw_data, cut_left=-24, cut_right=24, step=2, selling_price=selling): res = [] section = list(range(cut_left, cut_right, 2)) # red_line['low_pct']=100*(red_line['low']/red_line['pre_close']-1) raw_data = raw_data.loc[raw_data['ts_code'].isin(red_line['ts_code'])] for pb in section: if red_line.empty: continue PB = 'PB' red_line.loc[:, PB] = red_line['open'] * (1 + 0.01 * pb) raw_data.loc[:, PB] = raw_data['open'] * (1 + 0.01 * pb) grass = red_line.loc[(red_line[PB] >= red_line['low'])].copy() if grass.empty: continue raw_data.loc[:, PB] = raw_data.apply(lambda x: x['open'] if pb >= 0 else x[PB] if x[PB] >= x['low'] else None, axis=1) # for ps in section: # if ps not in raw_data.columns: # if isinstance(ps,int): # PS = 'PS' # raw_data[PS] = raw_data['pre_close'] * (1 + 0.01 * ps) # else: # PS=ps # # # raw_data[PS]=raw_data.apply(lambda x:x[PS] if x[PS]<=x['high'] else x['close'],axis=1) # # 集合竞价中,价格低于open,卖出,否则,价格小于当天最高价卖出,否则收盘价卖出 # # raw_data[PS]=raw_data.apply(lambda x:x['open'] if x[PS]<=x['open'] else x['up_limit'] if x[PS]>=x['up_limit'] else x[PS] if x[PS]<=x['high'] else x['close'],axis=1) # # # meat=sheep.wool2(grass,raw_data,PRICEB=PB,PRICES=PS).dropna() # if meat.empty: # continue # res.append([pb,ps,meat.shape[0],meat.iloc[-1,-1]]) meat = sheep.wool2(grass, raw_data, PRICEB=PB, PRICES=selling_price).dropna() if meat.empty: continue # grass_count=grass.groupby('trade_date')['trade_date'].count().reset_index() res.append([pb, meat.shape[0], meat['n'].sum(), meat.iloc[-1, -1]]) res = pd.DataFrame(res, columns=['pb', 'n_days', 'num', 'pct']) return res
disclosure=ts.pro_api().disclosure_date(end_date=need_date,fields='ts_code,ann_date,end_date,pre_date,actual_date,modify_date') # 解禁 # share_float=ts.pro_api().share_float(ts_code='300617.SZ' ) # 分红送股 dividend=ts.pro_api().dividend() dividend=dividend.loc['dividend'].[] disclosure=disclosure.loc[(disclosure['pre_date']>=need_date)&(disclosure['pre_date']<=end_date)].copy() # print(disclosure.shape,disclosure['ts_code'].unique().shape) data=read_data('daily',start_date=buy_date,end_date=end_date) # disclosure res=pd.DataFrame() for ts_code in disclosure['ts_code'].unique(): df=data.loc[data['ts_code']==ts_code].copy() df.sort_values('trade_date',inplace=True) df['buy_date']=df['trade_date'].shift(5) df['buy_price']=df['close'].shift(5) res=pd.concat([res,df],ignore_index=True) disclosure['trade_date']=disclosure['pre_date'] disclosure.sort_values('trade_date',ascending=False,inplace=True) print(disclosure.shape) disclosure=disclosure[['ts_code','trade_date']].drop_duplicates() print(disclosure.shape,disclosure['ts_code'].unique().shape) sheep.wool2(disclosure,res,PRICEB='buy_price') print()