[Mod]修改HuobiGateway的委托查询机制

This commit is contained in:
vn.py 2018-05-21 10:31:47 +08:00
parent e661a9f5d3
commit eddfd56e6d

View File

@ -6,7 +6,7 @@ vn.sec的gateway接入
import os import os
import json import json
from datetime import datetime from datetime import datetime, timedelta
from copy import copy from copy import copy
from math import pow from math import pow
@ -338,8 +338,6 @@ class HuobiTradeApi(TradeApi):
self.connectionStatus = False # 连接状态 self.connectionStatus = False # 连接状态
self.accountid = '' self.accountid = ''
self.todayDate = datetime.now().strftime('%Y-%m-%d')
self.orderDict = {} # 缓存委托数据的字典 self.orderDict = {} # 缓存委托数据的字典
self.symbols = [] # 所有交易代码的字符串集合 self.symbols = [] # 所有交易代码的字符串集合
@ -354,7 +352,7 @@ class HuobiTradeApi(TradeApi):
self.orderLocalDict = {} # 交易所委托编号和本地委托编号映射 self.orderLocalDict = {} # 交易所委托编号和本地委托编号映射
self.cancelReqDict = {} # 撤单请求字典 self.cancelReqDict = {} # 撤单请求字典
self.activeOrderSet = set() # 活动委托集合 #self.activeOrderSet = set() # 活动委托集合
#---------------------------------------------------------------------- #----------------------------------------------------------------------
def connect(self, exchange, accessKey, secretKey, symbols=''): def connect(self, exchange, accessKey, secretKey, symbols=''):
@ -382,10 +380,17 @@ class HuobiTradeApi(TradeApi):
if not self.accountid: if not self.accountid:
return return
#states = 'pre-submitted,submitting,submitted,partial-filled,partial-canceled,filled,canceled' now = datetime.now()
states = 'pre-submitted,submitting,submitted,partial-filled' oneday = timedelta(1)
todayDate = now.strftime('%Y-%m-%d')
yesterdayDate = (now - oneday).strftime('%Y-%m-%d')
statesAll = 'pre-submitted,submitting,submitted,partial-filled,partial-canceled,filled,canceled'
statesActive = 'submitted,partial-filled'
for symbol in self.symbols: for symbol in self.symbols:
self.getOrders(symbol, states, startDate=self.todayDate) self.getOrders(symbol, statesAll, startDate=todayDate) # 查询今日所有状态的委托
self.getOrders(symbol, statesActive, endDate=yesterdayDate) # 查询昨日往前所有未结束的委托
#---------------------------------------------------------------------- #----------------------------------------------------------------------
def qryTrade(self): def qryTrade(self):
@ -393,8 +398,11 @@ class HuobiTradeApi(TradeApi):
if not self.accountid: if not self.accountid:
return return
now = datetime.now()
todayDate = now.strftime('%Y-%m-%d')
for symbol in self.symbols: for symbol in self.symbols:
self.getMatchResults(symbol, startDate=self.todayDate, size=50) self.getMatchResults(symbol, startDate=todayDate, size=50) # 只查询今日最新50笔成交
#---------------------------------------------------------------------- #----------------------------------------------------------------------
def sendOrder(self, orderReq): def sendOrder(self, orderReq):
@ -522,21 +530,31 @@ class HuobiTradeApi(TradeApi):
def onGetOrders(self, data, reqid): def onGetOrders(self, data, reqid):
"""查询委托回调""" """查询委托回调"""
# 比对寻找已结束的委托号 # 比对寻找已结束的委托号
"""
newset = set([d['id'] for d in data]) newset = set([d['id'] for d in data])
print '-'*50
print [d['id'] for d in data]
print self.activeOrderSet
for id_ in self.activeOrderSet: for id_ in self.activeOrderSet:
if id_ not in newset: if id_ not in newset:
print 'finished:', id_
self.getOrder(id_) self.getOrder(id_)
self.activeOrderSet = newset #self.activeOrderSet = newset
"""
# 推送数据 # 推送数据
qryOrderID = None #qryOrderID = None
data.reverse() data.reverse()
for d in data: for d in data:
orderID = d['id'] orderID = d['id']
#self.activeOrderSet.add(orderID)
strOrderID = str(orderID) strOrderID = str(orderID)
updated = False updated = False
@ -654,18 +672,23 @@ class HuobiTradeApi(TradeApi):
#---------------------------------------------------------------------- #----------------------------------------------------------------------
def onGetOrder(self, data, reqid): def onGetOrder(self, data, reqid):
"""查询单一委托回调""" """查询单一委托回调"""
orderID = data['id'] #orderID = data['id']
strOrderID = str(orderID) #strOrderID = str(orderID)
localid = self.orderLocalDict[strOrderID] #localid = self.orderLocalDict[strOrderID]
order = self.orderDict[orderID] #order = self.orderDict[orderID]
order.tradedVolume = float(data['field-amount']) #order.tradedVolume = float(data['field-amount'])
order.status = statusMapReverse.get(data['state'], STATUS_UNKNOWN) #order.status = statusMapReverse.get(data['state'], STATUS_UNKNOWN)
if data['canceled-at']: #if data['canceled-at']:
order.cancelTime = datetime.fromtimestamp(data['canceled-at']/1000).strftime('%H:%M:%S') #order.cancelTime = datetime.fromtimestamp(data['canceled-at']/1000).strftime('%H:%M:%S')
self.gateway.onOrder(order) ## 完成的委托则从集合中移除
#if order.status in [STATUS_ALLTRADED, STATUS_CANCELLED]:
#self.activeOrderSet.remove(orderID)
#self.gateway.onOrder(order)
pass
#---------------------------------------------------------------------- #----------------------------------------------------------------------
def onGetMatchResult(self, data, reqid): def onGetMatchResult(self, data, reqid):
@ -680,6 +703,8 @@ class HuobiTradeApi(TradeApi):
self.localOrderDict[localid] = data self.localOrderDict[localid] = data
self.orderLocalDict[data] = localid self.orderLocalDict[data] = localid
#self.activeOrderSet.add(data)
if localid in self.cancelReqDict: if localid in self.cancelReqDict:
req = self.cancelReqDict[localid] req = self.cancelReqDict[localid]
self.cancelOrder(req) self.cancelOrder(req)