Skip to content
Merged

2.0.1 #193

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
2.0.1
-) Added User Settings Write/Read/Delete endpoints (REST)
-) Added Balance Available for Orders/Offers endpoint (REST)
-) Added Alerts endpoints (REST)
-) Fixed trades handling error

2.0.0
-) Implemented Movement endpoints (REST)
-) Fixed unawaited stop
Expand Down
4 changes: 4 additions & 0 deletions bfxapi/examples/ws/subscribe_trades_candles.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
117 changes: 117 additions & 0 deletions bfxapi/rest/bfx_rest.py
Original file line number Diff line number Diff line change
Expand Up @@ -1005,6 +1005,123 @@ async def claim_position(self, position_id, amount):
message = await self.post(endpoint, payload)
return message

async def get_alerts(self):
"""
Retrieve a list of active price alerts
"""
endpoint = f"auth/r/alerts"

message = await self.post(endpoint, {})
return message

async def set_alert(self, type, symbol, price):
"""
Sets up a price alert at the given value

# Attributes
@param type string
@param symbol string
@param price float
"""
endpoint = f"auth/w/alert/set"
payload = {
"type": type,
"symbol": symbol,
"price": price
}

message = await self.post(endpoint, payload)
return message

async def delete_alert(self, symbol, price):
"""
Delete an active alert

# Attributes
@param symbol string
@param price float
"""
endpoint = f"auth/w/alert/price:{symbol}:{price}/del"
payload = {
"symbol": symbol,
"price": price
}

message = await self.post(endpoint, payload)
return message

async def calc_order_avail(self, symbol, type, lev, dir=None, rate=None):
"""
Calculate the balance available for orders/offers

# Attributes
@param symbol str: Symbol (tBTCUSD, tBTCUST, fUSD, .... )
@param dir int: Direction of the order (1 for by, -1 for sell) (Mandator for EXCHANGE and MARGIN type, not used for FUNDING)
@param rate str: Order price (Mandator for EXCHANGE and MARGIN type, not used for FUNDING)
@param type str: Type of the order/offer EXCHANGE, MARGIN, DERIV, or FUNDING
@param lev str: Leverage that you want to use in calculating the max order amount (DERIV only)
"""
endpoint = f"auth/calc/order/avail"
payload = {
"symbol": symbol,
"type": type,
"lev": lev
}

if dir:
payload["dir"] = dir

if rate:
payload["rate"] = rate

message = await self.post(endpoint, payload)
return message

async def write_user_settings(self, settings):
"""
Allows you to create custom settings by creating key: value pairs

# Attributes
@param Settings object: object of keys and values to be set. Must follow regex pattern /^api:[A-Za-z0-9_-]*$/
"""
endpoint = f"auth/w/settings/set"
payload = {
"Settings": settings
}

message = await self.post(endpoint, payload)
return message

async def read_user_settings(self, keys):
"""
Allows you to read custom settings by providing a key

# Attributes
@param Keys array: the keys for which you wish to retrieve the values
"""
endpoint = f"auth/w/settings"
payload = {
"Keys": keys
}

message = await self.post(endpoint, payload)
return message

async def delete_user_settings(self, settings):
"""
Allows you to delete custom settings

# Attributes
@param settings object: object of keys to be deleted followed by value 1. Must follow regex pattern /^api:[A-Za-z0-9_-]*$/
"""
endpoint = f"auth/w/settings/del"
payload = {
"Settings": settings
}

message = await self.post(endpoint, payload)
return message

async def get_auth_pulse_hist(self, is_public=None):
"""
Allows you to retrieve your private pulse history or the public pulse history with an additional UID_LIKED field.
Expand Down
2 changes: 1 addition & 1 deletion bfxapi/version.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
This module contains the current version of the bfxapi lib
"""

__version__ = '2.0.0'
__version__ = '2.0.1'
40 changes: 31 additions & 9 deletions bfxapi/websockets/bfx_websocket.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@ def _parse_trade(tData, symbol):
'symbol': symbol
}

def _parse_account_trade(tData):

def _parse_user_trade(tData):
return {
'id': tData[0],
'symbol': tData[1],
Expand All @@ -81,7 +82,7 @@ def _parse_account_trade(tData):
}


def _parse_account_trade_update(tData):
def _parse_user_trade_update(tData):
return {
'id': tData[0],
'symbol': tData[1],
Expand All @@ -97,6 +98,7 @@ def _parse_account_trade_update(tData):
'cid': tData[11],
}


def _parse_deriv_status_update(sData, symbol):
return {
'symbol': symbol,
Expand Down Expand Up @@ -171,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
Expand Down Expand Up @@ -309,15 +312,29 @@ async def _system_auth_handler(self, socketId, data):

async def _trade_update_handler(self, data):
tData = data[2]
# [0,"tu",[738045455,"tTESTBTC:TESTUSD",1622169615771,66635385225,0.001,38175,"EXCHANGE LIMIT",39000,-1,-0.000002,"TESTBTC",1622169615685]]
tradeObj = _parse_account_trade_update(tData)
self._emit('trade_update', tradeObj)
# [209, 'tu', [312372989, 1542303108930, 0.35, 5688.61834032]]
if self.subscriptionManager.is_subscribed(data[0]):
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]
# [0,"te",[738045455,"tTESTBTC:TESTUSD",1622169615771,66635385225,0.001,38175,"EXCHANGE LIMIT",39000,-1,null,null,1622169615685]]
tradeObj = _parse_account_trade(tData)
self._emit('new_trade', tradeObj)
# [209, 'te', [312372989, 1542303108930, 0.35, 5688.61834032]]
if self.subscriptionManager.is_subscribed(data[0]):
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]]
Expand Down Expand Up @@ -436,7 +453,12 @@ async def _trade_handler(self, data):
# connection
data.reverse()
for t in data:
trade = _parse_trade(t, symbol)
trade = {
'mts': t[1],
'amount': t[2],
'price': t[3],
'symbol': symbol
}
self._emit('seed_trade', trade)

async def _candle_handler(self, data):
Expand Down
2 changes: 2 additions & 0 deletions docs/ws_v2.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
here = path.abspath(path.dirname(__file__))
setup(
name='bitfinex-api-py',
version='2.0.0',
version='2.0.1',
description='Official Bitfinex Python API',
long_description='A Python reference implementation of the Bitfinex API for both REST and websocket interaction',
long_description_content_type='text/markdown',
Expand Down