Exemplo n.º 1
0
 def get(self):
     code = self.get_argument('code', '').replace('\t', '    ')
     name = self.get_argument('name', 'untitled')
     symbols = self.get_argument('symbols', None)
     time_frame = self.get_argument('time_frame', None)
     start_time = self.get_argument('start_time', None)
     end_time = self.get_argument('end_time', None)
     commission = int(self.get_argument('commission', 0))
     slippage = int(self.get_argument('slippage', 0))
     user_id = self.get_argument('user_id', None)
     if user_id:
         self.write('callback(')
         try:
             result = yield tornado.gen.Task(run_backtest, user_id, name, code, [symbols],
                                             time_frame, start_time,
                                             end_time, commission, slippage)
         except TimeoutError:
             self.write({'stat': 'FALSE', 'error': '回测超时'})
             self.finish()
             return
         if result['stat'] == 'OK':
             cache = StrategyPerformanceJsonCache(user_id)
             performance = cache.get_performance()
             output = self.get_output(user_id, name)
             self.write({'stat': 'OK', 'result': performance.yield_curve, 'output': string_to_html(output),
                         'performance': performance.info_on_home_page})
         else:
             output = self.get_output(user_id, name)
             result['output'] = string_to_html(output)
             self.write(result)
         self.write(')')
     self.flush()
     self.finish()
Exemplo n.º 2
0
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))
Exemplo n.º 3
0
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))})
Exemplo n.º 4
0
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))})
Exemplo n.º 5
0
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))