[Mod]修改huobiGateway的委托号使用本地委托号

This commit is contained in:
vn.py 2018-03-18 21:54:39 +08:00
parent c209a7698b
commit f0ab8df80e
3 changed files with 81 additions and 45 deletions

View File

@ -11,7 +11,7 @@
"logActive": true, "logActive": true,
"logLevel": "debug", "logLevel": "debug",
"logConsole": true, "logConsole": false,
"logFile": true, "logFile": true,
"tdPenalty": ["IF", "IH", "IC"] "tdPenalty": ["IF", "IH", "IC"]

View File

@ -1,6 +1,6 @@
{ {
"exchange": "hadax", "exchange": "hadax",
"accessKey": "请在火币网站获取", "accessKey": "5c856252-f33605d0-a5aed4b1-ac608",
"secretKey": "请在火币网站获取", "secretKey": "87a284ac-6c731c62-ae3c39c8-c57d8",
"symbols": ["aaceth"] "symbols": ["aaceth"]
} }

View File

@ -122,20 +122,12 @@ class HuobiGateway(VtGateway):
self.tradeApi.cancelOrder(cancelOrderReq) self.tradeApi.cancelOrder(cancelOrderReq)
#---------------------------------------------------------------------- #----------------------------------------------------------------------
def qryPosition(self): def qryInfo(self):
"""查询持仓""" """查询委托、成交、持仓"""
self.tradeApi.qryPosition() self.tradeApi.qryOrder()
#----------------------------------------------------------------------
def qryTrade(self):
"""查询成交"""
self.tradeApi.qryTrade() self.tradeApi.qryTrade()
self.tradeApi.qryPosition()
#----------------------------------------------------------------------
def qryOrder(self):
"""查询委托"""
self.tradeApi.qryOrder()
#---------------------------------------------------------------------- #----------------------------------------------------------------------
def close(self): def close(self):
"""关闭""" """关闭"""
@ -149,11 +141,7 @@ class HuobiGateway(VtGateway):
"""初始化连续查询""" """初始化连续查询"""
if self.qryEnabled: if self.qryEnabled:
# 需要循环的查询函数列表 # 需要循环的查询函数列表
self.qryFunctionList = [ self.qryFunctionList = [self.qryInfo]
self.qryPosition,
self.qryTrade,
self.qryOrder
]
self.qryCount = 0 # 查询触发倒计时 self.qryCount = 0 # 查询触发倒计时
self.qryTrigger = 1 # 查询触发点 self.qryTrigger = 1 # 查询触发点
@ -331,6 +319,7 @@ class HuobiDataApi(DataApi):
tick.lowPrice = t['low'] tick.lowPrice = t['low']
tick.lastPrice = t['close'] tick.lastPrice = t['close']
tick.volume = t['vol'] tick.volume = t['vol']
tick.preClosePrice = tick.openPrice
if tick.bidPrice1: if tick.bidPrice1:
newtick = copy(tick) newtick = copy(tick)
@ -350,9 +339,6 @@ class HuobiTradeApi(TradeApi):
self.gateway = gateway # gateway对象 self.gateway = gateway # gateway对象
self.gatewayName = gateway.gatewayName # gateway对象名称 self.gatewayName = gateway.gatewayName # gateway对象名称
self.reqID = EMPTY_INT # 操作请求编号
self.localID = EMPTY_INT # 订单编号
self.connectionStatus = False # 连接状态 self.connectionStatus = False # 连接状态
self.accountid = '' self.accountid = ''
@ -365,7 +351,13 @@ class HuobiTradeApi(TradeApi):
self.tradeIDs = set() # 成交编号集合 self.tradeIDs = set() # 成交编号集合
self.qryOrderID = None # 查询起始委托编号 self.qryOrderID = None # 查询起始委托编号
self.localid = 100000 # 订单编号10000为起始
self.reqLocalDict = {} # 请求编号和本地委托编号映射
self.localOrderDict = {} # 本地委托编号和交易所委托编号映射
self.orderLocalDict = {} # 交易所委托编号和本地委托编号映射
self.cancelReqDict = {} # 撤单请求字典
#---------------------------------------------------------------------- #----------------------------------------------------------------------
def connect(self, exchange, accessKey, secretKey, symbols=''): def connect(self, exchange, accessKey, secretKey, symbols=''):
"""初始化连接""" """初始化连接"""
@ -388,6 +380,9 @@ class HuobiTradeApi(TradeApi):
#---------------------------------------------------------------------- #----------------------------------------------------------------------
def qryOrder(self): def qryOrder(self):
"""查询委托""" """查询委托"""
if not self.accountid:
return
states = 'pre-submitted,submitting,submitted,partial-filled,partial-canceled,filled,canceled' states = 'pre-submitted,submitting,submitted,partial-filled,partial-canceled,filled,canceled'
for symbol in self.symbols: for symbol in self.symbols:
self.getOrders(symbol, states, startDate=self.todayDate) self.getOrders(symbol, states, startDate=self.todayDate)
@ -397,6 +392,9 @@ class HuobiTradeApi(TradeApi):
#---------------------------------------------------------------------- #----------------------------------------------------------------------
def qryTrade(self): def qryTrade(self):
"""查询成交""" """查询成交"""
if not self.accountid:
return
for symbol in self.symbols: for symbol in self.symbols:
self.getMatchResults(symbol, startDate=self.todayDate) self.getMatchResults(symbol, startDate=self.todayDate)
#self.getMatchResults(symbol, startDate=self.todayDate, from_=self.qryTradeID) #self.getMatchResults(symbol, startDate=self.todayDate, from_=self.qryTradeID)
@ -404,28 +402,40 @@ class HuobiTradeApi(TradeApi):
#---------------------------------------------------------------------- #----------------------------------------------------------------------
def sendOrder(self, orderReq): def sendOrder(self, orderReq):
"""发单""" """发单"""
self.localID += 1 self.localid += 1
vtOrderID = '.'.join([self.gatewayName, str(self.localID)]) localid = str(self.localid)
vtOrderID = '.'.join([self.gatewayName, localid])
if orderReq.direction == DIRECTION_LONG: if orderReq.direction == DIRECTION_LONG:
type_ = 'buy-limit' type_ = 'buy-limit'
else: else:
type_ = 'sell-limit' type_ = 'sell-limit'
self.placeOrder(self.accountid, reqid = self.placeOrder(self.accountid,
str(orderReq.volume), str(orderReq.volume),
orderReq.symbol, orderReq.symbol,
type_, type_,
price=str(orderReq.price), price=str(orderReq.price),
source='api') source='api')
self.reqLocalDict[reqid] = localid
print 'send', vtOrderID
# 返回订单号 # 返回订单号
return vtOrderID return vtOrderID
#---------------------------------------------------------------------- #----------------------------------------------------------------------
def cancelOrder(self, cancelOrderReq): def cancelOrder(self, cancelOrderReq):
"""撤单""" """撤单"""
super(HuobiTradeApi, self).cancelOrder(cancelOrderReq.orderID) localid = cancelOrderReq.orderID
orderID = self.localOrderDict.get(localid, None)
if orderID:
super(HuobiTradeApi, self).cancelOrder(orderID)
if localid in self.cancelReqDict:
del self.cancelReqDict[localid]
else:
self.cancelReqDict[localid] = cancelOrderReq
#---------------------------------------------------------------------- #----------------------------------------------------------------------
def writeLog(self, content): def writeLog(self, content):
@ -500,25 +510,37 @@ class HuobiTradeApi(TradeApi):
pos.direction = DIRECTION_LONG pos.direction = DIRECTION_LONG
pos.vtPositionName = '.'.join([pos.vtSymbol, pos.direction]) pos.vtPositionName = '.'.join([pos.vtSymbol, pos.direction])
if d['type'] == 'trade': pos.position += float(d['balance'])
pos.position = float(d['balance']) if d['type'] == 'fozen':
else:
pos.frozen = float(d['balance']) pos.frozen = float(d['balance'])
posDict[symbol] = pos posDict[symbol] = pos
for pos in posDict.values(): for pos in posDict.values():
self.gateway.onPosition(pos) if pos.position:
self.gateway.onPosition(pos)
#---------------------------------------------------------------------- #----------------------------------------------------------------------
def onGetOrders(self, data, reqid): def onGetOrders(self, data, reqid):
"""查询委托回调""" """查询委托回调"""
qryOrderID = None qryOrderID = None
data.reverse()
for d in data: for d in data:
orderID = d['id'] orderID = d['id']
strOrderID = str(orderID)
updated = False updated = False
if strOrderID in self.orderLocalDict:
localid = self.orderLocalDict[strOrderID]
else:
self.localid += 1
localid = str(self.localid)
self.orderLocalDict[strOrderID] = localid
self.localOrderDict[localid] = strOrderID
order = self.orderDict.get(orderID, None) order = self.orderDict.get(orderID, None)
if not order: if not order:
updated = True updated = True
@ -526,7 +548,7 @@ class HuobiTradeApi(TradeApi):
order = VtOrderData() order = VtOrderData()
order.gatewayName = self.gatewayName order.gatewayName = self.gatewayName
order.orderID = str(orderID) order.orderID = localid
order.vtOrderID = '.'.join([order.gatewayName, order.orderID]) order.vtOrderID = '.'.join([order.gatewayName, order.orderID])
order.symbol = d['symbol'] order.symbol = d['symbol']
@ -541,7 +563,9 @@ class HuobiTradeApi(TradeApi):
order.direction = DIRECTION_LONG order.direction = DIRECTION_LONG
else: else:
order.direction = DIRECTION_SHORT order.direction = DIRECTION_SHORT
order.offset = OFFSET_NONE order.offset = OFFSET_NONE
self.orderDict[orderID] = order
# 数据更新,只有当成交数量或者委托状态变化时,才执行推送 # 数据更新,只有当成交数量或者委托状态变化时,才执行推送
if d['canceled-at']: if d['canceled-at']:
@ -573,6 +597,8 @@ class HuobiTradeApi(TradeApi):
#---------------------------------------------------------------------- #----------------------------------------------------------------------
def onGetMatchResults(self, data, reqid): def onGetMatchResults(self, data, reqid):
"""查询成交回调""" """查询成交回调"""
data.reverse()
for d in data: for d in data:
tradeID = d['match-id'] tradeID = d['match-id']
@ -600,10 +626,13 @@ class HuobiTradeApi(TradeApi):
else: else:
trade.direction = DIRECTION_SHORT trade.direction = DIRECTION_SHORT
trade.offset = OFFSET_NONE trade.offset = OFFSET_NONE
trade.orderID = str(d['order-id']) strOrderID = str(d['order-id'])
localid = self.orderLocalDict.get(strOrderID, '')
trade.orderID = localid
trade.vtOrderID = '.'.join([trade.gatewayName, trade.orderID]) trade.vtOrderID = '.'.join([trade.gatewayName, trade.orderID])
trade.tradeID = str(d['match-id'])
trade.tradeID = str(tradeID)
trade.vtTradeID = '.'.join([trade.gatewayName, trade.tradeID]) trade.vtTradeID = '.'.join([trade.gatewayName, trade.tradeID])
trade.price = float(d['price']) trade.price = float(d['price'])
@ -627,7 +656,14 @@ class HuobiTradeApi(TradeApi):
#---------------------------------------------------------------------- #----------------------------------------------------------------------
def onPlaceOrder(self, data, reqid): def onPlaceOrder(self, data, reqid):
"""委托回调""" """委托回调"""
print reqid, data localid = self.reqLocalDict[reqid]
self.localOrderDict[localid] = data
self.orderLocalDict[data] = localid
if localid in self.cancelReqDict:
req = self.cancelReqDict[localid]
self.cancelOrder(req)
#---------------------------------------------------------------------- #----------------------------------------------------------------------
def onCancelOrder(self, data, reqid): def onCancelOrder(self, data, reqid):