[Add]Bitfinex接口增加自动重连和持仓更新逻辑

This commit is contained in:
vn.py 2018-06-05 14:26:17 +08:00
parent 98dcecb470
commit 08e836b070
2 changed files with 38 additions and 4 deletions

View File

@ -2,6 +2,7 @@
import json
import requests
import traceback
from threading import Thread
from queue import Queue, Empty
@ -37,20 +38,35 @@ class BitfinexApi(object):
self.restThread = Thread(target=self.runRest)
self.restThread.start()
#----------------------------------------------------------------------
def reconnect(self):
""""""
self.ws = websocket.create_connection(WEBSOCKET_V2_URL)
#----------------------------------------------------------------------
def run(self):
""""""
while self.active:
stream = self.ws.recv()
data = json.loads(stream)
self.onData(data)
try:
stream = self.ws.recv()
data = json.loads(stream)
self.onData(data)
except:
msg = traceback.format_exc()
self.onError(msg)
self.reconnect()
#----------------------------------------------------------------------
def onData(self, data):
""""""
print data
#----------------------------------------------------------------------
def onError(self, msg):
""""""
print msg
#----------------------------------------------------------------------
def sendReq(self, req):
""""""

View File

@ -163,6 +163,7 @@ class GatewayApi(BitfinexApi):
self.apiKey = ''
self.secretKey = ''
self.symbols = []
self.currencys = []
self.tickDict = {}
self.bidDict = {}
@ -241,6 +242,8 @@ class GatewayApi(BitfinexApi):
#----------------------------------------------------------------------
def sendOrder(self, orderReq):
""""""
orderReq.volume = 0.02
self.orderId += 1
orderId = self.date + self.orderId
vtOrderID = '.'.join([self.gatewayName, str(orderId)])
@ -281,6 +284,16 @@ class GatewayApi(BitfinexApi):
self.sendReq(req)
#----------------------------------------------------------------------
def calc(self):
""""""
l = []
for currency in self.currencys:
l.append(['wallet_exchange_' + currency])
req = [0, 'calc', None, l]
self.sendReq(req)
#----------------------------------------------------------------------
def onData(self, data):
""""""
@ -416,11 +429,13 @@ class GatewayApi(BitfinexApi):
if name == 'ws':
for l in info:
self.onWallet(l)
self.writeLog(u'账户资金获取成功')
elif name == 'wu':
self.onWallet(info)
elif name == 'os':
for l in info:
self.onOrder(l)
self.writeLog(u'活动委托获取成功')
elif name in ['on', 'ou', 'oc']:
self.onOrder(info)
elif name == 'te':
@ -445,6 +460,7 @@ class GatewayApi(BitfinexApi):
else:
pos.frozen = pos.position - float(data[-1])
self.currencys.append(pos.symbol)
self.gateway.onPosition(pos)
#----------------------------------------------------------------------
@ -479,7 +495,9 @@ class GatewayApi(BitfinexApi):
self.orderLocalDict[data[0]] = order.orderID
self.gateway.onOrder(order)
self.gateway.onOrder(order)
self.calc()
#----------------------------------------------------------------------
def onTrade(self, data):