更新okcoin接口

This commit is contained in:
chenxy123 2016-11-17 22:00:09 +08:00
parent 3bbda28e94
commit ecb42c4540
3 changed files with 69 additions and 27 deletions

View File

@ -166,6 +166,13 @@ class OkCoinApi(object):
self.thread = Thread(target=self.ws.run_forever) self.thread = Thread(target=self.ws.run_forever)
self.thread.start() self.thread.start()
#----------------------------------------------------------------------
def close(self):
"""关闭接口"""
if self.thread.isAlive:
self.ws.close()
self.thread.join()
#---------------------------------------------------------------------- #----------------------------------------------------------------------
def sendMarketDataRequest(self, channel): def sendMarketDataRequest(self, channel):
"""发送行情请求""" """发送行情请求"""

View File

@ -14,6 +14,7 @@ import json
from datetime import datetime from datetime import datetime
from copy import copy from copy import copy
from threading import Condition from threading import Condition
from Queue import Queue
import vnokcoin import vnokcoin
from vtGateway import * from vtGateway import *
@ -107,8 +108,7 @@ class OkcoinGateway(VtGateway):
"""连接""" """连接"""
# 载入json文件 # 载入json文件
fileName = self.gatewayName + '_connect.json' fileName = self.gatewayName + '_connect.json'
path = os.path.abspath(os.path.dirname(__file__)) fileName = os.getcwd() + '/okcoinGateway/' + fileName
fileName = os.path.join(path, fileName)
try: try:
f = file(fileName) f = file(fileName)
@ -181,7 +181,7 @@ class OkcoinGateway(VtGateway):
#---------------------------------------------------------------------- #----------------------------------------------------------------------
def close(self): def close(self):
"""关闭""" """关闭"""
pass self.api.close()
#---------------------------------------------------------------------- #----------------------------------------------------------------------
def initQuery(self): def initQuery(self):
@ -245,6 +245,12 @@ class Api(vnokcoin.OkCoinApi):
self.lastOrderID = '' self.lastOrderID = ''
self.orderCondition = Condition() self.orderCondition = Condition()
self.localNo = 0 # 本地委托号
self.localNoQueue = Queue() # 未收到系统委托号的本地委托号队列
self.localNoDict = {} # key为本地委托号value为系统委托号
self.orderIdDict = {} # key为系统委托号value为本地委托号
self.cancelDict = {} # key为本地委托号value为撤单请求
self.initCallback() self.initCallback()
#---------------------------------------------------------------------- #----------------------------------------------------------------------
@ -479,25 +485,28 @@ class Api(vnokcoin.OkCoinApi):
return return
rawData = data['data'] rawData = data['data']
# 本地和系统委托号
orderId = str(rawData['orderId'])
localNo = self.orderIdDict[orderId]
# 委托信息 # 委托信息
orderID = str(rawData['orderId']) if orderId not in self.orderDict:
if orderID not in self.orderDict:
order = VtOrderData() order = VtOrderData()
order.gatewayName = self.gatewayName order.gatewayName = self.gatewayName
order.symbol = spotSymbolMap[rawData['symbol']] order.symbol = spotSymbolMap[rawData['symbol']]
order.vtSymbol = order.symbol order.vtSymbol = order.symbol
order.orderID = str(rawData['orderId']) order.orderID = localNo
order.vtOrderID = '.'.join([self.gatewayName, order.orderID]) order.vtOrderID = '.'.join([self.gatewayName, order.orderID])
order.price = float(rawData['tradeUnitPrice']) order.price = float(rawData['tradeUnitPrice'])
order.totalVolume = float(rawData['tradeAmount']) order.totalVolume = float(rawData['tradeAmount'])
order.direction, priceType = priceTypeMap[rawData['tradeType']] order.direction, priceType = priceTypeMap[rawData['tradeType']]
self.orderDict[orderID] = order self.orderDict[orderId] = order
else: else:
order = self.orderDict[orderID] order = self.orderDict[orderId]
order.tradedVolume = float(rawData['completedTradeAmount']) order.tradedVolume = float(rawData['completedTradeAmount'])
order.status = statusMap[rawData['status']] order.status = statusMap[rawData['status']]
@ -515,7 +524,7 @@ class Api(vnokcoin.OkCoinApi):
trade.tradeID = str(rawData['id']) trade.tradeID = str(rawData['id'])
trade.vtTradeID = '.'.join([self.gatewayName, trade.tradeID]) trade.vtTradeID = '.'.join([self.gatewayName, trade.tradeID])
trade.orderID = str(rawData['orderId']) trade.orderID = localNo
trade.vtOrderID = '.'.join([self.gatewayName, trade.orderID]) trade.vtOrderID = '.'.join([self.gatewayName, trade.orderID])
trade.price = float(rawData['sigTradePrice']) trade.price = float(rawData['sigTradePrice'])
@ -533,25 +542,30 @@ class Api(vnokcoin.OkCoinApi):
rawData = data['data'] rawData = data['data']
for d in rawData['orders']: for d in rawData['orders']:
orderID = str(d['order_id']) self.localNo += 1
localNo = str(self.localNo)
orderId = str(d['order_id'])
if orderID not in self.orderDict: self.localNoDict[localNo] = orderId
self.orderIdDict[orderId] = localNo
if orderId not in self.orderDict:
order = VtOrderData() order = VtOrderData()
order.gatewayName = self.gatewayName order.gatewayName = self.gatewayName
order.symbol = spotSymbolMap[d['symbol']] order.symbol = spotSymbolMap[d['symbol']]
order.vtSymbol = order.symbol order.vtSymbol = order.symbol
order.orderID = str(d['order_id']) order.orderID = localNo
order.vtOrderID = '.'.join([self.gatewayName, order.orderID]) order.vtOrderID = '.'.join([self.gatewayName, order.orderID])
order.price = d['price'] order.price = d['price']
order.totalVolume = d['amount'] order.totalVolume = d['amount']
order.direction, priceType = priceTypeMap[d['type']] order.direction, priceType = priceTypeMap[d['type']]
self.orderDict[orderID] = order self.orderDict[orderId] = order
else: else:
order = self.orderDict[orderID] order = self.orderDict[orderId]
order.tradedVolume = d['deal_amount'] order.tradedVolume = d['deal_amount']
order.status = statusMap[d['status']] order.status = statusMap[d['status']]
@ -614,12 +628,22 @@ class Api(vnokcoin.OkCoinApi):
def onSpotTrade(self, data): def onSpotTrade(self, data):
"""委托回报""" """委托回报"""
rawData = data['data'] rawData = data['data']
self.lastOrderID = rawData['order_id'] orderId = rawData['order_id']
# 收到委托号后,通知发送委托的线程返回委托号 # 尽管websocket接口的委托号返回是异步的但经过测试是
self.orderCondition.acquire() # 符合先发现回的规律因此这里通过queue获取之前发送的
self.orderCondition.notify() # 本地委托号,并把它和推送的系统委托号进行映射
self.orderCondition.release() localNo = self.localNoQueue.get_nowait()
self.localNoDict[localNo] = orderId
self.orderIdDict[orderId] = localNo
# 检查是否有系统委托号返回前就发出的撤单请求,若有则进
# 行撤单操作
if localNo in self.cancelDict:
req = self.cancelDict[localNo]
self.spotCancel(req)
del self.cancelDict[localNo]
#---------------------------------------------------------------------- #----------------------------------------------------------------------
def onSpotCancelOrder(self, data): def onSpotCancelOrder(self, data):
@ -633,21 +657,25 @@ class Api(vnokcoin.OkCoinApi):
type_ = priceTypeMapReverse[(req.direction, req.priceType)] type_ = priceTypeMapReverse[(req.direction, req.priceType)]
self.spotTrade(symbol, type_, str(req.price), str(req.volume)) self.spotTrade(symbol, type_, str(req.price), str(req.volume))
# 等待发单回调推送委托号信息 # 本地委托号加1并将对应字符串保存到队列中返回基于本地委托号的vtOrderID
self.orderCondition.acquire() self.localNo += 1
self.orderCondition.wait() self.localNoQueue.put(str(self.localNo))
self.orderCondition.release() vtOrderID = '.'.join([self.gatewayName, str(self.localNo)])
vtOrderID = '.'.join([self.gatewayName, self.lastOrderID])
self.lastOrderID = ''
return vtOrderID return vtOrderID
#---------------------------------------------------------------------- #----------------------------------------------------------------------
def spotCancel(self, req): def spotCancel(self, req):
"""撤单""" """撤单"""
symbol = spotSymbolMapReverse[req.symbol][:4] symbol = spotSymbolMapReverse[req.symbol][:4]
self.spotCancelOrder(symbol, req.orderID) localNo = req.orderID
if localNo in self.localNoDict:
orderID = self.localNoDict[localNo]
self.spotCancelOrder(symbol, orderID)
else:
# 如果在系统委托号返回前客户就发送了撤单请求,则保存
# 在cancelDict字典中等待返回后执行撤单任务
self.cancelDict[localNo] = req
#---------------------------------------------------------------------- #----------------------------------------------------------------------
def generateDateTime(s): def generateDateTime(s):

View File

@ -166,6 +166,13 @@ class OkCoinApi(object):
self.thread = Thread(target=self.ws.run_forever) self.thread = Thread(target=self.ws.run_forever)
self.thread.start() self.thread.start()
#----------------------------------------------------------------------
def close(self):
"""关闭接口"""
if self.thread.isAlive:
self.ws.close()
self.thread.join()
#---------------------------------------------------------------------- #----------------------------------------------------------------------
def sendMarketDataRequest(self, channel): def sendMarketDataRequest(self, channel):
"""发送行情请求""" """发送行情请求"""