From 3650bc7944f0381441be2430c0b1bb44214f8d76 Mon Sep 17 00:00:00 2001 From: itsdeka Date: Sun, 30 Jan 2022 12:54:57 +0100 Subject: [PATCH] fixed trades handling error added new_user_trade use to examples updated docs --- .../examples/ws/subscribe_trades_candles.py | 4 ++ bfxapi/websockets/bfx_websocket.py | 43 +++++++++++++++++++ docs/ws_v2.md | 2 + 3 files changed, 49 insertions(+) diff --git a/bfxapi/examples/ws/subscribe_trades_candles.py b/bfxapi/examples/ws/subscribe_trades_candles.py index 2e17fe3..d7135e6 100644 --- a/bfxapi/examples/ws/subscribe_trades_candles.py +++ b/bfxapi/examples/ws/subscribe_trades_candles.py @@ -20,6 +20,10 @@ def log_candle(candle): def log_trade(trade): print ("New trade: {}".format(trade)) +@bfx.ws.on('new_user_trade') +def log_user_trade(trade): + print ("New user trade: {}".format(trade)) + async def start(): await bfx.ws.subscribe('candles', 'tBTCUSD', timeframe='1m') await bfx.ws.subscribe('trades', 'tBTCUSD') diff --git a/bfxapi/websockets/bfx_websocket.py b/bfxapi/websockets/bfx_websocket.py index 6df4487..74c2371 100644 --- a/bfxapi/websockets/bfx_websocket.py +++ b/bfxapi/websockets/bfx_websocket.py @@ -67,6 +67,38 @@ def _parse_trade(tData, symbol): } +def _parse_user_trade(tData): + return { + 'id': tData[0], + 'symbol': tData[1], + 'mts_create': tData[2], + 'order_id': tData[3], + 'exec_amount': tData[4], + 'exec_price': tData[5], + 'order_type': tData[6], + 'order_price': tData[7], + 'maker': tData[8], + 'cid': tData[11], + } + + +def _parse_user_trade_update(tData): + return { + 'id': tData[0], + 'symbol': tData[1], + 'mts_create': tData[2], + 'order_id': tData[3], + 'exec_amount': tData[4], + 'exec_price': tData[5], + 'order_type': tData[6], + 'order_price': tData[7], + 'maker': tData[8], + 'fee': tData[9], + 'fee_currency': tData[10], + 'cid': tData[11], + } + + def _parse_deriv_status_update(sData, symbol): return { 'symbol': symbol, @@ -141,6 +173,7 @@ class BfxWebsocket(GenericWebsocket): - `funding_credit_snapshot` (array): Opening funding credit balances - `balance_update` (array): When the state of a balance is changed - `new_trade` (array): A new trade on the market has been executed + - `new_user_trade` (array): A new - your - trade has been executed - `new_ticker` (Ticker|FundingTicker): A new ticker update has been published - `new_funding_ticker` (FundingTicker): A new funding ticker update has been published - `new_trading_ticker` (Ticker): A new trading ticker update has been published @@ -284,6 +317,11 @@ class BfxWebsocket(GenericWebsocket): symbol = self.subscriptionManager.get(data[0]).symbol tradeObj = _parse_trade(tData, symbol) self._emit('trade_update', tradeObj) + else: + # user trade + # [0,"tu",[738045455,"tTESTBTC:TESTUSD",1622169615771,66635385225,0.001,38175,"EXCHANGE LIMIT",39000,-1,-0.000002,"TESTBTC",1622169615685]] + tradeObj = _parse_user_trade_update(tData) + self._emit('user_trade_update', tradeObj) async def _trade_executed_handler(self, data): tData = data[2] @@ -292,6 +330,11 @@ class BfxWebsocket(GenericWebsocket): symbol = self.subscriptionManager.get(data[0]).symbol tradeObj = _parse_trade(tData, symbol) self._emit('new_trade', tradeObj) + else: + # user trade + # [0, 'te', [37558151, 'tBTCUSD', 1643542688513, 1512164914, 0.0001, 30363, 'EXCHANGE MARKET', 100000, -1, None, None, 1643542688390]] + tradeObj = _parse_user_trade(tData) + self._emit('new_user_trade', tradeObj) async def _wallet_update_handler(self, data): # [0,"wu",["exchange","USD",89134.66933283,0]] diff --git a/docs/ws_v2.md b/docs/ws_v2.md index 783e74f..f0cc3bb 100644 --- a/docs/ws_v2.md +++ b/docs/ws_v2.md @@ -76,10 +76,12 @@ https://github.com/Crypto-toolbox/btfxwss - `funding_credit_snapshot` (array): Opening funding credit balances - `balance_update` (array): When the state of a balance is changed - `new_trade` (array): A new trade on the market has been executed + - `new_user_trade` (array): A new - your - trade has been executed - `new_ticker` (Ticker|FundingTicker): A new ticker update has been published - `new_funding_ticker` (FundingTicker): A new funding ticker update has been published - `new_trading_ticker` (Ticker): A new trading ticker update has been published - `trade_update` (array): A trade on the market has been updated + - `user_trade_update` (array): A - your - trade has been updated - `new_candle` (array): A new candle has been produced - `margin_info_updates` (array): New margin information has been broadcasted - `funding_info_updates` (array): New funding information has been broadcasted