def on_order(self, d): """""" order = OrderData( symbol=d["instrument_id"], exchange=Exchange.OKEX, type=ORDERTYPE_OKEX2VT[d["type"]], orderid=d["client_oid"], direction=DIRECTION_OKEX2VT[d["side"]], price=float(d["price"]), volume=float(d["size"]), traded=float(d["filled_size"]), datetime=generate_datetime(d["timestamp"]), status=STATUS_OKEX2VT[d["status"]], gateway_name=self.gateway_name, ) self.gateway.on_order(copy(order)) trade_volume = d.get("last_fill_qty", 0) if not trade_volume or float(trade_volume) == 0: return self.trade_count += 1 tradeid = f"{self.connect_time}{self.trade_count}" trade = TradeData(symbol=order.symbol, exchange=order.exchange, orderid=order.orderid, tradeid=tradeid, direction=order.direction, price=float(d["last_fill_px"]), volume=float(trade_volume), datetime=generate_datetime(d["last_fill_time"]), gateway_name=self.gateway_name) self.gateway.on_trade(trade)
def on_order(self, data: dict) -> None: """""" orderid = data["clientOrderId"] order = self.gateway.get_order(orderid) if not order: return traded_volume = float(data.get("tradeVolume", 0)) # Push order event order.traded += traded_volume order.status = STATUS_HUOBI2VT.get(data["orderStatus"], None) self.gateway.on_order(order) # Push trade event if not traded_volume: return trade = TradeData( symbol=order.symbol, exchange=Exchange.HUOBI, orderid=order.orderid, tradeid=str(data["tradeId"]), direction=order.direction, price=float(data["tradePrice"]), volume=float(data["tradeVolume"]), datetime=datetime.now(CHINA_TZ), gateway_name=self.gateway_name, ) self.gateway.on_trade(trade)
def cross_limit_order(self) -> None: """ Cross limit order with last bar/tick data. """ for order in list(self.active_limit_orders.values()): bar = self.bars[order.vt_symbol] long_cross_price = bar.low_price short_cross_price = bar.high_price long_best_price = bar.open_price short_best_price = bar.open_price # Push order update with status "not traded" (pending). if order.status == Status.SUBMITTING: order.status = Status.NOTTRADED self.strategy.update_order(order) # Check whether limit orders can be filled. long_cross = (order.direction == Direction.LONG and order.price >= long_cross_price and long_cross_price > 0) short_cross = (order.direction == Direction.SHORT and order.price <= short_cross_price and short_cross_price > 0) if not long_cross and not short_cross: continue # Push order update with status "all traded" (filled). order.traded = order.volume order.status = Status.ALLTRADED self.strategy.update_order(order) self.active_limit_orders.pop(order.vt_orderid) # Push trade update self.trade_count += 1 if long_cross: trade_price = min(order.price, long_best_price) else: trade_price = max(order.price, short_best_price) trade = TradeData( symbol=order.symbol, exchange=order.exchange, orderid=order.orderid, tradeid=str(self.trade_count), direction=order.direction, offset=order.offset, price=trade_price, volume=order.volume, datetime=self.datetime, gateway_name=self.gateway_name, ) self.strategy.update_trade(trade) self.trades[trade.vt_tradeid] = trade
def cross_order(self, order: OrderData, tick: TickData): """""" contract = self.main_engine.get_contract(order.vt_symbol) trade_price = 0 # Cross market order immediately after received if order.type == OrderType.MARKET: if order.direction == Direction.LONG: trade_price = tick.ask_price_1 + self.trade_slippage * contract.pricetick else: trade_price = tick.bid_price_1 - self.trade_slippage * contract.pricetick # Cross limit order only if price touched elif order.type == OrderType.LIMIT: if order.direction == Direction.LONG: if order.price >= tick.ask_price_1: trade_price = tick.ask_price_1 else: if order.price <= tick.bid_price_1: trade_price = tick.bid_price_1 # Cross limit order only if price broken elif order.type == OrderType.STOP: if order.direction == Direction.LONG: if tick.ask_price_1 >= order.price: trade_price = tick.ask_price_1 + self.trade_slippage * contract.pricetick else: if tick.bid_price_1 <= order.price: trade_price = tick.bid_price_1 - self.trade_slippage * contract.pricetick if trade_price: order.status = Status.ALLTRADED order.traded = order.volume self.put_event(EVENT_ORDER, order) trade = TradeData(symbol=order.symbol, exchange=order.exchange, orderid=order.orderid, tradeid=order.orderid, direction=order.direction, offset=order.offset, price=trade_price, volume=order.volume, datetime=datetime.now(LOCAL_TZ), gateway_name=order.gateway_name) self.put_event(EVENT_TRADE, trade) self.update_position(trade, contract)
def on_order(self, data: dict) -> None: """""" dt = generate_datetime(data["created_at"] / 1000) if data["client_order_id"]: orderid = data["client_order_id"] else: orderid = data["order_id"] order = OrderData(symbol=data["contract_code"], exchange=Exchange.HUOBI, orderid=orderid, type=ORDERTYPE_HUOBIS2VT[data["order_price_type"]], direction=DIRECTION_HUOBIS2VT[data["direction"]], offset=OFFSET_HUOBIS2VT[data["offset"]], price=data["price"], volume=data["volume"], traded=data["trade_volume"], status=STATUS_HUOBIS2VT[data["status"]], datetime=dt, gateway_name=self.gateway_name) self.gateway.on_order(order) # Push trade event trades = data["trade"] if not trades: return for d in trades: dt = generate_datetime(d["created_at"] / 1000) trade = TradeData( symbol=order.symbol, exchange=Exchange.HUOBI, orderid=order.orderid, tradeid=str(d["id"]), direction=order.direction, offset=order.offset, price=d["trade_price"], volume=d["trade_volume"], datetime=dt, gateway_name=self.gateway_name, ) self.gateway.on_trade(trade)
def on_order(self, packet: dict) -> None: """""" ord_data = packet["o"] key = (ord_data["o"], ord_data["f"]) order_type = ORDERTYPE_BINANCES2VT.get(key, None) if not order_type: return order = OrderData( symbol=ord_data["s"], exchange=Exchange.BINANCE, orderid=str(ord_data["c"]), type=order_type, direction=DIRECTION_BINANCES2VT[ord_data["S"]], price=float(ord_data["p"]), volume=float(ord_data["q"]), traded=float(ord_data["z"]), last_traded_volume=float(ord_data["l"]), status=STATUS_BINANCES2VT[ord_data["X"]], datetime=generate_datetime(packet["E"]), gateway_name=self.gateway_name ) # Push trade event trade_volume = float(ord_data["l"]) if trade_volume: trade_data = TradeData( symbol=order.symbol, exchange=order.exchange, orderid=order.orderid, tradeid=ord_data["t"], direction=order.direction, price=float(ord_data["L"]), volume=trade_volume, datetime=generate_datetime(ord_data["T"]), gateway_name=self.gateway_name, ) order.trade_data = trade_data else: order.trade_data = None self.gateway.on_order(order)
def on_order(self, packet: dict): """""" if packet["C"] == "": orderid = packet["c"] else: orderid = packet["C"] order = OrderData( symbol=packet["s"].lower(), exchange=Exchange.BINANCE, orderid=orderid, type=ORDERTYPE_BINANCE2VT[packet["o"]], direction=DIRECTION_BINANCE2VT[packet["S"]], price=float(packet["p"]), volume=float(packet["q"]), traded=float(packet["z"]), status=STATUS_BINANCE2VT[packet["X"]], datetime=generate_datetime(packet["O"]), gateway_name=self.gateway_name ) # Push trade event trade_volume = float(packet["l"]) if trade_volume: trade_data = TradeData( symbol=order.symbol, exchange=order.exchange, orderid=order.orderid, tradeid=packet["t"], direction=order.direction, price=float(packet["L"]), volume=trade_volume, datetime=generate_datetime(packet["T"]), gateway_name=self.gateway_name, ) order.trade_data = trade_data else: order.trade_data = None self.gateway.on_order(order)
def on_trade(self, l: List, t: int): """""" d = l[0] sys_orderid = d["order_id"] order = self.order_manager.get_order_with_sys_orderid(sys_orderid) if not order: return trade = TradeData( symbol=order.symbol, exchange=order.exchange, orderid=order.orderid, tradeid=d["id"], direction=order.direction, price=float(d["price"]), volume=abs(d["size"]), datetime=generate_datetime(d["create_time"]), gateway_name=self.gateway_name, ) self.gateway.on_trade(trade)
def on_order(self, data: dict) -> None: """""" direction = SIDE_OKEXO2VT[data["side"]] order = OrderData( symbol=data["instrument_id"], exchange=Exchange.OKEX, type=ORDERTYPE_OKEXO2VT[data["order_type"]], orderid=data["client_oid"], direction=direction, price=float(data["price"]), volume=float(data["size"]), traded=float(data["filled_qty"]), datetime=generate_datetime(data["timestamp"]), status=STATE_OKEXO2VT[data["state"]], gateway_name=self.gateway_name, ) self.gateway.on_order(copy(order)) trade_volume = data.get("last_fill_qty", 0) if not trade_volume or float(trade_volume) == 0: return self.trade_count += 1 tradeid = f"{self.connect_time}{self.trade_count}" trade = TradeData( symbol=order.symbol, exchange=order.exchange, orderid=order.orderid, tradeid=tradeid, direction=order.direction, offset=order.offset, price=float(data["last_fill_px"]), volume=float(trade_volume), datetime=order.datetime, gateway_name=self.gateway_name, ) self.gateway.on_trade(trade)
def on_order(self, data): """""" order = _parse_order_info(data, gateway_name=self.gateway_name) self.gateway.on_order(copy(order)) traded_volume = float(data.get("last_fill_qty", 0)) if traded_volume != 0: tradeid = f"{self.connect_time}{self._last_trade_id}" self._last_trade_id += 1 trade = TradeData( symbol=order.symbol, exchange=order.exchange, orderid=order.orderid, tradeid=tradeid, direction=order.direction, offset=order.offset, price=float(data["last_fill_px"]), volume=float(traded_volume), datetime=order.datetime, gateway_name=self.gateway_name, ) self.gateway.on_trade(trade)
def cross_stop_order(self): """ Cross stop order with last bar/tick data. """ if self.mode == BacktestingMode.BAR: long_cross_price = self.bar.high_price short_cross_price = self.bar.low_price long_best_price = self.bar.open_price short_best_price = self.bar.open_price else: long_cross_price = self.tick.last_price short_cross_price = self.tick.last_price long_best_price = long_cross_price short_best_price = short_cross_price for stop_order in list(self.active_stop_orders.values()): # Check whether stop order can be triggered. long_cross = ( stop_order.direction == Direction.LONG and stop_order.price <= long_cross_price ) short_cross = ( stop_order.direction == Direction.SHORT and stop_order.price >= short_cross_price ) if not long_cross and not short_cross: continue # Create order data. self.limit_order_count += 1 order = OrderData( symbol=self.symbol, exchange=self.exchange, orderid=str(self.limit_order_count), direction=stop_order.direction, offset=stop_order.offset, price=stop_order.price, volume=stop_order.volume, traded=stop_order.volume, status=Status.ALLTRADED, gateway_name=self.gateway_name, datetime=self.datetime ) self.limit_orders[order.vt_orderid] = order # Create trade data. if long_cross: trade_price = max(stop_order.price, long_best_price) pos_change = order.volume else: trade_price = min(stop_order.price, short_best_price) pos_change = -order.volume self.trade_count += 1 trade = TradeData( symbol=order.symbol, exchange=order.exchange, orderid=order.orderid, tradeid=str(self.trade_count), direction=order.direction, offset=order.offset, price=trade_price, volume=order.volume, datetime=self.datetime, gateway_name=self.gateway_name, ) self.trades[trade.vt_tradeid] = trade # Update stop order. stop_order.vt_orderids.append(order.vt_orderid) stop_order.status = StopOrderStatus.TRIGGERED if stop_order.stop_orderid in self.active_stop_orders: self.active_stop_orders.pop(stop_order.stop_orderid) # Push update to strategy. self.strategy.on_stop_order(stop_order) self.strategy.on_order(order) self.strategy.pos += pos_change self.strategy.on_trade(trade)
def cross_algo(self): """ Cross limit order with last bar/tick data. """ if self.mode == BacktestingMode.BAR: long_cross_price = self.bar.close_price short_cross_price = self.bar.close_price else: long_cross_price = self.tick.ask_price_1 short_cross_price = self.tick.bid_price_1 for algo in list(self.active_algos.values()): # Check whether limit orders can be filled. long_cross = (algo.direction == Direction.LONG and algo.price >= long_cross_price) short_cross = (algo.direction == Direction.SHORT and algo.price <= short_cross_price) if not long_cross and not short_cross: continue # Push order udpate with status "all traded" (filled). algo.traded = algo.volume algo.status = Status.ALLTRADED self.strategy.update_spread_algo(algo) self.active_algos.pop(algo.algoid) # Push trade update self.trade_count += 1 if long_cross: trade_price = long_cross_price pos_change = algo.volume else: trade_price = short_cross_price pos_change = -algo.volume trade = TradeData( symbol=self.spread.name, exchange=Exchange.LOCAL, orderid=algo.algoid, tradeid=str(self.trade_count), direction=algo.direction, offset=algo.offset, price=trade_price, volume=algo.volume, datetime=self.datetime, gateway_name=self.gateway_name, ) if self.mode == BacktestingMode.BAR: trade.value = self.bar.value else: trade.value = trade_price self.spread.net_pos += pos_change self.strategy.on_spread_pos() self.trades[trade.vt_tradeid] = trade