def run(self, event): if event.event_type is BROKER_EVENT.RECEIVE_ORDER: # 此时的message应该是订单类 """ OrderHandler 收到订单 orderhandler 调控转发给broker broker返回发单结果(成功/失败) orderhandler.order_queue 插入一个订单 执行回调 """ order = event.order # print(event.broker) order = event.broker.receive_order( QA_Event(event_type=BROKER_EVENT.TRADE, order=event.order)) # print(threading.current_thread().ident) order = self.order_queue.insert_order(order) if event.callback: event.callback(order) elif event.event_type is BROKER_EVENT.TRADE: # 实盘和本地 同步执行 res = [] for order in self.order_queue.pending: result = event.broker.query_orders(order.account_cookie, order.realorder_id) self.order_queue.set_status(order.order_id, result['header']['status']) if order.callback: order.callback(result) res.append(result) event.res = res return event elif event.event_type is BROKER_EVENT.SETTLE: self.order_queue.settle() elif event.event_type is MARKET_EVENT.QUERY_ORDER: """query_order和query_deal 需要联动使用 query_order 得到所有的订单列表 query_deal 判断订单状态--> 运行callback函数 实盘涉及到外部订单问题: 及 订单的来源 不完全从QUANTAXIS中发出, 则QA无法记录来源 (标记为外部订单) """ if self.if_start_orderquery: # if QA_util_if_tradetime(datetime.datetime.now()): # print(event.broker) # print(event.account_cookie) try: # 做一些容错处理 res = [ event.broker[i].query_orders(event.account_cookie[i], '') for i in range(len(event.account_cookie)) ] res = pd.concat(res, axis=0) if len(res) > 0 else None except: time.sleep(1) self.order_status = res if res is not None else self.order_status if len(self.order_status) > 0: QA_SU_save_order(self.order_status) # else: # time.sleep(1) # 这里加入随机的睡眠时间 以免被发现固定的刷新请求 # event=event event.event_type = MARKET_EVENT.QUERY_DEAL if event.event_queue.qsize() < 1: time.sleep(random.randint(1, 2)) # 非阻塞 event.event_queue.put( QA_Task(worker=self, engine='ORDER', event=event)) # time.sleep(random.randint(2,5)) # print(event.event_type) # print(event2.event_type) # self.run(event) # print(self.order_status) # print('UPDATE ORDERS') elif event.event_type is MARKET_EVENT.QUERY_DEAL: """order_handler- query_deal 将order_handler订单队列中的订单---和deal中匹配起来 """ # if len(self.order_queue.pending) > 0: # for order in self.order_queue.pending: # #self.query # waiting_realorder_id = [ # order.realorder_id for order in self.order_queue.trade_list] # result = event.broker.query_deal # time.sleep(1) if self.if_start_orderquery: self.deal_status = [ event.broker[i].query_orders(event.account_cookie[i], 'filled') for i in range(len(event.account_cookie)) ] # print(self.order_status) self.deal_status = pd.concat( self.deal_status, axis=0) if len(self.deal_status) > 0 else pd.DataFrame() if len(self.deal_status) > 0: QA_SU_save_deal(self.deal_status) # print(self.order_status) # 这里加入随机的睡眠时间 以免被发现固定的刷新请求 if event.event_queue.qsize() < 1: time.sleep(random.randint(2, 5)) event.event_type = MARKET_EVENT.QUERY_ORDER event.event_queue.put( QA_Task(worker=self, engine='ORDER', event=event)) # self.run(event) # self.run(event) elif event.event_type is MARKET_EVENT.QUERY_POSITION: pass
def run(self, event): if event.event_type is BROKER_EVENT.RECEIVE_ORDER: # 此时的message应该是订单类 """ OrderHandler 收到订单 orderhandler 调控转发给broker broker返回发单结果(成功/失败) orderhandler.order_queue 插入一个订单 执行回调 """ order = event.order order = event.broker.receive_order( QA_Event(event_type=BROKER_EVENT.TRADE, order=event.order, market_data=event.market_data)) order = self.order_queue.insert_order(order) if event.callback: event.callback(order) elif event.event_type is BROKER_EVENT.TRADE: # 实盘和本地 同步执行 self._trade() # event.event_queue.task_done() elif event.event_type is BROKER_EVENT.SETTLE: """订单队列的结算: 当队列中的订单都被处理过后: 算可以结算了 """ print('SETTLE ORDERHANDLER') self.if_start_orderquery = False if len(self.order_queue.pending) > 0: pass else: raise RuntimeWarning('ORDERHANDLER STILL HAVE UNTRADE ORDERS') self.order_queue.settle() self.order_status = pd.DataFrame() self.deal_status = pd.DataFrame() try: event.event_queue.task_done() except: pass elif event.event_type is BROKER_EVENT.NEXT_TRADEDAY: """下一个交易日 """ self.if_start_orderquery = True if self.if_start_orderquery: event.event_queue.put( QA_Task(worker=self, engine='ORDER', event=event)) elif event.event_type is MARKET_EVENT.QUERY_ORDER: """query_order和query_deal 需要联动使用 query_order 得到所有的订单列表 query_deal 判断订单状态--> 运行callback函数 实盘涉及到外部订单问题: 及 订单的来源 不完全从QUANTAXIS中发出, 则QA无法记录来源 (标记为外部订单) """ if self.if_start_orderquery: try: # 做一些容错处理 res = [ self.monitor[account].query_orders( account.account_cookie, '') for account in list(self.monitor.keys()) ] res = pd.concat(res, axis=0) if len(res) > 0 else None except: time.sleep(1) self.order_status = res if res is not None else self.order_status if len(self.order_status) > 0: QA_SU_save_order(self.order_status) # 这里加入随机的睡眠时间 以免被发现固定的刷新请求 event.event_type = MARKET_EVENT.QUERY_DEAL if event.event_queue.qsize() < 1: time.sleep(random.randint(1, 2)) # 非阻塞 if self.if_start_orderquery: event.event_queue.put( QA_Task(worker=self, engine='ORDER', event=event)) elif event.event_type is MARKET_EVENT.QUERY_DEAL: """order_handler- query_deal 将order_handler订单队列中的订单---和deal中匹配起来 """ if self.if_start_orderquery: res = [ self.monitor[account].query_orders(account.account_cookie, 'filled') for account in list(self.monitor.keys()) ] try: res = pd.concat( res, axis=0) if len(res) > 0 else pd.DataFrame() except: res = None self.deal_status = res if res is not None else self.deal_status if len(self.deal_status) > 0: QA_SU_save_deal(self.deal_status) # 检查pending订单, 更新订单状态 try: for order in self.order_queue.pending: if len(self.deal_status) > 0: if order.realorder_id in self.deal_status.index.levels[ 1]: # 此时有成交推送(但可能是多条) res = self.deal_status.loc[order.account_cookie, order.realorder_id] if isinstance(res, pd.Series): order.trade(str(res.trade_id), float(res.trade_price), int(res.trade_amount), str(res.trade_time)) elif isinstance(res, pd.DataFrame): if len(res) == 0: pass elif len(res) == 1: res = res.iloc[0] order.trade(str(res.trade_id), float(res.trade_price), int(res.trade_amount), str(res.trade_time)) else: for _, deal in res.iterrows: order.trade(str(deal.trade_id), float(deal.trade_price), int(deal.trade_amount), str(deal.trade_time)) except Exception as e: print(e) print(self.order_queue.order_list) print(self.deal_status.index) print(self.order_status) # event.event_queue.task_done() # 这里加入随机的睡眠时间 以免被发现固定的刷新请求 if event.event_queue.qsize() < 1: time.sleep(random.randint(2, 5)) event.event_type = MARKET_EVENT.QUERY_ORDER if self.if_start_orderquery: event.event_queue.put( QA_Task(worker=self, engine='ORDER', event=event)) elif event.event_type is MARKET_EVENT.QUERY_POSITION: pass
def run(self, event): if event.event_type is BROKER_EVENT.RECEIVE_ORDER: # 此时的message应该是订单类 """ OrderHandler 收到订单 orderhandler 调控转发给broker broker返回发单结果(成功/失败) orderhandler.order_queue 插入一个订单 执行回调 """ order = event.order order = event.broker.receive_order( QA_Event(event_type=BROKER_EVENT.TRADE, order=event.order, market_data=event.market_data)) # print(threading.current_thread().ident) order = self.order_queue.insert_order(order) if event.callback: event.callback(order) elif event.event_type is BROKER_EVENT.TRADE: # 实盘和本地 同步执行 self._trade() # event.event_queue.task_done() elif event.event_type is BROKER_EVENT.SETTLE: """订单队列的结算: 当队列中的订单都被处理过后: 算可以结算了 """ print('SETTLE ORDERHANDLER') # if len(self.order_queue.untrade) > 0: # self.if_start_orderquery = False # event.event_type = BROKER_EVENT.TRADE # event.event_queue.put( # QA_Task( # worker=self, # engine='ORDER', # event=event # ) # ) if len(self.order_queue.untrade)==0: self._trade() else: self._trade() # print(self.order_queue.untrade) self.order_queue.settle() self.order_status = pd.DataFrame() self.deal_status = pd.DataFrame() try: event.event_queue.task_done() except: pass elif event.event_type is MARKET_EVENT.QUERY_ORDER: """query_order和query_deal 需要联动使用 query_order 得到所有的订单列表 query_deal 判断订单状态--> 运行callback函数 实盘涉及到外部订单问题: 及 订单的来源 不完全从QUANTAXIS中发出, 则QA无法记录来源 (标记为外部订单) """ if self.if_start_orderquery: # if QA_util_if_tradetime(datetime.datetime.now()): # print(event.broker) # print(event.account_cookie) try: # 做一些容错处理 res = [self.monitor[account].query_orders( account.account_cookie, '') for account in list(self.monitor.keys())] res = pd.concat(res, axis=0) if len( res) > 0 else None #print(res) except: time.sleep(1) self.order_status = res if res is not None else self.order_status if len(self.order_status) > 0: #print(self.order_status) QA_SU_save_order(self.order_status) # else: # time.sleep(1) # 这里加入随机的睡眠时间 以免被发现固定的刷新请求 # event=event event.event_type = MARKET_EVENT.QUERY_DEAL if event.event_queue.qsize() < 1: time.sleep(random.randint(1, 2)) # event.event_queue.task_done() # 非阻塞 if self.if_start_orderquery: event.event_queue.put( QA_Task( worker=self, engine='ORDER', event=event ) ) # time.sleep(random.randint(2,5)) # print(event.event_type) # print(event2.event_type) # self.run(event) # print(self.order_status) # print('UPDATE ORDERS') elif event.event_type is MARKET_EVENT.QUERY_DEAL: """order_handler- query_deal 将order_handler订单队列中的订单---和deal中匹配起来 """ # if len(self.order_queue.pending) > 0: # for order in self.order_queue.pending: # #self.query # waiting_realorder_id = [ # order.realorder_id for order in self.order_queue.trade_list] # result = event.broker.query_deal # time.sleep(1) if self.if_start_orderquery: res = [self.monitor[account].query_orders( account.account_cookie, 'filled') for account in list(self.monitor.keys())] try: #res=[pd.DataFrame() if not isinstance(item,pd.DataFrame) else item for item in res] res = pd.concat(res, axis=0) if len( res) > 0 else pd.DataFrame() except: res = None self.deal_status = res if res is not None else self.deal_status if len(self.deal_status) > 0: QA_SU_save_deal(self.deal_status) # print(self.order_status) # 检查pending订单, 更新订单状态 try: for order in self.order_queue.pending: if len(self.deal_status) > 0: if order.realorder_id in self.deal_status.index.levels[1]: # 此时有成交推送(但可能是多条) # res = self.deal_status.loc[order.account_cookie, order.realorder_id] if isinstance(res, pd.Series): order.trade(str(res.trade_id), float(res.trade_price), int( res.trade_amount), str(res.trade_time)) elif isinstance(res, pd.DataFrame): if len(res) == 0: pass elif len(res) == 1: res = res.iloc[0] order.trade(str(res.trade_id), float(res.trade_price), int( res.trade_amount), str(res.trade_time)) else: print(res) print(len(res)) for _, deal in res.iterrows: order.trade(str(deal.trade_id), float(deal.trade_price), int( deal.trade_amount), str(deal.trade_time)) except Exception as e: print(e) print(self.order_queue.order_list) print(self.deal_status.index) print(self.order_status) # event.event_queue.task_done() # 这里加入随机的睡眠时间 以免被发现固定的刷新请求 if event.event_queue.qsize() < 1: time.sleep(random.randint(2, 5)) event.event_type = MARKET_EVENT.QUERY_ORDER if self.if_start_orderquery: event.event_queue.put( QA_Task( worker=self, engine='ORDER', event=event ) ) # self.run(event) # self.run(event) elif event.event_type is MARKET_EVENT.QUERY_POSITION: pass