def on_order(self, order: OrderData): """ Callback of new order data update. """ # 移除成交或撤销的订单 if not order.is_active() and order.vt_orderid in self.vt_orderids: self.vt_orderids.remove(order.vt_orderid)
def on_order(self, order: OrderData): """ 判断是否完全成交,如果成交获取成交数量,计数器加减 移除成交、拒绝、撤单的订单号 """ if order.vt_orderid in self.vt_orderids: if order.status == Status.ALLTRADED: if order.direction == Direction.LONG: self.target_pos = order.traded self.grid_count += 1 self.cumulative_usdt_volume += self.grid_usdt_volume # 买入时,实际得到币量为总买量减手续费 msg = f"开仓,成交量为:{self.target_pos}" self.write_log(msg) self.target_pos = 0 else: self.target_pos = -order.traded self.grid_count -= 1 self.cumulative_usdt_volume -= self.grid_usdt_volume msg = f"平仓,成交量为:{self.target_pos}" self.write_log(msg) self.target_pos = 0 if not order.is_active(): self.vt_orderids.remove(order.vt_orderid) self.sync_data()
def on_order(self, order: OrderData): """ Callback of new order data update. """ if not order.is_active(): self.vt_orderid = "" self.put_variables_event()
def on_order(self, order: OrderData): """""" on_order_time = datetime.now().time() self.write_log(f"on_order, {order.orderid} {order.status} {order.offset} {order.direction}, on_order_time:{on_order_time}") # ACTIVE_STATUSES = set([Status.SUBMITTING, Status.NOTTRADED, Status.PARTTRADED]) if order.is_active(): return # not ACTIVE_STATUSES = set([Status.ALLTRADED, Status.CANCELLED, Status.REJECTED]) if ((self.day_start <= self.on_bar_time < self.clearance_time) or (self.night_start <= self.on_bar_time <= self.night_end)): if order.status in [Status.CANCELLED, Status.REJECTED]: pos = copy.deepcopy(self.pos) if self.pos == 0: if self.ultosc > self.buy_dis: if not self.cta_engine.strategy_orderid_map[self.strategy_name]: self.buy(self.boll_up, self.trading_size, True) self.write_log(f"on_order, buy_svt:{list(self.cta_engine.strategy_orderid_map[self.strategy_name])}, volume:{self.trading_size}") elif self.ultosc < self.short_dis: if not self.cta_engine.strategy_orderid_map[self.strategy_name]: self.short(self.boll_down, self.trading_size, True) self.write_log(f"on_order, short_svt:{list(self.cta_engine.strategy_orderid_map[self.strategy_name])}, volume:{self.trading_size}") elif self.pos > 0: if not self.cta_engine.strategy_orderid_map[self.strategy_name]: self.sell(self.long_stop, abs(self.pos), True) self.write_log(f"on_order, sell_svt:{list(self.cta_engine.strategy_orderid_map[self.strategy_name])}, volume:{pos}") else: if not self.cta_engine.strategy_orderid_map[self.strategy_name]: self.cover(self.short_stop, abs(self.pos), True) self.write_log(f"on_order, cover_svt:{list(self.cta_engine.strategy_orderid_map[self.strategy_name])}, volume:{pos}") elif (self.clearance_time <= self.on_bar_time <= self.liq_time): if order.status in [Status.CANCELLED, Status.REJECTED]: pos = copy.deepcopy(self.pos) if self.pos > 0: if not self.cta_engine.strategy_orderid_map[self.strategy_name]: self.sell(self.liq_price - 5, abs(self.pos)) self.write_log(f"clearance time, on_order, sell volume:{pos}, on_order_time:{on_order_time}") elif self.pos < 0: if not self.cta_engine.strategy_orderid_map[self.strategy_name]: self.cover(self.liq_price + 5, abs(self.pos)) self.write_log(f"clearance time, on_order, cover volume:{pos}, time:{on_order_time}") self.put_event()
def on_order(self, order: OrderData): """ Callback of new order data update. """ # 只有实盘交易才使用BestLimit算法 if self.engine_type != EngineType.LIVE: return if not order.is_active() and order.vt_orderid in self.vt_orderids: self.vt_orderids.remove(order.vt_orderid) if not self.vt_orderids: self.order_price = 0
def on_order(self, order: OrderData): """""" self.write_log( f"on_order {order.orderid} {order.status} {order.offset} {order.direction}" ) # ACTIVE_STATUSES = set([Status.SUBMITTING, Status.NOTTRADED, Status.PARTTRADED]) if order.is_active(): return # not ACTIVE_STATUSES = set([Status.ALLTRADED, Status.CANCELLED, Status.REJECTED]) for buf_orderids in [ self.buy_lvt_orderids, self.sell_lvt_orderids, self.short_lvt_orderids, self.cover_lvt_orderids ]: if order.orderid in buf_orderids: buf_orderids.remove(order.orderid) print( f"on_order从buf_orderids:{buf_orderids}中移除{order.orderid}") if order.orderid in self.lvt_orderids: self.lvt_orderids.remove(order.orderid) print( f"on_order从lvt_orderids:{self.lvt_orderids}中移除{order.orderid}") self.current_time = datetime.now().time() if ((self.current_time >= self.day_start and self.current_time < self.day_end) or (self.current_time >= self.night_start and self.current_time <= self.night_end)): if order.status in [Status.CANCELLED, Status.REJECTED]: if self.pos == 0: if self.ultosc > self.buy_dis: if not self.svt_orderids and not self.lvt_orderids: self.buy_svt_orderids = self.buy( self.boll_up, self.trading_size, True) self.svt_orderids.extend(self.buy_svt_orderids) self.write_log( f"on_order开多仓,停止单号:{self.buy_svt_orderids},委托手数:{self.trading_size}" ) elif self.ultosc < self.short_dis: if not self.svt_orderids and not self.lvt_orderids: self.short_svt_orderids = self.short( self.boll_down, self.trading_size, True) self.svt_orderids.extend(self.short_svt_orderids) self.write_log( f"on_order开空仓,停止单号:{self.short_svt_orderids},委托手数:{self.trading_size}" ) elif self.pos > 0: if not self.sell_svt_orderids and not self.sell_lvt_orderids: self.sell_svt_orderids = self.sell( self.long_stop, abs(self.pos), True) self.write_log( f"on_order平多仓,停止单号:{self.sell_svt_orderids},委托手数:{abs(self.pos)}" ) else: if not self.cover_svt_orderids and not self.cover_lvt_orderids: self.cover_svt_orderids = self.cover( self.short_stop, abs(self.pos), True) self.write_log( f"on_order平空仓,停止单号:{self.cover_svt_orderids},委托手数:{abs(self.pos)}" ) elif self.current_time >= self.day_end and self.current_time <= self.liq_time: for buf_orderids in [ self.sell_vt_orderids, self.cover_vt_orderids ]: if order.orderid in buf_orderids: buf_orderids.remove(order.orderid) self.write_log( f"清仓时段on_order从buf_orderids:{buf_orderids}中移除{order.orderid}" ) if order.status in [Status.CANCELLED, Status.REJECTED]: if self.pos > 0: if not self.sell_vt_orderids: self.sell_vt_orderids = self.sell( bar.close_price - 5, abs(self.pos)) self.write_log( f"清仓时段on_order平多仓,限价单号:{self.sell_vt_orderids},委托手数:{abs(self.pos)}" ) elif self.pos < 0: if not self.cover_vt_orderids: self.cover_vt_orderids = self.cover( bar.close_price + 5, abs(self.pos)) self.write_log( f"清仓时段on_order平空仓,限价单号:{self.cover_vt_orderids},委托手数:{abs(self.pos)}" ) self.put_event()