def test_order_trade_task(): from jaqs.data.basic import TradeStat, Trade, Task, TaskInd, Order, OrderStatusInd order = Order() order.symbol = 'SPY' order.task_id = 10000001 order.entrust_no = '123' order.entrust_size = 10 order.entrust_action = 'Short' order.entrust_price = 1.2 order.entrust_time = 95055 order.entrust_date = 20171201 order.fill_price = 1.19 order.fill_size = 3 order.commission = 0.001 str(order) o2 = Order(order) o2.entrust_no = '124' o3 = Order.new_order('SPY', 'Buy', 10, 10, 20111111, 143029, 'Limit') oind = OrderStatusInd(order) OrderStatusInd.create_from_dict({'symbol': 'SPY'}) str(oind) task = Task(order.task_id, 'vwap', {'a': 'b'}, order, 'place_order', order.entrust_date) assert (not task.is_finished) task.task_status = common.TASK_STATUS.DONE assert task.is_finished tind = TaskInd(task.task_id, task.task_status, task.algo, 'success') str(tind) tind2 = TaskInd.create_from_dict({'task_id': 2011223}) trade = Trade(order) trade.set_fill_info(15, 20, 20171202, 112311, 12345) str(trade) t2 = Trade.create_from_dict({'symbol': 'SPY'}) tstat = TradeStat() str(tstat)
def _on_order_status(self, ind): """ Parameters ---------- ind : OrderStatusInd """ if ind.task_id not in self.tasks: return # TODO if ind.entrust_no == 101010 or ind.entrust_no == 202020: # trades generate by system return # add/update order order = self.orders.get(ind.entrust_no, None) if order is None: order = Order() order.copy(ind) self.orders[ind.entrust_no] = order task = self.get_task(ind.task_id) if task.function_name == 'place_order': order = task.data order.copy(ind) elif task.function_name == 'place_batch_order': for order in task.data: if order.entrust_no == ind.entrust_no: order.copy(ind) elif task.function_name == 'goal_portfolio': for order in task.data: if order.entrust_no == ind.entrust_no: order.copy(ind) # order status other than CANCELLED/REJECTED will be dealt with self.on_trade if (ind.order_status == common.ORDER_STATUS.CANCELLED) or (ind.order_status == common.ORDER_STATUS.REJECTED): # update TradeStat trade_stat = self._get_trade_stat(ind.symbol) release_size = ind.entrust_size - ind.fill_size if common.ORDER_ACTION.is_positive(ind.entrust_action): trade_stat.buy_want_size -= release_size else: trade_stat.sell_want_size -= release_size self.tradestat[self._make_trade_stat_key(ind.symbol)] = trade_stat # update task self._update_task_if_done(ind.task_id) """ entrust_no = ind.entrust_no order = self.orders.get(self._make_order_key(entrust_no, self.ctx.strategy.ctx.trade_date), None) if order is not None: order.order_status = ind.order_status tradestat = self.tradestat.get(ind.symbol) release_size = ind.entrust_size - ind.fill_size if ind.entrust_action == common.ORDER_ACTION.BUY: tradestat.buy_want_size -= release_size else: tradestat.sell_want_size -= release_size else: raise ValueError("order {} does not exist".format(entrust_no)) """ self.original_on_order_status(ind)