def __position_check(server_name_list):
    server_host = ServerConstant().get_server_model('host')
    session = server_host.get_db_session('common')
    maincontract_list = []
    query = session.query(FutureMainContract)
    for future_maincontract_db in query:
        maincontract_list.append(future_maincontract_db.pre_main_symbol)

    date_str = date_utils.get_today_str('%Y-%m-%d')
    for server_name in server_name_list:
        server_model = ServerConstant().get_server_model(server_name)
        session_portfolio = server_model.get_db_session('portfolio')
        query_pf_position = session_portfolio.query(PfPosition)
        for pf_position_db in query_pf_position.filter(PfPosition.date >= date_str):
            if pf_position_db.symbol.isdigit():
                continue
            filter_symbol = pf_position_db.symbol.split(' ')[0]
            if filter_symbol in maincontract_list:
                email_list.append('<font color=red>pf_position---server:%s, date:%s, id:%s, symbol:%s</font><br/>' % \
                                  (server_name, pf_position_db.date, pf_position_db.id, pf_position_db.symbol))
        query_position = session_portfolio.query(Position)
        for account_position_db in query_position.filter(Position.date >= date_str):
            if not account_position_db.symbol.isdigit():
                continue
            filter_symbol = account_position_db.symbol.split(' ')[0]
            if filter_symbol in maincontract_list:
                email_list.append('<font color=red>account_position---server:%s, date:%s, id:%s, symbol:%s</font><br/>' % \
                                  (server_name, pf_position_db.date, pf_position_db.id, pf_position_db.symbol))
def __account_position_enter(add_flag):
    print 'Enter XT_position_analysis add_account_position.'
    host_server_model = ServerConstant().get_server_model('host')
    global session_portfolio, session_om
    session_portfolio = host_server_model.get_db_session('portfolio')
    session_om = host_server_model.get_db_session('om')

    xt_position_file_list = FileUtils(file_path).filter_file(
        'PROXYXH_POSITION', now_date_str)
    for xt_file in xt_position_file_list:
        read_position_file_xt('%s/%s' % (file_path, xt_file), add_flag)

    session_portfolio.commit()
    session_om.commit()
    print 'Exit XT_position_analysis add_account_position.'
 def get_holiday_list(format_str='%Y-%m-%d'):
     holiday_list = []
     host_server_model = ServerConstant().get_server_model('host')
     session_common = host_server_model.get_db_session('history')
     query = session_common.query(HolidayInfo)
     for holiday_info_db in query:
         holiday_list.append(holiday_info_db.holiday.strftime(format_str))
     return holiday_list
def build_trading_time_dict():
    host_server_model = ServerConstant().get_server_model('host')
    session_history = host_server_model.get_db_session('history')
    query = session_history.query(InstrumentHistory)
    for instrument_history_db in query:
        trading_time_list = []
        if instrument_history_db.thours is None:
            continue
        for trading_time in instrument_history_db.thours.replace(
                '(', '').replace(')', '').split(';'):
            (start_time, end_time) = trading_time.split(',')
            if count_time_number(start_time) > count_time_number(end_time):
                trading_time_list.append((start_time, '24:00'))
                trading_time_list.append(('00:00', end_time))
            else:
                trading_time_list.append((start_time, end_time))

        trading_time_dict[instrument_history_db.ticker] = trading_time_list
def ctp_price_analysis(date):

    # platform_logger.info(get_log_format_string('Enter ctp_price_analysis', tag='='))
    print ('Enter ctp_price_analysis')
    host_server_model = ServerConstant().get_server_model('host')
    global session
    global filter_date_str

    date_ = '-'.join([date[:4], date[4:6], date[6:8]])
    session = host_server_model.get_db_session('common')
    if date is None or date == '':
        filter_date_str = now.strftime('%Y-%m-%d')
    else:
        filter_date_str = date_


    data_path = os.path.join(file_path, date)
    instrument_file_list = FileUtils(data_path).filter_file('CTP_INSTRUMENT', filter_date_str)
    print instrument_file_list
    market_file_list = FileUtils(data_path).filter_file('CTP_MARKET',
                                                         filter_date_str)
    ctp_file_list = []
    ctp_file_list.extend(instrument_file_list)
    ctp_file_list.extend(market_file_list)
    # platform_logger.info('src_file_path: %s' % data_path)
    print ('src_file_path: %s')% data_path
    # platform_logger.info('target_file_list: %s')% ctp_file_list
    print ('target_file_list: %s') % ctp_file_list

    for ctp_file in ctp_file_list:
        # platform_logger.info(get_log_format_string('start: %s' % ctp_file, tag='-'))
        print ('start: %s')% ctp_file
        read_price_file_ctp(os.path.join(data_path, ctp_file))

    set_main_submain()
    update_db()
    session.commit()
    # platform_logger.info(get_log_format_string('Exit ctp_price_analysis', tag='='))
    print ('Exit ctp_price_analysis')
def __account_trade_restrictions_check(server_name_list):

    email_list.append('<li>Check  Today Cancel</li>')
    email_list.append('<table border="1"><tr>')
    email_list.append('<th align="center" font-size:12px; bgcolor="#70bbd9"><b>%s</b></th>' % ' ')
    for server_name in server_name_list:
        email_list.append('<th align="center" font-size:12px; bgcolor="#70bbd9"><b>%s</b></th>' % server_name)
    email_list.append('</tr>')

    email_list.append('<tr><th align="center" font-size:12px; bgcolor="#ee4c50"><b>%s</b></th>' % 'account trade restrictions')
    for server_name in server_name_list:
        server_model = ServerConstant().get_server_model(server_name)
        session_portfolio = server_model.get_db_session('portfolio')
        query_sql = "select sum(TODAY_CANCEL) from portfolio.account_trade_restrictions t where t.TODAY_CANCEL > 0"
        today_cancel_sum_num = session_portfolio.execute(query_sql).first()[0]
        if today_cancel_sum_num == 0 or today_cancel_sum_num is None:
            email_list.append('<th align="center" font-size:12px; bgcolor="#ee4c50"><b>%s</b></th>' % today_cancel_sum_num)
        else:
            email_list.append('<th><b>%s</b></th>' % today_cancel_sum_num)
    email_list.append('</tr>')
    email_list.append('</table>')
    email_list.append('<br/><br/>')
def lts_price_analysis(date):
    # platform_logger.info(get_log_format_string('Enter Lts_price_analysis'))
    print('Enter Lts_price_analysis')
    global session
    global filter_date_str
    date_ = '-'.join([date[:4], date[4:6], date[6:8]])

    host_server_model = ServerConstant().get_server_model('host')
    session = host_server_model.get_db_session('common')
    if date is None or date == '':
        filter_date_str = now.strftime('%Y-%m-%d')
    else:
        filter_date_str = date_

    build_instrument_db_dict()
    data_path = os.path.join(file_path, date)
    instrument_file_list = FileUtils(data_path).filter_file('HUABAO_INSTRUMENT', filter_date_str)
    market_file_list = FileUtils(data_path).filter_file('HUABAO_MARKET', filter_date_str)

    lts_file_list = []
    lts_file_list.extend(instrument_file_list)
    lts_file_list.extend(market_file_list)
    # platform_logger.info('taget_files: %s' % lts_file_list)
    print ('Info: taget_files: %s') % lts_file_list

    for qd_file in lts_file_list:
        # platform_logger.info(get_log_format_string('Start: %s' % qd_file))
        print ('Info: Start: %s') % qd_file
        read_position_file_lts(os.path.join(data_path, qd_file))

    # platform_logger.info('update_structured_fund')
    print ('Info: update_structured_fund')

    update_structured_fund()
    update_db()
    session.commit()
    # platform_logger.info(get_log_format_string('Exit Lts_price_analysis'))
    print('Info: Exit Lts_price_analysis')
            symbol = 'SSE50'

        if symbol in open_trade_dict:
            open_trade_dict[symbol] += abs(int(trade_db.qty))
        else:
            open_trade_dict[symbol] = abs(int(trade_db.qty))
    for (symbol, today_open) in open_trade_dict.items():
        update_sql = "update portfolio.account_trade_restrictions set TODAY_OPEN = %s where \
                TICKER = '%s' and ACCOUNT_ID = %s" % (today_open, symbol,
                                                      account_id)
        session_portfolio.execute(update_sql)


if __name__ == '__main__':
    print 'Enter ts_position_analysis.'
    host_server_model = ServerConstant().get_server_model('host')
    session_common = host_server_model.get_db_session('common')
    session_portfolio = host_server_model.get_db_session('portfolio')
    session_om = host_server_model.get_db_session('om')

    __build_ticker_exchange(host_server_model)
    ts_position_file_list = FileUtils(file_path).filter_file(
        'ysorder', now_date_str)
    for ts_file in ts_position_file_list:
        read_position_file_ts('%s/%s' % (file_path, ts_file))

    session_common.commit()
    session_portfolio.commit()
    session_om.commit()
    print 'Exit ts_position_analysis.'
def build_ticker_exchange():
    host_server_model = ServerConstant().get_server_model('host')
    session_common = host_server_model.get_db_session('common')
    query = session_common.query(Instrument)
    for instrument_db in query.filter(Instrument.type_id == 1):
        instrument_dict[instrument_db.ticker] = instrument_db
Exemple #10
0
        # productID = getattr(messageInfo, 'ProductID', '')


def update_db():
    for (dict_key, future) in future_db_dict.items():
        session.merge(future)


def build_future_db_dict():
    query = session.query(Instrument)
    for future in query.filter(Instrument.exchange_id == 25):
        dict_key = '%s|%s' % (future.ticker, future.exchange_id)
        future_db_dict[dict_key] = future


if __name__ == '__main__':
    print 'Enter femas_price_analysis.'
    host_server_model = ServerConstant().get_server_model('host')
    session = host_server_model.get_db_session('common')
    build_future_db_dict()
    femas_td_file_list = FileUtils(file_path).filter_file('Femas_TD', today_str)
    for femas_td_file in femas_td_file_list:
        read_price_file_femas('%s/%s' % (file_path, femas_td_file))

    # femas_md_file_list = FileUtils(file_path).filter_file('Femas_MD', today_str)
    # for femas_md_file in femas_md_file_list:
    #     read_price_file_femas('%s/%s' % (file_path, femas_md_file))
    update_db()
    session.commit()
    print 'Exit femas_price_analysis.'
def __insert_db(strategy_online_list):
    server_model = ServerConstant().get_server_model('host')
    session = server_model.get_db_session('strategy')
    for strategy_online_db in strategy_online_list:
        session.merge(strategy_online_db)
    session.commit()
def __strategy_parameter_check(server_name_list):
    server_model = ServerConstant().get_server_model('host')
    session = server_model.get_db_session('common')
    query = session.query(Instrument)
    future_dict = dict()
    for instrument_db in query.filter(Instrument.type_id == 1):
        future_dict[instrument_db.ticker] = instrument_db

    maincontract_dict = dict()
    query = session.query(FutureMainContract)
    for future_maincontract_db in query:
        maincontract_dict[future_maincontract_db.ticker_type] = future_maincontract_db

    for server_name in server_name_list:
        email_list.append('<font >Strategy Parameter Check: %s</font><br/>' % (server_name))
        server_model = ServerConstant().get_server_model(server_name)
        server_session = server_model.get_db_session('strategy')

        strategy_check_result = []
        for strategy_name_group in server_session.query(StrategyParameter.name).group_by(StrategyParameter.name).all():
            strategy_name = strategy_name_group[0]
            if 'Calendar' not in strategy_name:
                continue
            print strategy_name
            strategy_parameter_db = server_session.query(StrategyParameter).filter(StrategyParameter.name == strategy_name).order_by(desc(StrategyParameter.time)).first()
            strategy_parameter_dict = json.loads(strategy_parameter_db.value)

            calendar_future_dict = dict()
            for (dict_key, dict_value) in strategy_parameter_dict.items():
                if 'BackFuture' in dict_key:
                    back_future_name = dict_value
                    if back_future_name not in future_dict:
                        email_list.append('<font color=red>strategy:%s BackFuture:%s can not find!</font><br/>' % (strategy_name, back_future_name))
                    else:
                        back_future_db = future_dict[back_future_name]
                        if back_future_db.exchange_id != 25 and (datetime.strptime(str(back_future_db.expire_date), '%Y-%m-%d') - datetime.now()).days <= 33:
                            strategy_check_result.append('<font color=red>strategy:%s BackFuture:%s expire_date:%s less than 30days!</font><br/>' % \
                                          (strategy_name, back_future_name,
                                           back_future_db.expire_date.strftime("%Y-%m-%d")))

                    ticker_type = filter(lambda x: not x.isdigit(), back_future_name)
                    if ticker_type in calendar_future_dict:
                        calendar_future_dict[ticker_type].append(back_future_name)
                    else:
                        calendar_future_dict[ticker_type] = [back_future_name, ]

                    ticker_month = filter(lambda x: x.isdigit(), back_future_name)
                    future_maincontract_db = maincontract_dict[ticker_type]
                    main_symbol_month = filter(lambda x: x.isdigit(), future_maincontract_db.main_symbol)
                    if str(ticker_month) < str(main_symbol_month):
                        strategy_check_result.append('<font color=red>strategy:%s BackFuture:%s, Main Contract is:%s</font><br/>' % (
                                    strategy_name, back_future_name, future_maincontract_db.main_symbol))
                elif 'FrontFuture' in dict_key:
                    front_future_name = dict_value
                    if front_future_name not in future_dict:
                        email_list.append('<font color=red>strategy:%s FrontFuture:%s can not find!</font><br/>' % (strategy_name, front_future_name))
                    else:
                        front_future_db = future_dict[front_future_name]
                        if front_future_db.exchange_id != 25 and (datetime.strptime(str(front_future_db.expire_date), '%Y-%m-%d') - datetime.now()).days <= 30:
                            strategy_check_result.append('<font color=red>strategy:%s FrontFuture:%s expire_date:%s less than 30days!</font><br/>' % \
                                          (strategy_name, front_future_name,
                                           front_future_db.expire_date.strftime("%Y-%m-%d")))

                    ticker_type = filter(lambda x: not x.isdigit(), front_future_name)
                    if ticker_type in calendar_future_dict:
                        calendar_future_dict[ticker_type].append(front_future_name)
                    else:
                        calendar_future_dict[ticker_type] = [front_future_name, ]

                    ticker_month = filter(lambda x: x.isdigit(), front_future_name)
                    future_maincontract_db = maincontract_dict[ticker_type]
                    main_symbol_month = filter(lambda x: x.isdigit(), future_maincontract_db.main_symbol)
                    if str(ticker_month) < str(main_symbol_month):
                        strategy_check_result.append('<font color=red>strategy:%s FrontFuture:%s, Main Contract is:%s</font><br/>' % (
                                    strategy_name, front_future_name, future_maincontract_db.main_symbol))

            for (key, ticker_list) in calendar_future_dict.items():
                if len(ticker_list) != 2:
                    continue
                if ticker_list[0] == ticker_list[1]:
                    strategy_check_result.append(
                        '<font color=red>Ticker:%s FrontFuture and BackFuture is same!</font><br/>' % ticker_list[0])

        if len(strategy_check_result) > 0:
            strategy_check_result.insert(0, '<li>server:%s, Check Strategy Parameter</li>' % server_name)
            email_list.extend(strategy_check_result)
            EmailUtils(EmailUtils.group4).send_email_group_all('[Error]Strategy Parameter Check', '\n'.join(strategy_check_result), 'html')
    email_list.append('<br/><br/>')