def __build_phone_trade_file(server_name, pf_account_name, db_position_df, memory_position_df): position_compare_df = pd.merge(db_position_df, memory_position_df, how='outer', on=['Symbol']).fillna(0) position_diff_df = position_compare_df[position_compare_df['Long_db'] != position_compare_df['Long']] phone_trade_list = [] position_diff_dict = position_diff_df.to_dict("index") for (dict_key, dict_values) in position_diff_dict.items(): if dict_values['Symbol'] == 'CNY': continue phone_trade_info = PhoneTradeInfo() phone_trade_info.fund = dict_values['FundName'] phone_trade_info.strategy1 = pf_account_name phone_trade_info.symbol = dict_values['Symbol'] phone_trade_info.hedgeflag = Hedge_Flag_Type_Enums.Speculation phone_trade_info.tradetype = Trade_Type_Enums.Normal phone_trade_info.iotype = IO_Type_Enums.Outer phone_trade_info.server_name = dict_values['ServerName'] phone_trade_info.exprice = dict_values['Last_Price'] if dict_values['Long_db'] < dict_values['Long']: phone_trade_info.direction = Direction_Enums.Buy phone_trade_info.exqty = dict_values['Long'] - dict_values['Long_db'] else: phone_trade_info.direction = Direction_Enums.Sell phone_trade_info.exqty = dict_values['Long_db'] - dict_values['Long'] phone_trade_list.append(phone_trade_info) server_save_path = os.path.join(PHONE_TRADE_FOLDER, server_name) if not os.path.exists(server_save_path): os.mkdir(server_save_path) phone_trade_file_path = '%s/position_repair_%s.csv' % (server_save_path, date_utils.get_today_str('%Y%m%d')) save_phone_trade_file(phone_trade_file_path, phone_trade_list)
def __build_phone_trade(self, phone_trade_df): phone_trade_list = [] trade_info_list = [ value for (key, value) in phone_trade_df.to_dict("index").items() ] for phone_trade_item in trade_info_list: phone_trade_info = PhoneTradeInfo() phone_trade_info.fund = phone_trade_item['Fund_Name'] strategy_name_item = phone_trade_item['Strategy_Name'].split('-') phone_trade_info.strategy1 = '%s.%s' % (strategy_name_item[1], strategy_name_item[0]) phone_trade_info.symbol = phone_trade_item['Symbol'] phone_trade_info.direction = Direction_Enums.Sell if phone_trade_item[ 'Cross_Qty'] < 0 else Direction_Enums.Buy phone_trade_info.tradetype = Trade_Type_Enums.Normal phone_trade_info.hedgeflag = Hedge_Flag_Type_Enums.Speculation phone_trade_info.exprice = phone_trade_item['Prev_Close'] phone_trade_info.exqty = abs(phone_trade_item['Cross_Qty']) phone_trade_info.iotype = IO_Type_Enums.Inner1 phone_trade_info.server_name = self.__server_name phone_trade_list.append(phone_trade_info) phone_trade_list.sort( key=lambda item: '%s|%s|%s' % (item.fund, item.symbol, item.exqty)) phone_trade_file_name = 'phone_trade.csv' phone_trade_file_path = '%s/%s' % (self.__base_save_folder, phone_trade_file_name) save_phone_trade_file(phone_trade_file_path, phone_trade_list, False)
def send_by_file(file_path): phone_trade_list = [] with open(file_path, 'rb') as fr: for line in fr.readlines(): line_item = line.replace('\r\n', '').split(',') if len(line_item) != 11: continue phone_trade_info = PhoneTradeInfo() phone_trade_info.fund = line_item[0] if line_item[1] == 'manual.default': phone_trade_info.strategy1 = '' else: phone_trade_info.strategy1 = line_item[1] phone_trade_info.symbol = line_item[2] phone_trade_info.direction = direction_dict[line_item[3]] phone_trade_info.tradetype = trade_type_dict[line_item[4]] phone_trade_info.hedgeflag = hedge_flag_dict[line_item[5]] phone_trade_info.exprice = float(line_item[6]) phone_trade_info.exqty = int(line_item[7]) phone_trade_info.iotype = io_type_dict[line_item[8]] if line_item[9] == 'manual.default': phone_trade_info.strategy2 = '' else: phone_trade_info.strategy2 = line_item[9] phone_trade_info.server_name = line_item[10] phone_trade_list.append(phone_trade_info) send_phone_trade(server_name, phone_trade_list)
def __build_phone_trade(buy_file_name, sell_file_name, ticker, phone_number): phone_trade_info = PhoneTradeInfo() phone_trade_info.fund = __rebuild_fund_name(buy_file_name) phone_trade_info.strategy1 = __rebuild_strategy_name(buy_file_name) phone_trade_info.symbol = ticker phone_trade_info.direction = Direction_Enums.Buy phone_trade_info.tradetype = Trade_Type_Enums.Normal phone_trade_info.hedgeflag = Hedge_Flag_Type_Enums.Speculation phone_trade_info.exqty = phone_number phone_trade_info.iotype = IO_Type_Enums.Inner2 phone_trade_info.strategy2 = __rebuild_strategy_name(sell_file_name) return phone_trade_info
def __default_short_volume_repair(self): phone_trade_list = [] pf_position_df = pd.merge(self.pf_position_df, self.__pf_account_df, how='left', on=['Strategy_Name']) filter_position_df = pf_position_df[ (pf_position_df['Strategy_Name'].str.find( self.__default_strategy_key) >= 0) & (pf_position_df['Volume'] < 0)] if len(filter_position_df) == 0: return for index, row in filter_position_df.iterrows(): default_fund, default_symbol = row['Fund'], row['Symbol'] default_need_volume = calculation_target_volume(abs(row['Volume'])) symbol_position_df = pf_position_df[ (pf_position_df['Strategy_Name'].str.find( self.__multifactor_strategy_key) >= 0) & (pf_position_df['Fund'] == default_fund) & (pf_position_df['Symbol'] == default_symbol) & (pf_position_df['Volume'] > 0)] for sub_index, sub_row in symbol_position_df.iterrows(): if default_need_volume <= 0: break trade_volume = default_need_volume if int( sub_row['Volume']) > default_need_volume else int( sub_row['Volume']) phone_trade_info = PhoneTradeInfo() phone_trade_info.fund = sub_row['Fund'] phone_trade_info.strategy1 = '%s.%s' % (sub_row['Group_Name'], sub_row['Name']) phone_trade_info.symbol = sub_row['Symbol'] phone_trade_info.hedgeflag = Hedge_Flag_Type_Enums.Speculation phone_trade_info.tradetype = Trade_Type_Enums.Normal phone_trade_info.iotype = IO_Type_Enums.Inner2 phone_trade_info.strategy2 = '%s.%s' % ( sub_row['Default_Group_Name'], sub_row['Default_Name']) phone_trade_info.server_name = self.__server_name phone_trade_info.exprice = sub_row['Prev_Close'] phone_trade_info.direction = Direction_Enums.Sell phone_trade_info.exqty = trade_volume phone_trade_list.append(phone_trade_info) default_need_volume -= trade_volume if phone_trade_list: send_phone_trade(self.__server_name, phone_trade_list) self.__phone_trade_list.extend(phone_trade_list)
def __default_long_volume_repair(self): phone_trade_list = [] pf_position_df = pd.merge(self.pf_position_df, self.__pf_account_df, how='left', on=['Strategy_Name']) filter_position_df = pf_position_df[ (pf_position_df['Strategy_Name'].str.find( self.__default_strategy_key) >= 0) & (pf_position_df['Volume'] >= 100)] if len(filter_position_df) == 0: return for index, row in filter_position_df.iterrows(): default_fund, default_symbol = row['Fund'], row['Symbol'] symbol_position_df = pf_position_df[ (pf_position_df['Fund'] == default_fund) & (pf_position_df['Strategy_Name'].str.find( self.__multifactor_strategy_key) >= 0) & (pf_position_df['Symbol'] == default_symbol)] if symbol_position_df.empty: continue default_sell_volume = round_down(row['Volume']) for sub_index, sub_row in symbol_position_df.iterrows(): phone_trade_info = PhoneTradeInfo() phone_trade_info.fund = sub_row['Fund'] phone_trade_info.strategy1 = '%s.%s' % (sub_row['Group_Name'], sub_row['Name']) phone_trade_info.symbol = sub_row['Symbol'] phone_trade_info.hedgeflag = Hedge_Flag_Type_Enums.Speculation phone_trade_info.tradetype = Trade_Type_Enums.Normal phone_trade_info.iotype = IO_Type_Enums.Inner2 phone_trade_info.strategy2 = '%s.%s' % ( sub_row['Default_Group_Name'], sub_row['Default_Name']) phone_trade_info.server_name = self.__server_name phone_trade_info.exprice = sub_row['Prev_Close'] phone_trade_info.direction = Direction_Enums.Buy phone_trade_info.exqty = default_sell_volume phone_trade_list.append(phone_trade_info) break if phone_trade_list: send_phone_trade(self.__server_name, phone_trade_list) self.__phone_trade_list.extend(phone_trade_list)
def __multifactor_stock_repair(self): phone_trade_list = [] pf_position_df = pd.merge(self.pf_position_df, self.__pf_account_df, how='left', on=['Strategy_Name']) filter_position_df = pf_position_df[ (pf_position_df['Strategy_Name'].str.find( self.__multifactor_strategy_key) >= 0) & ((pf_position_df['Volume'] % 100 != 0) | (pf_position_df['Volume'] < 0))] if len(filter_position_df) == 0: return phone_trade_list for index, row in filter_position_df.iterrows(): strategy_volume = row['Volume'] if strategy_volume >= 0: trade_volume = strategy_volume % 100 direction = Direction_Enums.Sell else: trade_volume = abs(strategy_volume) direction = Direction_Enums.Buy phone_trade_info = PhoneTradeInfo() phone_trade_info.fund = row['Fund'] phone_trade_info.strategy1 = '%s.%s' % (row['Group_Name'], row['Name']) phone_trade_info.symbol = row['Symbol'] phone_trade_info.hedgeflag = Hedge_Flag_Type_Enums.Speculation phone_trade_info.tradetype = Trade_Type_Enums.Normal phone_trade_info.iotype = IO_Type_Enums.Inner2 phone_trade_info.strategy2 = '%s.%s' % (row['Default_Group_Name'], row['Default_Name']) phone_trade_info.server_name = self.__server_name phone_trade_info.exprice = row['Prev_Close'] phone_trade_info.direction = direction phone_trade_info.exqty = trade_volume phone_trade_list.append(phone_trade_info) return phone_trade_list
def __main_contract_position_change(server_name, future_main_contract_db, pf_position_list): pre_main_symbol = future_main_contract_db.pre_main_symbol main_symbol = future_main_contract_db.main_symbol if future_main_contract_db.ticker_type in const.Index_Future_List: target_strategy_filter = 'SU-CalendarSpread' else: target_strategy_filter = 'Transfer-CalendarMA' pf_account_dict = dict() target_strategy_account_dict = dict() server_model = server_constant.get_server_model(server_name) session_portfolio = server_model.get_db_session('portfolio') query_pf_account = session_portfolio.query(PfAccount) for pf_account_db in query_pf_account: if target_strategy_filter in pf_account_db.fund_name: fund_name_items = pf_account_db.fund_name.split('-') if len(fund_name_items) >= 3: target_strategy_account_dict[ fund_name_items[2]] = pf_account_db continue elif 'PutCallParity' in pf_account_db.fund_name: continue elif 'MarketMaking1' in pf_account_db.fund_name: continue pf_account_dict[pf_account_db.fund_name] = pf_account_db phone_trade_list = [] for pf_position_item in pf_position_list: base_strategy_name, qty = pf_position_item if base_strategy_name not in pf_account_dict: continue pf_account_db = pf_account_dict[base_strategy_name] fund_name_items = pf_account_db.fund_name.split('-') if len(fund_name_items) < 3: custom_log.log_error_job('Error Fund_Name:%s' % pf_account_db.fund_name) continue fund = pf_account_db.fund_name.split('-')[2] transfer_account_db = target_strategy_account_dict[fund] close_phone_trade = PhoneTradeInfo() close_phone_trade.fund = fund close_phone_trade.strategy1 = '%s.%s' % (pf_account_db.group_name, pf_account_db.name) close_phone_trade.symbol = pre_main_symbol if qty == 0: continue elif qty > 0: close_phone_trade.direction = Direction_Enums.Sell close_phone_trade.exqty = qty else: close_phone_trade.direction = Direction_Enums.Buy close_phone_trade.exqty = abs(qty) close_phone_trade.tradetype = Trade_Type_Enums.Close close_phone_trade.hedgeflag = Hedge_Flag_Type_Enums.Speculation close_phone_trade.iotype = IO_Type_Enums.Inner2 close_phone_trade.server_name = server_name close_phone_trade.exprice = instrument_db_dict[pre_main_symbol].close close_phone_trade.strategy2 = '%s.%s' % ( transfer_account_db.group_name, transfer_account_db.name) phone_trade_list.append(close_phone_trade) open_phone_trade = PhoneTradeInfo() open_phone_trade.fund = fund open_phone_trade.strategy1 = '%s.%s' % (pf_account_db.group_name, pf_account_db.name) open_phone_trade.symbol = main_symbol if qty > 0: open_phone_trade.direction = Direction_Enums.Buy open_phone_trade.exqty = qty else: open_phone_trade.direction = Direction_Enums.Sell open_phone_trade.exqty = abs(qty) open_phone_trade.tradetype = Trade_Type_Enums.Open open_phone_trade.hedgeflag = Hedge_Flag_Type_Enums.Speculation open_phone_trade.iotype = IO_Type_Enums.Inner2 open_phone_trade.server_name = server_name open_phone_trade.exprice = instrument_db_dict[main_symbol].close open_phone_trade.strategy2 = '%s.%s' % (transfer_account_db.group_name, transfer_account_db.name) phone_trade_list.append(open_phone_trade) if len(phone_trade_list) > 0: send_phone_trade(server_name, phone_trade_list)
def __build_phone_trades(server_name, compare_result_list, pf_account_dict, pf_position_list): pf_position_dict = dict() for pf_position_info in pf_position_list: pf_account_id, fund, ticker, pf_long, pf_short = pf_position_info dict_key = '%s|%s' % (fund, ticker) pf_position_dict.setdefault(dict_key, []).append(pf_position_info) default_account_dict = dict() for (account_id, pf_account_db) in pf_account_dict.items(): if pf_account_db.group_name == 'manual': fund_name = pf_account_db.fund_name.split('-')[2] default_account_dict[fund_name] = pf_account_db instrument_dict = query_instrument_dict('host') phone_trade_list = [] for compare_info_item in compare_result_list: fund, ticker, real_long, real_short, pf_long, pf_short, diff = compare_info_item if ticker not in instrument_dict: custom_log.log_error_job('Server:%s, UnFind Ticker:%s' % (server_name, ticker)) continue instrument_db = instrument_dict[ticker] # *暂只处理股票的不一致 if instrument_db.type_id != Instrument_Type_Enums.CommonStock: continue # 期货和期权需要单独设置tradetype future_flag = False if instrument_db.type_id in [ Instrument_Type_Enums.Future, Instrument_Type_Enums.Option ]: future_flag = True prev_close = instrument_db.prev_close if prev_close is None: continue if pf_long > real_long: diff_long = pf_long - real_long find_key = '%s|%s' % (fund, ticker) pf_position_list = pf_position_dict[find_key] for pf_position_info in pf_position_list: pf_account_id, fund, ticker, item_long, item_short = pf_position_info if item_long == 0: continue pf_account_info = pf_account_dict[pf_account_id] phone_trade_info = PhoneTradeInfo() phone_trade_info.fund = fund phone_trade_info.strategy1 = '%s.%s' % ( pf_account_info.group_name, pf_account_info.name) phone_trade_info.symbol = ticker phone_trade_info.direction = Direction_Enums.Sell phone_trade_info.tradetype = Trade_Type_Enums.Close if future_flag else Trade_Type_Enums.Normal phone_trade_info.hedgeflag = Hedge_Flag_Type_Enums.Speculation phone_trade_info.exqty = min(item_long, diff_long) phone_trade_info.iotype = IO_Type_Enums.Inner1 phone_trade_info.server_name = server_name phone_trade_info.exprice = prev_close phone_trade_list.append(phone_trade_info) diff_long = max(diff_long - item_long, 0) if diff_long == 0: break if real_long > pf_long: default_pf_account = default_account_dict[fund] phone_trade_info = PhoneTradeInfo() phone_trade_info.fund = fund phone_trade_info.strategy1 = '%s.%s' % ( default_pf_account.group_name, default_pf_account.name) phone_trade_info.symbol = ticker phone_trade_info.direction = Direction_Enums.Buy phone_trade_info.tradetype = Trade_Type_Enums.Open if future_flag else Trade_Type_Enums.Normal phone_trade_info.hedgeflag = Hedge_Flag_Type_Enums.Speculation phone_trade_info.exqty = real_long - pf_long phone_trade_info.iotype = IO_Type_Enums.Inner1 phone_trade_info.server_name = server_name phone_trade_info.exprice = prev_close phone_trade_list.append(phone_trade_info) if pf_short > real_short: diff_short = pf_short - real_short find_key = '%s|%s' % (fund, ticker) pf_position_list = pf_position_dict[find_key] for pf_position_info in pf_position_list: pf_account_id, fund, ticker, item_long, item_short = pf_position_info if item_short == 0: continue pf_account_info = pf_account_dict[pf_account_id] phone_trade_info = PhoneTradeInfo() phone_trade_info.fund = fund phone_trade_info.strategy1 = '%s.%s' % ( pf_account_info.group_name, pf_account_info.name) phone_trade_info.symbol = ticker phone_trade_info.direction = Direction_Enums.Buy phone_trade_info.tradetype = Trade_Type_Enums.Close if future_flag else Trade_Type_Enums.Normal phone_trade_info.hedgeflag = Hedge_Flag_Type_Enums.Speculation phone_trade_info.exqty = min(item_short, diff_short) phone_trade_info.iotype = IO_Type_Enums.Inner1 phone_trade_info.server_name = server_name phone_trade_info.exprice = prev_close phone_trade_list.append(phone_trade_info) diff_short = max(diff_short - item_short, 0) if diff_short == 0: break if real_short > pf_short: default_pf_account = default_account_dict[fund] phone_trade_info = PhoneTradeInfo() phone_trade_info.fund = fund phone_trade_info.strategy1 = '%s.%s' % ( default_pf_account.group_name, default_pf_account.name) phone_trade_info.symbol = ticker phone_trade_info.direction = Direction_Enums.Sell phone_trade_info.tradetype = Trade_Type_Enums.Open if future_flag else Trade_Type_Enums.Normal phone_trade_info.hedgeflag = Hedge_Flag_Type_Enums.Speculation phone_trade_info.exqty = real_short - pf_short phone_trade_info.iotype = IO_Type_Enums.Inner1 phone_trade_info.server_name = server_name phone_trade_info.exprice = prev_close phone_trade_list.append(phone_trade_info) return phone_trade_list