vn.py官网
开源量化社区
Member
avatar
加入于:
帖子: 19
声望: 10

单台行情服务器,有出现Tick丢失的情况,这个策略有一定的 影响,可以通过增加多台行情服务器的方法,解决问题

from collections import deque
from vnpy.gateway.ctp.ctp_gateway import *

class CtpGatewayDouble(CtpGateway):
    default_setting = {
        "用户名": "",
        "密码": "",
        "经纪商代码": "",
        "交易服务器": "",
        "主行情服务器": "",
        "次行情服务器": "",
        "产品名称": "",
        "授权编码": "",
        "产品信息": "",
    }

    def __init__(self, event_engine):
        super(CtpGateway, self).__init__(event_engine, "Double")
        self.td_api = CtpTdApi(self)
        # 主行情
        self.md_api = CtpMdApi(self)
        # 备用行情
        self.sub_md_api = CtpMdApi(self)
        # 缓存
        self.tick_buffer = deque(maxlen=100_000)

    def connect(self, setting: dict):
        """"""
        userid = setting["用户名"]
        password = setting["密码"]
        brokerid = setting["经纪商代码"]
        td_address = setting["交易服务器"]
        md_address = setting["主行情服务器"]
        sub_md_address = setting["次行情服务器"]
        appid = setting["产品名称"]
        auth_code = setting["授权编码"]
        product_info = setting["产品信息"]

        if (not td_address.startswith("tcp://")) and (
            not td_address.startswith("ssl://")
        ):
            td_address = "tcp://" + td_address

        if (not md_address.startswith("tcp://")) and (
            not md_address.startswith("ssl://")
        ):
            md_address = "tcp://" + md_address

        if (not sub_md_address.startswith("tcp://")) and (
            not sub_md_address.startswith("ssl://")
        ):
            sub_md_address = "tcp://" + sub_md_address

        self.td_api.connect(
            td_address, userid, password, brokerid, auth_code, appid, product_info
        )
        self.md_api.connect(md_address, userid, password, brokerid)
        self.sub_md_api.connect(sub_md_address, userid, password, brokerid)

        self.init_query()

    def subscribe(self, req: SubscribeRequest):
        """"""
        self.md_api.subscribe(req)
        self.sub_md_api.subscribe(req)

    def close(self):
        """"""
        self.td_api.close()
        self.md_api.close()
        self.sub_md_api.close()

    def on_tick(self, tick):
        tick_hash = hash(tick)
        if tick_hash not in self.tick_buffer:
            self.tick_buffer.append(tick_hash)
            super().on_tick(tick)
Member
avatar
加入于:
帖子: 30
声望: 0

我有个问题想请教 ,你继承了CtpGateway类构建了一个CtpGatewayDouble类,但是我运行vnpy的时候他主程序还是运行的CtpGateway,主程序那里怎么修改呢?

Administrator
avatar
加入于:
帖子: 4985
声望: 291

小太阳 wrote:

我有个问题想请教 ,你继承了CtpGateway类构建了一个CtpGatewayDouble类,但是我运行vnpy的时候他主程序还是运行的CtpGateway,主程序那里怎么修改呢?

用脚本来启动,然后在脚本中加载这个CtpGatewayDouble,而不是CtpGateway本身即可,脚本请参考:https://github.com/vnpy/vnpy/blob/master/examples/vn_trader/run.py

Member
avatar
加入于:
帖子: 49
声望: 1

应该就是
在vnpy/gateway/ctp/ 增加以上文件: ctp_gateway_double.py
在vnpy/gateway/ctp/init.py 导入包

from .ctp_gateway_double import CtpGatewayDouble

在run.py里面:

from vnpy.gateway.ctp import CtpGateway
from vnpy.gateway.ctp import CtpGatewayDouble
main_engine.add_gateway(CtpGateway)
main_engine.add_gateway(CtpGatewayDouble)

在ctp_setting配置文件里面. 配置下ctp_setting增加下

 "次行情服务器":  "***.***.***.***:****",

为啥我会重复推tick...
不是用hash过滤了吗
请大佬解答下我哪里步骤错了.

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

BELLCOUSIN wrote:

应该就是
在vnpy/gateway/ctp/ 增加以上文件: ctp_gateway_double.py
在vnpy/gateway/ctp/init.py 导入包

from .ctp_gateway_double import CtpGatewayDouble

在run.py里面:

from vnpy.gateway.ctp import CtpGateway
from vnpy.gateway.ctp import CtpGatewayDouble
main_engine.add_gateway(CtpGateway)
main_engine.add_gateway(CtpGatewayDouble)

在ctp_setting配置文件里面. 配置下ctp_setting增加下

 "次行情服务器":  "***.***.***.***:****",

为啥我会重复推tick...
不是用hash过滤了吗
请大佬解答下我哪里步骤错了.

我觉得有可能是self.tick_buffer的问题

Member
avatar
加入于:
帖子: 140
声望: 13

shunyuzhiqian wrote:

BELLCOUSIN wrote:

应该就是
在vnpy/gateway/ctp/ 增加以上文件: ctp_gateway_double.py
在vnpy/gateway/ctp/init.py 导入包

from .ctp_gateway_double import CtpGatewayDouble

在run.py里面:

from vnpy.gateway.ctp import CtpGateway
from vnpy.gateway.ctp import CtpGatewayDouble
main_engine.add_gateway(CtpGateway)
main_engine.add_gateway(CtpGatewayDouble)

在ctp_setting配置文件里面. 配置下ctp_setting增加下

 "次行情服务器":  "***.***.***.***:****",

为啥我会重复推tick...
不是用hash过滤了吗
请大佬解答下我哪里步骤错了.

我觉得有可能是self.tick_buffer的问题

两个行情源同时连接,打印的时间戳各自是各自的,他肯定是会有重复tick的呀。

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

应该有个东西检测主行情服务断开了 再自动连接备用行情服务吧?

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

沪公网安备 31011502017034号