Ejemplo n.º 1
0
def test_error_result():
    argkws = {}
    result_args = {}
    username = "******"
    argkws['username'], argkws['date'], argkws['tstamp'] = username, str(
        20170515), str(114047)
    root_path = get_server_result_path(argkws)
    errors = os.path.join(root_path, 'output.txt')
    print errors
    content = []
    with open(errors, 'r+') as fin:
        for line in fin:
            content.append(line)
    error_html = '<br>'.join(content)
    result_args['error_html'] = error_html
    result_args['show_error'] = 1
    return render_template('test/test_backtest_result.html', **result_args)
Ejemplo n.º 2
0
def test_backtest_result():
    argkws = {}
    result_args = {}
    username = "******"
    argkws['username'], argkws['date'], argkws['tstamp'] = username, str(
        20170515), str(114047)
    root_dir = get_server_result_path(argkws)
    #1.get summary values
    summary_path = os.path.join(root_dir, 'output.txt')
    summary_values = get_summarys(summary_path)
    summary_values = map(
        lambda x: x.strip(),
        filter(lambda x: len(x.strip()) > 0,
               summary_values.strip().split('|')))
    summary_values.pop(1)
    result_args['summary_values'] = summary_values

    #2.get everyday performace
    all = output_parser(summary_path)
    everyday = all[2][3:-1]
    everyday_performace_values = map(
        lambda x: map(lambda z: z.strip(),
                      filter(lambda y: len(y.strip()) > 0, x.split('|'))),
        everyday)
    result_args['everyday_performace_values'] = everyday_performace_values

    #3.get charts paths
    base_path = r'/static/upload_results'
    base_path = os.path.join(base_path, argkws['username'], argkws['date'],
                             argkws['tstamp'], 'pta')
    result_args['pnl_path'] = os.path.join(base_path, "pnl.png")
    result_args['position_path'] = os.path.join(base_path, "position.png")
    result_args['volatility_path'] = os.path.join(base_path, "volatility.png")

    #4.get file addr
    base_path = r'/static/upload_results'
    base_path = os.path.join(base_path, argkws['username'], argkws['date'],
                             argkws['tstamp'])
    result_args['exit_list'] = os.path.join(
        base_path, "{0}_exit_list.csv".format(username))
    result_args['summary'] = os.path.join(
        base_path, "{0}_total_summary.csv".format(username))
    result_args['show_result'] = 1
    return render_template('test/test_backtest_result.html', **result_args)
Ejemplo n.º 3
0
def fill():
    main_page_forms = get_main_page_form_obj()

    if check_backtest(session) is True:
        session['show_tab'] = ()

#     print 'show session result & error = ',session['show_result'],session['show_error']
    result_args = {}
    if int(session['show_result']) == 1:
        argkws = {}
        argkws['username'],argkws['date'],argkws['tstamp'] = current_user.username,\
            str(session['last_backtest_tstamp'][0]),str(session['last_backtest_tstamp'][1])
        root_dir = get_server_result_path(argkws)

        if os.path.exists(root_dir):
            #1.get summary values
            summary_path = os.path.join(root_dir, 'output.txt')
            summary_values = get_summarys(summary_path)
            summary_values = map(lambda x: x.strip(),
                                 filter(lambda x: len(x.strip()) > 0,
                                        summary_values.strip().split('|')))
            summary_values.pop(1)
            result_args['summary_values'] = summary_values

            #2.get everyday performace
            all = output_parser(summary_path)
            everyday = all[2][3:-1]
            everyday_performace_values = map(lambda x: map(lambda z: z.strip(),filter(lambda y: len(y.strip())>0,x.split('|')) ),everyday)
            result_args[
                'everyday_performace_values'] = everyday_performace_values

            #3.get charts paths
            base_path = r'/static/upload_results'
            base_path = os.path.join(base_path, argkws['username'],
                                     argkws['date'], argkws['tstamp'], 'pta')
            result_args['pnl_path'] = os.path.join(base_path, "pnl.png")
            result_args['position_path'] = os.path.join(base_path,
                                                        "position.png")
            result_args['volatility_path'] = os.path.join(
                base_path, "volatility.png")

            #4.get file addr
            base_path = r'/static/upload_results'
            base_path = os.path.join(base_path, argkws['username'],
                                     argkws['date'], argkws['tstamp'])
            result_args['exit_list'] = os.path.join(
                base_path, "{0}_exit_list.csv".format(current_user.username))
            result_args['summary'] = os.path.join(
                base_path,
                "{0}_total_summary.csv".format(current_user.username))
        else:
            session['show_error'] = 1
            session['show_result'] = 0

    if int(session['show_error']) == 1:
        argkws = {}
        argkws['username'],argkws['date'],argkws['tstamp'] = current_user.username,\
            str(session['last_backtest_tstamp'][0]),str(session['last_backtest_tstamp'][1])
        root_path = get_server_result_path(argkws)
        errors = os.path.join(root_path, 'output.txt')
        #         print errors
        content = []
        if os.path.exists(errors):
            with open(errors, 'r+') as fin:
                for line in fin:
                    content.append(line)
            error_html = '<br>'.join(content)
        else:
            error_html = '<h2>Cpp Server Closed!</h2>'
            result_args['error_html'] = error_html

    args = get_main_page_arg_dict(*main_page_forms)
    args.update(result_args)
    return render_template('auth/fill.html', **args)
Ejemplo n.º 4
0
def backtest():
    main_page_forms = get_main_page_form_obj()
    sub_form = main_page_forms[0]
    min_backtest_gap_seconds = 10

    session['show_tab'] = ()
    if sub_form.submit1.data and sub_form.validate_on_submit():

        now = (get_today(), get_hourminsec())
        if session['last_backtest_tstamp'][0] != -1 and diff_seconds(
                now,
                session['last_backtest_tstamp']) < min_backtest_gap_seconds:
            flash('warning! Min backtest time interval is {0}, take a rest~~'.
                  format(min_backtest_gap_seconds))
            return redirect(url_for('auth.fill'))
        else:
            session['last_backtest_tstamp'] = now
            data_dict = web_datablock_to_server_datablock(session['data_block'])
            comset_dict = web_comset_data_to_server_data(session['comset'])
            global_config = web_global_config_to_server_global_config(
                session['global_config'])
            entry_rules = web_conditions_to_server_conditions_entry(\
                                session['entry_conditions'],session['global_config']['direction'],session['global_config']['quant'])
            exit_rules  = web_conditions_to_server_conditions_exit(\
                                session['exit_conditions'],session['global_config']['minTTL'],session['global_config']['maxTTL'])

            #             print 'data_block = ',data_dict
            #             print 'comset = ',comset_dict
            #             print 'global_config = ',global_config
            #             print 'entry_rules = ',entry_rules
            #             print 'exit_rules = ',exit_rules

            funcNames = ['config_data','create_shm','create_backtest_config',\
                            'backtest','create_pta','upload_pta']
            username = current_user.username

            requestID = session['requestID']
            session['requestID'] += 1

            today, tstamp = now
            loginID = '{0}_{1}'.format(today, tstamp)

            argkws = {}
            argkws['username'], argkws['date'], argkws[
                'tstamp'] = username, today, tstamp

            backtest_pydict = {}
            backtest_pydict['comset_data'] = comset_dict
            backtest_pydict['config_data'] = global_config
            backtest_pydict['entry_data'] = entry_rules
            backtest_pydict['exit_data'] = exit_rules

            backtest_pydict.update(argkws)

            os.makedirs(get_server_result_path(argkws))

            paras = [data_dict, {}, backtest_pydict, argkws, argkws, argkws]
            pipeline = 1
            paras_dict = dict(
                zip(map(lambda x: str(x), range(len(paras))), paras))
            #             print paras_dict
            session['last_request_id'] = requestID
            session['last_func_name'] = funcNames
            session['pipeline'] = pipeline

            cmd = current_app.rpc_client.create_cmd(today, username, loginID, requestID, funcNames, \
                                                        pipeline, **paras_dict)
            print 'send cmd = ', cmd, '\n'
            current_app.rpc_client.send_cmd(cmd)

            flash('Start back Testing, please wait for a while...')

    args = get_main_page_arg_dict(*main_page_forms)
    args['start_timer'] = 1
    session['show_result'], session['show_error'] = 0, 0
    return render_template('auth/fill.html', **args)