Пример #1
0
def save_account_cny(account_info, message_array, filter_date_str):
    position_list = []
    for tradingAccount in message_array:
        position_db = AccountPosition()
        real_account_id = getattr(tradingAccount, 'AccountID', 'NULL')
        if (real_account_id[12:14] == '70') and (account_info.accountsuffix == '70'):
            pass
        elif (real_account_id[12:14] == '01') and (account_info.accountsuffix == '01'):
            pass
        else:
            continue

        position_db.date = filter_date_str
        position_db.id = account_info.accountid
        position_db.symbol = 'CNY'
        # 结算准备金
        position_db.long = getattr(tradingAccount, 'Balance', 'NULL')

        if account_info.accountsuffix == '70':
            # 保证金可用余额
            position_db.long_avail = getattr(tradingAccount, 'Credit', 'NULL')
        else:
            # 现金
            position_db.long_avail = getattr(tradingAccount, 'Available', 'NULL')

        # 上次结算准备金
        position_db.prev_net = getattr(tradingAccount, 'PreBalance', '0')
        position_db.update_date = datetime.now()
        position_list.append(position_db)
    return position_list
Пример #2
0
def __get_account_cny(account_id, message_array):
    # 账户资金分拆标记位
    # account_partition_flag = False
    # now_time = long(date_utils.get_today_str('%H%M%S'))
    # if now_time < 180000 and special_account_flag:
    #     account_partition_flag = True
    account_name = ''
    position_db = AccountPosition()
    for trading_account in message_array:
        position_db.date = filter_date_str
        position_db.id = account_id
        position_db.symbol = 'CNY'
        # if account_partition_flag:
        #     position_db.long = float(getattr(trading_account, 'Balance', '0')) * 0.5
        #     position_db.long_avail = float(getattr(trading_account, 'Available', '0')) * 0.5
        #     position_db.prev_net = float(getattr(trading_account, 'PreBalance', '0')) * 0.5
        #     position_db.update_date = datetime.now()
        # else:
        #     position_db.long = getattr(trading_account, 'Balance', '0')
        #     position_db.long_avail = getattr(trading_account, 'Available', '0')
        #     position_db.prev_net = getattr(trading_account, 'PreBalance', '0')
        #     position_db.update_date = datetime.now()
        position_db.long = getattr(trading_account, 'Balance', '0')
        position_db.long_avail = getattr(trading_account, 'Available', '0')
        position_db.prev_net = getattr(trading_account, 'PreBalance', '0')
        position_db.update_date = datetime.now()

        account_name = getattr(trading_account, 'AccountID', '')
        break
    return account_name, position_db
Пример #3
0
def __get_account_cny(account_id, message_array):
    position_db = AccountPosition()
    for trading_account in message_array:
        position_db.date = filter_date_str
        position_db.id = account_id
        position_db.symbol = 'CNY'

        position_db.long = getattr(trading_account, 'total_asset', '0')
        position_db.long_avail = getattr(trading_account, 'buying_power', '0')
        position_db.prev_net = position_db.long_avail
        position_db.update_date = date_utils.get_now()
    return position_db
Пример #4
0
def save_account_cny(account_id, message_array):
    for trading_account in message_array:
        position_db = AccountPosition()
        position_db.date = today_str
        position_db.id = account_id
        position_db.symbol = 'CNY'

        position_db.long = getattr(trading_account, 'szFundBln', 'NULL')
        position_db.long_avail = getattr(trading_account, 'szFundAvl', 'NULL')
        position_db.prev_net = getattr(trading_account, 'szFundPrebln', '0')
        position_db.update_date = date_utils.get_now()
        position_list.append(position_db)
Пример #5
0
def save_account_cny(account_id, message_array):
    for trading_account in message_array:
        position_db = AccountPosition()
        position_db.date = today_str
        position_db.id = account_id
        position_db.symbol = 'CNY'

        available = getattr(trading_account, 'Available', '0')
        margin = getattr(trading_account, 'Margin', '0')
        position_db.long = float(available) + float(margin)
        position_db.long_avail = available
        position_db.prev_net = getattr(trading_account, 'PreBalance', '0')
        position_db.update_date = datetime.now()
        position_list.append(position_db)
Пример #6
0
def __get_account_cny(account_id, message_array):
    position_db = AccountPosition()
    for trading_account in message_array:
        money_type = getattr(trading_account, 'money_type', '0')
        if money_type != '0':
            continue

        position_db.date = filter_date_str
        position_db.id = account_id
        position_db.symbol = 'CNY'

        position_db.long = getattr(trading_account, 'fund_balance', '0')
        position_db.long_avail = getattr(trading_account, 'enable_balance',
                                         '0')
        position_db.prev_net = getattr(trading_account, 'fund_balance', '0')
        position_db.update_date = datetime.now()
    return position_db
Пример #7
0
def __get_account_cny(account_id, message_array):
    query_position = session_portfolio.query(AccountPosition)
    position_db = query_position.filter(
        AccountPosition.id == account_id,
        AccountPosition.date == filter_date_str,
        AccountPosition.symbol == 'CNY').first()
    if position_db is None:
        position_db = AccountPosition()

    for trading_account in message_array:
        position_db.date = filter_date_str
        position_db.id = account_id
        position_db.symbol = 'CNY'

        position_db.long = getattr(trading_account, 'm_dBalance', '0')
        position_db.long_avail = getattr(trading_account, 'm_dAvailable', '0')
        position_db.prev_net = getattr(trading_account, 'm_dPreBalance', '0')
        position_db.update_date = date_utils.get_now()
    return position_db
Пример #8
0
def __get_account_cny(account_id, base_model, pre_position_info_list):
    account_cny_db = None
    for position_info in pre_position_info_list:
        if position_info.symbol == 'CNY':
            account_cny_db = position_info

    if account_cny_db is None:
        account_cny_db = AccountPosition()
    account_cny_db.date = filter_date_str
    account_cny_db.id = account_id
    account_cny_db.symbol = 'CNY'

    # 获取实时账户净值的数值
    account_cny_db.long = getattr(base_model, 'RT_ACCOUNT_NET_WORTH', '0')
    # 获取实时现金余额的数值
    account_cny_db.long_avail = getattr(base_model, 'RT_CASH_BALANCE', '0')
    # 获取账户日初始净值的数值
    account_cny_db.prev_net = getattr(base_model, 'BD_ACCOUNT_NET_WORTH', '0')
    account_cny_db.update_date = date_utils.get_now()
    return account_cny_db
def save_account_cny(account_id, message_array):
    for trading_account in message_array:
        position_db = AccountPosition()
        position_db.date = today_str
        position_db.id = account_id
        position_db.symbol = getattr(trading_account, '15', '0')

        td_long = 0
        td_long_avail = 0
        group_array = getattr(trading_account, 'GroupB', '0')
        for group in group_array:
            (title_column, value_column) = group.split('|')
            title = title_column.split('=')[1]
            value = value_column.split('=')[1]
            if title == 'F':
                td_long = value
            elif title == 'FAV':
                td_long_avail = value
        position_db.long = td_long
        position_db.long_avail = td_long_avail
        position_db.prev_net = td_long
        position_db.update_date = date_utils.get_now()
        position_list.append(position_db)
Пример #10
0
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 = AccountPosition()
            position_db.date = filter_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 = date_utils.get_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
Пример #11
0
def __build_account_position(account_id, investor_position_array):
    position_list = []
    for investor_position in investor_position_array:
        position_db = AccountPosition()
        position_db.date = filter_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 = date_utils.get_now()
        position_list.append(position_db)
    return position_list
Пример #12
0
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 = AccountPosition()
        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)
Пример #13
0
def save_account_position(account_info, investor_position_array, filter_date_str):
    position_list = []
    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 == '1') and (ydPosition < 0):
                long_flag = False
            elif (posi_direction == '1') and (ydPosition >= 0):
                long_flag = True
            elif posi_direction == '2':
                long_flag = True
            elif posi_direction == '3':
                long_flag = False
            elif posi_direction == '4':
                long_flag = False
            else:
                print 'Error Type:PosiDirection'

            if long_flag:
                td_long += position
                td_long_cost += positionCost
                yd_long += ydPosition
                if (yd_long - closeVolume) > 0:
                    yd_long_remain = yd_long - closeVolume
                else:
                    yd_long_remain = 0
            else:
                td_short += abs(position)
                td_short_cost += abs(positionCost)
                yd_short += abs(ydPosition)
                if (yd_short - openVolume) > 0:
                    yd_short_remain = yd_short - 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 = AccountPosition()
            position_db.date = filter_date_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)
    return position_list
Пример #14
0
def save_account_position(account_id, investorPositionArray):
    for investorPosition in investorPositionArray:
        symbol = getattr(investorPosition, 'szStkCode', 'NULL')
        yd_long = getattr(investorPosition, 'llStkPrebln', '0')
        td_long = getattr(investorPosition, 'llStkBln', '0')
        td_long_avail = getattr(investorPosition, 'llStkAvl', '0')
        td_long_cost = getattr(investorPosition, 'szStkBcostRlt', '0')
        long_frozen = getattr(investorPosition, 'llStkTrdFrz', '0')

        yd_short = 0
        td_short = 0
        td_short_cost = 0
        td_short_avail = 0

        yd_long_remain = yd_long
        yd_short_remain = yd_short
        purchase_avail = yd_long
        prev_net = float(yd_long) - float(yd_short)

        position_db = AccountPosition()
        position_db.date = today_str
        position_db.id = account_id
        position_db.symbol = symbol
        position_db.hedgeflag = 0
        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.frozen = long_frozen
        position_db.update_date = date_utils.get_now()
        position_list.append(position_db)
Пример #15
0
def __build_account_position(account_id, account_name,
                             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 account_name in special_ctp_accounts and ('IC' in symbol
                                                     or 'IF' in symbol
                                                     or 'IH' 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 = AccountPosition()
        position_db.date = filter_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
Пример #16
0
def __build_account_position(account_id, investor_position_array):
    last_position_dict = dict()
    query_position = session_portfolio.query(AccountPosition)
    for position_db in query_position.filter(
            AccountPosition.id == account_id,
            AccountPosition.date == filter_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 = AccountPosition()
            position_db.yd_position_long = yd_long_remain
            position_db.yd_position_short = yd_short_remain

        position_db.date = filter_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 = date_utils.get_now()
        position_dict[key] = position_db
        position_list.append(position_db)
    return position_dict, position_list
Пример #17
0
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, 'stock_code', 'NULL')
        # 过滤掉SP j1609&j1701这种的持仓数据
        if '&' in symbol:
            continue

        # 转换hedgeFlag字典
        # hedge_flag = getattr(investorPosition, 'm_nHedgeFlag', '0')
        # hedge_flag = HEDGE_FLAG_MAP[hedge_flag]
        hedge_flag = 0

        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
        td_long_avail = 0

        for temp_position in ticker_position_list:
            td_long += float(getattr(temp_position, 'current_amount', '0'))
            td_long_cost += float(getattr(temp_position, 'cost_price', '0'))
            td_long_avail += float(getattr(temp_position, 'enable_amount',
                                           '0'))

        position_db = AccountPosition()
        position_db.date = filter_date_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 = 0
        position_db.short_cost = 0
        position_db.short_avail = 0

        position_db.ys_position_long = td_long
        position_db.yd_long_remain = td_long
        position_db.ys_position_short = 0
        position_db.yd_short_remain = 0
        position_db.prev_net = td_long
        position_db.frozen = 0
        position_db.update_date = datetime.now()
        position_dict[key] = position_db
        position_list.append(position_db)
    return position_dict, position_list
Пример #18
0
def __build_account_position(account_id, investor_position_array):
    last_position_dict = dict()
    query_position = session_portfolio.query(AccountPosition)
    for position_db in query_position.filter(
            AccountPosition.id == account_id,
            AccountPosition.date == filter_date_str):
        if position_db.symbol == 'CNY':
            continue
        last_position_dict[position_db.symbol] = position_db

    position_list = []
    position_dict = dict()
    for investorPosition in investor_position_array:
        symbol = getattr(investorPosition, 'ticker', 'NULL')
        # 过滤掉SP j1609&j1701这种的持仓数据
        if '&' in symbol:
            continue

        hedge_flag = 0
        td_long = getattr(investorPosition, 'total_qty', '0')
        avg_price = getattr(investorPosition, 'avg_price', '0')
        td_long_cost = float(td_long) * float(avg_price)
        td_long_avail = getattr(investorPosition, 'sellable_qty', '0')

        position_db = AccountPosition()
        position_db.date = filter_date_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 = 0
        position_db.short_cost = 0
        position_db.short_avail = 0

        position_db.yd_long_remain = td_long
        position_db.yd_short_remain = 0
        position_db.prev_net = td_long
        position_db.frozen = 0
        position_db.update_date = date_utils.get_now()
        position_list.append(position_db)
    return position_dict, position_list
Пример #19
0
def save_account_position(account_id, investor_position_array):
    for investorPosition in investor_position_array:
        symbol = getattr(investorPosition, '55', '0')
        hedge_flag = 0

        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

        prev_net = 0
        purchase_avail = 0

        long_Frozen = 0
        short_Frozen = 0

        purchase_avail = 0

        groupAArray = getattr(investorPosition, 'GroupA', '0')

        for group in groupAArray:
            (title_column, value_column) = group.split('|')
            title = title_column.split('=')[1]
            value = int(value_column.split('=')[1])
            if title == 'SQ':  # 当前拥股数
                td_long = value
            # elif (title == 'SB'): #股份余额
            #                 td_long = value
            #             elif (title == 'SBQ'): #今日买入数量
            #                 td_long = value
            elif title == 'SAV':  # 股份可用余额
                td_long_avail = value
                yd_long_remain = value
                purchase_avail = value
            elif title == 'LB':  # 昨日余额
                yd_long = value
            elif title == 'SS':  # 卖出冻结数
                long_Frozen += value
            elif title == 'SF':  # 人工冻结数
                long_Frozen += value

        groupBArray = getattr(investorPosition, 'GroupB', '0')
        for group in groupBArray:
            (title_column, value_column) = group.split('|')
            title = title_column.split('=')[1]
            value = value_column.split('=')[1]
            # if title == 'PC':  # 持仓成本
            #     td_long_cost = value
            if title == 'SMV':  # 持仓成本
                td_long_cost = value

        prev_net = yd_long - yd_short

        position_db = AccountPosition()
        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 = date_utils.get_now()
        position_list.append(position_db)