我尝试登录两个账号,第一个账号登录没有问题,但是第二个账号登录的时候,打印log时出现打印了两遍log。
另外,两个账户登录后,cta策略需要初始化两次还是只需要初始化一次。
最后,如果我启动策略能不能同时操作两个账户呢?
求助各位大神
我尝试登录两个账号,第一个账号登录没有问题,但是第二个账号登录的时候,打印log时出现打印了两遍log。
另外,两个账户登录后,cta策略需要初始化两次还是只需要初始化一次。
最后,如果我启动策略能不能同时操作两个账户呢?
求助各位大神
大神,那如果是同时登录两个CTP的话,我除了要扩展修改CtpGateway外,还需要修改其他的文件吗?
还有run.py中你要分别加载这两个接口
OK,谢谢大佬
大佬,我现在是直接导入两个引擎,然后同时开启两个引擎,一个引擎分一个账号。现在有两个疑问。
1.为什么开启第二个引擎后,每一个write_log都会打印两次(测试的发现)。
2.我这样的操作会不会导致两个引擎串线,从而导致两个引擎参数引用交叉。
代码较为简单,附上
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.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()
有可能互相影响的,不建议这么搞,更多应该开两个进程。
然后打印两次是因为日志模块,两次初始化会导致日志引擎的重复两次输出。