def get_signal(signal, aum, balance, EndDate, close_dict): signal['symbol'] = signal.index symbol_lst = signal.symbol.tolist() porfolio = Future() main_contract_dict = porfolio.get_main_symbol(product=symbol_lst, date=EndDate) main_contract = pd.DataFrame(main_contract_dict).T[['main_contract']] contract_lst = main_contract.main_contract.tolist() ExchangeID_dict = porfolio.get_ExchangeID(contract_lst=contract_lst) ExchangeInstID_dict = porfolio.get_ExchangeInstID( contract_lst=contract_lst) VolumeMultiple_dict = porfolio.get_VolumeMultiple(contract_lst) main_contract['symbol'] = main_contract.index signal_dict = {} for symbol in symbol_lst: main_contract = main_contract_dict[symbol]['main_contract'] trading_code = ExchangeID_dict[main_contract][ 'ExchangeID'] + '.' + ExchangeInstID_dict[main_contract][ 'ExchangeInstID'] signal_dict[symbol] = { 'symbol': symbol, 'trading_code': trading_code, 'weight': signal.loc[symbol]['weight'], 'last_price': close_dict[symbol], 'VolumeMultiple': VolumeMultiple_dict[main_contract]['VolumeMultiple'] } trading_info = pd.DataFrame(signal_dict).T trading_info['position'] = aum * balance / len(symbol_lst) * trading_info[ 'weight'] / trading_info['last_price'] / trading_info['VolumeMultiple'] # trading_info['position'] = trading_info['position'].apply(lambda x: int(np.around(x, 0))) print(trading_info) return trading_info
profit_df['date_time'] = profit_df.index profit_df = profit_df.assign( date_time=lambda df: df.date_time.apply(lambda x: str( x)[:10])) profit_df = profit_df.reset_index(drop=True) hq = pd.read_csv('e:/data/future_index/' + code.upper() + '_' + 'daily' + '_index.csv')[[ 'date_time', 'close' ]].assign( date_time=lambda df: df.date_time. apply(lambda x: str(x)[:10])) hq = hq[(hq['date_time'] > s_date) & (hq['date_time'] < e_date)] contract_lst = [code.upper()] VolumeMultiple = porfolio.get_VolumeMultiple(contract_lst)[ code.upper()]['VolumeMultiple'] aum_ini = hq.close.tolist()[0] * VolumeMultiple * 2 * level profit_df = hq.merge(profit_df, on=['date_time'], how='left').sort_values(['date_time']) # profit_df = profit_df.fillna(0) profit_df['chg'] = ( profit_df['profit'] - profit_df['close'].shift(1) * profit_df['count'] * VolumeMultiple * fee ) * level / profit_df['close'].shift(1) / (VolumeMultiple * 2) profit_df = profit_df.fillna(0) if method == 'sum': profit_df['net'] = 1 + profit_df['chg'].cumsum() else: profit_df['net'] = (1 + profit_df['chg']).cumprod()
# close_dict = {} # for index_code in symbol_lst: # code = code_dic[index_code] # index_hq = stock_price(code, hq_last_date, hq_last_date, 'daily') # close_dict[index_code] = index_hq.close.tolist()[-1] # print(close_dict) porfolio = Future() main_contract_dict = porfolio.get_main_symbol(product=symbol_lst, date=hq_last_date) main_contract = pd.DataFrame(main_contract_dict).T[['main_contract']] print(main_contract) PriceTick_dict = porfolio.get_PriceTick(symbol_lst) contract_lst = main_contract.main_contract.tolist() ExchangeID_dict = porfolio.get_ExchangeID(contract_lst=contract_lst) ExchangeInstID_dict = porfolio.get_ExchangeInstID(contract_lst=contract_lst) VolumeMultiple_dict = porfolio.get_VolumeMultiple(contract_lst) LongMarginRatio_dict = porfolio.get_LongMarginRatio(contract_lst) ShortMarginRatio_dict = porfolio.get_ShortMarginRatio(contract_lst) main_contract['symbol'] = main_contract.index print(main_contract) signal_dict = {} for symbol in symbol_lst: main_contract = main_contract_dict[symbol]['main_contract'] price_tick = PriceTick_dict[symbol]['PriceTick'] trading_code = ExchangeID_dict[main_contract]['ExchangeID'] + '.' + ExchangeInstID_dict[main_contract][ 'ExchangeInstID'] signal_dict[symbol] = { 'symbol': symbol, 'trading_code': trading_code, 'price_tick': price_tick, 'last_price': api.get_quote(trading_code).pre_close, 'VolumeMultiple': VolumeMultiple_dict[main_contract]['VolumeMultiple'],