def __get_order_list(account_id, order_array):
    order_list = []
    order_dict = dict()
    for order_info in order_array:
        order_db = Order()
        order_db.sys_id = getattr(order_info, 'OrderSysID', '')
        # sys_id为空表示交易未到达交易所即被打回
        if order_db.sys_id == '':
            continue

        order_db.account = account_id
        order_db.symbol = getattr(order_info, 'InstrumentID', '')
        if local_server_name == 'huabao':
            if not ('IH' in order_db.symbol or 'IC' in order_db.symbol or 'IF' in order_db.symbol):
                continue
        if local_server_name == 'zhongxin':
            if 'IH' in order_db.symbol or 'IC' in order_db.symbol or 'IF' in order_db.symbol:
                continue

        # 0:买,1:卖
        order_db.direction = getattr(order_info, 'Direction', '')
        # 0:开仓  1:平仓  3:平今  4:平昨
        order_db.trade_type = getattr(order_info, 'CombOffsetFlag', '')

        # 全部成交:'0' 部分成交还在队列中:'1',部分成交不在队列中:'2',未成交还在队列中:'3',
        # 未成交不在队列中:'4',撤单:'5',未知:'a',尚未触发:'b',已触发:'c'
        order_db.status = getattr(order_info, 'OrderStatus', '')

        # 已经提交:'0',撤单已经提交:'1',修改已经提交:'2',已经接受:'3',报单已经被拒绝:'4',撤单已经被拒绝:'5',改单已经被拒绝:'6'
        order_db.submit_status = getattr(order_info, 'OrderSubmitStatus', '')

        trading_day = getattr(order_info, 'InsertDate', '')
        insert_time = getattr(order_info, 'InsertTime', '')
        if (trading_day != '') and (insert_time != ''):
            insert_time_str = '%s-%s-%s %s' % (trading_day[0:4], trading_day[4:6], trading_day[6:8], insert_time)
            if insert_time > now_datetime_str:
                insert_time_str = '%s %s' % (last_trading_day, insert_time)
        else:
            insert_time_str = '%s 00:00:00' % (now_date_str,)
        order_db.insert_time = insert_time_str

        qty = getattr(order_info, 'VolumeTotalOriginal', '')
        if order_db.direction == '0':
            order_db.qty = qty
        elif order_db.direction == '1':
            order_db.qty = 0 - int(qty)
        order_db.price = getattr(order_info, 'LimitPrice', '')
        order_db.ex_qty = getattr(order_info, 'VolumeTraded', '')
        order_list.append(order_db)
        order_dict[order_db.sys_id] = order_db
    return order_list, order_dict
def __get_order_list(account_id, order_array):
    order_list = []
    order_dict = dict()
    for order_info in order_array:
        order_db = Order()
        order_db.sys_id = getattr(order_info, 'm_strOrderSysID', '')
        # sys_id为空表示交易未到达交易所即被打回
        if order_db.sys_id == '':
            continue

        order_db.account = account_id
        order_db.symbol = getattr(order_info, 'm_strInstrumentID', '')

        # 48:买,49:卖
        order_db.direction = getattr(order_info, 'm_nDirection', '')

        # 0:开仓  1:平仓  3:平今  4:平昨
        order_db.trade_type = getattr(order_info, 'm_eOffsetFlag', '')

        # 全部成交:'0' 部分成交还在队列中:'1',部分成交不在队列中:'2',未成交还在队列中:'3',
        # 未成交不在队列中:'4',撤单:'5',未知:'a',尚未触发:'b',已触发:'c'
        order_db.status = getattr(order_info, 'EEntrustStatus', '')

        # 已经提交:'0',撤单已经提交:'1',修改已经提交:'2',已经接受:'3',报单已经被拒绝:'4',撤单已经被拒绝:'5',改单已经被拒绝:'6'
        order_db.submit_status = getattr(order_info, 'EEntrustSubmitStatus',
                                         '')

        trading_day = getattr(order_info, 'm_strInsertDate', '')
        insert_time = getattr(order_info, 'm_strInsertTime', '')
        if (trading_day != '') and (insert_time != ''):
            insert_time_str = '%s-%s-%s %s' % (trading_day[0:4],
                                               trading_day[4:6],
                                               trading_day[6:8], insert_time)
            if insert_time > now_datetime_str:
                insert_time_str = '%s %s' % (last_trading_day, insert_time)
        else:
            insert_time_str = '%s 00:00:00' % (now_date_str, )
        order_db.insert_time = insert_time_str

        qty = getattr(order_info, 'm_nTotalVolume', '')
        if order_db.direction == '0':
            order_db.qty = qty
        elif order_db.direction == '1':
            order_db.qty = 0 - int(qty)
        order_db.price = getattr(order_info, 'm_dAveragePrice', '')
        order_db.ex_qty = getattr(order_info, 'm_nTradedVolume', '')
        order_list.append(order_db)
        order_dict[order_db.sys_id] = order_db
    return order_list, order_dict
def save_order(account_id, order_array):
    for order_info in order_array:
        order_db = Order()
        order_db.sys_id = getattr(order_info, 'OrderSysID', '')
        order_db.account = account_id
        order_db.symbol = getattr(order_info, 'InstrumentID', '')
        order_db.direction = getattr(order_info, 'Direction', '')
        order_db.trade_type = getattr(order_info, 'OffsetFlag', '')
        order_db.status = getattr(order_info, 'OrderStatus', '')

        order_submit_status = getattr(order_info, 'OrderSubmitStatus', '')
        if order_submit_status == '':
            order_submit_status = order_db.status
        order_db.submit_status = order_submit_status

        trading_day = getattr(order_info, 'TradingDay', '')
        insert_time = getattr(order_info, 'InsertTime', '')
        if (trading_day != '') and (insert_time != ''):
            insert_time_str = '%s-%s-%s %s' % (trading_day[0:4], trading_day[4:6], trading_day[6:8], insert_time)
        else:
            insert_time_str = '%s 00:00:00' % (today_str,)
        order_db.insert_time = insert_time_str

        order_db.qty = getattr(order_info, 'Volume', '')
        order_db.price = getattr(order_info, 'LimitPrice', '')
        order_db.ex_qty = getattr(order_info, 'VolumeTraded', '')
        order_list.append(order_db)
Ejemplo n.º 4
0
def __get_order_list(account_id, order_array):
    order_list = []
    order_dict = dict()
    for order_info in order_array:
        order_db = Order()
        order_db.sys_id = getattr(order_info, 'report_no', '')
        # sys_id为空表示交易未到达交易所即被打回
        if order_db.sys_id == '':
            continue

        order_db.account = account_id
        order_db.symbol = getattr(order_info, 'stock_code', '')

        # 48:买,49:卖
        order_db.direction = getattr(order_info, 'entrust_bs', '')

        # # 0:开仓  1:平仓  3:平今  4:平昨
        # order_db.trade_type = getattr(order_info, 'm_eOffsetFlag', '')
        order_db.trade_type = 0

        # 全部成交:'0' 部分成交还在队列中:'1',部分成交不在队列中:'2',未成交还在队列中:'3',
        # 未成交不在队列中:'4',撤单:'5',未知:'a',尚未触发:'b',已触发:'c'
        order_db.status = getattr(order_info, 'entrust_status', '')

        # # 已经提交:'0',撤单已经提交:'1',修改已经提交:'2',已经接受:'3',报单已经被拒绝:'4',撤单已经被拒绝:'5',改单已经被拒绝:'6'
        # order_db.submit_status = getattr(order_info, 'EEntrustSubmitStatus', '')
        order_db.submit_status = 0

        trading_day = getattr(order_info, 'entrust_date', '')
        insert_time = getattr(order_info, 'entrust_time', '').zfill(6)
        if (trading_day != '') and (insert_time != ''):
            insert_time_str = '%s-%s-%s %s:%s:%s' % (trading_day[0:4], trading_day[4:6], trading_day[6:8], \
                                                     insert_time[:2], insert_time[2:4], insert_time[4:6])
        else:
            insert_time_str = '%s 00:00:00' % (now_date_str, )
        order_db.insert_time = insert_time_str

        qty = getattr(order_info, 'entrust_amount', '')
        if order_db.direction == '0':
            order_db.qty = qty
        elif order_db.direction == '1':
            order_db.qty = 0 - float(qty)
        order_db.price = getattr(order_info, 'entrust_price', '')
        order_db.ex_qty = getattr(order_info, 'business_amount', '')
        order_list.append(order_db)
        order_dict[order_db.sys_id] = order_db
    return order_list, order_dict
def __build_order_trade(account_id, order_message_list):
    order_list = []
    trade_list = []
    order_dict = dict()
    for order_info in order_message_list:
        order_db = Order()

        order_db.sys_id = getattr(order_info, 'ORDER_ID', '')
        # sys_id为空表示交易未到达交易所即被打回
        # if order_db.sys_id == '':
        #     continue

        order_db.account = account_id

        message_symbol = getattr(order_info, 'SYMBOL', 'NULL').split('.')[0]
        if message_symbol not in instrument_dict:
            continue
        instrumet_db = instrument_dict[message_symbol]
        order_db.symbol = instrumet_db.ticker

        # 成交量
        filled_quantity = getattr(order_info, 'FILLED_QUANTITY', '')
        if filled_quantity >= 0:
            # 0:买,1:卖
            order_db.direction = 0
        else:
            order_db.direction = 1

        action = getattr(order_info, 'ACTION', '')
        if action == 'BUYTOOPEN':
            # 0:开仓  1:平仓  3:平今  4:平昨
            order_db.trade_type = 0
        elif action == 'BUYTOCLOSE':
            if 'yd' in order_db.sys_id:
                order_db.trade_type = 4
            else:
                order_db.trade_type = 1
        elif action == 'SELLTOOPEN':
            order_db.trade_type = 0
        elif action == 'SELLTOCLOSE':
            if 'yd' in order_db.sys_id:
                order_db.trade_type = 4
            else:
                order_db.trade_type = 1
        elif action == 'BUY':
            order_db.direction = 0
            order_db.trade_type = 2
        elif action == 'SELL':
            order_db.direction = 1
            order_db.trade_type = 2

        status = getattr(order_info, 'STATUS', '')
        if status == 'RECEIVED':
            # 全部成交:'0' 部分成交还在队列中:'1',部分成交不在队列中:'2',未成交还在队列中:'3',
            # 未成交不在队列中:'4',撤单:'5',未知:'a',尚未触发:'b',已触发:'c'
            continue
        elif status == 'CANCELED':
            order_db.status = 5
        elif status == 'FILLED':
            order_db.status = 0
        elif status == 'PARTIALLYFILLED':
            order_db.status = 1

        # # 已经提交:'0',撤单已经提交:'1',修改已经提交:'2',已经接受:'3',报单已经被拒绝:'4',撤单已经被拒绝:'5',改单已经被拒绝:'6'
        # order_db.submit_status = getattr(order_info, 'OrderSubmitStatus', '')

        insert_time_str = getattr(order_info, 'ENTERED_TIME', '')
        order_db.insert_time = insert_time_str.replace('/', '-')

        order_db.qty = getattr(order_info, 'ENTERED_QUANTITY', 0)

        order_db.price = getattr(order_info, 'LIMIT_PRICE', 0)

        # order_db.ex_qty = getattr(order_info, 'VolumeTraded', '')
        order_list.append(order_db)
        if status == 'FILLED':
            trade_db = __build_trade(account_id, order_info)
            trade_list.append(trade_db)

        order_dict[order_db.sys_id] = order_db

    order_list = sorted(order_list,
                        cmp=lambda x, y: cmp(x.insert_time, y.insert_time),
                        reverse=False)
    trade_list = sorted(trade_list,
                        cmp=lambda x, y: cmp(x.time, y.time),
                        reverse=False)
    return order_list, trade_list