def _generate_trade_data_from_order(self, order_data: OrderData): """ 将orderdata转换成成交单 """ p = TradeData(price=order_data.price, istraded=order_data.volume, volume=order_data.volume, trade_id=uuid.uuid1(), gateway_name=order_data.gateway_name, time=order_data.time, order_id=order_data.order_id) return p
def _generate_trade_data_from_order(self, order_data: OrderData): """ 将orderdata转换成成交单 """ p = TradeData(price=order_data.price, istraded=order_data.volume, volume=order_data.volume, tradeid=str(uuid.uuid1()), offset=order_data.offset, direction=order_data.direction, gateway_name=order_data.gateway_name, order_time=order_data.time, time=self.datetime, order_id=order_data.order_id, symbol=order_data.symbol, exchange=order_data.exchange) return p
def match_deal(self, data: OrderData): """ 撮合成交 """ if self.parmas.get("deal_pattern") == "match": """ 撮合成交 """ # todo: 是否可以模拟一定量的市场冲击响应? 以靠近更加逼真的回测效果 ???? elif self.parmas.get("deal_pattern") == "price": """ 见价成交 """ # 先判断价格和手数是否满足限制条件 if data.volume > self.parmas.get( "single_order_limit" ) or self.today_volume > self.parmas.get("single_day_limit"): """ 超出限制 直接返回不允许成交 """ return if data.price < self.drop_price or data.price > self.upper_price: """ 超出涨跌价格 """ return """ 判断账户资金是否足以支撑成交 """ if self.account.is_traded(data): """ 生成成交单 """ p = TradeData(price=data.price, istraded=data.volume, volume=data.volume, trade_id=uuid.uuid1(), gateway_name=data.gateway_name, time=data.time, order_id=data.order_id) self.account.update_trade(p) # 调用strategy的on_trade self.strategy.on_trade(p) self.today_volume += data.volume else: """ 当前账户不足以支撑成交 """ self.logger.error("资金不足啦!") return else: raise TypeError("未支持的成交机制")
def _cal_whether_traded(self, order: OrderData, tick: TickData) -> TradeData or None: """ 计算是否进行成交, 这个地方需要用到lru缓存来提升计算性能 :param order:报单 :param tick:当前行情 :return: 成交单或者空 """ if tick.ask_price_1 and tick.bid_price_1: pass if tick.ask_price_1 >= tick.bid_price_1: # 撮合成交 return TradeData( direction=order.direction, price=order.price, symbol=order.symbol, offset=order.offset, type=order.type, time=order.time, ) pass else: return None
def onRtnTrade(self, data: dict): """ Callback of trade status update. """ symbol = data["InstrumentID"] exchange = symbol_exchange_map.get(symbol, "") if not exchange: self.trade_data.append(data) return order_id = self.sysid_orderid_map[data["OrderSysID"]] trade = TradeData(symbol=symbol, exchange=exchange, order_id=order_id, tradeid=data["TradeID"], direction=DIRECTION_CTP2VT[data["Direction"]], offset=OFFSET_CTP2VT[data["OffsetFlag"]], price=data["Price"], volume=data["Volume"], time=data["TradeTime"], gateway_name=self.gateway_name) self.on_event(type=EVENT_TRADE, data=trade)
def on_trade(self, trade: TradeData) -> None: trades = [] for trade in self.app.recorder.get_all_trades(): trades.append(trade._to_dict()) data = {"type": "trade", "data": trades} self.io.emit('trade', data)
def on_trade(self, trade: TradeData) -> None: data = { "type": "trade", "data": trade._to_dict() } self.io.emit('trade', data)