老师好,我将脚本模式的例子程序修改了一下,使用一个数组来保存状态,这样是一个连接对应一个进程,但是ctp的连接数一般就几个,如果我要交易10个以上的
合约,连接数就超过上限了,怎么样通过有限的连接数交易多个合约?
-*-coding:utf-8
import multiprocessing
from vnpy.event import EventEngine
from vnpy.trader.engine import MainEngine
from vnpy.gateway.ctp import CtpGateway
from vnpy.gateway.rohon import RohonGateway
from vnpy.app.script_trader import ScriptTraderApp
from logging import INFO
from vnpy.trader.setting import SETTINGS
from vnpy.app.script_trader.engine import EVENT_SCRIPT_LOG
from time import sleep
from datetime import datetime, time
import signal
import os
SETTINGS["log.active"] = True
SETTINGS["log.level"] = INFO
SETTINGS["log.console"] = True
StopSignal=False
ScriptPath="D:\PSVN\ngst_run.py"
ngst_engine=None
ctp_setting = {
"用户名": "xxxxxx",
"密码": "xxxxx",
"经纪商代码": "9999",
"交易服务器": "180.168.146.187:10130",
"行情服务器": "180.168.146.187:10131",
"产品名称": "simnow_client_test",
"授权编码": "0000000000000000",
"产品信息": "simnow_client_test"
}
格式:合约代码,子进程对象,app对象
contract_list=[
["ag2006_ag2008",None,None],
["au2006_au2008",None,None],
["ni2006_ni2007",None,None],
["zn2006_zn2007",None,None]
]
def run_child(contract_id):
global ScriptPath,ngst_engine,contract_list
event_engine = EventEngine()
ngst_engine = MainEngine(event_engine)
ngst_engine.add_gateway(CtpGateway)
#ngst_engine.add_gateway(RohonGateway)
ngst_engine.connect(ctp_setting, "CTP")
ngst_engine.write_log("连接CTP接口")
#ngst_engine.connect(ctp_setting, "ROHON")
#ngst_engine.write_log("连接融航接口")
App=ngst_engine.add_app(ScriptTraderApp)
sleep(5)
if (App):
for i,contract in enumerate(contract_list):
if (contract_id==contract[0]):
contract_list[i][2]=App
ngst_engine.write_log(contract_id+"引擎创建成功")
App.start_strategy(ScriptPath,contract_id)
def run_parent():
global contract_list,ngst_engine
"""
Running in the parent process.
"""
print("启动CTA策略守护父进程")
# Chinese futures market trading period (day/night)
DAY_START = time(8, 45)
DAY_END = time(15, 30)
NIGHT_START = time(20, 45)
NIGHT_END = time(2, 45)
while True:
current_time = datetime.now().time()
trading = False
# Check whether in trading period
InSession=(current_time >= DAY_START and current_time <= DAY_END) \
or (current_time >= NIGHT_START) \
or (current_time <= NIGHT_END)
InSession=True
if (StopSignal == False and InSession):
trading = True
for i,contract in enumerate(contract_list):
# Start child process in trading period
if trading and contract[1] is None:
print(contract[0]+"启动子进程")
contract[1] = multiprocessing.Process(target=run_child,args=(contract[0],))
#contract[1] = multiprocessing.Process(target=run_child(contract[0]))
#contract[1].daemon=True
contract[1].start()
print(contract[0]+" PID:"+str(contract[1].pid)+"子进程启动成功")
# 退出子进程
if not trading and contract[1] is not None:
contract[2].stop_strategy()
print(contract[0]+"关闭子进程")
contract[1].terminate()
contract[1].join()
contract[1]=None
contract_list[i][1]=None
print(contract[0]+"子进程关闭成功")
sleep(10)
#for i,contract in enumerate(contract_list):
# contract[1].join()
if (StopSignal):
for i,contract in enumerate(contract_list):
if contract[1] is not None:
contract[2].stop_strategy()
print(contract[0]+"关闭子进程")
contract[1].terminate()
contract[1].join()
contract[1]=None
contract_list[i][1]=None
print(contract[0]+"子进程关闭成功")
sleep(1)
print("程序退出成功")
ngst_engine.close()
sleep(5)
#os._exit(0)
exit(0)
def sigint_handler(signum, frame):
global StopSignal
StopSignal = True
if name == "main":
signal.signal(signal.SIGINT, sigint_handler)
signal.signal(signal.SIGTERM, sigint_handler)
run_parent()