def run_backtest(file, config): cache = StrategyPerformanceJsonCache(user) # TODO 修改为JSON try: with codecs.open(file, 'r', 'utf-8') as f: code = f.read() f.close() backtesting = Backtesting(**config) backtesting.set_code(code) backtesting.start() performance = backtesting.get_performance() cache.put_performance(performance) cache.put('setting', json.dumps(backtesting.get_setting())) # TODO 修改为JSON output = get_output(user, name) result = {'stat': 'OK', 'result': performance.yield_curve, 'performance': performance.info_on_home_page} except SlaverThreadError as e: tb_message = get_user_friendly_traceback(*e.get_exc()) result = {"stat": "FALSE", "error": string_to_html('\n'.join(tb_message))} except Exception: tb_message = get_user_friendly_traceback(*sys.exc_info()) result = {"stat": "FALSE", "error": string_to_html('\n'.join(tb_message))} finally: output = get_output(user, name) result['output'] = string_to_html(output) cache.put('response', json.dumps(result))
def backtest(conn, *args): try: user = args[0] backtesting = Backtesting(*args) backtesting.start() performance = backtesting.get_performance() cache = StrategyPerformanceJsonCache(user) cache.put_performance(performance) cache.put('setting', json.dumps(backtesting.get_setting())) conn.put({"stat": "OK"}) except SlaverThreadError as e: tb_message = get_user_friendly_traceback(*e.get_exc()) conn.put({"stat": "FALSE", "error": string_to_html('\n'.join(tb_message))}) except Exception: tb_message = get_user_friendly_traceback(*sys.exc_info()) conn.put({"stat": "FALSE", "error": string_to_html('\n'.join(tb_message))})
def backtest(conn, *args): try: config = BfConfig(**{v[0]: v[1] for v in zip(["user", "name", "symbols", "time_frame", "start_time", "end_time", "commission", "slippage"], args)}) config.trading_mode = TradingMode.on_tick user = User(config.user) code = get_strategy(user, "LastBacktest").content backtesting = Backtesting() backtesting.set_code(code) backtesting.set_config(config) backtesting.start() performance = backtesting.get_performance() cache = StrategyPerformanceJsonCache(user.user_id) cache.put_performance(performance) cache.put('setting', json.dumps(backtesting.get_setting())) conn.put({"stat": "OK"}) except SlaverThreadError as e: tb_message = get_user_friendly_traceback(*e.get_exc()) conn.put({"stat": "FALSE", "error": string_to_html('\n'.join(tb_message))}) except Exception: tb_message = get_user_friendly_traceback(*sys.exc_info()) conn.put({"stat": "FALSE", "error": string_to_html('\n'.join(tb_message))})
def run_backtest(user, name, file, symbols, time_frame, start_time, end_time, commission, slippage): cache = StrategyPerformanceJsonCache(user) # TODO 修改为JSON try: with codecs.open(file, 'r', 'utf-8') as f: code = f.read() f.close() backtesting = Backtesting(user, name, code, symbols, time_frame, start_time, end_time, commission, slippage) backtesting.start() performance = backtesting.get_performance() cache.put_performance(performance) cache.put('setting', json.dumps(backtesting.get_setting())) # TODO 修改为JSON output = get_output(user, name) result = {'stat': 'OK', 'result': performance.yield_curve, 'performance': performance.info_on_home_page} except SlaverThreadError as e: tb_message = get_user_friendly_traceback(*e.get_exc()) result = {"stat": "FALSE", "error": string_to_html('\n'.join(tb_message))} except Exception: tb_message = get_user_friendly_traceback(*sys.exc_info()) result = {"stat": "FALSE", "error": string_to_html('\n'.join(tb_message))} finally: output = get_output(user, name) result['output'] = string_to_html(output) cache.put('response', json.dumps(result))