Skip to content

Commit 1f300dd

Browse files
committed
websocket: add new events and use t/f to define ticker
1 parent 554c8c6 commit 1f300dd

File tree

3 files changed

+22
-13
lines changed

3 files changed

+22
-13
lines changed

bfxapi/examples/ws/subscribe_tickers.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,12 @@
1212
def log_error(err):
1313
print ("Error: {}".format(err))
1414

15-
@bfx.ws.on('new_ticker')
16-
def log_candle(ticker):
15+
@bfx.ws.on('new_funding_ticker')
16+
def log_ticker(ticker):
1717
print ("New ticker: {}".format(ticker))
1818

1919
async def start():
20-
await bfx.ws.subscribe('ticker', 'tBTCUSD')
20+
await bfx.ws.subscribe('ticker', 'fUSD')
2121

2222
bfx.ws.on('connected', start)
2323
bfx.ws.run()

bfxapi/models/ticker.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ class TickerModel:
1111
ASK = 2
1212
ASK_SIZE = 3
1313
DAILY_CHANGE = 4
14-
DAILY_CHANGE_RELATIVE = 5
14+
DAILY_CHANGE_PERCENT = 5
1515
LAST_PRICE = 6
1616
VOLUME = 7
1717
HIGH = 8
@@ -24,7 +24,7 @@ class Ticker:
2424
ASK float Price of last lowest ask
2525
ASK_SIZE float Sum of the 25 lowest ask sizes
2626
DAILY_CHANGE float Amount that the last price has changed since yesterday
27-
DAILY_CHANGE_RELATIVE float Relative price change since yesterday (*100 for percentage change)
27+
DAILY_CHANGE_PERCENT float Relative price change since yesterday (*100 for percentage change)
2828
LAST_PRICE float Price of the last trade
2929
VOLUME float Daily volume
3030
HIGH float Daily high
@@ -60,7 +60,7 @@ def from_raw_ticker(raw_ticker, pair):
6060
raw_ticker[TickerModel.ASK],
6161
raw_ticker[TickerModel.ASK_SIZE],
6262
raw_ticker[TickerModel.DAILY_CHANGE],
63-
raw_ticker[TickerModel.DAILY_CHANGE_RELATIVE],
63+
raw_ticker[TickerModel.DAILY_CHANGE_PERCENT],
6464
raw_ticker[TickerModel.LAST_PRICE],
6565
raw_ticker[TickerModel.VOLUME],
6666
raw_ticker[TickerModel.HIGH],

bfxapi/websockets/bfx_websocket.py

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,9 @@ class BfxWebsocket(GenericWebsocket):
135135
- `funding_credit_snapshot` (array): opening funding credit balances
136136
- `balance_update` (array): when the state of a balance is changed
137137
- `new_trade` (array): a new trade on the market has been executed
138+
- `new_ticker` (Ticker|FundingTicker): a new ticker update has been published
139+
- `new_funding_ticker` (FundingTicker): a new funding ticker update has been published
140+
- `new_trading_ticker` (Ticker): a new trading ticker update has been published
138141
- `trade_update` (array): a trade on the market has been updated
139142
- `new_candle` (array): a new candle has been produced
140143
- `margin_info_updates` (array): new margin information has been broadcasted
@@ -212,14 +215,16 @@ async def _ws_data_handler(self, socketId, data, raw_message_str):
212215
# candles do not have an event
213216
if subscription.channel_name == 'candles':
214217
await self._candle_handler(data)
215-
if subscription.channel_name == 'book':
218+
elif subscription.channel_name == 'book':
216219
await self._order_book_handler(data, raw_message_str)
217-
if subscription.channel_name == 'trades':
220+
elif subscription.channel_name == 'trades':
218221
await self._trade_handler(data)
219-
if subscription.channel_name == 'status':
222+
elif subscription.channel_name == 'status':
220223
await self._status_handler(data)
221-
if subscription.channel_name == 'ticker':
224+
elif subscription.channel_name == 'ticker':
222225
await self._ticker_handler(data)
226+
else:
227+
self.logger.warn("Unknown channel type '{}'".format(subscription.channel_name))
223228
else:
224229
self.logger.warn(
225230
"Unknown data event: '{}' {}".format(dataEvent, data))
@@ -360,13 +365,17 @@ async def _status_handler(self, data):
360365

361366
async def _ticker_handler(self, data):
362367
symbol = self.subscriptionManager.get(data[0]).symbol
363-
if type(data[1]) is list:
368+
if type(data[1]) is list and len(symbol) > 0:
364369
raw_ticker = data[1]
365370
t = None
366-
if len(raw_ticker) == 10:
371+
if symbol[0] == 't':
367372
t = Ticker.from_raw_ticker(raw_ticker, symbol)
368-
else:
373+
self._emit('new_trading_ticker', t)
374+
elif symbol[0] == 'f':
369375
t = FundingTicker.from_raw_ticker(raw_ticker, symbol)
376+
self._emit('new_funding_ticker', t)
377+
else:
378+
self.logger.warn('Unknown ticker type: {}'.format(raw_ticker))
370379
self._emit('new_ticker', t)
371380

372381
async def _trade_handler(self, data):

0 commit comments

Comments
 (0)