vn.py量化社区
By Traders, For Traders.
Member
avatar
加入于:
帖子: 189
声望: 40

像是rest_client的代理连接中断还有ws_client的一些连接中断问题都不是gateway里面重连行情能解决的,必须重启CLI子进程才能解决,下面是我在处理这些连接问题的处理方法。

Member
avatar
加入于:
帖子: 189
声望: 40

0.rest_client和websocket_client里面的init函数里面加上gateway_name参数

    def init(self, host: str, proxy_host: str = "", proxy_port: int = 0,proxy_type: str = "http",ping_interval: int = 20, header: dict = None,gateway_name: str =""):
        #加入gateway_name方便判断错误来源
        self.gateway_name = gateway_name
  1. gateway里面传入gateway_name参数,以BYBIT为例(proxy_type是我自己加的参数,大家不用管)
         if server == "REAL":
             self.init(REST_HOST, proxy_host, proxy_port,proxy_type,gateway_name = self.gateway_name)
  2. rest_client里面修改如下

    from vnpy.trader.utility import load_json, save_json
     def _process_request(self, request: Request, session: requests.Session):
         # noinspection PyBroadException
         try:
             request = self.sign(request)
    
             url = self.make_full_url(request.path)
    
             response = session.request(
                 request.method,
                 url,
                 headers=request.headers,
                 params=request.params,
                 data=request.data,
                 proxies=self.proxies,
             )
             request.response = response
    
             status_code = response.status_code
             if status_code // 100 == 2:  # 2xx codes are all successful
                 if status_code == 204:
                     json_body = None
                 else:
                     json_body = response.json()
    
                 request.callback(json_body, request)
                 request.status = RequestStatus.success
             else:
                 request.status = RequestStatus.failed
    
                 if request.on_failed:
                     request.on_failed(status_code, request)
                 else:
                     self.on_failed(status_code, request)
         except Exception:
             request.status = RequestStatus.error
             t, v, tb = sys.exc_info()
             if request.on_error:
                 request.on_error(t, v, tb, request)
             else:
                 self.on_error(t, v, tb, request)
             #增量更新rest api连接错误
             connect_data = load_json("proxy_connect_status.json")
             connect_data.update({self.gateway_name:False})
             save_json("proxy_connect_status.json",connect_data)
     def request(
             self,
             method: str,
             path: str,
             params: dict = None,
             data: dict = None,
             headers: dict = None,
     ):
         request = Request(method, path, params, data, headers)
         request = self.sign(request)
    
         url = self.make_full_url(request.path)
         #代理连接出错写入proxy_connect_status
         try:
             response = requests.request(
                 request.method,
                 url,
                 headers=request.headers,
                 params=request.params,
                 data=request.data,
                 proxies=self.proxies,
             )
             return response
         except Exception as ex:
             #增量更新rest api连接错误
             connect_data = load_json("proxy_connect_status.json")
             connect_data.update({self.gateway_name:False})
             save_json("proxy_connect_status.json",connect_data)
             self.log_engine.info(f"{ex}")

    3.websocket_client里面修改如下

    from vnpy.trader.utility import load_json, save_json
     def _send_text(self, text: str):
         ws = self._ws
         if ws:
             try:
                 ws.send(text, opcode=websocket.ABNF.OPCODE_TEXT)
             except:
                 #增量更新websocket api连接错误
                 connect_data = load_json("websocket_connect_status.json")
                 connect_data.update({self.gateway_name:False})
                 save_json("websocket_connect_status.json",connect_data)
     def _ping(self):
         ws = self._ws
         if ws:
             try:
                 ws.send("ping", websocket.ABNF.OPCODE_PING)
             except:
                 #增量更新websocket api连接错误
                 connect_data = load_json("websocket_connect_status.json")
                 connect_data.update({self.gateway_name:False})
                 save_json("websocket_connect_status.json",connect_data)
Member
avatar
加入于:
帖子: 189
声望: 40

😂排版真烂,rest_client和websocket_client的修改搜save_json找的快

Member
avatar
加入于:
帖子: 189
声望: 40

BYBIT的父进程代码如下

GATEWAY_NAME = "BYBIT"
#----------------------------------------------------------------------
def run_proxy_connect_process():
    # 创建日志引擎
    event_engine = EventEngine()
    main_engine = MainEngine(event_engine)
    # 创建日志引擎
    log_engine = LogEngine(main_engine,event_engine)
    log_engine.info('启动行情记录守护父进程')
    process = None        # 子进程句柄
    recording = True
    while True:        
        # 记录状态则需要启动子进程
        if recording and process is None:
            log_engine.info('启动子进程')
            process = multiprocessing.Process(target=run_child_process)
            process.start()
            log_engine.info('子进程启动成功')
        # 非记录状态则退出子进程
        if not recording and process is not None:
            log_engine.info('关闭子进程')
            process.terminate()
            process.join()
            process = None
            log_engine.info('子进程关闭成功')
            #关闭子进程后重置代理连接状态proxy_connect_status
            proxy_status = load_json("proxy_connect_status.json")
            proxy_status.update({GATEWAY_NAME:True})
            save_json("proxy_connect_status.json",proxy_status)
        recording = load_json("proxy_connect_status.json").get(GATEWAY_NAME,False)  #读取rest_client代理连接状态
        sleep(3)

if __name__ == '__main__':
    #run_child_process()  #全天交易
    run_proxy_connect_process()     #bybit代理连接登录父进程
    #run_parent_process()  #过滤非交易时间记录行情数据
Member
avatar
加入于:
帖子: 14
声望: 0

为什么 websocket断开重连后,vnpy策略会失效?
只能用重启大法了吗?

Member
avatar
加入于:
帖子: 189
声望: 40

@xixi websocket行情断开重连后如果收不到行情的只能重启子进程

Member
avatar
加入于:
帖子: 14
声望: 0

好的

Member
avatar
加入于:
帖子: 189
声望: 40

@xixi 行情断开重连可以参考下vnpy github 这个更新resubscribe market data after reconnected of OKEX gateways,行情断开不用重启了,只处理websocket异常和代理异常即可

Member
avatar
加入于:
帖子: 189
声望: 40

@xixi 行情断开重连可以参考下vnpy github 这个更新resubscribe market data after reconnected of OKEX gateways,行情断开不用重启了,只处理websocket异常和代理异常即可

© 2015-2019 上海韦纳软件科技有限公司
备案服务号:沪ICP备18006526号-3

沪公网安备 31011502017034号