Beispiel #1
0
    def add_comb(self, comb, settings):
        """ 添加策略组合组合
        
        Args:
            comb (list): 一个策略组合
        """
        self._combs.append(comb)
        num_strategy = len(comb) 
        if 'capital' not in settings:
            settings['capital'] = 1000000.0 # 默认资金
            logger.info('BackTesting with default capital 1000000.0.' )

        assert (settings['capital'] > 0)
        if num_strategy == 1:
            settings['ratio'] = [1]
        elif num_strategy > 1 and 'ratio' not in settings:
            settings['ratio'] = [1.0/num_strategy] * num_strategy
        assert('ratio' in settings) 
        assert(len(settings['ratio']) == num_strategy)
        assert(sum(settings['ratio']) - 1.0 < 0.000001)
        assert(num_strategy>=1)
        ctxs = []
        for i, s in enumerate(comb):
            iset = { }
            if settings:
                iset = { 'capital': settings['capital'] * settings['ratio'][i] }
                #logger.debug(iset)
            ctxs.append(StrategyContext(s.name, iset))
        self.context.add_strategy_context(ctxs)
        blotters = [ ctx.blotter for ctx in  ctxs]
        return blotter.Profile(blotters, self._all_data, self.pcontracts[0], len(self._combs)-1)
Beispiel #2
0
 def _load_data(self, strpcons, dt_start, dt_end, n, spec_date):
     all_data = OrderedDict()
     max_window = -1
     logger.info("loading data...")
     pbar = ProgressBar().start()
     pcontracts = [PContract.from_string(s) for s in strpcons]
     pcontracts = sorted(pcontracts, reverse=True)
     for i, pcon in enumerate(pcontracts):
         strpcon = str(pcon)
         if strpcon in spec_date:
             dt_start = spec_date[strpcon][0]
             dt_end = spec_date[strpcon][1]
         assert (dt_start < dt_end)
         if n:
             wrapper = self._data_manager.get_last_bars(strpcon, n)
         else:
             wrapper = self._data_manager.get_bars(strpcon, dt_start,
                                                   dt_end)
         if len(wrapper) == 0:
             continue
         all_data[strpcon] = DataContext(wrapper)
         max_window = max(max_window, len(wrapper))
         pbar.update(i * 100.0 / len(strpcons))
         # progressbar.log('')
     if n:
         assert (max_window <= n)
     pbar.finish()
     if len(all_data) == 0:
         assert (False)
         # @TODO raise
     return all_data, max_window
Beispiel #3
0
    def add_comb(self, comb, settings):
        """ 添加策略组合组合

        Args:
            comb (list): 一个策略组合
        """
        self._combs.append(comb)
        num_strategy = len(comb)
        if 'capital' not in settings:
            settings['capital'] = 1000000.0  # 默认资金
            logger.info('BackTesting with default capital 1000000.0.')

        assert (settings['capital'] > 0)
        if num_strategy == 1:
            settings['ratio'] = [1]
        elif num_strategy > 1 and 'ratio' not in settings:
            settings['ratio'] = [1.0 / num_strategy] * num_strategy
        assert ('ratio' in settings)
        assert (len(settings['ratio']) == num_strategy)
        assert (sum(settings['ratio']) - 1.0 < 0.000001)
        assert (num_strategy >= 1)
        ctxs = []
        for i, s in enumerate(comb):
            iset = {}
            if settings:
                iset = {'capital': settings['capital'] * settings['ratio'][i]}
                # logger.debug(iset)
            ctxs.append(StrategyContext(s.name, iset))
        self.context.add_strategy_context(ctxs)
        return blotter.Profile(ctxs, self._all_data, self.pcontracts[0],
                               len(self._combs) - 1)
Beispiel #4
0
    def __init__(self,
                 name,
                 event_protocol="tcp://127.0.0.1:5555",
                 register_protocol="tcp://127.0.0.1:5557"):
        EventEngine.__init__(self)
        context = zmq.Context()
        self._name = name
        try:
            self._broadcast_event_socket = context.socket(zmq.PUB)
            self._broadcast_event_socket.bind(event_protocol)
            self._server_recv_event_socket = context.socket(zmq.PULL)
            self._server_recv_event_socket.bind(register_protocol)
            self._is_server = True
            logger.info('Start a ZMQEventEngine server: %s' % self._name)
        except zmq.error.ZMQError:
            logger.info('Start a ZMQEventEngine client: %s' % self._name)
            self._is_server = False

        self._emit_event_socket = context.socket(zmq.PUSH)
        self._emit_event_socket.connect(register_protocol)
        self._client_recv_event_socket = context.socket(zmq.SUB)
        self._client_recv_event_socket.connect(event_protocol)

        self._thread = Thread(target=self._run)
        self._queue_engine = QueueEventEngine()
        time.sleep(1)
Beispiel #5
0
 def _load_data(self, strpcons, dt_start, dt_end, n, spec_date):
     all_data = OrderedDict()     
     max_window = -1
     logger.info("loading data...")
     pbar = ProgressBar().start()
     for i, pcon  in enumerate(strpcons):
         #print "load data: %s" % pcon
         if pcon in spec_date:
             dt_start = spec_date[pcon][0]
             dt_end = spec_date[pcon][1]
         assert(dt_start < dt_end)
         if n:
             wrapper = self._data_manager.get_last_bars(pcon, n)
         else:
             wrapper = self._data_manager.get_bars(pcon, dt_start, dt_end)
         if len(wrapper) == 0:
             continue 
         all_data[pcon] = DataContext(wrapper)
         max_window = max(max_window, len(wrapper))
         pbar.update(i*100.0/len(strpcons))
         #progressbar.log('')
     if n:
         assert(max_window <= n) 
     pbar.finish()
     if len(all_data) == 0:
         assert(False)
         ## @TODO raise
     return all_data, max_window
Beispiel #6
0
 def _load_data(self, strpcons, dt_start, dt_end, n, spec_date):
     all_data = OrderedDict()
     max_window = -1
     logger.info("loading data...")
     pbar = ProgressBar().start()
     pcontracts = [PContract.from_string(s) for s in strpcons]
     pcontracts = sorted(pcontracts, reverse=True)
     for i, pcon in enumerate(pcontracts):
         strpcon = str(pcon)
         if strpcon in spec_date:
             dt_start = spec_date[strpcon][0]
             dt_end = spec_date[strpcon][1]
         assert(dt_start < dt_end)
         if n:
             wrapper = self._data_manager.get_last_bars(strpcon, n)
         else:
             wrapper = self._data_manager.get_bars(strpcon, dt_start, dt_end)
         if len(wrapper) == 0:
             continue
         all_data[strpcon] = DataContext(wrapper)
         max_window = max(max_window, len(wrapper))
         pbar.update(i*100.0/len(strpcons))
         # progressbar.log('')
     if n:
         assert(max_window <= n)
     pbar.finish()
     if len(all_data) == 0:
         assert(False)
         # @TODO raise
     return all_data, max_window
    def _run(self):
        """""" 
        poller = zmq.Poller()
        poller.register(self._client_recv_event_socket, zmq.POLLIN)
        if self._is_server:
            poller.register(self._server_recv_event_socket, zmq.POLLIN)
        logger.info('Run ZMQEventEngine...')
        while self._active:
            socks = dict(poller.poll(1))
            if self._client_recv_event_socket in socks and \
                    socks[self._client_recv_event_socket] == zmq.POLLIN:
                self._run_client()

            if self._is_server and  self._server_recv_event_socket in socks and \
                socks[self._server_recv_event_socket] == zmq.POLLIN:
                self._run_server()
        return
Beispiel #8
0
    def _run(self):
        """"""
        poller = zmq.Poller()
        poller.register(self._client_recv_event_socket, zmq.POLLIN)
        if self._is_server:
            poller.register(self._server_recv_event_socket, zmq.POLLIN)
        logger.info('Run ZMQEventEngine...')
        while self._active:
            socks = dict(poller.poll(1))
            if self._client_recv_event_socket in socks and \
                    socks[self._client_recv_event_socket] == zmq.POLLIN:
                self._run_client()

            if self._is_server and  self._server_recv_event_socket in socks and \
                socks[self._server_recv_event_socket] == zmq.POLLIN:
                self._run_server()
        return
Beispiel #9
0
    def __init__(self, event_protocol="tcp://127.0.0.1:5555", register_protocol="tcp://127.0.0.1:5557"):
        EventEngine.__init__(self)
        context = zmq.Context()  
        try:
            self._broadcast_event_socket = context.socket(zmq.PUB)  
            self._broadcast_event_socket.bind(event_protocol)  
            self._server_recv_event_socket = context.socket(zmq.PULL)
            self._server_recv_event_socket.bind(register_protocol)
            self._is_server = True
        except zmq.error.ZMQError:
            logger.info('start a ZMQEventEngine client')
            self._is_server = False

        self._emit_event_socket = context.socket(zmq.PUSH)  
        self._emit_event_socket.connect(register_protocol)  
        self._client_recv_event_socket = context.socket(zmq.SUB)  
        self._client_recv_event_socket.connect(event_protocol)  

        self._thread = Thread(target=self._run)
        self._queue_engine = QueueEventEngine()
        time.sleep(1)
Beispiel #10
0
 def _process_apiback(self, event):
     assert(event.route == self.EVENT_SERVER)
     self._timeout = 0
     rid = event.args['rid']
     try:
         with self._handlers_lock:
             handler = self._handlers[rid]
     except KeyError:
         logger.info('[RPCClient._process_apiback] 放弃超时任务的返回结果')
     else:
         try:
             if handler:
                 handler(event.args['ret'])
             else:
                 self._sync_ret = event.args['ret']
                 self._notify_server_data()
         except Exception as e:
             print e
         logger.debug("[RPCClient._process_apiback] 删除已经完成的任务 rid; %s" % rid)
         with self._handlers_lock:
             del self._handlers[rid]
Beispiel #11
0
 def _process_apiback(self, event):
     assert (event.route == self.EVENT_SERVER)
     self._timeout = 0
     rid = event.args['rid']
     try:
         with self._handlers_lock:
             handler = self._handlers[rid]
     except KeyError:
         logger.info('[RPCClient._process_apiback] 放弃超时任务的返回结果')
     else:
         try:
             if handler:
                 handler(event.args['ret'])
             else:
                 self._sync_ret = event.args['ret']
                 self._notify_server_data()
         except Exception as e:
             print e
         logger.debug("[RPCClient._process_apiback] 删除已经完成的任务 rid; %s" %
                      rid)
         with self._handlers_lock:
             del self._handlers[rid]
Beispiel #12
0
 def start(self):
     logger.info('start timer')
     self._timer_active = True
     self._timer.start()
Beispiel #13
0
 def run(self):
     # @TODO max_window 可用来显示回测进度
     # 初始化策略自定义时间序列变量
     logger.info("runing strategies...")
     self._init_strategies()
     pbar = ProgressBar().start()
     # todo 对单策略优化
     has_next = True
     # 遍历每个数据轮, 次数为数据的最大长度
     for pcon, data in self._all_data.iteritems():
         self.context.switch_to_pcontract(pcon)
         self.context.rolling_forward()
     while True:
         self.context.on_bar = False
         # 遍历数据轮的所有合约
         for pcon, data in self._all_data.iteritems():
             self.context.switch_to_pcontract(pcon)
             if self.context.time_aligned():
                 self.context.update_system_vars()
                 # 组合遍历
                 for i, combination in enumerate(self._combs):
                     # 策略遍历
                     for j, s in enumerate(combination):
                         self.context.switch_to_strategy(i, j)
                         self.context.update_user_vars()
                         s.on_symbol(self.context)
         # 确保单合约回测的默认值
         self.context.switch_to_pcontract(self._default_pcontract)
         self.context.on_bar = True
         # 遍历组合策略每轮数据的最后处理
         tick_test = settings['tick_test']
         for i, combination in enumerate(self._combs):
             # print self.context.ctx_datetime, "--"
             for j, s in enumerate(combination):
                 self.context.switch_to_strategy(i, j)
                 # 确保交易状态是基于开盘时间的。
                 self.context.process_trading_events(at_baropen=True)
                 s.on_bar(self.context)
                 if not tick_test:
                     # 保证有可能在当根Bar成交
                     self.context.process_trading_events(at_baropen=False)
         # print self.context.ctx_datetime
         self.context.ctx_datetime = datetime(2100, 1, 1)
         self.context.ctx_curbar += 1
         if self.context.ctx_curbar <= self._max_window:
             pbar.update(self.context.ctx_curbar * 100.0 / self._max_window)
         #
         toremove = []
         for pcon, data in self._all_data.iteritems():
             self.context.switch_to_pcontract(pcon)
             has_next = self.context.rolling_forward()
             if not has_next:
                 toremove.append(pcon)
         if toremove:
             for key in toremove:
                 del self._all_data[key]
             if len(self._all_data) == 0:
                 # 策略退出后的处理
                 for i, combination in enumerate(self._combs):
                     for j, s in enumerate(combination):
                         self.context.switch_to_strategy(i, j)
                         s.on_exit(self.context)
                 return
     pbar.finish()
Beispiel #14
0
 def run(self):
     ## @TODO max_window 可用来显示回测进度
     # 初始化策略自定义时间序列变量
     logger.info("runing strategies...")
     self._init_strategies()
     pbar = ProgressBar().start()
     # todo 对单策略优化
     has_next = True
     tick_test = settings['tick_test']
     # 遍历每个数据轮, 次数为数据的最大长度
     for pcon, data in self._all_data.iteritems():
         self.context.switch_to_contract(pcon)
         self.context.rolling_forward()
     while True:
         self.context.on_bar = False
         # 遍历数据轮的所有合约
         for pcon, data in self._all_data.iteritems():
             self.context.switch_to_contract(pcon)
             if self.context.time_aligned():
                 self.context.update_system_vars()
                 # 组合遍历
                 for i, combination in enumerate(self._combs):
                     # 策略遍历
                     for j, s in enumerate(combination):
                         self.context.switch_to_strategy(i, j)
                         self.context.update_user_vars()
                         s.on_symbol(self.context)
         ## 确保单合约回测的默认值
         self.context.switch_to_contract(self.pcontracts[0])
         self.context.on_bar = True
         # 遍历组合策略每轮数据的最后处理
         for i, combination in enumerate(self._combs):
             for j, s in enumerate(combination):
                 self.context.switch_to_strategy(i, j, True)
                 self.context.process_trading_events(at_baropen=True)
                 s.on_bar(self.context)
                 if not tick_test:
                     # 保证有可能在当根Bar成交
                     self.context.process_trading_events(at_baropen=False)
         #print self.context.ctx_datetime
         self.context.ctx_datetime = datetime(2100,1,1)
         self.context.step += 1
         if self.context.step <= self._max_window:
             pbar.update(self.context.step*100.0/self._max_window)
         # 
         toremove = []
         for pcon, data in self._all_data.iteritems():
             self.context.switch_to_contract(pcon)
             has_next = self.context.rolling_forward()
             if not has_next:
                 toremove.append(pcon)
         if toremove:
             for key in toremove:
                 del self._all_data[key]
             if len(self._all_data) == 0:
                 # 策略退出后的处理
                 for i, combination in enumerate(self._combs):
                     for j, s in enumerate(combination):
                         self.context.switch_to_strategy(i, j)
                         s.on_exit(self.context)
                 return
     pbar.finish()
Beispiel #15
0
 def start(self):
     logger.info('start timer')
     self._timer_active = True
     self._timer.start()