2015-08-14 08:11:53 +00:00
|
|
|
|
# encoding: UTF-8
|
|
|
|
|
|
2015-10-16 08:50:44 +00:00
|
|
|
|
'''
|
|
|
|
|
vn.ctp的gateway接入
|
|
|
|
|
|
|
|
|
|
考虑到现阶段大部分CTP中的ExchangeID字段返回的都是空值
|
|
|
|
|
vtSymbol直接使用symbol
|
|
|
|
|
'''
|
|
|
|
|
|
|
|
|
|
|
2015-09-28 03:42:35 +00:00
|
|
|
|
import os
|
|
|
|
|
import json
|
2016-04-02 09:04:58 +00:00
|
|
|
|
from copy import copy
|
2015-09-28 03:42:35 +00:00
|
|
|
|
|
2015-08-14 08:11:53 +00:00
|
|
|
|
from vnctpmd import MdApi
|
|
|
|
|
from vnctptd import TdApi
|
2015-09-28 03:42:35 +00:00
|
|
|
|
from ctpDataType import *
|
|
|
|
|
from vtGateway import *
|
2015-08-14 08:11:53 +00:00
|
|
|
|
|
2016-04-27 14:49:28 +00:00
|
|
|
|
|
2015-10-16 08:50:44 +00:00
|
|
|
|
# 以下为一些VT类型和CTP类型的映射字典
|
|
|
|
|
# 价格类型映射
|
|
|
|
|
priceTypeMap = {}
|
|
|
|
|
priceTypeMap[PRICETYPE_LIMITPRICE] = defineDict["THOST_FTDC_OPT_LimitPrice"]
|
|
|
|
|
priceTypeMap[PRICETYPE_MARKETPRICE] = defineDict["THOST_FTDC_OPT_AnyPrice"]
|
|
|
|
|
priceTypeMapReverse = {v: k for k, v in priceTypeMap.items()}
|
|
|
|
|
|
|
|
|
|
# 方向类型映射
|
|
|
|
|
directionMap = {}
|
|
|
|
|
directionMap[DIRECTION_LONG] = defineDict['THOST_FTDC_D_Buy']
|
|
|
|
|
directionMap[DIRECTION_SHORT] = defineDict['THOST_FTDC_D_Sell']
|
|
|
|
|
directionMapReverse = {v: k for k, v in directionMap.items()}
|
|
|
|
|
|
|
|
|
|
# 开平类型映射
|
|
|
|
|
offsetMap = {}
|
|
|
|
|
offsetMap[OFFSET_OPEN] = defineDict['THOST_FTDC_OF_Open']
|
|
|
|
|
offsetMap[OFFSET_CLOSE] = defineDict['THOST_FTDC_OF_Close']
|
|
|
|
|
offsetMap[OFFSET_CLOSETODAY] = defineDict['THOST_FTDC_OF_CloseToday']
|
2015-12-17 02:15:12 +00:00
|
|
|
|
offsetMap[OFFSET_CLOSEYESTERDAY] = defineDict['THOST_FTDC_OF_CloseYesterday']
|
2015-10-16 08:50:44 +00:00
|
|
|
|
offsetMapReverse = {v:k for k,v in offsetMap.items()}
|
|
|
|
|
|
|
|
|
|
# 交易所类型映射
|
|
|
|
|
exchangeMap = {}
|
|
|
|
|
exchangeMap[EXCHANGE_CFFEX] = 'CFFEX'
|
|
|
|
|
exchangeMap[EXCHANGE_SHFE] = 'SHFE'
|
|
|
|
|
exchangeMap[EXCHANGE_CZCE] = 'CZCE'
|
|
|
|
|
exchangeMap[EXCHANGE_DCE] = 'DCE'
|
2016-06-19 14:35:19 +00:00
|
|
|
|
exchangeMap[EXCHANGE_SSE] = 'SSE'
|
2015-10-16 08:50:44 +00:00
|
|
|
|
exchangeMap[EXCHANGE_UNKNOWN] = ''
|
|
|
|
|
exchangeMapReverse = {v:k for k,v in exchangeMap.items()}
|
|
|
|
|
|
|
|
|
|
# 持仓类型映射
|
|
|
|
|
posiDirectionMap = {}
|
|
|
|
|
posiDirectionMap[DIRECTION_NET] = defineDict["THOST_FTDC_PD_Net"]
|
|
|
|
|
posiDirectionMap[DIRECTION_LONG] = defineDict["THOST_FTDC_PD_Long"]
|
|
|
|
|
posiDirectionMap[DIRECTION_SHORT] = defineDict["THOST_FTDC_PD_Short"]
|
|
|
|
|
posiDirectionMapReverse = {v:k for k,v in posiDirectionMap.items()}
|
|
|
|
|
|
2016-07-25 14:32:55 +00:00
|
|
|
|
# 产品类型映射
|
|
|
|
|
productClassMap = {}
|
|
|
|
|
productClassMap[PRODUCT_FUTURES] = defineDict["THOST_FTDC_PC_Futures"]
|
|
|
|
|
productClassMap[PRODUCT_OPTION] = defineDict["THOST_FTDC_PC_Options"]
|
|
|
|
|
productClassMap[PRODUCT_COMBINATION] = defineDict["THOST_FTDC_PC_Combination"]
|
|
|
|
|
productClassMapReverse = {v:k for k,v in productClassMap.items()}
|
|
|
|
|
|
|
|
|
|
|
2015-08-14 08:11:53 +00:00
|
|
|
|
|
|
|
|
|
########################################################################
|
|
|
|
|
class CtpGateway(VtGateway):
|
|
|
|
|
"""CTP接口"""
|
|
|
|
|
|
|
|
|
|
#----------------------------------------------------------------------
|
2015-09-28 03:42:35 +00:00
|
|
|
|
def __init__(self, eventEngine, gatewayName='CTP'):
|
2015-08-14 08:11:53 +00:00
|
|
|
|
"""Constructor"""
|
2015-09-28 03:42:35 +00:00
|
|
|
|
super(CtpGateway, self).__init__(eventEngine, gatewayName)
|
2015-08-14 08:11:53 +00:00
|
|
|
|
|
2015-09-28 03:42:35 +00:00
|
|
|
|
self.mdApi = CtpMdApi(self) # 行情API
|
|
|
|
|
self.tdApi = CtpTdApi(self) # 交易API
|
2015-08-14 08:11:53 +00:00
|
|
|
|
|
2015-09-28 03:42:35 +00:00
|
|
|
|
self.mdConnected = False # 行情API连接状态,登录完成后为True
|
|
|
|
|
self.tdConnected = False # 交易API连接状态
|
2015-08-14 08:11:53 +00:00
|
|
|
|
|
2015-10-16 08:50:44 +00:00
|
|
|
|
self.qryEnabled = False # 是否要启动循环查询
|
|
|
|
|
|
2015-09-28 03:42:35 +00:00
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
def connect(self):
|
|
|
|
|
"""连接"""
|
|
|
|
|
# 载入json文件
|
|
|
|
|
fileName = self.gatewayName + '_connect.json'
|
2016-04-14 15:07:06 +00:00
|
|
|
|
fileName = os.getcwd() + '/ctpGateway/' + fileName
|
2015-12-23 06:15:41 +00:00
|
|
|
|
|
2015-09-28 03:42:35 +00:00
|
|
|
|
try:
|
|
|
|
|
f = file(fileName)
|
|
|
|
|
except IOError:
|
|
|
|
|
log = VtLogData()
|
|
|
|
|
log.gatewayName = self.gatewayName
|
|
|
|
|
log.logContent = u'读取连接配置出错,请检查'
|
|
|
|
|
self.onLog(log)
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
# 解析json文件
|
|
|
|
|
setting = json.load(f)
|
|
|
|
|
try:
|
|
|
|
|
userID = str(setting['userID'])
|
|
|
|
|
password = str(setting['password'])
|
|
|
|
|
brokerID = str(setting['brokerID'])
|
|
|
|
|
tdAddress = str(setting['tdAddress'])
|
|
|
|
|
mdAddress = str(setting['mdAddress'])
|
|
|
|
|
except KeyError:
|
|
|
|
|
log = VtLogData()
|
|
|
|
|
log.gatewayName = self.gatewayName
|
|
|
|
|
log.logContent = u'连接配置缺少字段,请检查'
|
|
|
|
|
self.onLog(log)
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
# 创建行情和交易接口对象
|
|
|
|
|
self.mdApi.connect(userID, password, brokerID, mdAddress)
|
|
|
|
|
self.tdApi.connect(userID, password, brokerID, tdAddress)
|
2015-10-16 08:50:44 +00:00
|
|
|
|
|
|
|
|
|
# 初始化并启动查询
|
|
|
|
|
self.initQuery()
|
2015-08-14 08:11:53 +00:00
|
|
|
|
|
2015-09-28 03:42:35 +00:00
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
def subscribe(self, subscribeReq):
|
|
|
|
|
"""订阅行情"""
|
2015-10-16 08:50:44 +00:00
|
|
|
|
self.mdApi.subscribe(subscribeReq)
|
2015-09-28 03:42:35 +00:00
|
|
|
|
|
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
def sendOrder(self, orderReq):
|
|
|
|
|
"""发单"""
|
|
|
|
|
return self.tdApi.sendOrder(orderReq)
|
|
|
|
|
|
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
def cancelOrder(self, cancelOrderReq):
|
|
|
|
|
"""撤单"""
|
|
|
|
|
self.tdApi.cancelOrder(cancelOrderReq)
|
|
|
|
|
|
|
|
|
|
#----------------------------------------------------------------------
|
2015-12-30 14:01:17 +00:00
|
|
|
|
def qryAccount(self):
|
2015-09-28 03:42:35 +00:00
|
|
|
|
"""查询账户资金"""
|
2015-12-30 14:01:17 +00:00
|
|
|
|
self.tdApi.qryAccount()
|
2015-09-28 03:42:35 +00:00
|
|
|
|
|
|
|
|
|
#----------------------------------------------------------------------
|
2015-12-30 14:01:17 +00:00
|
|
|
|
def qryPosition(self):
|
2015-09-28 03:42:35 +00:00
|
|
|
|
"""查询持仓"""
|
2015-12-30 14:01:17 +00:00
|
|
|
|
self.tdApi.qryPosition()
|
2015-09-28 03:42:35 +00:00
|
|
|
|
|
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
def close(self):
|
|
|
|
|
"""关闭"""
|
2015-10-16 08:50:44 +00:00
|
|
|
|
if self.mdConnected:
|
|
|
|
|
self.mdApi.close()
|
|
|
|
|
if self.tdConnected:
|
|
|
|
|
self.tdApi.close()
|
|
|
|
|
|
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
def initQuery(self):
|
|
|
|
|
"""初始化连续查询"""
|
|
|
|
|
if self.qryEnabled:
|
|
|
|
|
# 需要循环的查询函数列表
|
2015-12-30 14:01:17 +00:00
|
|
|
|
self.qryFunctionList = [self.qryAccount, self.qryPosition]
|
2015-10-16 08:50:44 +00:00
|
|
|
|
|
|
|
|
|
self.qryCount = 0 # 查询触发倒计时
|
|
|
|
|
self.qryTrigger = 2 # 查询触发点
|
|
|
|
|
self.qryNextFunction = 0 # 上次运行的查询函数索引
|
|
|
|
|
|
|
|
|
|
self.startQuery()
|
|
|
|
|
|
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
def query(self, event):
|
|
|
|
|
"""注册到事件处理引擎上的查询函数"""
|
|
|
|
|
self.qryCount += 1
|
|
|
|
|
|
|
|
|
|
if self.qryCount > self.qryTrigger:
|
|
|
|
|
# 清空倒计时
|
|
|
|
|
self.qryCount = 0
|
|
|
|
|
|
|
|
|
|
# 执行查询函数
|
|
|
|
|
function = self.qryFunctionList[self.qryNextFunction]
|
|
|
|
|
function()
|
|
|
|
|
|
|
|
|
|
# 计算下次查询函数的索引,如果超过了列表长度,则重新设为0
|
|
|
|
|
self.qryNextFunction += 1
|
|
|
|
|
if self.qryNextFunction == len(self.qryFunctionList):
|
|
|
|
|
self.qryNextFunction = 0
|
|
|
|
|
|
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
def startQuery(self):
|
|
|
|
|
"""启动连续查询"""
|
|
|
|
|
self.eventEngine.register(EVENT_TIMER, self.query)
|
|
|
|
|
|
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
def setQryEnabled(self, qryEnabled):
|
|
|
|
|
"""设置是否要启动循环查询"""
|
|
|
|
|
self.qryEnabled = qryEnabled
|
|
|
|
|
|
2015-08-14 08:11:53 +00:00
|
|
|
|
|
2015-09-09 01:20:02 +00:00
|
|
|
|
|
2015-08-14 08:11:53 +00:00
|
|
|
|
########################################################################
|
|
|
|
|
class CtpMdApi(MdApi):
|
|
|
|
|
"""CTP行情API实现"""
|
|
|
|
|
|
|
|
|
|
#----------------------------------------------------------------------
|
2015-09-28 03:42:35 +00:00
|
|
|
|
def __init__(self, gateway):
|
2015-08-14 08:11:53 +00:00
|
|
|
|
"""Constructor"""
|
|
|
|
|
super(CtpMdApi, self).__init__()
|
|
|
|
|
|
|
|
|
|
self.gateway = gateway # gateway对象
|
|
|
|
|
self.gatewayName = gateway.gatewayName # gateway对象名称
|
|
|
|
|
|
|
|
|
|
self.reqID = EMPTY_INT # 操作请求编号
|
|
|
|
|
|
|
|
|
|
self.connectionStatus = False # 连接状态
|
|
|
|
|
self.loginStatus = False # 登录状态
|
|
|
|
|
|
2015-09-28 03:42:35 +00:00
|
|
|
|
self.subscribedSymbols = set() # 已订阅合约代码
|
|
|
|
|
|
|
|
|
|
self.userID = EMPTY_STRING # 账号
|
|
|
|
|
self.password = EMPTY_STRING # 密码
|
|
|
|
|
self.brokerID = EMPTY_STRING # 经纪商代码
|
|
|
|
|
self.address = EMPTY_STRING # 服务器地址
|
2015-08-14 08:11:53 +00:00
|
|
|
|
|
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
def onFrontConnected(self):
|
|
|
|
|
"""服务器连接"""
|
|
|
|
|
self.connectionStatus = True
|
|
|
|
|
|
|
|
|
|
log = VtLogData()
|
|
|
|
|
log.gatewayName = self.gatewayName
|
|
|
|
|
log.logContent = u'行情服务器连接成功'
|
|
|
|
|
self.gateway.onLog(log)
|
|
|
|
|
self.login()
|
|
|
|
|
|
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
def onFrontDisconnected(self, n):
|
|
|
|
|
"""服务器断开"""
|
|
|
|
|
self.connectionStatus = False
|
|
|
|
|
self.loginStatus = False
|
|
|
|
|
self.gateway.mdConnected = False
|
|
|
|
|
|
|
|
|
|
log = VtLogData()
|
|
|
|
|
log.gatewayName = self.gatewayName
|
|
|
|
|
log.logContent = u'行情服务器连接断开'
|
|
|
|
|
self.gateway.onLog(log)
|
|
|
|
|
|
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
def onHeartBeatWarning(self, n):
|
|
|
|
|
"""心跳报警"""
|
|
|
|
|
# 因为API的心跳报警比较常被触发,且与API工作关系不大,因此选择忽略
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
def onRspError(self, error, n, last):
|
|
|
|
|
"""错误回报"""
|
|
|
|
|
err = VtErrorData()
|
|
|
|
|
err.gatewayName = self.gatewayName
|
|
|
|
|
err.errorID = error['ErrorID']
|
|
|
|
|
err.errorMsg = error['ErrorMsg'].decode('gbk')
|
|
|
|
|
self.gateway.onError(err)
|
|
|
|
|
|
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
def onRspUserLogin(self, data, error, n, last):
|
|
|
|
|
"""登陆回报"""
|
|
|
|
|
# 如果登录成功,推送日志信息
|
|
|
|
|
if error['ErrorID'] == 0:
|
|
|
|
|
self.loginStatus = True
|
|
|
|
|
self.gateway.mdConnected = True
|
|
|
|
|
|
|
|
|
|
log = VtLogData()
|
2015-10-16 08:50:44 +00:00
|
|
|
|
log.gatewayName = self.gatewayName
|
2015-08-14 08:11:53 +00:00
|
|
|
|
log.logContent = u'行情服务器登录完成'
|
|
|
|
|
self.gateway.onLog(log)
|
|
|
|
|
|
|
|
|
|
# 重新订阅之前订阅的合约
|
|
|
|
|
for subscribeReq in self.subscribedSymbols:
|
|
|
|
|
self.subscribe(subscribeReq)
|
|
|
|
|
|
|
|
|
|
# 否则,推送错误信息
|
|
|
|
|
else:
|
|
|
|
|
err = VtErrorData()
|
|
|
|
|
err.gatewayName = self.gatewayName
|
|
|
|
|
err.errorID = error['ErrorID']
|
|
|
|
|
err.errorMsg = error['ErrorMsg'].decode('gbk')
|
|
|
|
|
self.gateway.onError(err)
|
2016-03-30 12:20:52 +00:00
|
|
|
|
|
2015-08-14 08:11:53 +00:00
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
def onRspUserLogout(self, data, error, n, last):
|
|
|
|
|
"""登出回报"""
|
|
|
|
|
# 如果登出成功,推送日志信息
|
|
|
|
|
if error['ErrorID'] == 0:
|
|
|
|
|
self.loginStatus = False
|
2016-07-31 13:53:02 +00:00
|
|
|
|
self.gateway.mdConnected = False
|
2015-08-14 08:11:53 +00:00
|
|
|
|
|
|
|
|
|
log = VtLogData()
|
|
|
|
|
log.gatewayName = self.gatewayName
|
|
|
|
|
log.logContent = u'行情服务器登出完成'
|
|
|
|
|
self.gateway.onLog(log)
|
|
|
|
|
|
|
|
|
|
# 否则,推送错误信息
|
|
|
|
|
else:
|
|
|
|
|
err = VtErrorData()
|
|
|
|
|
err.gatewayName = self.gatewayName
|
|
|
|
|
err.errorID = error['ErrorID']
|
|
|
|
|
err.errorMsg = error['ErrorMsg'].decode('gbk')
|
|
|
|
|
self.gateway.onError(err)
|
|
|
|
|
|
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
def onRspSubMarketData(self, data, error, n, last):
|
|
|
|
|
"""订阅合约回报"""
|
|
|
|
|
# 通常不在乎订阅错误,选择忽略
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
def onRspUnSubMarketData(self, data, error, n, last):
|
|
|
|
|
"""退订合约回报"""
|
|
|
|
|
# 同上
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
def onRtnDepthMarketData(self, data):
|
|
|
|
|
"""行情推送"""
|
|
|
|
|
tick = VtTickData()
|
2015-10-16 08:50:44 +00:00
|
|
|
|
tick.gatewayName = self.gatewayName
|
2015-08-14 08:11:53 +00:00
|
|
|
|
|
|
|
|
|
tick.symbol = data['InstrumentID']
|
2015-10-16 08:50:44 +00:00
|
|
|
|
tick.exchange = exchangeMapReverse.get(data['ExchangeID'], u'未知')
|
|
|
|
|
tick.vtSymbol = tick.symbol #'.'.join([tick.symbol, EXCHANGE_UNKNOWN])
|
2015-08-14 08:11:53 +00:00
|
|
|
|
|
|
|
|
|
tick.lastPrice = data['LastPrice']
|
|
|
|
|
tick.volume = data['Volume']
|
|
|
|
|
tick.openInterest = data['OpenInterest']
|
2015-10-16 08:50:44 +00:00
|
|
|
|
tick.time = '.'.join([data['UpdateTime'], str(data['UpdateMillisec']/100)])
|
|
|
|
|
tick.date = data['TradingDay']
|
2015-09-28 03:42:35 +00:00
|
|
|
|
|
|
|
|
|
tick.openPrice = data['OpenPrice']
|
|
|
|
|
tick.highPrice = data['HighestPrice']
|
|
|
|
|
tick.lowPrice = data['LowestPrice']
|
2015-10-16 08:50:44 +00:00
|
|
|
|
tick.preClosePrice = data['PreClosePrice']
|
|
|
|
|
|
|
|
|
|
tick.upperLimit = data['UpperLimitPrice']
|
|
|
|
|
tick.lowerLimit = data['LowerLimitPrice']
|
2015-08-14 08:11:53 +00:00
|
|
|
|
|
|
|
|
|
# CTP只有一档行情
|
|
|
|
|
tick.bidPrice1 = data['BidPrice1']
|
|
|
|
|
tick.bidVolume1 = data['BidVolume1']
|
|
|
|
|
tick.askPrice1 = data['AskPrice1']
|
|
|
|
|
tick.askVolume1 = data['AskVolume1']
|
|
|
|
|
|
|
|
|
|
self.gateway.onTick(tick)
|
|
|
|
|
|
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
def onRspSubForQuoteRsp(self, data, error, n, last):
|
|
|
|
|
"""订阅期权询价"""
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
def onRspUnSubForQuoteRsp(self, data, error, n, last):
|
|
|
|
|
"""退订期权询价"""
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
def onRtnForQuoteRsp(self, data):
|
|
|
|
|
"""期权询价推送"""
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
#----------------------------------------------------------------------
|
2015-09-28 03:42:35 +00:00
|
|
|
|
def connect(self, userID, password, brokerID, address):
|
2015-08-14 08:11:53 +00:00
|
|
|
|
"""初始化连接"""
|
2015-09-28 03:42:35 +00:00
|
|
|
|
self.userID = userID # 账号
|
|
|
|
|
self.password = password # 密码
|
|
|
|
|
self.brokerID = brokerID # 经纪商代码
|
|
|
|
|
self.address = address # 服务器地址
|
|
|
|
|
|
2015-08-14 08:11:53 +00:00
|
|
|
|
# 如果尚未建立服务器连接,则进行连接
|
|
|
|
|
if not self.connectionStatus:
|
|
|
|
|
# 创建C++环境中的API对象,这里传入的参数是需要用来保存.con文件的文件夹路径
|
2016-04-14 15:07:06 +00:00
|
|
|
|
path = os.getcwd() + '/temp/' + self.gatewayName + '/'
|
2015-08-14 08:11:53 +00:00
|
|
|
|
if not os.path.exists(path):
|
|
|
|
|
os.makedirs(path)
|
|
|
|
|
self.createFtdcMdApi(path)
|
|
|
|
|
|
|
|
|
|
# 注册服务器地址
|
|
|
|
|
self.registerFront(self.address)
|
|
|
|
|
|
|
|
|
|
# 初始化连接,成功会调用onFrontConnected
|
|
|
|
|
self.init()
|
|
|
|
|
|
|
|
|
|
# 若已经连接但尚未登录,则进行登录
|
|
|
|
|
else:
|
|
|
|
|
if not self.loginStatus:
|
|
|
|
|
self.login()
|
|
|
|
|
|
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
def subscribe(self, subscribeReq):
|
|
|
|
|
"""订阅合约"""
|
2015-10-16 08:50:44 +00:00
|
|
|
|
# 这里的设计是,如果尚未登录就调用了订阅方法
|
|
|
|
|
# 则先保存订阅请求,登录完成后会自动订阅
|
|
|
|
|
if self.loginStatus:
|
|
|
|
|
self.subscribeMarketData(str(subscribeReq.symbol))
|
2015-08-14 08:11:53 +00:00
|
|
|
|
self.subscribedSymbols.add(subscribeReq)
|
|
|
|
|
|
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
def login(self):
|
|
|
|
|
"""登录"""
|
|
|
|
|
# 如果填入了用户名密码等,则登录
|
|
|
|
|
if self.userID and self.password and self.brokerID:
|
|
|
|
|
req = {}
|
|
|
|
|
req['UserID'] = self.userID
|
|
|
|
|
req['Password'] = self.password
|
|
|
|
|
req['BrokerID'] = self.brokerID
|
|
|
|
|
self.reqID += 1
|
2015-09-28 03:42:35 +00:00
|
|
|
|
self.reqUserLogin(req, self.reqID)
|
|
|
|
|
|
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
def close(self):
|
|
|
|
|
"""关闭"""
|
|
|
|
|
self.exit()
|
2015-08-14 08:11:53 +00:00
|
|
|
|
|
2015-09-09 01:20:02 +00:00
|
|
|
|
|
2015-08-14 08:11:53 +00:00
|
|
|
|
########################################################################
|
|
|
|
|
class CtpTdApi(TdApi):
|
|
|
|
|
"""CTP交易API实现"""
|
2015-09-28 03:42:35 +00:00
|
|
|
|
|
2015-08-14 08:11:53 +00:00
|
|
|
|
#----------------------------------------------------------------------
|
2015-09-28 03:42:35 +00:00
|
|
|
|
def __init__(self, gateway):
|
2015-08-14 08:11:53 +00:00
|
|
|
|
"""API对象的初始化函数"""
|
|
|
|
|
super(CtpTdApi, self).__init__()
|
|
|
|
|
|
|
|
|
|
self.gateway = gateway # gateway对象
|
|
|
|
|
self.gatewayName = gateway.gatewayName # gateway对象名称
|
|
|
|
|
|
|
|
|
|
self.reqID = EMPTY_INT # 操作请求编号
|
|
|
|
|
self.orderRef = EMPTY_INT # 订单编号
|
|
|
|
|
|
|
|
|
|
self.connectionStatus = False # 连接状态
|
|
|
|
|
self.loginStatus = False # 登录状态
|
|
|
|
|
|
2015-09-28 03:42:35 +00:00
|
|
|
|
self.userID = EMPTY_STRING # 账号
|
|
|
|
|
self.password = EMPTY_STRING # 密码
|
|
|
|
|
self.brokerID = EMPTY_STRING # 经纪商代码
|
|
|
|
|
self.address = EMPTY_STRING # 服务器地址
|
2015-08-14 08:11:53 +00:00
|
|
|
|
|
2015-10-16 08:50:44 +00:00
|
|
|
|
self.frontID = EMPTY_INT # 前置机编号
|
|
|
|
|
self.sessionID = EMPTY_INT # 会话编号
|
|
|
|
|
|
2016-04-27 14:49:28 +00:00
|
|
|
|
self.posBufferDict = {} # 缓存持仓数据的字典
|
2016-06-05 11:31:51 +00:00
|
|
|
|
self.symbolExchangeDict = {} # 保存合约代码和交易所的印射关系
|
2016-07-01 15:07:41 +00:00
|
|
|
|
self.symbolSizeDict = {} # 保存合约代码和合约大小的印射关系
|
2016-04-02 09:04:58 +00:00
|
|
|
|
|
2015-08-14 08:11:53 +00:00
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
def onFrontConnected(self):
|
|
|
|
|
"""服务器连接"""
|
|
|
|
|
self.connectionStatus = True
|
2016-07-25 14:32:55 +00:00
|
|
|
|
|
2015-08-14 08:11:53 +00:00
|
|
|
|
log = VtLogData()
|
|
|
|
|
log.gatewayName = self.gatewayName
|
|
|
|
|
log.logContent = u'交易服务器连接成功'
|
|
|
|
|
self.gateway.onLog(log)
|
|
|
|
|
|
2016-07-25 14:32:55 +00:00
|
|
|
|
self.login()
|
|
|
|
|
|
2015-08-14 08:11:53 +00:00
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
def onFrontDisconnected(self, n):
|
|
|
|
|
"""服务器断开"""
|
|
|
|
|
self.connectionStatus = False
|
|
|
|
|
self.loginStatus = False
|
|
|
|
|
self.gateway.tdConnected = False
|
2016-07-25 14:32:55 +00:00
|
|
|
|
|
2015-08-14 08:11:53 +00:00
|
|
|
|
log = VtLogData()
|
|
|
|
|
log.gatewayName = self.gatewayName
|
|
|
|
|
log.logContent = u'交易服务器连接断开'
|
2016-07-25 14:32:55 +00:00
|
|
|
|
self.gateway.onLog(log)
|
|
|
|
|
|
2015-08-14 08:11:53 +00:00
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
def onHeartBeatWarning(self, n):
|
|
|
|
|
""""""
|
|
|
|
|
pass
|
2016-07-25 14:32:55 +00:00
|
|
|
|
|
2015-08-14 08:11:53 +00:00
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
def onRspAuthenticate(self, data, error, n, last):
|
|
|
|
|
""""""
|
|
|
|
|
pass
|
2016-07-25 14:32:55 +00:00
|
|
|
|
|
2015-08-14 08:11:53 +00:00
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
def onRspUserLogin(self, data, error, n, last):
|
|
|
|
|
"""登陆回报"""
|
|
|
|
|
# 如果登录成功,推送日志信息
|
|
|
|
|
if error['ErrorID'] == 0:
|
2015-10-16 08:50:44 +00:00
|
|
|
|
self.frontID = str(data['FrontID'])
|
|
|
|
|
self.sessionID = str(data['SessionID'])
|
2015-08-14 08:11:53 +00:00
|
|
|
|
self.loginStatus = True
|
2016-06-11 03:30:03 +00:00
|
|
|
|
self.gateway.tdConnected = True
|
2015-08-14 08:11:53 +00:00
|
|
|
|
|
|
|
|
|
log = VtLogData()
|
|
|
|
|
log.gatewayName = self.gatewayName
|
|
|
|
|
log.logContent = u'交易服务器登录完成'
|
|
|
|
|
self.gateway.onLog(log)
|
|
|
|
|
|
|
|
|
|
# 确认结算信息
|
|
|
|
|
req = {}
|
|
|
|
|
req['BrokerID'] = self.brokerID
|
|
|
|
|
req['InvestorID'] = self.userID
|
|
|
|
|
self.reqID += 1
|
|
|
|
|
self.reqSettlementInfoConfirm(req, self.reqID)
|
|
|
|
|
|
|
|
|
|
# 否则,推送错误信息
|
|
|
|
|
else:
|
|
|
|
|
err = VtErrorData()
|
2016-04-02 09:04:58 +00:00
|
|
|
|
err.gatewayName = self.gatewayName
|
2015-08-14 08:11:53 +00:00
|
|
|
|
err.errorID = error['ErrorID']
|
|
|
|
|
err.errorMsg = error['ErrorMsg'].decode('gbk')
|
|
|
|
|
self.gateway.onError(err)
|
2016-07-25 14:32:55 +00:00
|
|
|
|
|
2015-08-14 08:11:53 +00:00
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
def onRspUserLogout(self, data, error, n, last):
|
|
|
|
|
"""登出回报"""
|
|
|
|
|
# 如果登出成功,推送日志信息
|
|
|
|
|
if error['ErrorID'] == 0:
|
|
|
|
|
self.loginStatus = False
|
|
|
|
|
self.gateway.tdConnected = False
|
|
|
|
|
|
|
|
|
|
log = VtLogData()
|
|
|
|
|
log.gatewayName = self.gatewayName
|
|
|
|
|
log.logContent = u'交易服务器登出完成'
|
|
|
|
|
self.gateway.onLog(log)
|
|
|
|
|
|
|
|
|
|
# 否则,推送错误信息
|
|
|
|
|
else:
|
|
|
|
|
err = VtErrorData()
|
|
|
|
|
err.gatewayName = self.gatewayName
|
|
|
|
|
err.errorID = error['ErrorID']
|
|
|
|
|
err.errorMsg = error['ErrorMsg'].decode('gbk')
|
|
|
|
|
self.gateway.onError(err)
|
2016-07-25 14:32:55 +00:00
|
|
|
|
|
2015-08-14 08:11:53 +00:00
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
def onRspUserPasswordUpdate(self, data, error, n, last):
|
|
|
|
|
""""""
|
|
|
|
|
pass
|
2016-07-25 14:32:55 +00:00
|
|
|
|
|
2015-08-14 08:11:53 +00:00
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
def onRspTradingAccountPasswordUpdate(self, data, error, n, last):
|
|
|
|
|
""""""
|
|
|
|
|
pass
|
2016-07-25 14:32:55 +00:00
|
|
|
|
|
2015-08-14 08:11:53 +00:00
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
def onRspOrderInsert(self, data, error, n, last):
|
|
|
|
|
"""发单错误(柜台)"""
|
|
|
|
|
err = VtErrorData()
|
|
|
|
|
err.gatewayName = self.gatewayName
|
|
|
|
|
err.errorID = error['ErrorID']
|
|
|
|
|
err.errorMsg = error['ErrorMsg'].decode('gbk')
|
|
|
|
|
self.gateway.onError(err)
|
2016-07-25 14:32:55 +00:00
|
|
|
|
|
2015-08-14 08:11:53 +00:00
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
def onRspParkedOrderInsert(self, data, error, n, last):
|
|
|
|
|
""""""
|
|
|
|
|
pass
|
2016-07-25 14:32:55 +00:00
|
|
|
|
|
2015-08-14 08:11:53 +00:00
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
def onRspParkedOrderAction(self, data, error, n, last):
|
|
|
|
|
""""""
|
|
|
|
|
pass
|
2016-07-25 14:32:55 +00:00
|
|
|
|
|
2015-08-14 08:11:53 +00:00
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
def onRspOrderAction(self, data, error, n, last):
|
|
|
|
|
"""撤单错误(柜台)"""
|
|
|
|
|
err = VtErrorData()
|
|
|
|
|
err.gatewayName = self.gatewayName
|
|
|
|
|
err.errorID = error['ErrorID']
|
|
|
|
|
err.errorMsg = error['ErrorMsg'].decode('gbk')
|
|
|
|
|
self.gateway.onError(err)
|
2016-07-25 14:32:55 +00:00
|
|
|
|
|
2015-08-14 08:11:53 +00:00
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
def onRspQueryMaxOrderVolume(self, data, error, n, last):
|
|
|
|
|
""""""
|
|
|
|
|
pass
|
2016-07-25 14:32:55 +00:00
|
|
|
|
|
2015-08-14 08:11:53 +00:00
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
def onRspSettlementInfoConfirm(self, data, error, n, last):
|
|
|
|
|
"""确认结算信息回报"""
|
|
|
|
|
log = VtLogData()
|
|
|
|
|
log.gatewayName = self.gatewayName
|
|
|
|
|
log.logContent = u'结算信息确认完成'
|
|
|
|
|
self.gateway.onLog(log)
|
2016-07-25 14:32:55 +00:00
|
|
|
|
|
2015-08-14 08:11:53 +00:00
|
|
|
|
# 查询合约代码
|
|
|
|
|
self.reqID += 1
|
|
|
|
|
self.reqQryInstrument({}, self.reqID)
|
|
|
|
|
|
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
def onRspRemoveParkedOrder(self, data, error, n, last):
|
|
|
|
|
""""""
|
|
|
|
|
pass
|
2016-07-25 14:32:55 +00:00
|
|
|
|
|
2015-08-14 08:11:53 +00:00
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
def onRspRemoveParkedOrderAction(self, data, error, n, last):
|
|
|
|
|
""""""
|
|
|
|
|
pass
|
2016-07-25 14:32:55 +00:00
|
|
|
|
|
2015-08-14 08:11:53 +00:00
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
def onRspExecOrderInsert(self, data, error, n, last):
|
|
|
|
|
""""""
|
|
|
|
|
pass
|
2016-07-25 14:32:55 +00:00
|
|
|
|
|
2015-08-14 08:11:53 +00:00
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
def onRspExecOrderAction(self, data, error, n, last):
|
|
|
|
|
""""""
|
|
|
|
|
pass
|
2016-07-25 14:32:55 +00:00
|
|
|
|
|
2015-08-14 08:11:53 +00:00
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
def onRspForQuoteInsert(self, data, error, n, last):
|
|
|
|
|
""""""
|
|
|
|
|
pass
|
2016-07-25 14:32:55 +00:00
|
|
|
|
|
2015-08-14 08:11:53 +00:00
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
def onRspQuoteInsert(self, data, error, n, last):
|
|
|
|
|
""""""
|
|
|
|
|
pass
|
2016-07-25 14:32:55 +00:00
|
|
|
|
|
2015-08-14 08:11:53 +00:00
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
def onRspQuoteAction(self, data, error, n, last):
|
|
|
|
|
""""""
|
|
|
|
|
pass
|
2016-07-25 14:32:55 +00:00
|
|
|
|
|
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
def onRspLockInsert(self, data, error, n, last):
|
|
|
|
|
""""""
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
def onRspCombActionInsert(self, data, error, n, last):
|
|
|
|
|
""""""
|
|
|
|
|
pass
|
|
|
|
|
|
2015-08-14 08:11:53 +00:00
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
def onRspQryOrder(self, data, error, n, last):
|
|
|
|
|
""""""
|
|
|
|
|
pass
|
2016-07-25 14:32:55 +00:00
|
|
|
|
|
2015-08-14 08:11:53 +00:00
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
def onRspQryTrade(self, data, error, n, last):
|
|
|
|
|
""""""
|
|
|
|
|
pass
|
2016-07-25 14:32:55 +00:00
|
|
|
|
|
2015-08-14 08:11:53 +00:00
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
def onRspQryInvestorPosition(self, data, error, n, last):
|
|
|
|
|
"""持仓查询回报"""
|
2016-04-27 14:49:28 +00:00
|
|
|
|
# 获取缓存字典中的持仓缓存,若无则创建并初始化
|
2016-04-02 09:04:58 +00:00
|
|
|
|
positionName = '.'.join([data['InstrumentID'], data['PosiDirection']])
|
2015-09-09 01:20:02 +00:00
|
|
|
|
|
2016-04-27 14:49:28 +00:00
|
|
|
|
if positionName in self.posBufferDict:
|
|
|
|
|
posBuffer = self.posBufferDict[positionName]
|
2016-04-02 09:04:58 +00:00
|
|
|
|
else:
|
2016-04-27 14:49:28 +00:00
|
|
|
|
posBuffer = PositionBuffer(data, self.gatewayName)
|
|
|
|
|
self.posBufferDict[positionName] = posBuffer
|
2015-09-09 01:20:02 +00:00
|
|
|
|
|
2016-04-27 14:49:28 +00:00
|
|
|
|
# 更新持仓缓存,并获取VT系统中持仓对象的返回值
|
2016-06-05 11:31:51 +00:00
|
|
|
|
exchange = self.symbolExchangeDict.get(data['InstrumentID'], EXCHANGE_UNKNOWN)
|
2016-07-01 15:07:41 +00:00
|
|
|
|
size = self.symbolSizeDict.get(data['InstrumentID'], 1)
|
2016-06-05 11:31:51 +00:00
|
|
|
|
if exchange == EXCHANGE_SHFE:
|
2016-07-01 15:07:41 +00:00
|
|
|
|
pos = posBuffer.updateShfeBuffer(data, size)
|
2016-06-05 11:31:51 +00:00
|
|
|
|
else:
|
2016-07-01 15:07:41 +00:00
|
|
|
|
pos = posBuffer.updateBuffer(data, size)
|
2016-04-27 14:49:28 +00:00
|
|
|
|
self.gateway.onPosition(pos)
|
2016-07-25 14:32:55 +00:00
|
|
|
|
|
2015-08-14 08:11:53 +00:00
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
def onRspQryTradingAccount(self, data, error, n, last):
|
|
|
|
|
"""资金账户查询回报"""
|
2015-09-09 01:20:02 +00:00
|
|
|
|
account = VtAccountData()
|
|
|
|
|
account.gatewayName = self.gatewayName
|
2016-07-25 14:32:55 +00:00
|
|
|
|
|
2015-09-09 01:20:02 +00:00
|
|
|
|
# 账户代码
|
|
|
|
|
account.accountID = data['AccountID']
|
|
|
|
|
account.vtAccountID = '.'.join([self.gatewayName, account.accountID])
|
2016-07-25 14:32:55 +00:00
|
|
|
|
|
2015-09-09 01:20:02 +00:00
|
|
|
|
# 数值相关
|
|
|
|
|
account.preBalance = data['PreBalance']
|
|
|
|
|
account.available = data['Available']
|
|
|
|
|
account.commission = data['Commission']
|
|
|
|
|
account.margin = data['CurrMargin']
|
|
|
|
|
account.closeProfit = data['CloseProfit']
|
|
|
|
|
account.positionProfit = data['PositionProfit']
|
2016-07-25 14:32:55 +00:00
|
|
|
|
|
2015-09-09 01:20:02 +00:00
|
|
|
|
# 这里的balance和快期中的账户不确定是否一样,需要测试
|
|
|
|
|
account.balance = (data['PreBalance'] - data['PreCredit'] - data['PreMortgage'] +
|
|
|
|
|
data['Mortgage'] - data['Withdraw'] + data['Deposit'] +
|
2015-10-16 08:50:44 +00:00
|
|
|
|
data['CloseProfit'] + data['PositionProfit'] + data['CashIn'] -
|
2015-09-09 01:20:02 +00:00
|
|
|
|
data['Commission'])
|
2016-07-25 14:32:55 +00:00
|
|
|
|
|
2015-09-09 01:20:02 +00:00
|
|
|
|
# 推送
|
|
|
|
|
self.gateway.onAccount(account)
|
2016-07-25 14:32:55 +00:00
|
|
|
|
|
2015-08-14 08:11:53 +00:00
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
def onRspQryInvestor(self, data, error, n, last):
|
2016-07-25 14:32:55 +00:00
|
|
|
|
""""""
|
2015-08-14 08:11:53 +00:00
|
|
|
|
pass
|
2016-07-25 14:32:55 +00:00
|
|
|
|
|
2015-08-14 08:11:53 +00:00
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
def onRspQryTradingCode(self, data, error, n, last):
|
|
|
|
|
""""""
|
|
|
|
|
pass
|
2016-07-25 14:32:55 +00:00
|
|
|
|
|
2015-08-14 08:11:53 +00:00
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
def onRspQryInstrumentMarginRate(self, data, error, n, last):
|
|
|
|
|
""""""
|
|
|
|
|
pass
|
2016-07-25 14:32:55 +00:00
|
|
|
|
|
2015-08-14 08:11:53 +00:00
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
def onRspQryInstrumentCommissionRate(self, data, error, n, last):
|
|
|
|
|
""""""
|
|
|
|
|
pass
|
2016-07-25 14:32:55 +00:00
|
|
|
|
|
2015-08-14 08:11:53 +00:00
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
def onRspQryExchange(self, data, error, n, last):
|
|
|
|
|
""""""
|
|
|
|
|
pass
|
2016-07-25 14:32:55 +00:00
|
|
|
|
|
2015-08-14 08:11:53 +00:00
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
def onRspQryProduct(self, data, error, n, last):
|
|
|
|
|
""""""
|
|
|
|
|
pass
|
2016-07-25 14:32:55 +00:00
|
|
|
|
|
2015-08-14 08:11:53 +00:00
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
def onRspQryInstrument(self, data, error, n, last):
|
2015-09-09 01:20:02 +00:00
|
|
|
|
"""合约查询回报"""
|
|
|
|
|
contract = VtContractData()
|
|
|
|
|
contract.gatewayName = self.gatewayName
|
2015-10-16 08:50:44 +00:00
|
|
|
|
|
2015-09-09 01:20:02 +00:00
|
|
|
|
contract.symbol = data['InstrumentID']
|
2015-10-16 08:50:44 +00:00
|
|
|
|
contract.exchange = exchangeMapReverse[data['ExchangeID']]
|
|
|
|
|
contract.vtSymbol = contract.symbol #'.'.join([contract.symbol, contract.exchange])
|
|
|
|
|
contract.name = data['InstrumentName'].decode('GBK')
|
2016-07-25 14:32:55 +00:00
|
|
|
|
|
2015-09-09 01:20:02 +00:00
|
|
|
|
# 合约数值
|
|
|
|
|
contract.size = data['VolumeMultiple']
|
|
|
|
|
contract.priceTick = data['PriceTick']
|
|
|
|
|
contract.strikePrice = data['StrikePrice']
|
|
|
|
|
contract.underlyingSymbol = data['UnderlyingInstrID']
|
2016-07-25 14:32:55 +00:00
|
|
|
|
|
|
|
|
|
contract.productClass = productClassMapReverse.get(data['ProductClass'], PRODUCT_UNKNOWN)
|
|
|
|
|
|
2015-09-09 01:20:02 +00:00
|
|
|
|
# 期权类型
|
2015-10-16 08:50:44 +00:00
|
|
|
|
if data['OptionsType'] == '1':
|
2015-09-09 01:20:02 +00:00
|
|
|
|
contract.optionType = OPTION_CALL
|
2015-10-16 08:50:44 +00:00
|
|
|
|
elif data['OptionsType'] == '2':
|
2015-09-09 01:20:02 +00:00
|
|
|
|
contract.optionType = OPTION_PUT
|
2016-07-25 14:32:55 +00:00
|
|
|
|
|
2016-06-05 11:31:51 +00:00
|
|
|
|
# 缓存代码和交易所的印射关系
|
|
|
|
|
self.symbolExchangeDict[contract.symbol] = contract.exchange
|
2016-07-01 15:07:41 +00:00
|
|
|
|
self.symbolSizeDict[contract.symbol] = contract.size
|
2016-07-25 14:32:55 +00:00
|
|
|
|
|
2015-09-09 01:20:02 +00:00
|
|
|
|
# 推送
|
|
|
|
|
self.gateway.onContract(contract)
|
2016-07-25 14:32:55 +00:00
|
|
|
|
|
2015-10-16 08:50:44 +00:00
|
|
|
|
if last:
|
|
|
|
|
log = VtLogData()
|
|
|
|
|
log.gatewayName = self.gatewayName
|
|
|
|
|
log.logContent = u'交易合约信息获取完成'
|
|
|
|
|
self.gateway.onLog(log)
|
2016-07-25 14:32:55 +00:00
|
|
|
|
|
2015-08-14 08:11:53 +00:00
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
def onRspQryDepthMarketData(self, data, error, n, last):
|
|
|
|
|
""""""
|
|
|
|
|
pass
|
2016-07-25 14:32:55 +00:00
|
|
|
|
|
2015-08-14 08:11:53 +00:00
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
def onRspQrySettlementInfo(self, data, error, n, last):
|
2016-07-25 14:32:55 +00:00
|
|
|
|
""""""
|
2015-08-14 08:11:53 +00:00
|
|
|
|
pass
|
2016-07-25 14:32:55 +00:00
|
|
|
|
|
2015-08-14 08:11:53 +00:00
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
def onRspQryTransferBank(self, data, error, n, last):
|
|
|
|
|
""""""
|
|
|
|
|
pass
|
2016-07-25 14:32:55 +00:00
|
|
|
|
|
2015-08-14 08:11:53 +00:00
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
def onRspQryInvestorPositionDetail(self, data, error, n, last):
|
|
|
|
|
""""""
|
|
|
|
|
pass
|
2016-07-25 14:32:55 +00:00
|
|
|
|
|
2015-08-14 08:11:53 +00:00
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
def onRspQryNotice(self, data, error, n, last):
|
|
|
|
|
""""""
|
|
|
|
|
pass
|
2016-07-25 14:32:55 +00:00
|
|
|
|
|
2015-08-14 08:11:53 +00:00
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
def onRspQrySettlementInfoConfirm(self, data, error, n, last):
|
|
|
|
|
""""""
|
|
|
|
|
pass
|
2016-07-25 14:32:55 +00:00
|
|
|
|
|
2015-08-14 08:11:53 +00:00
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
def onRspQryInvestorPositionCombineDetail(self, data, error, n, last):
|
|
|
|
|
""""""
|
|
|
|
|
pass
|
2016-07-25 14:32:55 +00:00
|
|
|
|
|
2015-08-14 08:11:53 +00:00
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
def onRspQryCFMMCTradingAccountKey(self, data, error, n, last):
|
|
|
|
|
""""""
|
|
|
|
|
pass
|
2016-07-25 14:32:55 +00:00
|
|
|
|
|
2015-08-14 08:11:53 +00:00
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
def onRspQryEWarrantOffset(self, data, error, n, last):
|
|
|
|
|
""""""
|
|
|
|
|
pass
|
2016-07-25 14:32:55 +00:00
|
|
|
|
|
2015-08-14 08:11:53 +00:00
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
def onRspQryInvestorProductGroupMargin(self, data, error, n, last):
|
|
|
|
|
""""""
|
|
|
|
|
pass
|
2016-07-25 14:32:55 +00:00
|
|
|
|
|
2015-08-14 08:11:53 +00:00
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
def onRspQryExchangeMarginRate(self, data, error, n, last):
|
|
|
|
|
""""""
|
|
|
|
|
pass
|
2016-07-25 14:32:55 +00:00
|
|
|
|
|
2015-08-14 08:11:53 +00:00
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
def onRspQryExchangeMarginRateAdjust(self, data, error, n, last):
|
|
|
|
|
""""""
|
|
|
|
|
pass
|
2016-07-25 14:32:55 +00:00
|
|
|
|
|
2015-08-14 08:11:53 +00:00
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
def onRspQryExchangeRate(self, data, error, n, last):
|
|
|
|
|
""""""
|
|
|
|
|
pass
|
2016-07-25 14:32:55 +00:00
|
|
|
|
|
2015-08-14 08:11:53 +00:00
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
def onRspQrySecAgentACIDMap(self, data, error, n, last):
|
|
|
|
|
""""""
|
|
|
|
|
pass
|
2016-07-25 14:32:55 +00:00
|
|
|
|
|
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
def onRspQryProductExchRate(self, data, error, n, last):
|
|
|
|
|
""""""
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
def onRspQryProductGroup(self, data, error, n, last):
|
|
|
|
|
""""""
|
|
|
|
|
pass
|
|
|
|
|
|
2015-08-14 08:11:53 +00:00
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
def onRspQryOptionInstrTradeCost(self, data, error, n, last):
|
|
|
|
|
""""""
|
|
|
|
|
pass
|
2016-07-25 14:32:55 +00:00
|
|
|
|
|
2015-08-14 08:11:53 +00:00
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
def onRspQryOptionInstrCommRate(self, data, error, n, last):
|
|
|
|
|
""""""
|
|
|
|
|
pass
|
2016-07-25 14:32:55 +00:00
|
|
|
|
|
2015-08-14 08:11:53 +00:00
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
def onRspQryExecOrder(self, data, error, n, last):
|
|
|
|
|
""""""
|
|
|
|
|
pass
|
2016-07-25 14:32:55 +00:00
|
|
|
|
|
2015-08-14 08:11:53 +00:00
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
def onRspQryForQuote(self, data, error, n, last):
|
|
|
|
|
""""""
|
|
|
|
|
pass
|
2016-07-25 14:32:55 +00:00
|
|
|
|
|
2015-08-14 08:11:53 +00:00
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
def onRspQryQuote(self, data, error, n, last):
|
|
|
|
|
""""""
|
|
|
|
|
pass
|
2016-07-25 14:32:55 +00:00
|
|
|
|
|
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
def onRspQryLock(self, data, error, n, last):
|
|
|
|
|
""""""
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
def onRspQryLockPosition(self, data, error, n, last):
|
|
|
|
|
""""""
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
def onRspQryInvestorLevel(self, data, error, n, last):
|
|
|
|
|
""""""
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
def onRspQryExecFreeze(self, data, error, n, last):
|
|
|
|
|
""""""
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
def onRspQryCombInstrumentGuard(self, data, error, n, last):
|
|
|
|
|
""""""
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
def onRspQryCombAction(self, data, error, n, last):
|
|
|
|
|
""""""
|
|
|
|
|
pass
|
|
|
|
|
|
2015-08-14 08:11:53 +00:00
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
def onRspQryTransferSerial(self, data, error, n, last):
|
|
|
|
|
""""""
|
|
|
|
|
pass
|
2016-07-25 14:32:55 +00:00
|
|
|
|
|
2015-08-14 08:11:53 +00:00
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
def onRspQryAccountregister(self, data, error, n, last):
|
|
|
|
|
""""""
|
|
|
|
|
pass
|
2016-07-25 14:32:55 +00:00
|
|
|
|
|
2015-08-14 08:11:53 +00:00
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
def onRspError(self, error, n, last):
|
|
|
|
|
"""错误回报"""
|
|
|
|
|
err = VtErrorData()
|
|
|
|
|
err.gatewayName = self.gatewayName
|
|
|
|
|
err.errorID = error['ErrorID']
|
|
|
|
|
err.errorMsg = error['ErrorMsg'].decode('gbk')
|
|
|
|
|
self.gateway.onError(err)
|
2016-07-25 14:32:55 +00:00
|
|
|
|
|
2015-08-14 08:11:53 +00:00
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
def onRtnOrder(self, data):
|
|
|
|
|
"""报单回报"""
|
|
|
|
|
# 更新最大报单编号
|
|
|
|
|
newref = data['OrderRef']
|
|
|
|
|
self.orderRef = max(self.orderRef, int(newref))
|
|
|
|
|
|
|
|
|
|
# 创建报单数据对象
|
|
|
|
|
order = VtOrderData()
|
|
|
|
|
order.gatewayName = self.gatewayName
|
|
|
|
|
|
|
|
|
|
# 保存代码和报单号
|
|
|
|
|
order.symbol = data['InstrumentID']
|
2015-10-16 08:50:44 +00:00
|
|
|
|
order.exchange = exchangeMapReverse[data['ExchangeID']]
|
|
|
|
|
order.vtSymbol = order.symbol #'.'.join([order.symbol, order.exchange])
|
2015-08-14 08:11:53 +00:00
|
|
|
|
|
|
|
|
|
order.orderID = data['OrderRef']
|
|
|
|
|
|
|
|
|
|
# 方向
|
|
|
|
|
if data['Direction'] == '0':
|
|
|
|
|
order.direction = DIRECTION_LONG
|
|
|
|
|
elif data['Direction'] == '1':
|
|
|
|
|
order.direction = DIRECTION_SHORT
|
|
|
|
|
else:
|
|
|
|
|
order.direction = DIRECTION_UNKNOWN
|
|
|
|
|
|
2015-09-09 01:20:02 +00:00
|
|
|
|
# 开平
|
|
|
|
|
if data['CombOffsetFlag'] == '0':
|
|
|
|
|
order.offset = OFFSET_OPEN
|
|
|
|
|
elif data['CombOffsetFlag'] == '1':
|
|
|
|
|
order.offset = OFFSET_CLOSE
|
|
|
|
|
else:
|
|
|
|
|
order.offset = OFFSET_UNKNOWN
|
|
|
|
|
|
|
|
|
|
# 状态
|
|
|
|
|
if data['OrderStatus'] == '0':
|
|
|
|
|
order.status = STATUS_ALLTRADED
|
|
|
|
|
elif data['OrderStatus'] == '1':
|
|
|
|
|
order.status = STATUS_PARTTRADED
|
|
|
|
|
elif data['OrderStatus'] == '3':
|
|
|
|
|
order.status = STATUS_NOTTRADED
|
|
|
|
|
elif data['OrderStatus'] == '5':
|
|
|
|
|
order.status = STATUS_CANCELLED
|
|
|
|
|
else:
|
|
|
|
|
order.status = STATUS_UNKNOWN
|
|
|
|
|
|
|
|
|
|
# 价格、报单量等数值
|
|
|
|
|
order.price = data['LimitPrice']
|
|
|
|
|
order.totalVolume = data['VolumeTotalOriginal']
|
|
|
|
|
order.tradedVolume = data['VolumeTraded']
|
|
|
|
|
order.orderTime = data['InsertTime']
|
|
|
|
|
order.cancelTime = data['CancelTime']
|
|
|
|
|
order.frontID = data['FrontID']
|
|
|
|
|
order.sessionID = data['SessionID']
|
|
|
|
|
|
2015-10-16 08:50:44 +00:00
|
|
|
|
# CTP的报单号一致性维护需要基于frontID, sessionID, orderID三个字段
|
|
|
|
|
# 但在本接口设计中,已经考虑了CTP的OrderRef的自增性,避免重复
|
|
|
|
|
# 唯一可能出现OrderRef重复的情况是多处登录并在非常接近的时间内(几乎同时发单)
|
|
|
|
|
# 考虑到VtTrader的应用场景,认为以上情况不会构成问题
|
|
|
|
|
order.vtOrderID = '.'.join([self.gatewayName, order.orderID])
|
|
|
|
|
|
2015-09-09 01:20:02 +00:00
|
|
|
|
# 推送
|
|
|
|
|
self.gateway.onOrder(order)
|
2016-07-25 14:32:55 +00:00
|
|
|
|
|
2015-08-14 08:11:53 +00:00
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
def onRtnTrade(self, data):
|
|
|
|
|
"""成交回报"""
|
2015-09-09 01:20:02 +00:00
|
|
|
|
# 创建报单数据对象
|
|
|
|
|
trade = VtTradeData()
|
2015-10-16 08:50:44 +00:00
|
|
|
|
trade.gatewayName = self.gatewayName
|
2015-09-09 01:20:02 +00:00
|
|
|
|
|
|
|
|
|
# 保存代码和报单号
|
|
|
|
|
trade.symbol = data['InstrumentID']
|
2015-10-16 08:50:44 +00:00
|
|
|
|
trade.exchange = exchangeMapReverse[data['ExchangeID']]
|
|
|
|
|
trade.vtSymbol = trade.symbol #'.'.join([trade.symbol, trade.exchange])
|
2015-09-09 01:20:02 +00:00
|
|
|
|
|
|
|
|
|
trade.tradeID = data['TradeID']
|
|
|
|
|
trade.vtTradeID = '.'.join([self.gatewayName, trade.tradeID])
|
|
|
|
|
|
|
|
|
|
trade.orderID = data['OrderRef']
|
|
|
|
|
trade.vtOrderID = '.'.join([self.gatewayName, trade.orderID])
|
|
|
|
|
|
|
|
|
|
# 方向
|
2015-10-16 08:50:44 +00:00
|
|
|
|
trade.direction = directionMapReverse.get(data['Direction'], '')
|
2015-09-09 01:20:02 +00:00
|
|
|
|
|
|
|
|
|
# 开平
|
2015-10-16 08:50:44 +00:00
|
|
|
|
trade.offset = offsetMapReverse.get(data['OffsetFlag'], '')
|
2015-09-09 01:20:02 +00:00
|
|
|
|
|
|
|
|
|
# 价格、报单量等数值
|
|
|
|
|
trade.price = data['Price']
|
|
|
|
|
trade.volume = data['Volume']
|
|
|
|
|
trade.tradeTime = data['TradeTime']
|
2015-08-14 08:11:53 +00:00
|
|
|
|
|
2015-09-09 01:20:02 +00:00
|
|
|
|
# 推送
|
|
|
|
|
self.gateway.onTrade(trade)
|
2016-07-25 14:32:55 +00:00
|
|
|
|
|
2015-08-14 08:11:53 +00:00
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
def onErrRtnOrderInsert(self, data, error):
|
|
|
|
|
"""发单错误回报(交易所)"""
|
|
|
|
|
err = VtErrorData()
|
|
|
|
|
err.gatewayName = self.gatewayName
|
|
|
|
|
err.errorID = error['ErrorID']
|
|
|
|
|
err.errorMsg = error['ErrorMsg'].decode('gbk')
|
|
|
|
|
self.gateway.onError(err)
|
2016-07-25 14:32:55 +00:00
|
|
|
|
|
2015-08-14 08:11:53 +00:00
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
def onErrRtnOrderAction(self, data, error):
|
|
|
|
|
"""撤单错误回报(交易所)"""
|
|
|
|
|
err = VtErrorData()
|
|
|
|
|
err.gatewayName = self.gatewayName
|
|
|
|
|
err.errorID = error['ErrorID']
|
|
|
|
|
err.errorMsg = error['ErrorMsg'].decode('gbk')
|
|
|
|
|
self.gateway.onError(err)
|
2016-07-25 14:32:55 +00:00
|
|
|
|
|
2015-08-14 08:11:53 +00:00
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
def onRtnInstrumentStatus(self, data):
|
|
|
|
|
""""""
|
|
|
|
|
pass
|
2016-07-25 14:32:55 +00:00
|
|
|
|
|
2015-08-14 08:11:53 +00:00
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
def onRtnTradingNotice(self, data):
|
|
|
|
|
""""""
|
|
|
|
|
pass
|
2016-07-25 14:32:55 +00:00
|
|
|
|
|
2015-08-14 08:11:53 +00:00
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
def onRtnErrorConditionalOrder(self, data):
|
|
|
|
|
""""""
|
|
|
|
|
pass
|
2016-07-25 14:32:55 +00:00
|
|
|
|
|
2015-08-14 08:11:53 +00:00
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
def onRtnExecOrder(self, data):
|
|
|
|
|
""""""
|
|
|
|
|
pass
|
2016-07-25 14:32:55 +00:00
|
|
|
|
|
2015-08-14 08:11:53 +00:00
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
def onErrRtnExecOrderInsert(self, data, error):
|
|
|
|
|
""""""
|
|
|
|
|
pass
|
2016-07-25 14:32:55 +00:00
|
|
|
|
|
2015-08-14 08:11:53 +00:00
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
def onErrRtnExecOrderAction(self, data, error):
|
|
|
|
|
""""""
|
|
|
|
|
pass
|
2016-07-25 14:32:55 +00:00
|
|
|
|
|
2015-08-14 08:11:53 +00:00
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
def onErrRtnForQuoteInsert(self, data, error):
|
|
|
|
|
""""""
|
|
|
|
|
pass
|
2016-07-25 14:32:55 +00:00
|
|
|
|
|
2015-08-14 08:11:53 +00:00
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
def onRtnQuote(self, data):
|
|
|
|
|
""""""
|
|
|
|
|
pass
|
2016-07-25 14:32:55 +00:00
|
|
|
|
|
2015-08-14 08:11:53 +00:00
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
def onErrRtnQuoteInsert(self, data, error):
|
|
|
|
|
""""""
|
|
|
|
|
pass
|
2016-07-25 14:32:55 +00:00
|
|
|
|
|
2015-08-14 08:11:53 +00:00
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
def onErrRtnQuoteAction(self, data, error):
|
|
|
|
|
""""""
|
|
|
|
|
pass
|
2016-07-25 14:32:55 +00:00
|
|
|
|
|
2015-08-14 08:11:53 +00:00
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
def onRtnForQuoteRsp(self, data):
|
|
|
|
|
""""""
|
|
|
|
|
pass
|
2016-07-25 14:32:55 +00:00
|
|
|
|
|
2015-08-14 08:11:53 +00:00
|
|
|
|
#----------------------------------------------------------------------
|
2016-07-01 15:20:05 +00:00
|
|
|
|
def onRtnCFMMCTradingAccountToken(self, data):
|
|
|
|
|
""""""
|
2016-07-25 14:32:55 +00:00
|
|
|
|
pass
|
|
|
|
|
|
2016-07-01 15:20:05 +00:00
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
def onRtnLock(self, data):
|
|
|
|
|
""""""
|
2016-07-25 14:32:55 +00:00
|
|
|
|
pass
|
|
|
|
|
|
2016-07-01 15:20:05 +00:00
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
def onErrRtnLockInsert(self, data, error):
|
|
|
|
|
""""""
|
2016-07-25 14:32:55 +00:00
|
|
|
|
pass
|
|
|
|
|
|
2016-07-01 15:20:05 +00:00
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
def onRtnCombAction(self, data):
|
|
|
|
|
""""""
|
2016-07-25 14:32:55 +00:00
|
|
|
|
pass
|
|
|
|
|
|
2016-07-01 15:20:05 +00:00
|
|
|
|
#----------------------------------------------------------------------
|
2016-07-25 14:32:55 +00:00
|
|
|
|
def onErrRtnCombActionInsert(self, data, error):
|
2016-07-01 15:20:05 +00:00
|
|
|
|
""""""
|
2016-07-25 14:32:55 +00:00
|
|
|
|
pass
|
|
|
|
|
|
2016-07-01 15:20:05 +00:00
|
|
|
|
#----------------------------------------------------------------------
|
2015-08-14 08:11:53 +00:00
|
|
|
|
def onRspQryContractBank(self, data, error, n, last):
|
|
|
|
|
""""""
|
|
|
|
|
pass
|
2016-07-25 14:32:55 +00:00
|
|
|
|
|
2015-08-14 08:11:53 +00:00
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
def onRspQryParkedOrder(self, data, error, n, last):
|
|
|
|
|
""""""
|
|
|
|
|
pass
|
2016-07-25 14:32:55 +00:00
|
|
|
|
|
2015-08-14 08:11:53 +00:00
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
def onRspQryParkedOrderAction(self, data, error, n, last):
|
|
|
|
|
""""""
|
|
|
|
|
pass
|
2016-07-25 14:32:55 +00:00
|
|
|
|
|
2015-08-14 08:11:53 +00:00
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
def onRspQryTradingNotice(self, data, error, n, last):
|
|
|
|
|
""""""
|
|
|
|
|
pass
|
2016-07-25 14:32:55 +00:00
|
|
|
|
|
2015-08-14 08:11:53 +00:00
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
def onRspQryBrokerTradingParams(self, data, error, n, last):
|
|
|
|
|
""""""
|
|
|
|
|
pass
|
2016-07-25 14:32:55 +00:00
|
|
|
|
|
2015-08-14 08:11:53 +00:00
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
def onRspQryBrokerTradingAlgos(self, data, error, n, last):
|
|
|
|
|
""""""
|
|
|
|
|
pass
|
2016-07-25 14:32:55 +00:00
|
|
|
|
|
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
def onRspQueryCFMMCTradingAccountToken(self, data, error, n, last):
|
|
|
|
|
""""""
|
|
|
|
|
pass
|
|
|
|
|
|
2015-08-14 08:11:53 +00:00
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
def onRtnFromBankToFutureByBank(self, data):
|
|
|
|
|
""""""
|
|
|
|
|
pass
|
2016-07-25 14:32:55 +00:00
|
|
|
|
|
2015-08-14 08:11:53 +00:00
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
def onRtnFromFutureToBankByBank(self, data):
|
|
|
|
|
""""""
|
|
|
|
|
pass
|
2016-07-25 14:32:55 +00:00
|
|
|
|
|
2015-08-14 08:11:53 +00:00
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
def onRtnRepealFromBankToFutureByBank(self, data):
|
|
|
|
|
""""""
|
|
|
|
|
pass
|
2016-07-25 14:32:55 +00:00
|
|
|
|
|
2015-08-14 08:11:53 +00:00
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
def onRtnRepealFromFutureToBankByBank(self, data):
|
|
|
|
|
""""""
|
|
|
|
|
pass
|
2016-07-25 14:32:55 +00:00
|
|
|
|
|
2015-08-14 08:11:53 +00:00
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
def onRtnFromBankToFutureByFuture(self, data):
|
|
|
|
|
""""""
|
|
|
|
|
pass
|
2016-07-25 14:32:55 +00:00
|
|
|
|
|
2015-08-14 08:11:53 +00:00
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
def onRtnFromFutureToBankByFuture(self, data):
|
|
|
|
|
""""""
|
|
|
|
|
pass
|
2016-07-25 14:32:55 +00:00
|
|
|
|
|
2015-08-14 08:11:53 +00:00
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
def onRtnRepealFromBankToFutureByFutureManual(self, data):
|
|
|
|
|
""""""
|
|
|
|
|
pass
|
2016-07-25 14:32:55 +00:00
|
|
|
|
|
2015-08-14 08:11:53 +00:00
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
def onRtnRepealFromFutureToBankByFutureManual(self, data):
|
|
|
|
|
""""""
|
|
|
|
|
pass
|
2016-07-25 14:32:55 +00:00
|
|
|
|
|
2015-08-14 08:11:53 +00:00
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
def onRtnQueryBankBalanceByFuture(self, data):
|
|
|
|
|
""""""
|
|
|
|
|
pass
|
2016-07-25 14:32:55 +00:00
|
|
|
|
|
2015-08-14 08:11:53 +00:00
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
def onErrRtnBankToFutureByFuture(self, data, error):
|
|
|
|
|
""""""
|
|
|
|
|
pass
|
2016-07-25 14:32:55 +00:00
|
|
|
|
|
2015-08-14 08:11:53 +00:00
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
def onErrRtnFutureToBankByFuture(self, data, error):
|
|
|
|
|
""""""
|
|
|
|
|
pass
|
2016-07-25 14:32:55 +00:00
|
|
|
|
|
2015-08-14 08:11:53 +00:00
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
def onErrRtnRepealBankToFutureByFutureManual(self, data, error):
|
|
|
|
|
""""""
|
|
|
|
|
pass
|
2016-07-25 14:32:55 +00:00
|
|
|
|
|
2015-08-14 08:11:53 +00:00
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
def onErrRtnRepealFutureToBankByFutureManual(self, data, error):
|
|
|
|
|
""""""
|
|
|
|
|
pass
|
2016-07-25 14:32:55 +00:00
|
|
|
|
|
2015-08-14 08:11:53 +00:00
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
def onErrRtnQueryBankBalanceByFuture(self, data, error):
|
|
|
|
|
""""""
|
|
|
|
|
pass
|
2016-07-25 14:32:55 +00:00
|
|
|
|
|
2015-08-14 08:11:53 +00:00
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
def onRtnRepealFromBankToFutureByFuture(self, data):
|
|
|
|
|
""""""
|
|
|
|
|
pass
|
2016-07-25 14:32:55 +00:00
|
|
|
|
|
2015-08-14 08:11:53 +00:00
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
def onRtnRepealFromFutureToBankByFuture(self, data):
|
|
|
|
|
""""""
|
|
|
|
|
pass
|
2016-07-25 14:32:55 +00:00
|
|
|
|
|
2015-08-14 08:11:53 +00:00
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
def onRspFromBankToFutureByFuture(self, data, error, n, last):
|
|
|
|
|
""""""
|
|
|
|
|
pass
|
2016-07-25 14:32:55 +00:00
|
|
|
|
|
2015-08-14 08:11:53 +00:00
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
def onRspFromFutureToBankByFuture(self, data, error, n, last):
|
|
|
|
|
""""""
|
|
|
|
|
pass
|
2016-07-25 14:32:55 +00:00
|
|
|
|
|
2015-08-14 08:11:53 +00:00
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
def onRspQueryBankAccountMoneyByFuture(self, data, error, n, last):
|
|
|
|
|
""""""
|
|
|
|
|
pass
|
2016-07-25 14:32:55 +00:00
|
|
|
|
|
2015-08-14 08:11:53 +00:00
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
def onRtnOpenAccountByBank(self, data):
|
|
|
|
|
""""""
|
|
|
|
|
pass
|
2016-07-25 14:32:55 +00:00
|
|
|
|
|
2015-08-14 08:11:53 +00:00
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
def onRtnCancelAccountByBank(self, data):
|
|
|
|
|
""""""
|
|
|
|
|
pass
|
2016-07-25 14:32:55 +00:00
|
|
|
|
|
2015-08-14 08:11:53 +00:00
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
def onRtnChangeAccountByBank(self, data):
|
|
|
|
|
""""""
|
|
|
|
|
pass
|
2016-07-25 14:32:55 +00:00
|
|
|
|
|
|
|
|
|
|
2015-08-14 08:11:53 +00:00
|
|
|
|
#----------------------------------------------------------------------
|
2015-09-28 03:42:35 +00:00
|
|
|
|
def connect(self, userID, password, brokerID, address):
|
2015-09-09 01:20:02 +00:00
|
|
|
|
"""初始化连接"""
|
2015-09-28 03:42:35 +00:00
|
|
|
|
self.userID = userID # 账号
|
|
|
|
|
self.password = password # 密码
|
|
|
|
|
self.brokerID = brokerID # 经纪商代码
|
|
|
|
|
self.address = address # 服务器地址
|
|
|
|
|
|
2015-09-09 01:20:02 +00:00
|
|
|
|
# 如果尚未建立服务器连接,则进行连接
|
|
|
|
|
if not self.connectionStatus:
|
|
|
|
|
# 创建C++环境中的API对象,这里传入的参数是需要用来保存.con文件的文件夹路径
|
2016-04-14 15:07:06 +00:00
|
|
|
|
path = os.getcwd() + '/temp/' + self.gatewayName + '/'
|
2015-09-09 01:20:02 +00:00
|
|
|
|
if not os.path.exists(path):
|
|
|
|
|
os.makedirs(path)
|
|
|
|
|
self.createFtdcTraderApi(path)
|
|
|
|
|
|
|
|
|
|
# 注册服务器地址
|
|
|
|
|
self.registerFront(self.address)
|
|
|
|
|
|
|
|
|
|
# 初始化连接,成功会调用onFrontConnected
|
|
|
|
|
self.init()
|
|
|
|
|
|
|
|
|
|
# 若已经连接但尚未登录,则进行登录
|
|
|
|
|
else:
|
|
|
|
|
if not self.loginStatus:
|
|
|
|
|
self.login()
|
|
|
|
|
|
2015-08-14 08:11:53 +00:00
|
|
|
|
#----------------------------------------------------------------------
|
2015-09-09 01:20:02 +00:00
|
|
|
|
def login(self):
|
|
|
|
|
"""连接服务器"""
|
|
|
|
|
# 如果填入了用户名密码等,则登录
|
|
|
|
|
if self.userID and self.password and self.brokerID:
|
|
|
|
|
req = {}
|
|
|
|
|
req['UserID'] = self.userID
|
|
|
|
|
req['Password'] = self.password
|
|
|
|
|
req['BrokerID'] = self.brokerID
|
|
|
|
|
self.reqID += 1
|
|
|
|
|
self.reqUserLogin(req, self.reqID)
|
2015-08-14 08:11:53 +00:00
|
|
|
|
|
|
|
|
|
#----------------------------------------------------------------------
|
2015-12-30 14:01:17 +00:00
|
|
|
|
def qryAccount(self):
|
2015-08-14 08:11:53 +00:00
|
|
|
|
"""查询账户"""
|
2015-09-09 01:20:02 +00:00
|
|
|
|
self.reqID += 1
|
|
|
|
|
self.reqQryTradingAccount({}, self.reqID)
|
2015-08-14 08:11:53 +00:00
|
|
|
|
|
|
|
|
|
#----------------------------------------------------------------------
|
2015-12-30 14:01:17 +00:00
|
|
|
|
def qryPosition(self):
|
2015-08-14 08:11:53 +00:00
|
|
|
|
"""查询持仓"""
|
2015-09-09 01:20:02 +00:00
|
|
|
|
self.reqID += 1
|
2015-08-14 08:11:53 +00:00
|
|
|
|
req = {}
|
2015-09-09 01:20:02 +00:00
|
|
|
|
req['BrokerID'] = self.brokerID
|
|
|
|
|
req['InvestorID'] = self.userID
|
|
|
|
|
self.reqQryInvestorPosition(req, self.reqID)
|
2015-08-14 08:11:53 +00:00
|
|
|
|
|
|
|
|
|
#----------------------------------------------------------------------
|
2015-09-09 01:20:02 +00:00
|
|
|
|
def sendOrder(self, orderReq):
|
2015-08-14 08:11:53 +00:00
|
|
|
|
"""发单"""
|
2015-09-09 01:20:02 +00:00
|
|
|
|
self.reqID += 1
|
|
|
|
|
self.orderRef += 1
|
|
|
|
|
|
2015-08-14 08:11:53 +00:00
|
|
|
|
req = {}
|
|
|
|
|
|
2015-09-28 03:42:35 +00:00
|
|
|
|
req['InstrumentID'] = orderReq.symbol
|
|
|
|
|
req['LimitPrice'] = orderReq.price
|
|
|
|
|
req['VolumeTotalOriginal'] = orderReq.volume
|
|
|
|
|
|
|
|
|
|
# 下面如果由于传入的类型本接口不支持,则会返回空字符串
|
2016-04-27 14:49:28 +00:00
|
|
|
|
req['OrderPriceType'] = priceTypeMap.get(orderReq.priceType, '')
|
|
|
|
|
req['Direction'] = directionMap.get(orderReq.direction, '')
|
|
|
|
|
req['CombOffsetFlag'] = offsetMap.get(orderReq.offset, '')
|
2015-09-28 03:42:35 +00:00
|
|
|
|
|
2015-09-09 01:20:02 +00:00
|
|
|
|
req['OrderRef'] = str(self.orderRef)
|
|
|
|
|
req['InvestorID'] = self.userID
|
|
|
|
|
req['UserID'] = self.userID
|
|
|
|
|
req['BrokerID'] = self.brokerID
|
|
|
|
|
|
2015-08-14 08:11:53 +00:00
|
|
|
|
req['CombHedgeFlag'] = defineDict['THOST_FTDC_HF_Speculation'] # 投机单
|
|
|
|
|
req['ContingentCondition'] = defineDict['THOST_FTDC_CC_Immediately'] # 立即发单
|
|
|
|
|
req['ForceCloseReason'] = defineDict['THOST_FTDC_FCC_NotForceClose'] # 非强平
|
|
|
|
|
req['IsAutoSuspend'] = 0 # 非自动挂起
|
|
|
|
|
req['TimeCondition'] = defineDict['THOST_FTDC_TC_GFD'] # 今日有效
|
|
|
|
|
req['VolumeCondition'] = defineDict['THOST_FTDC_VC_AV'] # 任意成交量
|
|
|
|
|
req['MinVolume'] = 1 # 最小成交量为1
|
|
|
|
|
|
2016-04-27 14:49:28 +00:00
|
|
|
|
# 判断FAK和FOK
|
|
|
|
|
if orderReq.priceType == PRICETYPE_FAK:
|
|
|
|
|
req['OrderPriceType'] = defineDict["THOST_FTDC_OPT_LimitPrice"]
|
|
|
|
|
req['TimeCondition'] = defineDict['THOST_FTDC_TC_IOC']
|
|
|
|
|
req['VolumeCondition'] = defineDict['THOST_FTDC_VC_AV']
|
|
|
|
|
if orderReq.priceType == PRICETYPE_FOK:
|
|
|
|
|
req['OrderPriceType'] = defineDict["THOST_FTDC_OPT_LimitPrice"]
|
|
|
|
|
req['TimeCondition'] = defineDict['THOST_FTDC_TC_IOC']
|
|
|
|
|
req['VolumeCondition'] = defineDict['THOST_FTDC_VC_CV']
|
|
|
|
|
|
2015-09-09 01:20:02 +00:00
|
|
|
|
self.reqOrderInsert(req, self.reqID)
|
2015-08-14 08:11:53 +00:00
|
|
|
|
|
2015-09-28 03:42:35 +00:00
|
|
|
|
# 返回订单号(字符串),便于某些算法进行动态管理
|
2015-10-16 08:50:44 +00:00
|
|
|
|
vtOrderID = '.'.join([self.gatewayName, str(self.orderRef)])
|
|
|
|
|
return vtOrderID
|
2015-08-14 08:11:53 +00:00
|
|
|
|
|
|
|
|
|
#----------------------------------------------------------------------
|
2015-09-09 01:20:02 +00:00
|
|
|
|
def cancelOrder(self, cancelOrderReq):
|
2015-08-14 08:11:53 +00:00
|
|
|
|
"""撤单"""
|
2015-09-09 01:20:02 +00:00
|
|
|
|
self.reqID += 1
|
2015-09-28 03:42:35 +00:00
|
|
|
|
|
2015-08-14 08:11:53 +00:00
|
|
|
|
req = {}
|
|
|
|
|
|
2015-09-28 03:42:35 +00:00
|
|
|
|
req['InstrumentID'] = cancelOrderReq.symbol
|
|
|
|
|
req['ExchangeID'] = cancelOrderReq.exchange
|
2015-10-16 08:50:44 +00:00
|
|
|
|
req['OrderRef'] = cancelOrderReq.orderID
|
2015-09-28 03:42:35 +00:00
|
|
|
|
req['FrontID'] = cancelOrderReq.frontID
|
|
|
|
|
req['SessionID'] = cancelOrderReq.sessionID
|
2015-08-14 08:11:53 +00:00
|
|
|
|
|
|
|
|
|
req['ActionFlag'] = defineDict['THOST_FTDC_AF_Delete']
|
2015-09-09 01:20:02 +00:00
|
|
|
|
req['BrokerID'] = self.brokerID
|
|
|
|
|
req['InvestorID'] = self.userID
|
2015-08-14 08:11:53 +00:00
|
|
|
|
|
2015-09-28 03:42:35 +00:00
|
|
|
|
self.reqOrderAction(req, self.reqID)
|
|
|
|
|
|
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
def close(self):
|
|
|
|
|
"""关闭"""
|
2015-10-16 08:50:44 +00:00
|
|
|
|
self.exit()
|
|
|
|
|
|
|
|
|
|
|
2016-04-27 14:49:28 +00:00
|
|
|
|
########################################################################
|
|
|
|
|
class PositionBuffer(object):
|
|
|
|
|
"""用来缓存持仓的数据,处理上期所的数据返回分今昨的问题"""
|
|
|
|
|
|
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
def __init__(self, data, gatewayName):
|
|
|
|
|
"""Constructor"""
|
|
|
|
|
self.symbol = data['InstrumentID']
|
|
|
|
|
self.direction = posiDirectionMapReverse.get(data['PosiDirection'], '')
|
|
|
|
|
|
|
|
|
|
self.todayPosition = EMPTY_INT
|
|
|
|
|
self.ydPosition = EMPTY_INT
|
|
|
|
|
self.todayPositionCost = EMPTY_FLOAT
|
|
|
|
|
self.ydPositionCost = EMPTY_FLOAT
|
|
|
|
|
|
|
|
|
|
# 通过提前创建持仓数据对象并重复使用的方式来降低开销
|
|
|
|
|
pos = VtPositionData()
|
|
|
|
|
pos.symbol = self.symbol
|
|
|
|
|
pos.vtSymbol = self.symbol
|
|
|
|
|
pos.gatewayName = gatewayName
|
|
|
|
|
pos.direction = self.direction
|
|
|
|
|
pos.vtPositionName = '.'.join([pos.vtSymbol, pos.direction])
|
|
|
|
|
self.pos = pos
|
|
|
|
|
|
|
|
|
|
#----------------------------------------------------------------------
|
2016-07-01 15:07:41 +00:00
|
|
|
|
def updateShfeBuffer(self, data, size):
|
2016-06-05 11:31:51 +00:00
|
|
|
|
"""更新上期所缓存,返回更新后的持仓数据"""
|
2016-04-27 14:49:28 +00:00
|
|
|
|
# 昨仓和今仓的数据更新是分在两条记录里的,因此需要判断检查该条记录对应仓位
|
2016-06-01 15:28:11 +00:00
|
|
|
|
# 因为今仓字段TodayPosition可能变为0(被全部平仓),因此分辨今昨仓需要用YdPosition字段
|
|
|
|
|
if data['YdPosition']:
|
2016-04-27 14:49:28 +00:00
|
|
|
|
self.ydPosition = data['Position']
|
2016-06-01 15:28:11 +00:00
|
|
|
|
self.ydPositionCost = data['PositionCost']
|
|
|
|
|
else:
|
|
|
|
|
self.todayPosition = data['Position']
|
|
|
|
|
self.todayPositionCost = data['PositionCost']
|
2016-04-27 14:49:28 +00:00
|
|
|
|
|
|
|
|
|
# 持仓的昨仓和今仓相加后为总持仓
|
|
|
|
|
self.pos.position = self.todayPosition + self.ydPosition
|
|
|
|
|
self.pos.ydPosition = self.ydPosition
|
|
|
|
|
|
|
|
|
|
# 如果手头还有持仓,则通过加权平均方式计算持仓均价
|
|
|
|
|
if self.todayPosition or self.ydPosition:
|
|
|
|
|
self.pos.price = ((self.todayPositionCost + self.ydPositionCost)/
|
2016-07-01 15:07:41 +00:00
|
|
|
|
((self.todayPosition + self.ydPosition) * size))
|
2016-04-27 14:49:28 +00:00
|
|
|
|
# 否则价格为0
|
|
|
|
|
else:
|
|
|
|
|
self.pos.price = 0
|
|
|
|
|
|
|
|
|
|
return copy(self.pos)
|
2016-06-05 11:31:51 +00:00
|
|
|
|
|
|
|
|
|
#----------------------------------------------------------------------
|
2016-07-01 15:07:41 +00:00
|
|
|
|
def updateBuffer(self, data, size):
|
2016-06-05 11:31:51 +00:00
|
|
|
|
"""更新其他交易所的缓存,返回更新后的持仓数据"""
|
|
|
|
|
# 其他交易所并不区分今昨,因此只关心总仓位,昨仓设为0
|
|
|
|
|
self.pos.position = data['Position']
|
|
|
|
|
self.pos.ydPosition = 0
|
|
|
|
|
|
|
|
|
|
if data['Position']:
|
2016-07-01 15:07:41 +00:00
|
|
|
|
self.pos.price = data['PositionCost'] / (data['Position'] * size)
|
2016-06-05 11:31:51 +00:00
|
|
|
|
else:
|
|
|
|
|
self.pos.price = 0
|
|
|
|
|
|
|
|
|
|
return copy(self.pos)
|
|
|
|
|
|
2016-04-27 14:49:28 +00:00
|
|
|
|
|
2015-10-16 08:50:44 +00:00
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
def test():
|
|
|
|
|
"""测试"""
|
|
|
|
|
from PyQt4 import QtCore
|
|
|
|
|
import sys
|
|
|
|
|
|
|
|
|
|
def print_log(event):
|
|
|
|
|
log = event.dict_['data']
|
|
|
|
|
print ':'.join([log.logTime, log.logContent])
|
|
|
|
|
|
|
|
|
|
app = QtCore.QCoreApplication(sys.argv)
|
|
|
|
|
|
|
|
|
|
eventEngine = EventEngine()
|
|
|
|
|
eventEngine.register(EVENT_LOG, print_log)
|
|
|
|
|
eventEngine.start()
|
|
|
|
|
|
|
|
|
|
gateway = CtpGateway(eventEngine)
|
|
|
|
|
gateway.connect()
|
|
|
|
|
|
|
|
|
|
sys.exit(app.exec_())
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
|
test()
|