def rolling_forward(self): """ Retrieve data of next step """ self.has_pending_data, self._next_bar = self._helper.rolling_forward() if not self.has_pending_data: return False, None self.next_datetime = self._raw_data.index[self._next_bar] if self.datetime[0] >= self.next_datetime and self.curbar != 0: log.error('合约[%s] 数据时间逆序或冗余' % self.pcontract) raise return True, self.has_pending_data
def _process(self, event): """ 基于多线程的处理事件 """ if event.route not in self._routes: log.warning("事件%s 没有被处理" % event.route) return for handler in self._routes[event.route]: try: log.debug("处理事件%s" % event.route) _thread.start_new_thread(handler, (event, )) #handler(event) except Exception as e: log.error(e)
def __init__(self, str_contract): ## @TODO 修改参数为(code, exchange) info = str_contract.split('.') if len(info) == 2: code = info[0].upper() exchange = info[1].upper() else: log.error('错误的合约格式: %s' % str_contract) log.exception() self.exchange = exchange self.code = code if self.exchange == 'SZ' or self.exchange == 'SH': self.is_stock = True elif self.exchange == 'SHFE': self.is_stock = False elif self.exchange == 'TEST' and self.code == 'STOCK': self.is_stock = True elif self.exchange == 'TEST': self.is_stock = False else: log.error('Unknown exchange: {0}', self.exchange) assert (False)
def _process_apiback(self, event): assert (event.route == self.EVENT_FROM_SERVER) self._timeout = 0 rid = event.args['rid'] try: with self._handlers_lock: handler = self._handlers[rid] except KeyError: log.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: log.error(e) log.debug("[RPCClient._process_apiback] 删除已经完成的任务 rid; %s" % rid) with self._handlers_lock: del self._handlers[rid]
def make_market(self, bars, at_baropen): """ 价格撮合""" if len(self._open_orders) == 0: return fill_orders = set() for order in self._open_orders: if order.side == TradeSide.CANCEL: fill_orders.add(order) transact = Transaction(order) self.events.put(FillEvent(transact)) continue try: bar = bars[order.contract] except KeyError: log.error('所交易的合约[%s]数据不存在' % order.contract) continue transact = Transaction(order) if self._strict: if at_baropen: if order.price_type == PriceType.LMT: price = bar.open if (order.side == TradeSide.OPEN and \ (order.direction == Direction.LONG and order.price >= price or \ order.direction == Direction.SHORT and order.price <= price)) or \ (order.side == TradeSide.CLOSE and \ (order.direction == Direction.LONG and order.price <= price or \ order.direction == Direction.SHORT and order.price >= price)): transact.price = order.price transact.datetime = bar.datetime fill_orders.add(order) self.events.put(FillEvent(transact)) elif order.price_type == PriceType.MKT: transact.price = bar.open transact.datetime = bar.datetime # recompute commission when price changed transact.compute_commission() fill_orders.add(order) self.events.put(FillEvent(transact)) else: if order.price_type == PriceType.LMT: # 限价单以最高和最低价格为成交的判断条件. if (order.side == TradeSide.OPEN and \ (order.direction == Direction.LONG and order.price >= bar.low or \ order.direction == Direction.SHORT and order.price <= bar.high)) or \ (order.side == TradeSide.CLOSE and \ (order.direction == Direction.LONG and order.price <= bar.high or \ order.direction == Direction.SHORT and order.price >= bar.low)): transact.price = order.price # Bar的结束时间做为交易成交时间. transact.datetime = bar.datetime fill_orders.add(order) self.events.put(FillEvent(transact)) elif order.price_type == PriceType.MKT: # 市价单以最高或最低价格为成交价格. if order.side == TradeSide.OPEN: if order.direction == Direction.LONG: transact.price = bar.high else: transact.price = bar.low elif order.side == TradeSide.CLOSE: if order.direction == Direction.LONG: transact.price = bar.low else: transact.price = bar.high transact.datetime = bar.datetime # recompute commission when price changed transact.compute_commission() fill_orders.add(order) self.events.put(FillEvent(transact)) else: transact.datetime = bar.datetime fill_orders.add(order) # self.events.put(FillEvent(transact)) # end of for if fill_orders: self._open_orders -= fill_orders