Skip to content

Commit

Permalink
websocket: add new events and use t/f to define ticker
Browse files Browse the repository at this point in the history
  • Loading branch information
JacobPlaster committed Apr 16, 2020
1 parent 554c8c6 commit 1f300dd
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 13 deletions.
6 changes: 3 additions & 3 deletions bfxapi/examples/ws/subscribe_tickers.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@
def log_error(err):
print ("Error: {}".format(err))

@bfx.ws.on('new_ticker')
def log_candle(ticker):
@bfx.ws.on('new_funding_ticker')
def log_ticker(ticker):
print ("New ticker: {}".format(ticker))

async def start():
await bfx.ws.subscribe('ticker', 'tBTCUSD')
await bfx.ws.subscribe('ticker', 'fUSD')

bfx.ws.on('connected', start)
bfx.ws.run()
6 changes: 3 additions & 3 deletions bfxapi/models/ticker.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class TickerModel:
ASK = 2
ASK_SIZE = 3
DAILY_CHANGE = 4
DAILY_CHANGE_RELATIVE = 5
DAILY_CHANGE_PERCENT = 5
LAST_PRICE = 6
VOLUME = 7
HIGH = 8
Expand All @@ -24,7 +24,7 @@ class Ticker:
ASK float Price of last lowest ask
ASK_SIZE float Sum of the 25 lowest ask sizes
DAILY_CHANGE float Amount that the last price has changed since yesterday
DAILY_CHANGE_RELATIVE float Relative price change since yesterday (*100 for percentage change)
DAILY_CHANGE_PERCENT float Relative price change since yesterday (*100 for percentage change)
LAST_PRICE float Price of the last trade
VOLUME float Daily volume
HIGH float Daily high
Expand Down Expand Up @@ -60,7 +60,7 @@ def from_raw_ticker(raw_ticker, pair):
raw_ticker[TickerModel.ASK],
raw_ticker[TickerModel.ASK_SIZE],
raw_ticker[TickerModel.DAILY_CHANGE],
raw_ticker[TickerModel.DAILY_CHANGE_RELATIVE],
raw_ticker[TickerModel.DAILY_CHANGE_PERCENT],
raw_ticker[TickerModel.LAST_PRICE],
raw_ticker[TickerModel.VOLUME],
raw_ticker[TickerModel.HIGH],
Expand Down
23 changes: 16 additions & 7 deletions bfxapi/websockets/bfx_websocket.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,9 @@ 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_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
- `new_candle` (array): a new candle has been produced
- `margin_info_updates` (array): new margin information has been broadcasted
Expand Down Expand Up @@ -212,14 +215,16 @@ async def _ws_data_handler(self, socketId, data, raw_message_str):
# candles do not have an event
if subscription.channel_name == 'candles':
await self._candle_handler(data)
if subscription.channel_name == 'book':
elif subscription.channel_name == 'book':
await self._order_book_handler(data, raw_message_str)
if subscription.channel_name == 'trades':
elif subscription.channel_name == 'trades':
await self._trade_handler(data)
if subscription.channel_name == 'status':
elif subscription.channel_name == 'status':
await self._status_handler(data)
if subscription.channel_name == 'ticker':
elif subscription.channel_name == 'ticker':
await self._ticker_handler(data)
else:
self.logger.warn("Unknown channel type '{}'".format(subscription.channel_name))
else:
self.logger.warn(
"Unknown data event: '{}' {}".format(dataEvent, data))
Expand Down Expand Up @@ -360,13 +365,17 @@ async def _status_handler(self, data):

async def _ticker_handler(self, data):
symbol = self.subscriptionManager.get(data[0]).symbol
if type(data[1]) is list:
if type(data[1]) is list and len(symbol) > 0:
raw_ticker = data[1]
t = None
if len(raw_ticker) == 10:
if symbol[0] == 't':
t = Ticker.from_raw_ticker(raw_ticker, symbol)
else:
self._emit('new_trading_ticker', t)
elif symbol[0] == 'f':
t = FundingTicker.from_raw_ticker(raw_ticker, symbol)
self._emit('new_funding_ticker', t)
else:
self.logger.warn('Unknown ticker type: {}'.format(raw_ticker))
self._emit('new_ticker', t)

async def _trade_handler(self, data):
Expand Down

0 comments on commit 1f300dd

Please sign in to comment.