[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,
"logLevel": "debug",
"logConsole": true,
"logConsole": false,
"logFile": true,
"tdPenalty": ["IF", "IH", "IC"]

View File

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

View File

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