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

代码如下,只改了ctp_setting 设置:

import multiprocessing
import sys
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

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

ctp_setting = {
"用户名": "176927",
"密码": "Ligz19871122",
"经纪商代码": "9999",
"交易服务器": "80.168.146.187:10100",
"行情服务器": "180.168.146.187:10110",
"产品名称": "simnow_client_test",
"授权编码": "0000000000000000",
"产品信息": ""
}

Chinese futures market trading period (day/night)

DAY_START = time(8, 45)
DAY_END = time(15, 0)

NIGHT_START = time(20, 45)
NIGHT_END = time(2, 45)

def check_trading_period():
""""""
current_time = datetime.now().time()

trading = False
if (
    (current_time >= DAY_START and current_time <= DAY_END)
    or (current_time >= NIGHT_START)
    or (current_time <= NIGHT_END)
):
    trading = True

return trading


def run_child():
"""
Running in the child process.
"""
SETTINGS["log.file"] = True

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_LOG, log_engine.process_log_event)
main_engine.write_log("注册日志事件监听")

main_engine.connect(ctp_setting, "CTP")
main_engine.write_log("连接CTP接口")

sleep(10)

cta_engine.init_engine()
main_engine.write_log("CTA策略初始化完成")
cta_engine.init_all_strategies()
sleep(60)   # Leave enough time to complete strategy initialization
main_engine.write_log("CTA策略全部初始化")

cta_engine.start_all_strategies()
main_engine.write_log("CTA策略全部启动")

while True:
    sleep(10)

    trading = check_trading_period()
    if not trading:
        print("关闭子进程")
        main_engine.close()
        sys.exit(0)


def run_parent():
"""
Running in the parent process.
"""
print("启动CTA策略守护父进程")

child_process = None

while True:
    trading = check_trading_period()

    # Start child process in trading period
    if trading and child_process is None:
        print("启动子进程")
        child_process = multiprocessing.Process(target=run_child)
        child_process.start()
        print("子进程启动成功")

    # 非记录时间则退出子进程
    if not trading and child_process is not None:
        if not child_process.is_alive():
            child_process = None
            print("子进程关闭成功")

    sleep(5)


if name == "main":
run_parent()

我在图形界面下添加了一个 AtrRsiStrategy ,
cta_strategy_setting.json 内容
{
"atrrsi": {
"class_name": "AtrRsiStrategy",
"vt_symbol": "ni2103.SHFE",
"setting": {
"class_name": "AtrRsiStrategy",
"atr_length": 22,
"atr_ma_length": 10,
"rsi_length": 5,
"rsi_entry": 16,
"trailing_percent": 0.8,
"fixed_size": 1
}
}
}

cta_strategy_data.json文件内容如下:
{
"atrrsi": {
"pos": 0,
"atr_value": 199.45744212131393,
"atr_ma": 195.76038481162726,
"rsi_value": 29.932724785216948,
"rsi_buy": 66,
"rsi_sell": 34,
"intra_trade_high": 129650.0,
"intra_trade_low": 129480.0
}
}

运行结果:

启动CTA策略守护父进程
启动子进程
子进程启动成功
2021-01-12 21:58:53,388 INFO: 主引擎创建成功
2021-01-12 21:58:53,388 INFO: 注册日志事件监听
2021-01-12 21:58:53,388 INFO: 连接CTP接口
2021-01-12 21:58:53,508 INFO: 行情服务器连接成功
2021-01-12 21:58:53,558 INFO: 行情服务器登录成功
2021-01-12 21:59:05,558 INFO: RQData数据接口初始化成功
2021-01-12 21:59:05,575 INFO: CTA策略引擎初始化成功
2021-01-12 21:59:05,575 INFO: CTA策略初始化完成
2021-01-12 21:59:05,580 INFO: atrrsi开始执行初始化
2021-01-12 21:59:05,590 INFO: atrrsi: 策略初始化
None
2021-01-12 21:59:06,364 INFO: atrrsi: 行情订阅失败,找不到合约ni2103.SHFE
2021-01-12 21:59:06,364 INFO: atrrsi初始化完成
2021-01-12 22:00:05,590 INFO: CTA策略全部初始化
2021-01-12 22:00:05,590 INFO: atrrsi: 策略启动
2021-01-12 22:00:05,590 INFO: CTA策略全部启动

结果里面的None,是我打印了合约信息,合约信息为None,所以订阅不了,图形界面下没有问题,现在无界面一直弄不好,困扰了好长时间了,请帮忙解决一下,谢谢

Member
avatar
加入于:
帖子: 1987
声望: 131

没有合约查询成功的日志输出,自然找不到合约。交易服务器地址写错了吧

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

xiaohe wrote:

没有合约查询成功的日志输出,自然找不到合约。交易服务器地址写错了吧

我昨晚也通过对比发现,日志里面确实是没有查询合约成功的日志输出,谢谢! 大意了,不知道什么时候复制把交易服务器地址弄错了!

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

沪公网安备 31011502017034号