mirror of
https://github.com/aljazceru/bitfinex-api-py.git
synced 2025-12-19 06:44:22 +01:00
General fixups
This commit is contained in:
@@ -5,14 +5,19 @@ class OrderType:
|
||||
MARKET = 'MARKET'
|
||||
LIMIT = 'LIMIT'
|
||||
STOP = 'STOP'
|
||||
STOP_LIMIT = 'STOP LIMIT'
|
||||
TRAILING_STOP = 'TRAILING STOP'
|
||||
FILL_OR_KILL = 'FOK'
|
||||
EXCHANGE_MARKET = 'EXCHANGE MARKET'
|
||||
EXCHANGE_LIMIT = 'EXCHANGE LIMIT'
|
||||
EXCHANGE_STOP = 'EXCHANGE STOP'
|
||||
EXCHANGE_STOP_LIMIT = 'EXCHANGE STOP LIMIT'
|
||||
EXCHANGE_TRAILING_STOP = 'EXCHANGE TRAILING STOP'
|
||||
EXCHANGE_FILL_OR_KILL = 'EXCHANGE FOK'
|
||||
|
||||
LIMIT_ORDERS = [OrderType.LIMIT, OrderType.STOP_LIMIT, OrderType.EXCHANGE_LIMIT,
|
||||
OrderType.EXCHANGE_STOP_LIMIT, OrderType.FILL_OR_KILL, OrderType.EXCHANGE_FILL_OR_KILL]
|
||||
|
||||
class OrderSide:
|
||||
BUY = 'buy'
|
||||
SELL = 'sell'
|
||||
@@ -81,6 +86,7 @@ class Order:
|
||||
self.symbol = symbol
|
||||
self.mtsCreate = mtsCreate
|
||||
self.mtsUpdate = mtsUpdate
|
||||
# self.amount = amount
|
||||
self.amount = amount
|
||||
self.amountOrig = amountOrig
|
||||
self.type = oType
|
||||
@@ -93,19 +99,25 @@ class Order:
|
||||
self.priceAuxLimit = priceAuxLimit
|
||||
self.notfiy = notfiy
|
||||
self.placeId = placeId
|
||||
self.tag = ""
|
||||
self.fee = 0
|
||||
|
||||
self.is_pending_bool = True
|
||||
self.is_confirmed_bool = False
|
||||
self.is_open_bool = False
|
||||
|
||||
self.date = datetime.datetime.fromtimestamp(mtsCreate/1000.0)
|
||||
## if cancelled then priceAvg wont exist
|
||||
if priceAvg:
|
||||
## if cancelled then priceAvg wont exist
|
||||
self.fee = (priceAvg * abs(amount)) * 0.002
|
||||
## check if order is taker or maker
|
||||
if self.type in LIMIT_ORDERS:
|
||||
self.fee = (priceAvg * abs(amount)) * 0.001
|
||||
else:
|
||||
self.fee = (priceAvg * abs(amount)) * 0.002
|
||||
|
||||
@staticmethod
|
||||
def from_raw_order(raw_order):
|
||||
id = raw_order[OrderClosedModel.ID]
|
||||
oid = raw_order[OrderClosedModel.ID]
|
||||
gId = raw_order[OrderClosedModel.GID]
|
||||
cId = raw_order[OrderClosedModel.CID]
|
||||
symbol = raw_order[OrderClosedModel.SYMBOL]
|
||||
@@ -124,7 +136,7 @@ class Order:
|
||||
notfiy = raw_order[OrderClosedModel.NOTIFY]
|
||||
placeId = raw_order[OrderClosedModel.PLACE_ID]
|
||||
|
||||
return Order(id, gId, cId, symbol, mtsCreate, mtsUpdate, amount, amountOrig, oType,
|
||||
return Order(oid, gId, cId, symbol, mtsCreate, mtsUpdate, amount, amountOrig, oType,
|
||||
typePrev, flags, status, price, priceAvg, priceTrailing, priceAuxLimit, notfiy, placeId)
|
||||
|
||||
def set_confirmed(self):
|
||||
|
||||
@@ -12,6 +12,12 @@ class OrderBook:
|
||||
self.asks = []
|
||||
self.bids = []
|
||||
|
||||
def get_bids(self):
|
||||
return self.bids
|
||||
|
||||
def get_asks(self):
|
||||
return self.asks
|
||||
|
||||
def updateFromSnapshot(self, data):
|
||||
# [[4642.3, 1, 4.192], [4641.5, 1, 1]]
|
||||
for order in data:
|
||||
|
||||
@@ -148,6 +148,8 @@ class BfxWebsocket(GenericWebsocket):
|
||||
await self._candle_handler(data)
|
||||
if subscription.channel_name == 'book':
|
||||
await self._order_book_handler(data)
|
||||
if subscription.channel_name == 'trades':
|
||||
await self._trade_handler(data)
|
||||
else:
|
||||
self.logger.warn("Unknown data event: '{}' {}".format(dataEvent, data))
|
||||
|
||||
@@ -228,7 +230,7 @@ class BfxWebsocket(GenericWebsocket):
|
||||
notificationType = nInfo[6]
|
||||
notificationText = nInfo[7]
|
||||
if notificationType == 'ERROR':
|
||||
self._emit('error', notificationText)
|
||||
# self._emit('error', notificationText)
|
||||
self.logger.error("Notification ERROR: {}".format(notificationText))
|
||||
else:
|
||||
self.logger.info("Notification SUCCESS: {}".format(notificationText))
|
||||
@@ -279,14 +281,11 @@ class BfxWebsocket(GenericWebsocket):
|
||||
for t in data:
|
||||
trade = {
|
||||
'mts': t[1],
|
||||
'price': t[2],
|
||||
'amount': t[3],
|
||||
'amount': t[2],
|
||||
'price': t[3],
|
||||
'symbol': symbol
|
||||
}
|
||||
self._emit('seed_trade', trade)
|
||||
else:
|
||||
tradeObj = _parse_trade_snapshot_item(data, symbol)
|
||||
self._emit('new_trade', tradeObj)
|
||||
|
||||
async def _candle_handler(self, data):
|
||||
subscription = self.subscriptionManager.get(data[0])
|
||||
@@ -321,6 +320,9 @@ class BfxWebsocket(GenericWebsocket):
|
||||
# re-build orderbook with snapshot
|
||||
await self.subscriptionManager.resubscribe(chanId)
|
||||
return
|
||||
if obInfo == []:
|
||||
self.orderBooks[symbol] = OrderBook()
|
||||
return
|
||||
isSnapshot = type(obInfo[0]) is list
|
||||
if isSnapshot:
|
||||
self.orderBooks[symbol] = OrderBook()
|
||||
@@ -370,6 +372,9 @@ class BfxWebsocket(GenericWebsocket):
|
||||
}
|
||||
await self.ws.send(json.dumps(payload))
|
||||
|
||||
def get_orderbook(self, symbol):
|
||||
return self.orderBooks.get(symbol, None)
|
||||
|
||||
async def subscribe(self, *args, **kwargs):
|
||||
return await self.subscriptionManager.subscribe(*args, **kwargs)
|
||||
|
||||
|
||||
@@ -26,6 +26,9 @@ class GenericWebsocket(object):
|
||||
def run(self):
|
||||
self.loop.run_until_complete(self._main(self.host))
|
||||
|
||||
def get_task_executable(self):
|
||||
return self._main(self.host)
|
||||
|
||||
async def _main(self, host):
|
||||
async with websockets.connect(host) as websocket:
|
||||
self.ws = websocket
|
||||
@@ -35,6 +38,9 @@ class GenericWebsocket(object):
|
||||
message = await websocket.recv()
|
||||
await self.on_message(message)
|
||||
|
||||
def remove_all_listeners(self, event):
|
||||
self.events.remove_all_listeners(event)
|
||||
|
||||
def on(self, event, func=None):
|
||||
if not func:
|
||||
return self.events.on(event)
|
||||
|
||||
Reference in New Issue
Block a user