def get_data(start, end, symbol, exchange, level): """ using rqdatac to make an example """ import rqdatac as rq from rqdatac import get_price, id_convert username = "******" password = "******" \ "t7AbdBZEBqRK74fIJH5vsaAfFQgl-tuB8l03axrW8cyN6-nBUho_6Y5VCRI63Mx_PN54nsQOpc1psIGEz" \ "gND8c6Y=bqMVlABkpSlrDNk4DgG-1QXNknJtk0Kkw2axvFDa0E_XPMqOcBxifuRa_DFI2svseXU-8A" \ "eLjchnTkeuvQkKh6nrfehVDiXjoMeq5sXgqpbgFAd4A5j2B1a0gpE3cb5kXb42n13fGwFaGris" \ "8-eKzz_jncvuAamkJEQQV0aLdiw=" host = "rqdatad-pro.ricequant.com" port = 16011 rq.init(username, password, (host, port)) symbol_rq = id_convert(symbol) data = get_price(symbol_rq, start_date=start, end_date=end, frequency=level, fields=None, adjust_type='pre', skip_suspended=False, market='cn', expect_df=False) origin = data.to_dict(orient='records') result = [] for x in origin: do = {} do['open_price'] = x['open'] do['low_price'] = x['low'] do['high_price'] = x['high'] do['close_price'] = x['close'] do['datetime'] = datetime.strptime(str(x['trading_date']), "%Y-%m-%d %H:%M:%S") do['symbol'] = symbol do['local_symbol'] = symbol + "." + exchange do['exchange'] = exchange result.append(do) return result
for D in [5,10,20,40]: for threshold in [0.3]: print("rank"+str(rank)+"D"+str(D)+"threshold"+str(threshold)+".csv") position=set_position(rank,threshold,D) position.to_csv("rank"+str(rank)+"D"+str(D)+"threshold"+str(threshold)+"_position.csv",index=None) price=pd.read_csv("price.csv") price=price.pivot(index='trade_date',columns='stcode',values='price') #转换日期格式 price.index=pd.to_datetime(price.index,format='%Y%m%d') price.index=price.index.strftime("%Y-%m-%d") #转换股票代码 import rqdatac as rq rq.init() new_col=rq.id_convert(list(price.columns)) price.columns=new_col ##处理个股收益率 def forward_return(price,holding_period): """ :param price: 收盘价 :param holding_period:持有期 :return: 收益率序列 """ df_return=price/price.shift(-1*holding_period)-1 return df_return forward_return(price,5).to_csv("rt_5.csv") forward_return(price,10).to_csv("rt_10.csv") forward_return(price,20).to_csv("rt_20.csv") forward_return(price,40).to_csv("rt_40.csv")
'CI005008.WI': '建材', 'CI005024.WI': '交通运输', 'CI005004.WI': '电力及公用事业', 'CI005029.WI': '综合', } group_list = ['周期', '制造', '医药', '消费', '科技', '金融', '基地', '综合'] temp = w.wset( "shstockholdings", f"date={date_end};field=wind_code,sec_name,hold_stocks,change_holdstocks") sh_holdings = pd.DataFrame(temp.Data, index=temp.Fields).T temp = w.wset( "szstockholdings", f"date={date_end};field=wind_code,sec_name,hold_stocks,change_holdstocks") sz_holdings = pd.DataFrame(temp.Data, index=temp.Fields).T sh_holdings['rq_code'] = rq.id_convert(sh_holdings['wind_code'].tolist()) sz_holdings['rq_code'] = rq.id_convert(sz_holdings['wind_code'].tolist()) sh_holdings = sh_holdings.set_index('rq_code') sz_holdings = sz_holdings.set_index('rq_code') hk_to_all_stock = (sh_holdings['change_holdstocks'].append( sz_holdings['change_holdstocks']) * md.loc[date_end]).dropna() / 1e8 citics = rq.get_instrument_industry(hk_to_all_stock.index, source='citics_2019', level=1, date=date_end) temp_sh = w.wset("shszhkstockholdings", "date=" + date + ";tradeboard=shn") temp_sz = w.wset("shszhkstockholdings", "date=" + date + ";tradeboard=szn") sh_holding_detail = pd.DataFrame(temp_sh.Data, index=temp_sh.Fields).T sh_holding_detail['wind_code'] = sh_holding_detail['wind_code'].apply(
def id_convert(self, ctpCode): return rq.id_convert(ctpCode)