991 lines
35 KiB
Python
991 lines
35 KiB
Python
|
# encoding: UTF-8
|
|||
|
|
|||
|
from vnctpmd import MdApi
|
|||
|
from vnctptd import TdApi
|
|||
|
|
|||
|
from gateway import *
|
|||
|
|
|||
|
import os
|
|||
|
|
|||
|
|
|||
|
########################################################################
|
|||
|
class CtpGateway(VtGateway):
|
|||
|
"""CTP接口"""
|
|||
|
|
|||
|
#----------------------------------------------------------------------
|
|||
|
def __init__(self, eventEngine):
|
|||
|
"""Constructor"""
|
|||
|
super(CtpGateway, self).__init__(eventEngine)
|
|||
|
|
|||
|
self.mdApi = None # 行情API
|
|||
|
self.tdApi = None # 交易API
|
|||
|
|
|||
|
self.mdConnected = False # 行情API连接状态
|
|||
|
self.tdConnected = False # 交易API连接状态
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
########################################################################
|
|||
|
class CtpMdApi(MdApi):
|
|||
|
"""CTP行情API实现"""
|
|||
|
|
|||
|
#----------------------------------------------------------------------
|
|||
|
def __init__(self, gateway, userID, password, brokerID, address):
|
|||
|
"""Constructor"""
|
|||
|
super(CtpMdApi, self).__init__()
|
|||
|
|
|||
|
self.gateway = gateway # gateway对象
|
|||
|
self.gatewayName = gateway.gatewayName # gateway对象名称
|
|||
|
|
|||
|
self.reqID = EMPTY_INT # 操作请求编号
|
|||
|
|
|||
|
self.connectionStatus = False # 连接状态
|
|||
|
self.loginStatus = False # 登录状态
|
|||
|
|
|||
|
self.userID = userID # 账号
|
|||
|
self.password = password # 密码
|
|||
|
self.brokerID = brokerID # 经纪商代码
|
|||
|
self.address = address # 服务器地址
|
|||
|
|
|||
|
self.subscribedSymbols = set() # 已订阅合约代码
|
|||
|
|
|||
|
#----------------------------------------------------------------------
|
|||
|
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()
|
|||
|
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)
|
|||
|
|
|||
|
#----------------------------------------------------------------------
|
|||
|
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)
|
|||
|
|
|||
|
#----------------------------------------------------------------------
|
|||
|
def onRspSubMarketData(self, data, error, n, last):
|
|||
|
"""订阅合约回报"""
|
|||
|
# 通常不在乎订阅错误,选择忽略
|
|||
|
pass
|
|||
|
|
|||
|
#----------------------------------------------------------------------
|
|||
|
def onRspUnSubMarketData(self, data, error, n, last):
|
|||
|
"""退订合约回报"""
|
|||
|
# 同上
|
|||
|
pass
|
|||
|
|
|||
|
#----------------------------------------------------------------------
|
|||
|
def onRtnDepthMarketData(self, data):
|
|||
|
"""行情推送"""
|
|||
|
tick = VtTickData()
|
|||
|
|
|||
|
tick.symbol = data['InstrumentID']
|
|||
|
tick.vtSymbol = '.'.join([self.gatewayName, tick.symbol])
|
|||
|
|
|||
|
tick.lastPrice = data['LastPrice']
|
|||
|
tick.volume = data['Volume']
|
|||
|
tick.openInterest = data['OpenInterest']
|
|||
|
tick.tickTime = '.'.join([data['UpdateTime'], str(data['UpdateMillisec']/100]))
|
|||
|
|
|||
|
# 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
|
|||
|
|
|||
|
#----------------------------------------------------------------------
|
|||
|
def connect(self):
|
|||
|
"""初始化连接"""
|
|||
|
# 如果尚未建立服务器连接,则进行连接
|
|||
|
if not self.connectionStatus:
|
|||
|
# 创建C++环境中的API对象,这里传入的参数是需要用来保存.con文件的文件夹路径
|
|||
|
path = os.getcwd() + '\\temp\\' + self.gatewayName + '\\'
|
|||
|
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):
|
|||
|
"""订阅合约"""
|
|||
|
self.subscribeMarketData(subscribeReq.symbol)
|
|||
|
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
|
|||
|
self.reqUserLogin(req, self.reqID)
|
|||
|
|
|||
|
|
|||
|
########################################################################
|
|||
|
class CtpTdApi(TdApi):
|
|||
|
"""CTP交易API实现"""
|
|||
|
|
|||
|
#----------------------------------------------------------------------
|
|||
|
def __init__(self, gateway, userID, password, brokerID, address):
|
|||
|
"""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 # 登录状态
|
|||
|
|
|||
|
self.userID = userID # 账号
|
|||
|
self.password = password # 密码
|
|||
|
self.brokerID = brokerID # 经纪商代码
|
|||
|
self.address = address # 服务器地址
|
|||
|
|
|||
|
#----------------------------------------------------------------------
|
|||
|
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.tdConnected = False
|
|||
|
|
|||
|
log = VtLogData()
|
|||
|
log.gatewayName = self.gatewayName
|
|||
|
log.logContent = u'交易服务器连接断开'
|
|||
|
self.gateway.onLog(log)
|
|||
|
|
|||
|
#----------------------------------------------------------------------
|
|||
|
def onHeartBeatWarning(self, n):
|
|||
|
""""""
|
|||
|
pass
|
|||
|
|
|||
|
#----------------------------------------------------------------------
|
|||
|
def onRspAuthenticate(self, data, error, n, last):
|
|||
|
""""""
|
|||
|
pass
|
|||
|
|
|||
|
#----------------------------------------------------------------------
|
|||
|
def onRspUserLogin(self, data, error, n, last):
|
|||
|
"""登陆回报"""
|
|||
|
# 如果登录成功,推送日志信息
|
|||
|
if error['ErrorID'] == 0:
|
|||
|
self.loginStatus = True
|
|||
|
self.gateway.mdConnected = True
|
|||
|
|
|||
|
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()
|
|||
|
err.gatewayName = self.gateway
|
|||
|
err.errorID = error['ErrorID']
|
|||
|
err.errorMsg = error['ErrorMsg'].decode('gbk')
|
|||
|
self.gateway.onError(err)
|
|||
|
|
|||
|
#----------------------------------------------------------------------
|
|||
|
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)
|
|||
|
|
|||
|
#----------------------------------------------------------------------
|
|||
|
def onRspUserPasswordUpdate(self, data, error, n, last):
|
|||
|
""""""
|
|||
|
pass
|
|||
|
|
|||
|
#----------------------------------------------------------------------
|
|||
|
def onRspTradingAccountPasswordUpdate(self, data, error, n, last):
|
|||
|
""""""
|
|||
|
pass
|
|||
|
|
|||
|
#----------------------------------------------------------------------
|
|||
|
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)
|
|||
|
|
|||
|
#----------------------------------------------------------------------
|
|||
|
def onRspParkedOrderInsert(self, data, error, n, last):
|
|||
|
""""""
|
|||
|
pass
|
|||
|
|
|||
|
#----------------------------------------------------------------------
|
|||
|
def onRspParkedOrderAction(self, data, error, n, last):
|
|||
|
""""""
|
|||
|
pass
|
|||
|
|
|||
|
#----------------------------------------------------------------------
|
|||
|
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)
|
|||
|
|
|||
|
#----------------------------------------------------------------------
|
|||
|
def onRspQueryMaxOrderVolume(self, data, error, n, last):
|
|||
|
""""""
|
|||
|
pass
|
|||
|
|
|||
|
#----------------------------------------------------------------------
|
|||
|
def onRspSettlementInfoConfirm(self, data, error, n, last):
|
|||
|
"""确认结算信息回报"""
|
|||
|
log = VtLogData()
|
|||
|
log.gatewayName = self.gatewayName
|
|||
|
log.logContent = u'结算信息确认完成'
|
|||
|
self.gateway.onLog(log)
|
|||
|
|
|||
|
# 查询合约代码
|
|||
|
self.reqID += 1
|
|||
|
self.reqQryInstrument({}, self.reqID)
|
|||
|
|
|||
|
#----------------------------------------------------------------------
|
|||
|
def onRspRemoveParkedOrder(self, data, error, n, last):
|
|||
|
""""""
|
|||
|
pass
|
|||
|
|
|||
|
#----------------------------------------------------------------------
|
|||
|
def onRspRemoveParkedOrderAction(self, data, error, n, last):
|
|||
|
""""""
|
|||
|
pass
|
|||
|
|
|||
|
#----------------------------------------------------------------------
|
|||
|
def onRspExecOrderInsert(self, data, error, n, last):
|
|||
|
""""""
|
|||
|
pass
|
|||
|
|
|||
|
#----------------------------------------------------------------------
|
|||
|
def onRspExecOrderAction(self, data, error, n, last):
|
|||
|
""""""
|
|||
|
pass
|
|||
|
|
|||
|
#----------------------------------------------------------------------
|
|||
|
def onRspForQuoteInsert(self, data, error, n, last):
|
|||
|
""""""
|
|||
|
pass
|
|||
|
|
|||
|
#----------------------------------------------------------------------
|
|||
|
def onRspQuoteInsert(self, data, error, n, last):
|
|||
|
""""""
|
|||
|
pass
|
|||
|
|
|||
|
#----------------------------------------------------------------------
|
|||
|
def onRspQuoteAction(self, data, error, n, last):
|
|||
|
""""""
|
|||
|
pass
|
|||
|
|
|||
|
#----------------------------------------------------------------------
|
|||
|
def onRspQryOrder(self, data, error, n, last):
|
|||
|
""""""
|
|||
|
pass
|
|||
|
|
|||
|
#----------------------------------------------------------------------
|
|||
|
def onRspQryTrade(self, data, error, n, last):
|
|||
|
""""""
|
|||
|
pass
|
|||
|
|
|||
|
#----------------------------------------------------------------------
|
|||
|
def onRspQryInvestorPosition(self, data, error, n, last):
|
|||
|
"""持仓查询回报"""
|
|||
|
if error['ErrorID'] == 0:
|
|||
|
event = Event(type_=EVENT_POSITION)
|
|||
|
event.dict_['data'] = data
|
|||
|
self.__eventEngine.put(event)
|
|||
|
else:
|
|||
|
event = Event(type_=EVENT_LOG)
|
|||
|
log = u'持仓查询回报,错误代码:' + unicode(error['ErrorID']) + u',' + u'错误信息:' + error['ErrorMsg'].decode('gbk')
|
|||
|
event.dict_['log'] = log
|
|||
|
self.__eventEngine.put(event)
|
|||
|
|
|||
|
#----------------------------------------------------------------------
|
|||
|
def onRspQryTradingAccount(self, data, error, n, last):
|
|||
|
"""资金账户查询回报"""
|
|||
|
if error['ErrorID'] == 0:
|
|||
|
event = Event(type_=EVENT_ACCOUNT)
|
|||
|
event.dict_['data'] = data
|
|||
|
self.__eventEngine.put(event)
|
|||
|
else:
|
|||
|
event = Event(type_=EVENT_LOG)
|
|||
|
log = u'账户查询回报,错误代码:' + unicode(error['ErrorID']) + u',' + u'错误信息:' + error['ErrorMsg'].decode('gbk')
|
|||
|
event.dict_['log'] = log
|
|||
|
self.__eventEngine.put(event)
|
|||
|
|
|||
|
#----------------------------------------------------------------------
|
|||
|
def onRspQryInvestor(self, data, error, n, last):
|
|||
|
"""投资者查询回报"""
|
|||
|
pass
|
|||
|
|
|||
|
#----------------------------------------------------------------------
|
|||
|
def onRspQryTradingCode(self, data, error, n, last):
|
|||
|
""""""
|
|||
|
pass
|
|||
|
|
|||
|
#----------------------------------------------------------------------
|
|||
|
def onRspQryInstrumentMarginRate(self, data, error, n, last):
|
|||
|
""""""
|
|||
|
pass
|
|||
|
|
|||
|
#----------------------------------------------------------------------
|
|||
|
def onRspQryInstrumentCommissionRate(self, data, error, n, last):
|
|||
|
""""""
|
|||
|
pass
|
|||
|
|
|||
|
#----------------------------------------------------------------------
|
|||
|
def onRspQryExchange(self, data, error, n, last):
|
|||
|
""""""
|
|||
|
pass
|
|||
|
|
|||
|
#----------------------------------------------------------------------
|
|||
|
def onRspQryProduct(self, data, error, n, last):
|
|||
|
""""""
|
|||
|
pass
|
|||
|
|
|||
|
#----------------------------------------------------------------------
|
|||
|
def onRspQryInstrument(self, data, error, n, last):
|
|||
|
"""
|
|||
|
合约查询回报
|
|||
|
由于该回报的推送速度极快,因此不适合全部存入队列中处理,
|
|||
|
选择先储存在一个本地字典中,全部收集完毕后再推送到队列中
|
|||
|
(由于耗时过长目前使用其他进程读取)
|
|||
|
"""
|
|||
|
if error['ErrorID'] == 0:
|
|||
|
event = Event(type_=EVENT_INSTRUMENT)
|
|||
|
event.dict_['data'] = data
|
|||
|
event.dict_['last'] = last
|
|||
|
self.__eventEngine.put(event)
|
|||
|
else:
|
|||
|
event = Event(type_=EVENT_LOG)
|
|||
|
log = u'合约投资者回报,错误代码:' + unicode(error['ErrorID']) + u',' + u'错误信息:' + error['ErrorMsg'].decode('gbk')
|
|||
|
event.dict_['log'] = log
|
|||
|
self.__eventEngine.put(event)
|
|||
|
|
|||
|
#----------------------------------------------------------------------
|
|||
|
def onRspQryDepthMarketData(self, data, error, n, last):
|
|||
|
""""""
|
|||
|
pass
|
|||
|
|
|||
|
#----------------------------------------------------------------------
|
|||
|
def onRspQrySettlementInfo(self, data, error, n, last):
|
|||
|
"""查询结算信息回报"""
|
|||
|
pass
|
|||
|
|
|||
|
#----------------------------------------------------------------------
|
|||
|
def onRspQryTransferBank(self, data, error, n, last):
|
|||
|
""""""
|
|||
|
pass
|
|||
|
|
|||
|
#----------------------------------------------------------------------
|
|||
|
def onRspQryInvestorPositionDetail(self, data, error, n, last):
|
|||
|
""""""
|
|||
|
pass
|
|||
|
|
|||
|
#----------------------------------------------------------------------
|
|||
|
def onRspQryNotice(self, data, error, n, last):
|
|||
|
""""""
|
|||
|
pass
|
|||
|
|
|||
|
#----------------------------------------------------------------------
|
|||
|
def onRspQrySettlementInfoConfirm(self, data, error, n, last):
|
|||
|
""""""
|
|||
|
pass
|
|||
|
|
|||
|
#----------------------------------------------------------------------
|
|||
|
def onRspQryInvestorPositionCombineDetail(self, data, error, n, last):
|
|||
|
""""""
|
|||
|
pass
|
|||
|
|
|||
|
#----------------------------------------------------------------------
|
|||
|
def onRspQryCFMMCTradingAccountKey(self, data, error, n, last):
|
|||
|
""""""
|
|||
|
pass
|
|||
|
|
|||
|
#----------------------------------------------------------------------
|
|||
|
def onRspQryEWarrantOffset(self, data, error, n, last):
|
|||
|
""""""
|
|||
|
pass
|
|||
|
|
|||
|
#----------------------------------------------------------------------
|
|||
|
def onRspQryInvestorProductGroupMargin(self, data, error, n, last):
|
|||
|
""""""
|
|||
|
pass
|
|||
|
|
|||
|
#----------------------------------------------------------------------
|
|||
|
def onRspQryExchangeMarginRate(self, data, error, n, last):
|
|||
|
""""""
|
|||
|
pass
|
|||
|
|
|||
|
#----------------------------------------------------------------------
|
|||
|
def onRspQryExchangeMarginRateAdjust(self, data, error, n, last):
|
|||
|
""""""
|
|||
|
pass
|
|||
|
|
|||
|
#----------------------------------------------------------------------
|
|||
|
def onRspQryExchangeRate(self, data, error, n, last):
|
|||
|
""""""
|
|||
|
pass
|
|||
|
|
|||
|
#----------------------------------------------------------------------
|
|||
|
def onRspQrySecAgentACIDMap(self, data, error, n, last):
|
|||
|
""""""
|
|||
|
pass
|
|||
|
|
|||
|
#----------------------------------------------------------------------
|
|||
|
def onRspQryOptionInstrTradeCost(self, data, error, n, last):
|
|||
|
""""""
|
|||
|
pass
|
|||
|
|
|||
|
#----------------------------------------------------------------------
|
|||
|
def onRspQryOptionInstrCommRate(self, data, error, n, last):
|
|||
|
""""""
|
|||
|
pass
|
|||
|
|
|||
|
#----------------------------------------------------------------------
|
|||
|
def onRspQryExecOrder(self, data, error, n, last):
|
|||
|
""""""
|
|||
|
pass
|
|||
|
|
|||
|
#----------------------------------------------------------------------
|
|||
|
def onRspQryForQuote(self, data, error, n, last):
|
|||
|
""""""
|
|||
|
pass
|
|||
|
|
|||
|
#----------------------------------------------------------------------
|
|||
|
def onRspQryQuote(self, data, error, n, last):
|
|||
|
""""""
|
|||
|
pass
|
|||
|
|
|||
|
#----------------------------------------------------------------------
|
|||
|
def onRspQryTransferSerial(self, data, error, n, last):
|
|||
|
""""""
|
|||
|
pass
|
|||
|
|
|||
|
#----------------------------------------------------------------------
|
|||
|
def onRspQryAccountregister(self, data, error, n, last):
|
|||
|
""""""
|
|||
|
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 onRtnOrder(self, data):
|
|||
|
"""报单回报"""
|
|||
|
# 更新最大报单编号
|
|||
|
newref = data['OrderRef']
|
|||
|
self.orderRef = max(self.orderRef, int(newref))
|
|||
|
|
|||
|
# 创建报单数据对象
|
|||
|
order = VtOrderData()
|
|||
|
order.gatewayName = self.gatewayName
|
|||
|
|
|||
|
# 保存代码和报单号
|
|||
|
order.symbol = data['InstrumentID']
|
|||
|
order.vtSymbol = '.'.join([self.gatewayName, order.symbol])
|
|||
|
|
|||
|
order.orderID = data['OrderRef']
|
|||
|
order.vtOrderID = '.'.join([self.gatewayName, order.orderID])
|
|||
|
|
|||
|
# 方向
|
|||
|
if data['Direction'] == '0':
|
|||
|
order.direction = DIRECTION_LONG
|
|||
|
elif data['Direction'] == '1':
|
|||
|
order.direction = DIRECTION_SHORT
|
|||
|
else:
|
|||
|
order.direction = DIRECTION_UNKNOWN
|
|||
|
|
|||
|
# 多空
|
|||
|
if data['']
|
|||
|
|
|||
|
#----------------------------------------------------------------------
|
|||
|
def onRtnTrade(self, data):
|
|||
|
"""成交回报"""
|
|||
|
# 常规成交事件
|
|||
|
event1 = Event(type_=EVENT_TRADE)
|
|||
|
event1.dict_['data'] = data
|
|||
|
self.__eventEngine.put(event1)
|
|||
|
|
|||
|
# 特定合约成交事件
|
|||
|
event2 = Event(type_=(EVENT_TRADE_CONTRACT+data['InstrumentID']))
|
|||
|
event2.dict_['data'] = data
|
|||
|
self.__eventEngine.put(event2)
|
|||
|
|
|||
|
#----------------------------------------------------------------------
|
|||
|
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)
|
|||
|
|
|||
|
#----------------------------------------------------------------------
|
|||
|
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)
|
|||
|
|
|||
|
#----------------------------------------------------------------------
|
|||
|
def onRtnInstrumentStatus(self, data):
|
|||
|
""""""
|
|||
|
pass
|
|||
|
|
|||
|
#----------------------------------------------------------------------
|
|||
|
def onRtnTradingNotice(self, data):
|
|||
|
""""""
|
|||
|
pass
|
|||
|
|
|||
|
#----------------------------------------------------------------------
|
|||
|
def onRtnErrorConditionalOrder(self, data):
|
|||
|
""""""
|
|||
|
pass
|
|||
|
|
|||
|
#----------------------------------------------------------------------
|
|||
|
def onRtnExecOrder(self, data):
|
|||
|
""""""
|
|||
|
pass
|
|||
|
|
|||
|
#----------------------------------------------------------------------
|
|||
|
def onErrRtnExecOrderInsert(self, data, error):
|
|||
|
""""""
|
|||
|
pass
|
|||
|
|
|||
|
#----------------------------------------------------------------------
|
|||
|
def onErrRtnExecOrderAction(self, data, error):
|
|||
|
""""""
|
|||
|
pass
|
|||
|
|
|||
|
#----------------------------------------------------------------------
|
|||
|
def onErrRtnForQuoteInsert(self, data, error):
|
|||
|
""""""
|
|||
|
pass
|
|||
|
|
|||
|
#----------------------------------------------------------------------
|
|||
|
def onRtnQuote(self, data):
|
|||
|
""""""
|
|||
|
pass
|
|||
|
|
|||
|
#----------------------------------------------------------------------
|
|||
|
def onErrRtnQuoteInsert(self, data, error):
|
|||
|
""""""
|
|||
|
pass
|
|||
|
|
|||
|
#----------------------------------------------------------------------
|
|||
|
def onErrRtnQuoteAction(self, data, error):
|
|||
|
""""""
|
|||
|
pass
|
|||
|
|
|||
|
#----------------------------------------------------------------------
|
|||
|
def onRtnForQuoteRsp(self, data):
|
|||
|
""""""
|
|||
|
pass
|
|||
|
|
|||
|
#----------------------------------------------------------------------
|
|||
|
def onRspQryContractBank(self, data, error, n, last):
|
|||
|
""""""
|
|||
|
pass
|
|||
|
|
|||
|
#----------------------------------------------------------------------
|
|||
|
def onRspQryParkedOrder(self, data, error, n, last):
|
|||
|
""""""
|
|||
|
pass
|
|||
|
|
|||
|
#----------------------------------------------------------------------
|
|||
|
def onRspQryParkedOrderAction(self, data, error, n, last):
|
|||
|
""""""
|
|||
|
pass
|
|||
|
|
|||
|
#----------------------------------------------------------------------
|
|||
|
def onRspQryTradingNotice(self, data, error, n, last):
|
|||
|
""""""
|
|||
|
pass
|
|||
|
|
|||
|
#----------------------------------------------------------------------
|
|||
|
def onRspQryBrokerTradingParams(self, data, error, n, last):
|
|||
|
""""""
|
|||
|
pass
|
|||
|
|
|||
|
#----------------------------------------------------------------------
|
|||
|
def onRspQryBrokerTradingAlgos(self, data, error, n, last):
|
|||
|
""""""
|
|||
|
pass
|
|||
|
|
|||
|
#----------------------------------------------------------------------
|
|||
|
def onRtnFromBankToFutureByBank(self, data):
|
|||
|
""""""
|
|||
|
pass
|
|||
|
|
|||
|
#----------------------------------------------------------------------
|
|||
|
def onRtnFromFutureToBankByBank(self, data):
|
|||
|
""""""
|
|||
|
pass
|
|||
|
|
|||
|
#----------------------------------------------------------------------
|
|||
|
def onRtnRepealFromBankToFutureByBank(self, data):
|
|||
|
""""""
|
|||
|
pass
|
|||
|
|
|||
|
#----------------------------------------------------------------------
|
|||
|
def onRtnRepealFromFutureToBankByBank(self, data):
|
|||
|
""""""
|
|||
|
pass
|
|||
|
|
|||
|
#----------------------------------------------------------------------
|
|||
|
def onRtnFromBankToFutureByFuture(self, data):
|
|||
|
""""""
|
|||
|
pass
|
|||
|
|
|||
|
#----------------------------------------------------------------------
|
|||
|
def onRtnFromFutureToBankByFuture(self, data):
|
|||
|
""""""
|
|||
|
pass
|
|||
|
|
|||
|
#----------------------------------------------------------------------
|
|||
|
def onRtnRepealFromBankToFutureByFutureManual(self, data):
|
|||
|
""""""
|
|||
|
pass
|
|||
|
|
|||
|
#----------------------------------------------------------------------
|
|||
|
def onRtnRepealFromFutureToBankByFutureManual(self, data):
|
|||
|
""""""
|
|||
|
pass
|
|||
|
|
|||
|
#----------------------------------------------------------------------
|
|||
|
def onRtnQueryBankBalanceByFuture(self, data):
|
|||
|
""""""
|
|||
|
pass
|
|||
|
|
|||
|
#----------------------------------------------------------------------
|
|||
|
def onErrRtnBankToFutureByFuture(self, data, error):
|
|||
|
""""""
|
|||
|
pass
|
|||
|
|
|||
|
#----------------------------------------------------------------------
|
|||
|
def onErrRtnFutureToBankByFuture(self, data, error):
|
|||
|
""""""
|
|||
|
pass
|
|||
|
|
|||
|
#----------------------------------------------------------------------
|
|||
|
def onErrRtnRepealBankToFutureByFutureManual(self, data, error):
|
|||
|
""""""
|
|||
|
pass
|
|||
|
|
|||
|
#----------------------------------------------------------------------
|
|||
|
def onErrRtnRepealFutureToBankByFutureManual(self, data, error):
|
|||
|
""""""
|
|||
|
pass
|
|||
|
|
|||
|
#----------------------------------------------------------------------
|
|||
|
def onErrRtnQueryBankBalanceByFuture(self, data, error):
|
|||
|
""""""
|
|||
|
pass
|
|||
|
|
|||
|
#----------------------------------------------------------------------
|
|||
|
def onRtnRepealFromBankToFutureByFuture(self, data):
|
|||
|
""""""
|
|||
|
pass
|
|||
|
|
|||
|
#----------------------------------------------------------------------
|
|||
|
def onRtnRepealFromFutureToBankByFuture(self, data):
|
|||
|
""""""
|
|||
|
pass
|
|||
|
|
|||
|
#----------------------------------------------------------------------
|
|||
|
def onRspFromBankToFutureByFuture(self, data, error, n, last):
|
|||
|
""""""
|
|||
|
pass
|
|||
|
|
|||
|
#----------------------------------------------------------------------
|
|||
|
def onRspFromFutureToBankByFuture(self, data, error, n, last):
|
|||
|
""""""
|
|||
|
pass
|
|||
|
|
|||
|
#----------------------------------------------------------------------
|
|||
|
def onRspQueryBankAccountMoneyByFuture(self, data, error, n, last):
|
|||
|
""""""
|
|||
|
pass
|
|||
|
|
|||
|
#----------------------------------------------------------------------
|
|||
|
def onRtnOpenAccountByBank(self, data):
|
|||
|
""""""
|
|||
|
pass
|
|||
|
|
|||
|
#----------------------------------------------------------------------
|
|||
|
def onRtnCancelAccountByBank(self, data):
|
|||
|
""""""
|
|||
|
pass
|
|||
|
|
|||
|
#----------------------------------------------------------------------
|
|||
|
def onRtnChangeAccountByBank(self, data):
|
|||
|
""""""
|
|||
|
pass
|
|||
|
|
|||
|
#----------------------------------------------------------------------
|
|||
|
def login(self, address, userid, password, brokerid):
|
|||
|
"""连接服务器"""
|
|||
|
self.__userid = userid
|
|||
|
self.__password = password
|
|||
|
self.__brokerid = brokerid
|
|||
|
|
|||
|
# 数据重传模式设为从本日开始
|
|||
|
self.subscribePrivateTopic(0)
|
|||
|
self.subscribePublicTopic(0)
|
|||
|
|
|||
|
# 注册服务器地址
|
|||
|
self.registerFront(address)
|
|||
|
|
|||
|
# 初始化连接,成功会调用onFrontConnected
|
|||
|
self.init()
|
|||
|
|
|||
|
#----------------------------------------------------------------------
|
|||
|
def getInstrument(self):
|
|||
|
"""查询合约"""
|
|||
|
self.__reqid = self.__reqid + 1
|
|||
|
self.reqQryInstrument({}, self.__reqid)
|
|||
|
|
|||
|
#----------------------------------------------------------------------
|
|||
|
def getAccount(self):
|
|||
|
"""查询账户"""
|
|||
|
self.__reqid = self.__reqid + 1
|
|||
|
self.reqQryTradingAccount({}, self.__reqid)
|
|||
|
|
|||
|
#----------------------------------------------------------------------
|
|||
|
def getInvestor(self):
|
|||
|
"""查询投资者"""
|
|||
|
self.__reqid = self.__reqid + 1
|
|||
|
self.reqQryInvestor({}, self.__reqid)
|
|||
|
|
|||
|
#----------------------------------------------------------------------
|
|||
|
def getPosition(self):
|
|||
|
"""查询持仓"""
|
|||
|
self.__reqid = self.__reqid + 1
|
|||
|
req = {}
|
|||
|
req['BrokerID'] = self.__brokerid
|
|||
|
req['InvestorID'] = self.__userid
|
|||
|
self.reqQryInvestorPosition(req, self.__reqid)
|
|||
|
|
|||
|
#----------------------------------------------------------------------
|
|||
|
def sendOrder(self, instrumentid, exchangeid, price, pricetype, volume, direction, offset):
|
|||
|
"""发单"""
|
|||
|
self.__reqid = self.__reqid + 1
|
|||
|
req = {}
|
|||
|
|
|||
|
req['InstrumentID'] = instrumentid
|
|||
|
req['OrderPriceType'] = pricetype
|
|||
|
req['LimitPrice'] = price
|
|||
|
req['VolumeTotalOriginal'] = volume
|
|||
|
req['Direction'] = direction
|
|||
|
req['CombOffsetFlag'] = offset
|
|||
|
|
|||
|
self.__orderref = self.__orderref + 1
|
|||
|
req['OrderRef'] = str(self.__orderref)
|
|||
|
|
|||
|
req['InvestorID'] = self.__userid
|
|||
|
req['UserID'] = self.__userid
|
|||
|
req['BrokerID'] = self.__brokerid
|
|||
|
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
|
|||
|
|
|||
|
self.reqOrderInsert(req, self.__reqid)
|
|||
|
|
|||
|
# 返回订单号,便于某些算法进行动态管理
|
|||
|
return self.__orderref
|
|||
|
|
|||
|
#----------------------------------------------------------------------
|
|||
|
def cancelOrder(self, instrumentid, exchangeid, orderref, frontid, sessionid):
|
|||
|
"""撤单"""
|
|||
|
self.__reqid = self.__reqid + 1
|
|||
|
req = {}
|
|||
|
|
|||
|
req['InstrumentID'] = instrumentid
|
|||
|
req['ExchangeID'] = exchangeid
|
|||
|
req['OrderRef'] = orderref
|
|||
|
req['FrontID'] = frontid
|
|||
|
req['SessionID'] = sessionid
|
|||
|
|
|||
|
req['ActionFlag'] = defineDict['THOST_FTDC_AF_Delete']
|
|||
|
req['BrokerID'] = self.__brokerid
|
|||
|
req['InvestorID'] = self.__userid
|
|||
|
|
|||
|
self.reqOrderAction(req, self.__reqid)
|
|||
|
|
|||
|
#----------------------------------------------------------------------
|
|||
|
def getSettlement(self):
|
|||
|
"""查询结算信息"""
|
|||
|
self.__reqid = self.__reqid + 1
|
|||
|
req = {}
|
|||
|
|
|||
|
req['BrokerID'] = self.__brokerid
|
|||
|
req['InvestorID'] = self.__userid
|
|||
|
|
|||
|
self.reqQrySettlementInfo(req, self.__reqid)
|
|||
|
|
|||
|
#----------------------------------------------------------------------
|
|||
|
def confirmSettlement(self):
|
|||
|
"""确认结算信息"""
|
|||
|
self.__reqid = self.__reqid + 1
|
|||
|
req = {}
|
|||
|
|
|||
|
req['BrokerID'] = self.__brokerid
|
|||
|
req['InvestorID'] = self.__userid
|
|||
|
|
|||
|
self.reqSettlementInfoConfirm(req, self.__reqid)
|