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

我尝试登录两个账号,第一个账号登录没有问题,但是第二个账号登录的时候,打印log时出现打印了两遍log。
另外,两个账户登录后,cta策略需要初始化两次还是只需要初始化一次。
最后,如果我启动策略能不能同时操作两个账户呢?
求助各位大神

Administrator
avatar
加入于:
帖子: 3810
声望: 199
  1. 同一个接口只能登录一次
  2. 如果要同时登录两个CTP,需要自己扩展修改CtpGateway,然后加载两个CtpGateway
  3. 对于不同的接口,比如股票的XTP和期货的CTP,可以直接同时登录使用,并在策略中同时交易这两个接口的合约
Member
avatar
加入于:
帖子: 33
声望: 0

大神,那如果是同时登录两个CTP的话,我除了要扩展修改CtpGateway外,还需要修改其他的文件吗?

Administrator
avatar
加入于:
帖子: 3810
声望: 199

还有run.py中你要分别加载这两个接口

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

OK,谢谢大佬

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

大佬,我现在是直接导入两个引擎,然后同时开启两个引擎,一个引擎分一个账号。现在有两个疑问。
1.为什么开启第二个引擎后,每一个write_log都会打印两次(测试的发现)。
2.我这样的操作会不会导致两个引擎串线,从而导致两个引擎参数引用交叉。
代码较为简单,附上

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

import multiprocessing
from time import sleep
from datetime import datetime, time
from logging import INFO

from vnpy.event import EventEngine
from vnpy.trader.setting import SETTINGS
from vnpy.trader.engine import MainEngine

from vnpy.gateway.ctptest import CtptestGateway

from vnpy.gateway.ctp import CtpGateway
from vnpy.app.cta_strategy import CtaStrategyApp
from vnpy.app.cta_strategy.base import EVENT_CTA_LOG as EVENT_CTA_LOG1
from vnpy.app.cta_strategy.base import EVENT_CTA_LOG as EVENT_CTA_LOG2

from vnpy.event import EventEngine as EventEngine1
from vnpy.trader.engine import MainEngine as MainEngine1
from vnpy.gateway.ctp import CtpGateway as CtpGateway1
from vnpy.app.cta_strategy import CtaStrategyApp as CtaStrategyApp1

SETTINGS["log.active"] = True
SETTINGS["log.level"] = INFO
SETTINGS["log.console"] = True
SETTINGS["log.file"] = True

ctp_setting1 = {
"用户名": "",
"密码": "",
"经纪商代码": "",
"交易服务器": "",
"行情服务器": "1",
"产品名称": "",
"授权编码": "",
"产品信息": ""
}

ctp_setting2 = {
"用户名": "",
"密码": "",
"经纪商代码": "",
"交易服务器": "",
"行情服务器": "",
"产品名称": "",
"授权编码": ",
"产品信息": ""
}
def run_accountfi():
"""
Running in the child process.
"""

# 登录账户一
event_engine = EventEngine() 
main_engine = MainEngine(event_engine) 
main_engine.add_gateway(CtpGateway) 
cta_engine = main_engine.add_app(CtaStrategyApp)
main_engine.write_log("主引擎创建成功")
log_engine = main_engine.get_engine("log")
event_engine.register(EVENT_CTA_LOG1, log_engine.process_log_event)
main_engine.write_log("注册日志事件监听")
main_engine.connect(ctp_setting1, "CTP")
main_engine.write_log("连接CTP接口")
sleep(10)

# 登陆账户二
event_engine1 = EventEngine1() 
main_engine1 = MainEngine(event_engine1) 
main_engine1.add_gateway(CtpGateway1) 
cta_engine1 = main_engine1.add_app(CtaStrategyApp1)
main_engine1.write_log("账户2主引擎创建成功")
log_engine1 = main_engine1.get_engine("log")
event_engine1.register(EVENT_CTA_LOG2, log_engine1.process_log_event)
main_engine1.write_log("账户2注册日志事件监听")
main_engine1.connect(ctp_setting2, "CTP")
main_engine1.write_log("账户2连接CTP接口")
sleep(10)

# 初始化账户一策略
cta_engine.init_engine()
main_engine.write_log("CTA策略初始化完成")
sleep(2)
cta_engine.add_strategy('TrySellStrategy','try_sell3','IC1909.CFFEX',{"volume":1,"volume_t":1})
main_engine.write_log("CTA策略==卖出==操作添加完成")
sleep(2)
cta_engine.init_strategy('try_sell3')
main_engine.write_log("CTA策略==卖出==操作初始化完成")   
sleep(2)
cta_engine.add_strategy('TryBuy3Strategy','try_buy3','IC1909.CFFEX',{"volume":1,"volume_t":1})
main_engine.write_log("CTA策略==买入==操作添加完成")
sleep(2)
cta_engine.init_strategy('try_buy3')
main_engine.write_log("CTA策略==买入==操作初始化完成")   
sleep(2)

# 初始化账户二策略
cta_engine1.init_engine()
main_engine1.write_log("账户2CTA策略初始化完成")
sleep(2)
cta_engine1.add_strategy('TrySell2Strategy','try_sell_ACCOUT2','IC1909.CFFEX',{"volume":1,"volume_t":1})
main_engine1.write_log("账户2,CTA策略==卖出==操作添加完成")
sleep(2)
cta_engine1.init_strategy('try_sell_ACCOUT2')
main_engine1.write_log("账户2,CTA策略==卖出==操作初始化完成")   
sleep(2)
cta_engine1.add_strategy('TryBuy32Strategy','try_buy_ACCOUT2','IC1909.CFFEX',{"volume":1,"volume_t":1})
main_engine1.write_log("账户2,CTA策略==买入==操作添加完成")
sleep(2)
cta_engine1.init_strategy('try_buy_ACCOUT2')
main_engine1.write_log("账户2,CTA策略==买入==操作初始化完成")   
sleep(2)

# 启动策略
cta_engine.start_strategy('try_buy3')
main_engine.write_log("CTA策略账户【一】==买入==启动完成")
cta_engine1.start_strategy('try_buy_ACCOUT2')
main_engine1.write_log("CTA策略账户【二】==买入==启动完成") 
cta_engine.start_strategy('try_sell3')
main_engine.write_log("CTA策略账户【一】==卖出==启动完成")
cta_engine1.start_strategy('try_sell_ACCOUT2')
main_engine1.write_log("CTA策略账户【二】==卖出==启动完成")

if name == 'main'
run_accountfi()

Administrator
avatar
加入于:
帖子: 3810
声望: 199

有可能互相影响的,不建议这么搞,更多应该开两个进程。

然后打印两次是因为日志模块,两次初始化会导致日志引擎的重复两次输出。

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