def handle_sync_response(self, api_id, sync=False): waited_time = 0 while waited_time < REQUEST_TIMEOUT: try: report_header = self._sync_req_resp_channel.recv( flags=zmq.NOBLOCK) report_body = self._sync_req_resp_channel.recv( flags=zmq.NOBLOCK) except zmq.ZMQError as e: time.sleep(0.1) waited_time += 0.1 else: header = dtp_struct.ResponseHeader() header.ParseFromString(report_header) api_id = header.api_id rsp_type = DTPType.get_proto_type(api_id) body = rsp_type() body.ParseFromString(report_body) payload = Payload(header, body) mail = Mail(api_id=api_id, api_type='rsp', sync=sync, content=payload) self.logger.info('{}, {}'.format(message2dict(header), message2dict(body))) if sync: return mail return self.dispatcher.put(mail) mail = Mail(api_id=api_id, api_type='rsp', sync=sync, ret_code=-1, err_message='请求超时') self.logger.error('请求超时 api_id={}'.format(api_id)) if sync: return mail return self.dispatcher.put(mail)
def send_order(self, order_original_id, exchange, code, price, quantity, order_side, order_type=dtp_type.ORDER_TYPE_LIMIT): """ 报单委托 """ mail = Mail(api_type='req', api_id=PLACE_ORDER, account=self._account, token=self._token, order_original_id=order_original_id, exchange=exchange, code=code, price=price, quantity=quantity, order_side=order_side, order_type=order_type) self.dispatcher.put(mail) self.logger.info('报单委托 {}'.format(mail))
def handle_counter_response(self): sock = self._async_resp_channel while self._running: topic = sock.recv() report_header = sock.recv() report_body = sock.recv() self.logger.debug('topic: {}'.format(topic)) header = dtp_struct.ReportHeader() header.ParseFromString(report_header) rsp_type = DTPType.get_proto_type(header.api_id) try: body = rsp_type() body.ParseFromString(report_body) except Exception: self.logger.warning('未知响应 api_id={}, {}'.format( header.api_id, header.message)) continue self.logger.info('{}, {}'.format(message2dict(header), message2dict(body))) self.dispatcher.put( Mail(api_id=header.api_id, api_type='rsp', content=Payload(header, body)))
def query_ration(self, **kw): """ 查询配售权益 """ mail = Mail(api_type='req', api_id=QUERY_RATION_REQUEST, sync=kw.get('sync', False), account=self._account, token=self._token) return self.dispatcher.put(mail)
def query_capital(self, **kw): """ 查询账户资金 """ mail = Mail(api_type='req', api_id=QUERY_CAPITAL_REQUEST, sync=kw.get('sync', False), account=self._account, token=self._token) return self.dispatcher.put(mail)
def query_trades(self, **kw): """ 查询成交 """ mail = Mail(api_type='req', api_id=QUERY_FILLS_REQUEST, sync=kw.get('sync', False), account=self._account, token=self._token) return self.dispatcher.put(mail)
def cancel_order(self, **kw): """ 撤单 """ mail = Mail(api_type='req', api_id=CANCEL_ORDER, account=self._account, token=self._token, exchange=kw['exchange'], order_exchange_id=kw['order_exchange_id']) self.dispatcher.put(mail)
def place_order_batch(self, orders): """ 批量下单 """ mail = Mail(api_type='req', api_id=PLACE_BATCH_ORDER, account=self._account, token=self._token, order_list=orders) self.dispatcher.put(mail) self.logger.info('批量买入委托 {}'.format(mail))
def login_account(self, **kw): mail = Mail(api_type='req', api_id=LOGIN_ACCOUNT_REQUEST, **kw) return self.dispatcher.put(mail)
def logout(self, **kw): mail = Mail(api_type='req', api_id=LOGOUT_ACCOUNT_REQUEST, account=self._account, token=self._token) self.dispatcher.put(mail)
def on_data(self, data): for listener in self._listeners: listener.put(Mail(api_id=self.name, api_type='rsp', content=data))