def __build_account_position(account_id, investor_position_array): position_list = [] for investor_position in investor_position_array: position_db = Position() position_db.date = now_date_str position_db.id = account_id message_symbol = getattr(investor_position, 'SYMBOL', 'NULL').split('.')[0] if message_symbol not in instrument_dict: continue instrumet_db = instrument_dict[message_symbol] position_db.symbol = instrumet_db.ticker position_db.hedgeflag = 0 long_qty_message = int(getattr(investor_position, 'LONG_QUANTITY', 0)) long_avail_qty_message = int( getattr(investor_position, 'AVAILABLE_QUANTITY', 0)) short_qty_message = abs( int(getattr(investor_position, 'SHORT_QUANTITY', 0))) if long_qty_message < 0: long_qty = 0 long_avail_qty = 0 short_qty = abs(long_qty_message) else: long_qty = long_qty_message long_avail_qty = long_avail_qty_message short_qty = short_qty_message average_price = float(getattr(investor_position, 'AVERAGE_PRICE', 0)) position_db.long = long_qty position_db.long_cost = long_qty * average_price * float( instrumet_db.fut_val_pt) position_db.long_avail = long_avail_qty position_db.yd_position_long = long_qty position_db.yd_long_remain = long_qty position_db.short = short_qty position_db.short_cost = short_qty * average_price * float( instrumet_db.fut_val_pt) position_db.short_avail = short_qty position_db.yd_position_short = short_qty position_db.yd_short_remain = short_qty position_db.prev_net = position_db.yd_position_long - position_db.yd_position_short position_db.frozen = 0 position_db.update_date = datetime.now() position_list.append(position_db) return position_list
def __build_account_position(account_id, investor_position_array): last_position_dict = dict() query_position = session_portfolio.query(Position) for position_db in query_position.filter(Position.id == account_id, Position.date == now_date_str): if position_db.symbol == 'CNY': continue last_position_dict[position_db.symbol] = position_db position_list = [] position_dict = dict() ticker_position_dict = dict() for investorPosition in investor_position_array: symbol = getattr(investorPosition, 'm_strInstrumentID', 'NULL') # 过滤掉SP j1609&j1701这种的持仓数据 if '&' in symbol: continue # 转换hedgeFlag字典 hedge_flag = getattr(investorPosition, 'm_nHedgeFlag', '0') hedge_flag = HEDGE_FLAG_MAP[hedge_flag] key = '%s|%s' % (symbol, hedge_flag) if key in ticker_position_dict: ticker_position_dict.get(key).append(investorPosition) else: ticker_position_dict[key] = [investorPosition] for (key, ticker_position_list) in ticker_position_dict.items(): (symbol, hedge_flag) = key.split('|') td_long = 0 td_long_cost = 0.0 yd_long_remain = 0 td_short = 0 td_short_cost = 0.0 yd_short_remain = 0 long_frozen = 0 for temp_position in ticker_position_list: qty_value = int(getattr(temp_position, 'm_nPosition', '0')) qty_cost_value = float( getattr(temp_position, 'm_dPositionCost', '0')) # 1:净,2:多头,3:空头 posiDirection_str = getattr(temp_position, 'm_nDirection', '0') posiDirection = Direction_Map[posiDirection_str] # 1:今日持仓,0:历史持仓 is_today = getattr(temp_position, 'm_bIsToday', '0') if is_today == '0' and posiDirection == '2': yd_long_remain = qty_value td_long_cost += qty_cost_value elif is_today == '0' and posiDirection == '3': yd_short_remain = qty_value td_short_cost += qty_cost_value elif is_today == '1' and posiDirection == '2': td_long = qty_value td_long_cost += qty_cost_value elif is_today == '1' and posiDirection == '3': td_short = qty_value td_short_cost += qty_cost_value else: print 'error m_bIsToday:%s, posiDirection:%s' % (is_today, posiDirection) continue prev_net = yd_long_remain - yd_short_remain if symbol in last_position_dict: position_db = last_position_dict[symbol] else: position_db = Position() position_db.yd_position_long = yd_long_remain position_db.yd_position_short = yd_short_remain position_db.date = now_date_str position_db.id = account_id position_db.symbol = symbol position_db.hedgeflag = hedge_flag position_db.long = td_long + yd_long_remain position_db.long_cost = td_long_cost position_db.long_avail = td_long + yd_long_remain position_db.short = td_short + yd_short_remain position_db.short_cost = td_short_cost position_db.short_avail = td_short + yd_short_remain position_db.yd_long_remain = yd_long_remain position_db.yd_short_remain = yd_short_remain position_db.prev_net = prev_net position_db.frozen = long_frozen position_db.update_date = datetime.now() position_dict[key] = position_db position_list.append(position_db) return position_dict, position_list
def __calculation_position(account_id, position_info_list, trade_list): position_info_dict = dict() # 根据yd值还原早上持仓 for position_db in position_info_list: position_db.td_buy_long = 0 position_db.long_avail = position_db.yd_position_long position_db.yd_long_remain = position_db.yd_position_long position_db.td_sell_short = 0 position_db.short_avail = position_db.yd_position_short position_db.yd_short_remain = position_db.yd_position_short position_info_dict[position_db.symbol] = position_db for trade_db in trade_list: if trade_db.symbol.upper() not in instrument_dict: print 'error trade ticker:%s' % trade_db.symbol continue instrument_db = instrument_dict[trade_db.symbol.upper()] if trade_db.symbol not in position_info_dict: position_db = Position() position_db.date = now_date_str position_db.id = account_id position_db.symbol = trade_db.symbol position_db.hedgeflag = 0 qty = trade_db.qty total_cost = trade_db.qty * trade_db.price * instrument_db.fut_val_pt if qty >= 0: position_db.long = qty position_db.long_cost = total_cost position_db.long_avail = qty position_db.yd_position_long = 0 position_db.yd_long_remain = 0 position_db.short = 0 position_db.short_cost = 0 position_db.short_avail = 0 position_db.yd_position_short = 0 position_db.yd_short_remain = 0 position_db.day_long = trade_db.qty position_db.day_long_cost = trade_db.qty * trade_db.price * instrument_db.fut_val_pt else: position_db.short = qty position_db.short_cost = total_cost position_db.short_avail = qty position_db.yd_position_short = 0 position_db.yd_short_remain = 0 position_db.long = 0 position_db.long_cost = 0 position_db.long_avail = 0 position_db.yd_position_long = 0 position_db.yd_long_remain = 0 position_db.day_short = trade_db.qty position_db.day_short_cost = trade_db.qty * trade_db.price * instrument_db.fut_val_pt position_db.prev_net = 0 position_db.frozen = 0 position_db.update_date = datetime.now() position_info_dict[position_db.symbol] = position_db else: position_db = position_info_dict[trade_db.symbol] # 期货 if instrument_db.type_id == 1: if trade_db.direction == 0: # 买开 if trade_db.offsetflag == 0: position_db.td_buy_long += trade_db.qty position_db.long_cost += trade_db.qty * trade_db.price * instrument_db.fut_val_pt # 买平 elif trade_db.offsetflag == 1: a = min(trade_db.qty, position_db.short) position_db.yd_short_remain -= max( trade_db.qty - position_db.short, 0) position_db.td_sell_short -= a position_db.short_cost -= trade_db.qty * trade_db.price * instrument_db.fut_val_pt # 买平昨 elif trade_db.offsetflag == 4: position_db.yd_short_remain -= trade_db.qty position_db.day_long += trade_db.qty position_db.day_long_cost += trade_db.qty * trade_db.price * instrument_db.fut_val_pt elif trade_db.direction == 1: # 卖开 if trade_db.offsetflag == 0: position_db.td_sell_short += trade_db.qty position_db.short_cost += trade_db.qty * trade_db.price * instrument_db.fut_val_pt # 卖平 elif trade_db.offsetflag == 1: a = min(trade_db.qty, position_db.long) position_db.yd_long_remain -= max( trade_db.qty - position_db.long, 0) position_db.td_buy_long -= a position_db.long_cost -= trade_db.qty * trade_db.price * instrument_db.fut_val_pt # 卖平昨 elif trade_db.offsetflag == 4: position_db.yd_long_remain -= trade_db.qty position_db.day_short += trade_db.qty position_db.day_short_cost += trade_db.qty * trade_db.price * instrument_db.fut_val_pt position_db.long_avail = position_db.td_buy_long + position_db.yd_long_remain position_db.short_avail = position_db.td_sell_short + position_db.yd_short_remain position_db.long = position_db.long_avail position_db.short = position_db.short_avail # 股票 elif instrument_db.type_id == 4: if trade_db.direction == 0: position_db.long += trade_db.qty position_db.long_cost += trade_db.qty * trade_db.price * instrument_db.fut_val_pt elif trade_db.direction == 1: position_db.long -= trade_db.qty position_db.long_cost -= trade_db.qty * trade_db.price * instrument_db.fut_val_pt position_db.short_avail -= trade_db.qty position_info_dict[position_db.symbol] = position_db result_list = [] for key in position_info_dict.keys(): result_list.append(position_info_dict[key]) return result_list
def save_account_position(account_info, investor_position_array): tick_position_dict = dict() for investor_position in investor_position_array: symbol = getattr(investor_position, 'InstrumentID', 'NULL') # 投机:'1'|套保:'3' # hedge_flag = getattr(investor_position, 'HedgeFlag', '0') # hedge_flag = const.HEDGE_FLAG_MAP[hedge_flag] hedge_flag = 0 key = '%s|%s' % (symbol, hedge_flag) if key in tick_position_dict: tick_position_dict.get(key).append(investor_position) else: tick_position_dict[key] = [investor_position] for (key, tickPositionList) in tick_position_dict.items(): (symbol, hedge_flag) = key.split('|') td_long = 0 td_long_cost = 0.0 td_long_avail = 0 td_short = 0 td_short_cost = 0.0 td_short_avail = 0 yd_long = 0 yd_long_remain = 0 yd_short = 0 yd_short_remain = 0 prev_net = 0 purchase_avail = 0 long_frozen = 0 short_frozen = 0 save_flag = False for position_message in tickPositionList: real_account_id = getattr(position_message, 'AccountID', 'NULL') if (real_account_id[12:14] == '70') and (account_info.accountsuffix == '70'): save_flag = True elif (real_account_id[12:14] == '01') and (account_info.accountsuffix == '01'): save_flag = True else: continue position = float(getattr(position_message, 'Position', '0')) positionCost = float(getattr(position_message, 'PositionCost', '0')) ydPosition = float(getattr(position_message, 'YdPosition', '0')) # todayPurRedVolume = getattr(position_message, 'TodayPurRedVolume', '0') # todayPosition = getattr(position_message, 'TodayPosition', '0') openVolume = float(getattr(position_message, 'OpenVolume', '0')) closeVolume = float(getattr(position_message, 'CloseVolume', '0')) long_frozen = getattr(position_message, 'LongFrozen', '0') short_frozen = getattr(position_message, 'ShortFrozen', '0') long_flag = True # 净:'1'|多头:'2'|空头:'3'|备兑:'4' posi_direction = getattr(position_message, 'PosiDirection', '0') if posi_direction == '2': long_flag = True elif posi_direction == '3': long_flag = False elif (posi_direction == '1') and (ydPosition < 0): long_flag = False elif (posi_direction == '1') and (ydPosition >= 0): long_flag = True else: print 'Error in OnRspQryInvestorPosition' if long_flag: td_long = position td_long_cost = positionCost yd_long = ydPosition if (ydPosition - closeVolume) > 0: yd_long_remain = ydPosition - closeVolume else: yd_long_remain = 0 else: td_short = abs(position) td_short_cost = abs(positionCost) yd_short = abs(ydPosition) if (abs(ydPosition) - openVolume) > 0: yd_short_remain = abs(ydPosition) - openVolume else: yd_short_remain = 0 if save_flag: td_long_avail = yd_long td_short_avail = yd_short purchase_avail = yd_long prev_net = int(yd_long) - int(yd_short) position_db = Position() position_db.date = today_str position_db.id = account_info.accountid position_db.symbol = symbol position_db.hedgeflag = hedge_flag position_db.long = td_long position_db.long_cost = td_long_cost position_db.long_avail = td_long_avail position_db.short = td_short position_db.short_cost = td_short_cost position_db.short_avail = td_short_avail position_db.yd_position_long = yd_long position_db.yd_position_short = yd_short position_db.yd_long_remain = yd_long_remain position_db.yd_short_remain = yd_short_remain position_db.prev_net = prev_net position_db.purchase_avail = purchase_avail position_db.frozen = long_frozen position_db.update_date = datetime.now() position_list.append(position_db)
def __build_account_position(account_id, investor_position_array): position_list = [] position_dict = dict() ticker_position_dict = dict() for investorPosition in investor_position_array: symbol = getattr(investorPosition, 'InstrumentID', 'NULL') # 过滤掉SP j1609&j1701这种的持仓数据 if '&' in symbol: continue if local_server_name == 'huabao': if not ('IH' in symbol or 'IC' in symbol or 'IF' in symbol): continue if local_server_name == 'zhongxin': if 'IH' in symbol or 'IC' in symbol or 'IF' in symbol: continue # 转换hedgeFlag字典 hedge_flag = getattr(investorPosition, 'HedgeFlag', '0') hedge_flag = const.HEDGE_FLAG_MAP[hedge_flag] key = '%s|%s' % (symbol, hedge_flag) if key in ticker_position_dict: ticker_position_dict.get(key).append(investorPosition) else: ticker_position_dict[key] = [investorPosition] for (key, ticker_position_list) in ticker_position_dict.items(): (symbol, hedge_flag) = key.split('|') td_long = 0 td_long_avail = 0 td_long_cost = 0.0 yd_long = 0 yd_long_remain = 0 td_short = 0 td_short_avail = 0 td_short_cost = 0.0 yd_short = 0 yd_short_remain = 0 long_Frozen = 0 short_Frozen = 0 prev_net = 0 posiDirection_dict = dict() for temp_position in ticker_position_list: # 1:净,2:多头,3:空头 posiDirection = getattr(temp_position, 'PosiDirection', '0') if posiDirection in posiDirection_dict: posiDirection_dict[posiDirection].append(temp_position) else: posiDirection_dict[posiDirection] = [temp_position] for (posiDirection, direction_position_list) in posiDirection_dict.items(): position = 0 ydPosition = 0 position_cost = 0.0 for temp_position in direction_position_list: # 1:今日持仓,2:历史持仓 positionDate = getattr(temp_position, 'PositionDate', '1') if positionDate == '1': position = int(getattr(temp_position, 'Position', '0')) position_cost = float(getattr(temp_position, 'PositionCost', '0')) ydPosition = int(getattr(temp_position, 'YdPosition', '0')) elif positionDate == '2': position += int(getattr(temp_position, 'Position', '0')) position_cost = float(getattr(temp_position, 'PositionCost', '0')) ydPosition += int(getattr(temp_position, 'YdPosition', '0')) else: print 'error positionDate:', positionDate continue if posiDirection == '1': td_long = position td_long_avail = position yd_long = ydPosition yd_long_remain = ydPosition td_long_cost = position_cost elif posiDirection == '2': td_long = position td_long_avail = position yd_long = ydPosition yd_long_remain = ydPosition td_long_cost = position_cost elif posiDirection == '3': td_short = position td_short_avail = position yd_short = ydPosition yd_short_remain = ydPosition td_short_cost = position_cost else: print 'error posiDirection:', posiDirection prev_net = yd_long - yd_short position_db = Position() position_db.date = now_date_str position_db.id = account_id position_db.symbol = symbol position_db.hedgeflag = hedge_flag position_db.long = td_long if symbol == '510050': pre_settlement_price = float(getattr(temp_position, 'PreSettlementPrice', '0')) position_db.long_cost = td_long * pre_settlement_price position_db.long_avail = yd_long_remain else: position_db.long_cost = td_long_cost position_db.long_avail = td_long_avail position_db.short = td_short position_db.short_cost = td_short_cost position_db.short_avail = td_short_avail position_db.yd_position_long = yd_long position_db.yd_position_short = yd_short position_db.yd_long_remain = yd_long_remain position_db.yd_short_remain = yd_short_remain position_db.prev_net = prev_net position_db.frozen = long_Frozen position_db.update_date = datetime.now() position_dict[key] = position_db position_list.append(position_db) return position_dict, position_list
def save_account_position(account_id, investor_position_array): tick_position_dict = dict() for investor_position in investor_position_array: symbol = getattr(investor_position, 'InstrumentID', 'NULL') hedge_flag = getattr(investor_position, 'HedgeFlag', '0') if hedge_flag == '1': hedge_flag = '0' elif hedge_flag == '2': hedge_flag = '1' elif hedge_flag == '3': hedge_flag = '2' key = '%s|%s' % (symbol, hedge_flag) if key in tick_position_dict: tick_position_dict.get(key).append(investor_position) else: tick_position_dict[key] = [investor_position] for (key, tickPositionList) in tick_position_dict.items(): (symbol, hedge_flag) = key.split('|') td_long = 0 td_long_cost = 0.0 td_long_avail = 0 yd_long = 0 yd_long_remain = 0 td_short = 0 td_short_cost = 0.0 td_short_avail = 0 yd_short = 0 yd_short_remain = 0 long_frozen = 0 short_frozen = 0 prev_net = 0 for temp_position in tickPositionList: # 转换hedgeFlag字典 direction = getattr(temp_position, 'Direction', 'NULL') position = int(getattr(temp_position, 'Position', 'NULL')) position_cost = getattr(temp_position, 'PositionCost', 'NULL') yd_position = int(getattr(temp_position, 'YdPosition', 'NULL')) if direction == '0': td_long = position td_long_avail = position td_long_cost = position_cost yd_long = yd_position else: td_short = position td_short_avail = position td_short_cost = position_cost yd_short = yd_position prev_net = yd_long - yd_short (yd_long_remain, yd_short_remain) = calculation_by_trade(symbol, yd_long, yd_short) position_db = Position() position_db.date = today_str position_db.id = account_id position_db.symbol = symbol position_db.hedgeflag = hedge_flag position_db.long = td_long position_db.long_cost = td_long_cost position_db.long_avail = td_long_avail position_db.short = td_short position_db.short_cost = td_short_cost position_db.short_avail = td_short_avail position_db.yd_position_long = yd_long position_db.yd_position_short = yd_short position_db.yd_long_remain = yd_long_remain position_db.yd_short_remain = yd_short_remain position_db.prev_net = prev_net # position_db.purchase_avail = purchase_avail position_db.frozen = long_frozen position_db.update_date = datetime.now() position_list.append(position_db)