Пример #1
0
def train(date, strategy_id='机器学习1号', working_dir=working_dir, ui_log=None):
    date = QA_util_get_real_date(date)
    QA_util_log_info('##JOB01 Now Model Init ==== {}'.format(str(date)),
                     ui_log)
    model1 = model()
    QA_util_log_info('##JOB02 Now Prepare Date ==== {}'.format(str(date)),
                     ui_log)
    model1.get_data(start=str(int(date[0:4]) - 3) + "-01-01", end=date)
    QA_util_log_info('##JOB03 Now Set Target ==== {}'.format(str(date)),
                     ui_log)
    model1.set_target(mark=0.3, type='percent')
    QA_util_log_info(
        '##JOB04 Now Set Train time range ==== {}'.format(str(date)), ui_log)
    model1.set_train_rng(train_start=str(int(date[0:4]) - 3) + "-01-01",
                         train_end=(datetime.strptime(date, "%Y-%m-%d") -
                                    delta4).strftime('%Y-%m-%d'),
                         test_start=(datetime.strptime(date, "%Y-%m-%d") -
                                     delta3).strftime('%Y-%m-%d'),
                         test_end=(datetime.strptime(date, "%Y-%m-%d") -
                                   delta1).strftime('%Y-%m-%d'))
    model1.prepare_data()
    other_params = {
        'learning_rate': 0.1,
        'n_estimators': 100,
        'max_depth': 5,
        'min_child_weight': 1,
        'seed': 0,
        'subsample': 0.8,
        'colsample_bytree': 0.8,
        'gamma': 0,
        'reg_alpha': 0,
        'reg_lambda': 1
    }
    model1.build_model(other_params)
    QA_util_log_info('##JOB05 Now Model Trainnig ==== {}'.format(str(date)),
                     ui_log)
    model1.model_running()
    model1.model_check()
    QA_util_log_info('##JOB06 Now Save Model ==== {}'.format(str(date)),
                     ui_log)
    important = model1.model_important()
    model1.save_model('index', working_dir=working_dir)
    QA_util_log_info(
        '##JOB06 Now Model Trainning Report ==== {}'.format(str(date)), ui_log)
    msg1 = '模型训练日期:{model_date}'.format(model_date=model1.info['date'])
    body1 = build_table(pd.DataFrame(model1.info['train_report']), '训练集情况')
    body2 = build_table(pd.DataFrame(model1.info['rng_report']), '测试集情况')
    body3 = build_table(important.head(50), '特征重要性')

    msg = build_email(build_head(), msg1, body1, body2, body3)

    send_email('模型训练报告', msg, 'date')
    send_actionnotice(strategy_id,
                      '报告:{}'.format(date),
                      '模型训练完成,请查收结果',
                      direction='HOLD',
                      offset='HOLD',
                      volume=None)
Пример #2
0
def trading(trading_date,
            percent=percent,
            strategy_id='机器学习1号',
            account1='name:client-1',
            working_dir=working_dir,
            ui_log=None):

    try:
        QA_util_log_info(
            '##JOB01 Now Load Model ==== {}'.format(str(trading_date)), ui_log)
        model_temp, info_temp = load_model('stock', working_dir=working_dir)
    except:
        send_email('错误报告', '无法正确加载模型,请检查', trading_date)
        send_actionnotice(strategy_id,
                          '错误报告:{}'.format(trading_date),
                          '无法正确加载模型,请检查',
                          direction='HOLD',
                          offset='HOLD',
                          volume=None)

    QA_util_log_info(
        '##JOB02 Now Model Predict ==== {}'.format(str(trading_date)), ui_log)
    tar, tar1 = model_predict(model_temp,
                              str(trading_date[0:7]) + "-01", trading_date,
                              info_temp['cols'])
    r_tar = tar[tar['RANK'] <= 5].loc[trading_date][[
        'Z_PROB', 'O_PROB', 'RANK'
    ]]
    try:
        QA_util_log_info(
            '##JOB03 Now Chect Account Server ==== {}'.format(
                str(trading_date)), ui_log)
        client = get_Client()
        account1 = account1
        client.cancel_all(account1)
        account_info = client.get_account(account1)
        print(account_info)
    except:
        send_email('错误报告', '云服务器错误,请检查', trading_date)
        send_actionnotice(strategy_id,
                          '错误报告:{}'.format(trading_date),
                          '云服务器错误,请检查',
                          direction='HOLD',
                          offset='HOLD',
                          volume=None)

    h1 = int(datetime.datetime.now().strftime("%H"))
    m1 = int(datetime.datetime.now().strftime("%M"))
    while h1 == 14 and m1 <= 50:
        h1 = int(datetime.datetime.now().strftime("%H"))
        m1 = int(datetime.datetime.now().strftime("%M"))
        time.sleep(30)

    QA_util_log_info('##JOB04 Now Trading ==== {}'.format(str(trading_date)),
                     ui_log)
    res = trade_roboot(r_tar, account1, trading_date, percent, strategy_id)
    return (res)
Пример #3
0
def send_trading_message(account1, strategy_id, account_info, code, NAME,
                         INDUSTRY, mark, direction, type, priceType, price,
                         client):
    codes = []
    try:
        if direction == 'SELL':
            if type == 'MARKET':
                client.sell(account1,
                            symbol=code,
                            type=type,
                            priceType=priceType,
                            amount=abs(mark))
            elif type == 'LIMIT':
                client.sell(account1,
                            symbol=code,
                            type=type,
                            amount=abs(mark),
                            price=price)
            else:
                pass
        elif direction == 'BUY':
            if type == 'MARKET':
                client.buy(account1,
                           symbol=code,
                           type=type,
                           priceType=priceType,
                           amount=abs(mark))
            elif type == 'LIMIT':
                client.buy(account1,
                           symbol=code,
                           type=type,
                           amount=abs(mark),
                           price=price)
            else:
                pass
        elif direction == 'HOLD':
            pass
        send_actionnotice(strategy_id,
                          account_info,
                          '{code}({NAME},{INDUSTRY})'.format(
                              code=code, NAME=NAME, INDUSTRY=INDUSTRY),
                          direction=direction,
                          offset='OPEN',
                          volume=abs(mark),
                          price=price)
    except Exception as e:
        send_actionnotice(strategy_id,
                          account_info,
                          '{code}({NAME},{INDUSTRY}) 交易失败'.format(
                              code=code, NAME=NAME, INDUSTRY=INDUSTRY),
                          direction=direction,
                          offset='OPEN',
                          volume=abs(mark))
        print(e)
        codes.append(code)
    return (codes)
Пример #4
0
def train(date, strategy_id='机器学习1号', working_dir=working_dir, ui_log=None):
    QA_util_log_info('##JOB01 Now Model Init ==== {}'.format(str(date)),
                     ui_log)
    model1 = model()
    QA_util_log_info('##JOB02 Now Prepare Date ==== {}'.format(str(date)),
                     ui_log)
    model1.get_data(start=str(int(date[0:4]) - 3) + "-01-01", end=date)
    QA_util_log_info('##JOB03 Now Set Target ==== {}'.format(str(date)),
                     ui_log)
    model1.set_target(mark=0.42, type='percent')
    QA_util_log_info(
        '##JOB04 Now Set Train time range ==== {}'.format(str(date)), ui_log)
    model1.set_train_rng(train_start=str(int(date[0:4]) - 3) + "-01-01",
                         train_end=(datetime.strptime(date, "%Y-%m-%d") -
                                    delta4).strftime('%Y-%m-%d'),
                         test_start=(datetime.strptime(date, "%Y-%m-%d") -
                                     delta3).strftime('%Y-%m-%d'),
                         test_end=date)
    model1.prepare_data()
    model1.build_model(n_estimators=200)
    QA_util_log_info('##JOB05 Now Model Trainnig ==== {}'.format(str(date)),
                     ui_log)
    model1.model_running()
    model1.model_check()
    QA_util_log_info('##JOB06 Now Save Model ==== {}'.format(str(date)),
                     ui_log)
    important = model1.model_important()
    model1.save_model(name='stock', working_dir=working_dir)
    QA_util_log_info(
        '##JOB06 Now Model Trainning Report ==== {}'.format(str(date)), ui_log)
    msg1 = '模型训练日期:{model_date}'.format(model_date=model1.info['date'])
    body1 = build_table(pd.DataFrame(model1.info['train_report']), '训练集情况')
    body2 = build_table(pd.DataFrame(model1.info['test_report']), '测试集情况')
    body3 = build_table(important.head(50), '特征重要性')

    msg = build_email(build_head(), msg1, body1, body2, body3)

    send_email('模型训练报告', msg, 'date')
    send_actionnotice(strategy_id,
                      '报告:{}'.format(date),
                      '模型训练完成,请查收结果',
                      direction='HOLD',
                      offset='HOLD',
                      volume=None)
Пример #5
0
def trading(trading_date, percent=percent, strategy_id= '机器学习1号', account1= 'name:client-1', working_dir= working_dir, ui_log= None, exceptions= exceptions):

    try:
        QA_util_log_info(
            '##JOB01 Now Load Model ==== {}'.format(str(trading_date)), ui_log)
        stock_model_temp,stock_info_temp = Stock.load_model('stock',working_dir = working_dir)
        index_model_temp,index_info_temp = Index.load_model('index',working_dir = working_dir)
        safe_model_temp,safe_info_temp = Index.load_model('safe',working_dir = working_dir)
    except:
        send_email('错误报告', '无法正确加载模型,请检查', trading_date)
        send_actionnotice(strategy_id,
                          '错误报告:{}'.format(trading_date),
                          '无法正确加载模型,请检查',
                          direction = 'HOLD',
                          offset='HOLD',
                          volume=None
                          )

    QA_util_log_info(
        '##JOB02 Now Model Predict ==== {}'.format(str(trading_date)), ui_log)
    #tar,tar1 = model_predict(model_temp, str(trading_date[0:7])+"-01",trading_date,info_temp['cols'])

    #index_list,index_report,index_top_report = Index.check_model(index_model_temp, QA_util_get_last_day(trading_date),QA_util_get_last_day(trading_date),index_info_temp['cols'], 'INDEXT_TARGET5', 0.3)
    index_tar,index_b  = Index.model_predict(index_model_temp, str(trading_date[0:7])+"-01",trading_date,index_info_temp['cols'])

    #safe_list,safe_report,safe_top_report = Index.check_model(safe_model_temp, QA_util_get_last_day(trading_date),QA_util_get_last_day(trading_date),safe_info_temp['cols'], 'INDEXT_TARGET', 0.3)
    safe_tar,safe_b  = Index.model_predict(safe_model_temp, str(trading_date[0:7])+"-01",trading_date,index_info_temp['cols'])

    #stock_list,report,top_report = Stock.check_model(stock_model_temp, QA_util_get_last_day(trading_date),QA_util_get_last_day(trading_date),stock_info_temp['cols'], 0.42)
    stock_tar,stock_b  = Stock.model_predict(stock_model_temp, str(trading_date[0:7])+"-01",trading_date,stock_info_temp['cols'])

    tar = combine_model(index_b, stock_b, safe_b, str(trading_date[0:7])+"-01",trading_date)
    try:
        r_tar = tar.loc[trading_date][['Z_PROB','O_PROB','RANK']]
    except:
        r_tar = None
        send_email('交易报告:'+ trading_date, "空仓状态", 'date')

    try:
        QA_util_log_info(
            '##JOB03 Now Chect Account Server ==== {}'.format(str(trading_date)), ui_log)
        client = get_Client()
        client.cancel_all(account1)
        account_info = client.get_account(account1)
        print(account_info)
    except:
        send_email('错误报告', '云服务器错误,请检查', trading_date)
        send_actionnotice(strategy_id,
                          '错误报告:{}'.format(trading_date),
                          '云服务器错误,请检查',
                          direction = 'HOLD',
                          offset='HOLD',
                          volume=None
                          )

    h1 = int(datetime.datetime.now().strftime("%H"))
    m1 = int(datetime.datetime.now().strftime("%M"))
    while h1 == 9 and m1 <= 29 :
        h1 = int(datetime.datetime.now().strftime("%H"))
        m1 = int(datetime.datetime.now().strftime("%M"))
        time.sleep(30)

    QA_util_log_info(
        '##JOB04 Now Trading ==== {}'.format(str(trading_date)), ui_log)
    res = trade_roboot(r_tar, account1, trading_date, percent, strategy_id, type='morning', exceptions = exceptions)
    return(res)
Пример #6
0
def predict(trading_date,
            strategy_id='机器学习1号',
            account1='name:client-1',
            working_dir=working_dir,
            ui_log=None,
            exceptions=exceptions):

    try:
        QA_util_log_info(
            '##JOB01 Now Got Account Info ==== {}'.format(str(trading_date)),
            ui_log)
        client = get_Client()
        account1 = account1
        account_info = client.get_account(account1)
        print(account_info)
        sub_accounts = client.get_positions(account1)['sub_accounts']
        try:
            frozen = float(
                client.get_positions(account1)['positions'].set_index(
                    '证券代码').loc[exceptions]['市值'].sum())
        except:
            frozen = 0
        sub_accounts = sub_accounts - frozen
    except:
        send_email('错误报告', '云服务器错误,请检查', trading_date)
        send_actionnotice(strategy_id,
                          '错误报告:{}'.format(trading_date),
                          '云服务器错误,请检查',
                          direction='HOLD',
                          offset='HOLD',
                          volume=None)
    try:
        QA_util_log_info(
            '##JOB02 Now Load Model ==== {}'.format(str(trading_date)), ui_log)
        stock_model_temp, stock_info_temp = Stock.load_model(
            'stock', working_dir=working_dir)
        index_model_temp, index_info_temp = Index.load_model(
            'index', working_dir=working_dir)
        safe_model_temp, safe_info_temp = Index.load_model(
            'safe', working_dir=working_dir)
    except:
        send_email('错误报告', '无法正确加载模型,请检查', trading_date)
        send_actionnotice(strategy_id,
                          '错误报告:{}'.format(trading_date),
                          '无法正确加载模型,请检查',
                          direction='HOLD',
                          offset='HOLD',
                          volume=None)

    QA_util_log_info(
        '##JOB03 Now Model Predict ==== {}'.format(str(trading_date)), ui_log)
    #index_list,index_report,index_top_report = Index.check_model(index_model_temp, QA_util_get_last_day(trading_date),QA_util_get_last_day(trading_date),index_info_temp['cols'], 'INDEXT_TARGET5', 0.3)
    index_tar, index_b = Index.model_predict(index_model_temp,
                                             str(trading_date[0:7]) + "-01",
                                             trading_date,
                                             index_info_temp['cols'])

    #safe_list,safe_report,safe_top_report = Index.check_model(safe_model_temp, QA_util_get_last_day(trading_date),QA_util_get_last_day(trading_date),safe_info_temp['cols'], 'INDEXT_TARGET', 0.3)
    safe_tar, safe_b = Index.model_predict(safe_model_temp,
                                           str(trading_date[0:7]) + "-01",
                                           trading_date,
                                           index_info_temp['cols'])

    stock_list, report, top_report = Stock.check_model(
        stock_model_temp, QA_util_get_last_day(trading_date),
        QA_util_get_last_day(trading_date), stock_info_temp['cols'], 0.42)
    stock_tar, stock_b = Stock.model_predict(stock_model_temp,
                                             str(trading_date[0:7]) + "-01",
                                             trading_date,
                                             stock_info_temp['cols'])

    tar = combine_model(index_b, stock_b, safe_b,
                        str(trading_date[0:7]) + "-01", trading_date)
    QA_util_log_info(
        '##JOB03 Now Concat Result ==== {}'.format(str(trading_date)), ui_log)
    try:
        tar1 = tar.loc[trading_date]
    except:
        tar1 = None

    QA_util_log_info(
        '##JOB04 Now Funding Decision ==== {}'.format(str(trading_date)),
        ui_log)
    if tar1 is None:
        res = None
    else:
        tar2 = tar1[['Z_PROB', 'O_PROB', 'RANK']]
        close = QA_fetch_stock_day_adv(
            list(tar1.index), QA_util_get_last_day(trading_date, 60),
            trading_date).to_qfq().data.loc[trading_date].reset_index(
                'date')['close']
        info = QA_fetch_stock_fianacial_adv(
            list(tar1.index), trading_date,
            trading_date).data.reset_index('date')[['NAME', 'INDUSTRY']]
        res = tar2.join(close).join(info)
        #res = pd.concat([tar2,close,info],axis=1)
        avg_account = sub_accounts['总 资 产'] / tar1.shape[0]
        res = res.assign(tar=avg_account[0] * percent)
        res['cnt'] = (res['tar'] / res['close'] /
                      100).apply(lambda x: round(x, 0) * 100)
        res['real'] = res['cnt'] * res['close']

    QA_util_log_info(
        '##JOB05 Now Current Report ==== {}'.format(str(trading_date)), ui_log)
    table1 = tar[tar['RANK'] <= 5].groupby('date').mean()
    if exceptions is not None:
        frozen_positions = client.get_positions(account1)['positions'][[
            '证券代码', '证券名称', '股票余额', '可用余额', '冻结数量', '参考盈亏', '盈亏比例(%)'
        ]].set_index('证券代码').loc[exceptions]
    else:
        frozen_positions = pd.DataFrame()

    QA_util_log_info(
        '##JOB06 Now Current Holding ==== {}'.format(str(trading_date)),
        ui_log)
    positions = client.get_positions(account1)['positions'][[
        '证券代码', '证券名称', '股票余额', '可用余额', '冻结数量', '参考盈亏', '盈亏比例(%)'
    ]]

    QA_util_log_info(
        '##JOB07 Now Message Building ==== {}'.format(str(trading_date)),
        ui_log)
    try:
        msg1 = '模型训练日期:{model_date}'.format(model_date=stock_info_temp['date'])
        body1 = build_table(
            table1,
            'safe模型结果_{}'.format(str(QA_util_get_last_day(trading_date))))
        body3 = build_table(positions, '目前持仓')
        body4 = build_table(
            pd.DataFrame(report),
            '指数模型结果_{}'.format(str(QA_util_get_last_day(trading_date))))
        body5 = build_table(
            pd.DataFrame(top_report),
            '选股模型结果_{}'.format(str(QA_util_get_last_day(trading_date))))
        #body6 = build_table(stock_list, '上一交易日模型交易清单{}'.format(str(QA_util_get_last_day(trading_date))))
        body7 = build_table(frozen_positions, '目前锁定持仓')
        if res is not None:
            body2 = build_table(res, '目标持仓')
            msg = build_email(build_head(), msg1, body1, body3, body5, body4,
                              body2, body7)
            title = '交易报告'
        else:
            msg = build_email(build_head(), msg1, body1, body3, body5, body4,
                              body1, body7)
            title = '空仓交易报告'
        send_email(title + trading_date, msg, 'date')
    except:
        send_email('交易报告:' + trading_date, "消息构建失败", 'date')
    return (tar)
Пример #7
0
def train(date, strategy_id='机器学习1号', working_dir=working_dir, ui_log=None):
    QA_util_log_info('##JOB01 Now Model Init ==== {}'.format(str(date)),
                     ui_log)
    stock_model = StockModel()
    index_model = IndexModel()
    QA_util_log_info('##JOB02 Now Prepare Date ==== {}'.format(str(date)),
                     ui_log)
    stock_model.get_data(start=str(int(date[0:4]) - 3) + "-01-01", end=date)
    QA_util_log_info('##JOB03 Now Set Target ==== {}'.format(str(date)),
                     ui_log)
    stock_model.set_target(mark=0.42, type='percent')
    QA_util_log_info(
        '##JOB04 Now Set Train time range ==== {}'.format(str(date)), ui_log)
    stock_model.set_train_rng(train_start=str(int(date[0:4]) - 3) + "-01-01",
                              train_end=(datetime.strptime(date, "%Y-%m-%d") -
                                         delta4).strftime('%Y-%m-%d'),
                              test_start=(datetime.strptime(date, "%Y-%m-%d") -
                                          delta3).strftime('%Y-%m-%d'),
                              test_end=date)
    stock_model.prepare_data()
    stock_model.build_model(n_estimators=200)
    QA_util_log_info('##JOB05 Now Model Trainnig ==== {}'.format(str(date)),
                     ui_log)
    stock_model.model_running()
    stock_model.model_check()
    QA_util_log_info('##JOB06 Now Save Model ==== {}'.format(str(date)),
                     ui_log)
    important = stock_model.model_important()
    stock_model.save_model('stock', working_dir=working_dir)
    body1 = build_table(pd.DataFrame(stock_model.info['train_report']),
                        '个股模型训练集情况')
    body2 = build_table(pd.DataFrame(stock_model.info['test_report']),
                        '个股模型测试集情况')
    body3 = build_table(important.head(50), '个股模型特征重要性')

    msg1 = '模型训练日期:{model_date}'.format(model_date=stock_model.info['date'])
    del stock_model

    QA_util_log_info('##JOB02 Now Prepare Date ==== {}'.format(str(date)),
                     ui_log)
    index_model.get_data(start=str(int(date[0:4]) - 3) + "-01-01", end=date)
    QA_util_log_info('##JOB03 Now Set Target ==== {}'.format(str(date)),
                     ui_log)
    index_model.set_target('INDEX_TARGET5', mark=0.3, type='percent')
    QA_util_log_info(
        '##JOB04 Now Set Train time range ==== {}'.format(str(date)), ui_log)
    index_model.set_train_rng(train_start=str(int(date[0:4]) - 3) + "-01-01",
                              train_end=(datetime.strptime(date, "%Y-%m-%d") -
                                         delta4).strftime('%Y-%m-%d'),
                              test_start=(datetime.strptime(date, "%Y-%m-%d") -
                                          delta3).strftime('%Y-%m-%d'),
                              test_end=date)
    index_model.prepare_data()
    index_model.build_model(n_estimators=200)
    QA_util_log_info('##JOB05 Now Model Trainnig ==== {}'.format(str(date)),
                     ui_log)
    index_model.model_running()
    index_model.model_check()
    QA_util_log_info('##JOB06 Now Save Model ==== {}'.format(str(date)),
                     ui_log)
    important = index_model.model_important()
    index_model.save_model('index', working_dir=working_dir)
    body4 = build_table(pd.DataFrame(index_model.info['train_report']),
                        '指数模型训练集情况')
    body5 = build_table(pd.DataFrame(index_model.info['test_report']),
                        '指数模型测试集情况')
    body6 = build_table(important.head(50), '指数模型特征重要性')

    index_model.set_target('INDEX_TARGET', mark=0.3, type='percent')
    index_model.prepare_data()
    index_model.build_model(n_estimators=200)
    index_model.model_running()
    index_model.model_check()
    index_model.save_model('safe', working_dir=working_dir)

    body7 = build_table(pd.DataFrame(index_model.info['train_report']),
                        '安全模型训练集情况')
    body8 = build_table(pd.DataFrame(index_model.info['test_report']),
                        '安全模型测试集情况')
    body9 = build_table(important.head(50), '安全模型特征重要性')

    QA_util_log_info(
        '##JOB06 Now Model Trainning Report ==== {}'.format(str(date)), ui_log)
    msg = build_email(build_head(), msg1, body1, body2, body3, body4, body5,
                      body6, body7, body8, body9)

    send_email('模型训练报告', msg, 'date')
    send_actionnotice(strategy_id,
                      '报告:{}'.format(date),
                      '模型训练完成,请查收结果',
                      direction='HOLD',
                      offset='HOLD',
                      volume=None)
Пример #8
0
def predict(trading_date,
            strategy_id='机器学习1号',
            account1='name:client-1',
            working_dir=working_dir,
            ui_log=None):

    try:
        QA_util_log_info(
            '##JOB01 Now Got Account Info ==== {}'.format(str(trading_date)),
            ui_log)
        client = get_Client()
        account1 = account1
        account_info = client.get_account(account1)
        print(account_info)
        sub_accounts = client.get_positions(account1)['sub_accounts']
    except:
        send_email('错误报告', '云服务器错误,请检查', trading_date)
        send_actionnotice(strategy_id,
                          '错误报告:{}'.format(trading_date),
                          '云服务器错误,请检查',
                          direction='HOLD',
                          offset='HOLD',
                          volume=None)
    try:
        QA_util_log_info(
            '##JOB02 Now Load Model ==== {}'.format(str(trading_date)), ui_log)
        model_temp, info_temp = load_model('index', working_dir=working_dir)
    except:
        send_email('错误报告', '无法正确加载模型,请检查', trading_date)
        send_actionnotice(strategy_id,
                          '错误报告:{}'.format(trading_date),
                          '无法正确加载模型,请检查',
                          direction='HOLD',
                          offset='HOLD',
                          volume=None)

    QA_util_log_info(
        '##JOB03 Now Model Predict ==== {}'.format(str(trading_date)), ui_log)
    stock_list, report, top_report = check_model(
        model_temp, QA_util_get_last_day(trading_date),
        QA_util_get_last_day(trading_date), info_temp['cols'], 0.42)
    tar, b = model_predict(model_temp,
                           str(trading_date[0:7]) + "-01", trading_date,
                           info_temp['cols'])

    QA_util_log_info(
        '##JOB03 Now Concat Result ==== {}'.format(str(trading_date)), ui_log)
    tar1 = tar[tar['RANK'] <= 5].loc[trading_date]
    tar2 = tar1[['Z_PROB', 'O_PROB', 'RANK']]
    print(trading_date)
    close = QA_fetch_stock_day_adv(
        list(tar1.index), trading_date,
        trading_date).data.loc[trading_date].reset_index('date')['close']
    info = QA_fetch_stock_fianacial_adv(
        list(tar1.index), trading_date,
        trading_date).data.reset_index('date')[['NAME', 'INDUSTRY']]
    res = pd.concat([tar2, close, info], axis=1)

    QA_util_log_info(
        '##JOB04 Now Funding Decision ==== {}'.format(str(trading_date)),
        ui_log)
    avg_account = sub_accounts['总 资 产'] / tar1.shape[0]
    res = res.assign(tar=avg_account[0] * percent)
    res['cnt'] = (res['tar'] / res['close'] /
                  100).apply(lambda x: round(x, 0) * 100)
    res['real'] = res['cnt'] * res['close']

    QA_util_log_info(
        '##JOB05 Now Current Report ==== {}'.format(str(trading_date)), ui_log)
    table1 = tar[tar['RANK'] <= 5].groupby('date').mean()

    QA_util_log_info(
        '##JOB06 Now Current Holding ==== {}'.format(str(trading_date)),
        ui_log)
    positions = client.get_positions(account1)['positions'][[
        '证券代码', '证券名称', '股票余额', '可用余额', '冻结数量', '参考盈亏', '盈亏比例(%)'
    ]]

    QA_util_log_info(
        '##JOB07 Now Message Building ==== {}'.format(str(trading_date)),
        ui_log)
    try:
        msg1 = '模型训练日期:{model_date}'.format(model_date=info_temp['date'])
        body1 = build_table(table1, '近段时间内模型盈利报告')
        body2 = build_table(res, '目标持仓')
        body3 = build_table(positions, '目前持仓')
        body4 = build_table(
            pd.DataFrame(report),
            '上一交易日模型报告{}'.format(str(QA_util_get_last_day(trading_date))))
        body5 = build_table(
            pd.DataFrame(top_report),
            '上一交易日模型报告Top{}'.format(str(QA_util_get_last_day(trading_date))))
        body6 = build_table(
            stock_list,
            '上一交易日模型交易清单{}'.format(str(QA_util_get_last_day(trading_date))))

        msg = build_email(build_head(), msg1, body5, body4, body6, body1,
                          body2, body3)
    except:
        send_email('交易报告:' + trading_date, "消息构建失败", 'date')
    send_email('交易报告:' + trading_date, msg, 'date')
Пример #9
0
def trade_roboot(target,
                 account,
                 trading_date,
                 percent,
                 strategy_id,
                 type='end',
                 exceptions=None):
    logging.basicConfig(level=logging.DEBUG)
    client = strategyease_sdk.Client(host=yun_ip,
                                     port=yun_port,
                                     key=easytrade_password)
    account1 = account
    client.cancel_all(account1)
    account_info = client.get_account(account1)
    sub_accounts = client.get_positions(
        account1)['sub_accounts']['总 资 产'].values[0]
    try:
        frozen = float(
            client.get_positions(account1)['positions'].set_index(
                '证券代码').loc[exceptions]['市值'].sum())
    except:
        frozen = 0
    sub_accounts = sub_accounts - frozen
    positions = client.get_positions(account1)['positions'][[
        '证券代码', '证券名称', '股票余额', '可用余额', '冻结数量', '参考盈亏', '盈亏比例(%)'
    ]]

    if target is None:
        e = send_trading_message(account1,
                                 strategy_id,
                                 account_info,
                                 None,
                                 "触发清仓",
                                 None,
                                 0,
                                 direction='SELL',
                                 type='MARKET',
                                 priceType=4,
                                 price=None,
                                 client=client)

    res = build(target, positions, sub_accounts, trading_date, percent,
                exceptions, 100)
    res1 = res

    while (res[res['mark'] < 0].shape[0] + res[res['mark'] > 0].shape[0]) > 0:

        h1 = int(datetime.datetime.now().strftime("%H"))
        if h1 >= 15:
            break

        if res[res['mark'] < 0].shape[0] == 0:
            pass
        else:
            for i in res[res['mark'] < 0].index:
                if type == 'end':
                    cnt = float(res.at[i, 'cnt'])
                    tar = float(res.at[i, '股票余额'])
                    NAME = res.at[i, 'NAME']
                    INDUSTRY = res.at[i, 'INDUSTRY']
                    mark = abs(float(res.at[i, 'mark']))

                    print(
                        '卖出 {code}({NAME},{INDUSTRY}) {cnt}股, 目标持仓:{target},总金额:{tar}'
                        .format(code=i,
                                NAME=NAME,
                                INDUSTRY=INDUSTRY,
                                cnt=abs(mark),
                                target=cnt,
                                tar=tar))
                    e = send_trading_message(account1,
                                             strategy_id,
                                             account_info,
                                             i,
                                             NAME,
                                             INDUSTRY,
                                             mark,
                                             direction='SELL',
                                             type='MARKET',
                                             priceType=4,
                                             price=None,
                                             client=client)
                elif type == 'morning':
                    cnt = float(res.at[i, 'cnt'])
                    tar = float(res.at[i, '股票余额'])
                    NAME = res.at[i, 'NAME']
                    INDUSTRY = res.at[i, 'INDUSTRY']
                    mark = abs(float(res.at[i, 'mark']))
                    price = round(float(res.at[i, 'close'] * 1.0995), 2)
                    print(
                        '早盘挂单卖出 {code}({NAME},{INDUSTRY}) {cnt}股, 目标持仓:{target},单价:{price},总金额:{tar}'
                        .format(code=i,
                                NAME=NAME,
                                INDUSTRY=INDUSTRY,
                                cnt=abs(mark),
                                target=cnt,
                                tar=tar,
                                price=price))
                    e = send_trading_message(account1,
                                             strategy_id,
                                             account_info,
                                             i,
                                             NAME,
                                             INDUSTRY,
                                             mark,
                                             direction='SELL',
                                             type='LIMIT',
                                             priceType=None,
                                             price=price,
                                             client=client)
                else:
                    pass
                time.sleep(5)

        time.sleep(30)

        for i in res[res['mark'] == 0].index:
            cnt = float(res.at[i, 'cnt'])
            tar = float(res.at[i, 'real'])
            NAME = res.at[i, 'NAME']
            INDUSTRY = res.at[i, 'INDUSTRY']
            mark = abs(float(res.at[i, 'mark']))
            print('继续持有 {code}({NAME},{INDUSTRY}), 目标持仓:{target},总金额:{tar}'.
                  format(code=i,
                         NAME=NAME,
                         INDUSTRY=INDUSTRY,
                         target=cnt,
                         tar=tar))
            send_actionnotice(strategy_id,
                              account_info,
                              '{code}({NAME},{INDUSTRY})'.format(
                                  code=i, NAME=NAME, INDUSTRY=INDUSTRY),
                              direction='HOLD',
                              offset='HOLD',
                              volume=abs(mark))
        time.sleep(10)

        if res[res['mark'] > 0].shape[0] == 0:
            pass
        else:
            for i in res[res['mark'] > 0].index:
                if type == 'end':
                    cnt = float(res.at[i, 'cnt'])
                    tar = float(res.at[i, 'real'])
                    NAME = res.at[i, 'NAME']
                    INDUSTRY = res.at[i, 'INDUSTRY']
                    mark = abs(float(res.at[i, 'mark']))
                    print(
                        '买入 {code}({NAME},{INDUSTRY}) {cnt}股, 目标持仓:{target},总金额:{tar}'
                        .format(code=i,
                                NAME=NAME,
                                INDUSTRY=INDUSTRY,
                                cnt=abs(mark),
                                target=cnt,
                                tar=tar))
                    e = send_trading_message(account1,
                                             strategy_id,
                                             account_info,
                                             i,
                                             NAME,
                                             INDUSTRY,
                                             mark,
                                             direction='BUY',
                                             type='MARKET',
                                             priceType=4,
                                             price=None,
                                             client=client)
                elif type == 'morning':
                    cnt = float(res.at[i, 'cnt'])
                    tar = float(res.at[i, 'real'])
                    NAME = res.at[i, 'NAME']
                    INDUSTRY = res.at[i, 'INDUSTRY']
                    mark = abs(float(res.at[i, 'mark']))
                    price = round(float(res.at[i, 'close'] * (1 - 0.0995)), 2)
                    print(
                        '早盘挂单买入 {code}({NAME},{INDUSTRY}) {cnt}股, 目标持仓:{target},单价:{price},总金额:{tar}'
                        .format(code=i,
                                NAME=NAME,
                                INDUSTRY=INDUSTRY,
                                cnt=abs(mark),
                                target=cnt,
                                price=price,
                                tar=tar))
                    e = send_trading_message(account1,
                                             strategy_id,
                                             account_info,
                                             i,
                                             NAME,
                                             INDUSTRY,
                                             mark,
                                             direction='BUY',
                                             type='LIMIT',
                                             priceType=None,
                                             price=price,
                                             client=client)
                else:
                    pass
                time.sleep(5)

        time.sleep(30)
        if type == 'end':
            sub_accounts = client.get_positions(
                account1)['sub_accounts']['总 资 产'].values[0] - frozen
            positions = client.get_positions(account1)['positions'][[
                '证券代码', '证券名称', '股票余额', '可用余额', '冻结数量', '参考盈亏', '盈亏比例(%)'
            ]]
            res = build(target, positions, sub_accounts, trading_date, percent,
                        exceptions, 100)
        elif type == 'morning':
            break
        else:
            break

    return (res1)
Пример #10
0
from QUANTTOOLS.message_func.wechat import send_actionnotice
from  QUANTAXIS.QAUtil import QA_util_today_str
mark_day = QA_util_today_str()
logging.basicConfig(level=logging.DEBUG)
client = strategyease_sdk.Client(host='132.232.89.97', port=8888, key='123456')
account1='name:client-1'

try:
    account_info = client.get_account(account1)
    print(account_info)
except:
    send_email('错误报告', '云服务器错误,请检查', 'date')
    send_actionnotice('自动打新',
                      '错误报告:{}'.format(mark_day),
                      '云服务器错误,请检查',
                      direction = 'HOLD',
                      offset='HOLD',
                      volume=None
                      )
#to_do
#自动打新
try:
    client.purchase_new_stocks(account1)
    send_email('每日打新', '打新成功', 'date')
    send_actionnotice('自动打新',
                      '报告:{}'.format(mark_day),
                      '自动打新完成,请查收结果',
                      direction = 'BUY',
                      offset='OPEN',
                      volume=None
                      )