Пример #1
0
def command_line_runner():
    parser = get_parser()
    args = vars(parser.parse_args())

    if args['version']:
        print(__version__)
        return

    if not args['configuration_id']:
        parser.print_help()
        return

    refresh = False
    monitor = -1

    if args['refresh']:
        refresh = bool(int(args['refresh']))

    if args['monitor']:
        monitor = float(args['monitor'])

    configuration_id = int(args['configuration_id'])

    print_now(
        'Start running tool for configuration_id = {}, refresh = {}, monitor = {}'
        .format(configuration_id, refresh, monitor))
    process(configuration_id, refresh, monitor)
Пример #2
0
def reset_runner(configuration_id):
    run_result.remove_run_result_trades_by_configuration_id(configuration_id)
    run_result.reset_run_results_by_configuration_id(configuration_id)

    files = glob.glob('/saved_models/*')
    for f in files:
        os.remove(f)

    print_now('Reset before running done, configuration_id = {}'.format(
        configuration_id))
Пример #3
0
    def do():
        trades_before_run = smart_refresh_trades(configuration_id)
        (_, _, report) = mt4.run(trades_before_run)

        if report['hasNewTrades'] == True:
            print_now(
                'Running TradeResultPredictor, configuration_id = {}'.format(
                    configuration_id))
            predictor.run()
        else:
            print_now('No new trades, configuration_id = {}'.format(
                configuration_id))
Пример #4
0
def get_connection():
    """ get connection """
    server = config.get('mssql', 'server')
    database = config.get('mssql', 'database')
    username = config.get('mssql', 'username')
    password = config.get('mssql', 'password')
    driver= '{ODBC Driver 13 for SQL Server}'
    
    cnxn = None
    attempts = 0
    start_run_time = datetime.now()

    while cnxn is None:
        try:
            cnxn = pyodbc.connect('DRIVER='+driver+';PORT=1433;SERVER='+server+';PORT=1443;DATABASE='+database+';UID='+username+';PWD='+password)
        except:
            attempts += 1
            if attempts % 10:
                print_now('Unable to open database connection...')

    if attempts > 0:
        print_now('Opening database connection took {}'.format(datetime.now() - start_run_time))

    return cnxn
Пример #5
0
    def run(self):
        start_run_time = datetime.now()

        if self.verbose:
            print_now("Start running terminals, configuration_id = {}".format(self.configuration_id))

        runs = get_by_configuration_id(self.configuration_id)

        for run in runs:
            run_id = run['RunId']
            run_name = run['Name']
            symbol = run['TestSymbol']
            date_from = run['TestDateFrom']
            date_to = run['TestDateTo']

            number_of_waits = 0

            while True:
                # run batch of commands equal to number of terminals
                commands = []
                results = dict()

                for idx in range(len(self.terminal_pool)):
                    run_result_item = run_result.get_for_processing_by_run_id(run_id)

                    # no configuration for processing
                    if run_result_item is None and len(commands) == 0:
                        if self.verbose:
                            print_now("No configuration found for processing, run_name = {}".format(run_name))
                        time.sleep(0.1) # wait for a while
                        number_of_waits = number_of_waits + 1
                        if number_of_waits >= MAX_WAITS_COUNT:
                            break
                        else:
                            continue
                    elif run_result_item is None:
                        break

                    config = get_option_by_id(run_result_item['OptionId'])
                    run_result_id = run_result_item['ResultId']

                    terminal_idx = idx % len(self.terminal_pool)
                    terminal  = self.terminal_pool[terminal_idx]
                    terminal_path = terminal['exe_path']
                    data_path = terminal['data_path']
                    set_file_name = create_set_file(data_path, config, run_result_id)
                    run_result_date_from = self._get_run_result_date_from(date_from, run_result_id)
                    ini_file = self._create_ini_file(data_path, run_result_id, run_result_date_from, date_to, symbol, set_file_name)
                    cmd = COMMAND_TEMPLATE.format(terminal_path, ini_file)
                    commands.append(cmd)

                    # for result processing
                    results[data_path] = run_result_id

                if number_of_waits >= MAX_WAITS_COUNT:
                    if self.verbose:
                        print_now("Stop waiting for configuration options to process, configuration_id = {}".format(self.configuration_id))
                    break

                exec_commands(commands, len(self.terminal_pool))

                reports = prepare_results(results)

                reports = self._adjust_reports(date_from, reports)

                for report in reports:
                    run_result.update_run_result_with_report(report)
        
        end_run_time = datetime.now()

        if self.verbose:
            elapsed = end_run_time - start_run_time
            print_now("Running terminals took {}, configuration_id = {}".format(elapsed, self.configuration_id))

        return (start_run_time, end_run_time)
Пример #6
0
    def run(self):
        print_now('TradeResultPredictor starting running, configuration = {}'.format(self.configuration_id))

        run_results = get_completed_run_results_by_configuration_id(self.configuration_id)
        tbl = PrettyTable()
        tbl.field_names = [
            'iMA_Period', 
            'TotalNetProfit', 
            'MaximalDrawdown',
            'TotalTrades',
            'Last 3 trades',
            'Last trade date',
            'Predicted 3 trades',
            'Total for predicted 3 trades',
            'Train RMSE / Test RMSE'
        ]

        for run_results in run_results:
            configuration_option = get_option_by_id(run_results['OptionId'])

            trades = get_run_result_trades_by_result_id(run_results['ResultId'])
            # drop unneeded columns
            dataset = trades.drop(trades.columns.difference(['Profit']), 1).values[:,:]

            perceptron = MultilayerPerceptron(run_results['ResultId'], dataset, verbose=self.verbose)
            stats = perceptron.train()
            predicted_trades = perceptron.predict(look_forth = 3)

            testScoreRMSE = 'N/A'
            if stats['TestScore']['RMSE'] != 'N/A':
                testScoreRMSE = '{0:.2f}'.format(stats['TestScore']['RMSE'])

            tbl.add_row([
                configuration_option['iMA_Period'],
                run_results['TotalNetProfit'],
                run_results['MaximalDrawdown'],
                run_results['TotalTrades'],
                ' | '.join(['{0:.2f}'.format(t['Profit']) for idx, t in trades.tail(3).iterrows()]),
                trades['CloseTime'].iloc[-1].strftime('%Y.%m.%d %H:%M'),
                ' | '.join(['{0:.2f}'.format(t) for t in predicted_trades]),
                '{0:.2f}'.format(np.sum(predicted_trades)),
                '{0:.2f} / {1}'.format(stats['TrainScore']['RMSE'], testScoreRMSE)
            ])

        if config.has_section("smtp"):
            smtp_user = config.get('smtp', 'user')
            smtp_password = config.get('smtp', 'password')
            recipients = config.get('smtp', 'recipients')

            tbl_attr = { 
                'style': 'border: 1px solid black; border-collapse: collapse;',
                'cellpadding': 5,
                'border': 1
            }

            subject = 'WSRT predictions {}'.format(datetime.now().strftime('%Y-%m-%d %H:%M'))

            elapsed = datetime.now() - self.start_time
            body = 'Report generated on {}<br/>'.format(datetime.now().strftime('%Y-%m-%d %H:%M'))
            body += 'Report generation took {}<br/>'.format(elapsed)
            
            body += '<br/>'
            body += tbl.get_html_string(attributes=tbl_attr)
            body += '<br/><br/>'

            body += 'Thanks'

            self._send_notification(subject, body, smtp_user, smtp_password, recipients)
            
            print_now('TradeResultPredictor notifications sent, configuration_id = {}'.format(self.configuration_id))