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

调用mainengine.send order(req,’CTP’)下单
创建req=OrderRequest()对象
其中参数设置offset=Offset.OPEN

运行结果 日志提示:’请选择开平方向’

查了下应该是运行到如下代码时判断出了问题,
可req.offset是在OFFSET_VT2CTP里面的。
求大神解惑?

def send_order(self, req: OrderRequest):
"""
Send new order.
"""
if req.offset not in OFFSET_VT2CTP:
self.gateway.write_log("请选择开平方向")
return ""

**

源代码

**
import multiprocessing
import sys

from time import sleep

from logging import INFO

from vnpy.event import EventEngine

from vnpy.trader.object import OrderRequest
from vnpy.trader.setting import SETTINGS
from vnpy.trader.engine import MainEngine
from vnpy.gateway.ctp import CtpGateway

from constant import Direction, Offset, OrderType, Exchange

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

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

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

event_engine = EventEngine()
main_engine = MainEngine(event_engine)
ctp_engine=main_engine.add_gateway(CtpGateway)


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

sleep(10)

oms_engine = main_engine.get_engine('oms')
pos = oms_engine.get_all_positions()

exchange = Exchange('SHFE')
req = OrderRequest('rb2105', exchange, direction=Direction.LONG, type=OrderType.LIMIT, volume=1)
req.price = 4858
req.offset = Offset.OPEN

print(req.offset)

main_engine.send_order(req)

print(pos)



if name == "main":
run_child()

Administrator
avatar
加入于:
帖子: 4646
声望: 266

请贴上完整的代码

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

代码已贴上,请多指教!!!

Member
avatar
加入于:
帖子: 2102
声望: 139

请在ctp_gateway的send_order函数中打印一下收到的req看一下与你填写的是否有差别

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

不好意思,现在还是小白一个,摸着石头过河阶段
再请教下是:ctp_gateway.py下的
class CtpGateway(BaseGateway):类中这样添加对吗?
def send_order(self, req: OrderRequest):
""""""
print('收到的参数是'.format(req.offset))
if req.type == OrderType.RFQ:
vt_orderid = self.td_api.send_rfq(req)
else:

        vt_orderid = self.td_api.send_order(req)

    return vt_orderid

结果没有任何输出 同样位置print(req)也无输出

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

description

调整了一下,参数能打印出来了,是对的

感觉是在 判断上出了问题
def send_order(self, req: OrderRequest):
"""
Send new order.
"""
if req.offset not in OFFSET_VT2CTP:
self.gateway.write_log("请选择开平方向")
return ""

请解惑,感谢感谢

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

参数打印输出结果如下:
description

Member
avatar
加入于:
帖子: 2102
声望: 139

description
那就请打印一下进入开平判断前后的offset吧

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

description

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

进入判断 if req.offset not in OFFSET_VT2CTP:时调用了下面的函数
def hash(self):
return hash(self.name)

description

之后就进入了self.gateway.write_log("请选择开平方向")语句

是否返回的值是OPEN,而不是Offset.OPEN,所以判断不在OFFSET_VT2CTP

OFFSET_VT2CTP = {
Offset.OPEN: THOST_FTDC_OF_Open,
Offset.CLOSE: THOST_FTDC_OFEN_Close,
Offset.CLOSETODAY: THOST_FTDC_OFEN_CloseToday,
Offset.CLOSEYESTERDAY: THOST_FTDC_OFEN_CloseYesterday,
}

多有打扰,实在是麻烦了

Member
avatar
加入于:
帖子: 2102
声望: 139

是Offset.OPEN不是OPEN,要不你用脚本策略试试看吧

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

沪公网安备 31011502017034号