Skip to content

Comments

feat(bybit): Add spot support and migrate to API V5#1017

Merged
bmoscon merged 2 commits intobmoscon:masterfrom
kosmodromov:feat/bybit-V5-spot-futures
Apr 1, 2024
Merged

feat(bybit): Add spot support and migrate to API V5#1017
bmoscon merged 2 commits intobmoscon:masterfrom
kosmodromov:feat/bybit-V5-spot-futures

Conversation

@kosmodromov
Copy link
Contributor

@kosmodromov kosmodromov commented Mar 7, 2024

This PR adds support for the spot market on Bybit exchange and migrate to V5 API.

Updated market data endpoints:

  • Trades: spot, perps, futures
  • L2_book: spot, perps, futures
  • Candles: spot, perps, futures
  • Tickers: perps, futures
  • Funding: perps
  • Index: perps, futures
  • Open Interest: perps, futures
  • Liquidations: perps, futures

Only linear futures is supported. No inverse futures support.
Private endpoints remain unmodified.

from cryptofeed import FeedHandler
from cryptofeed.exchanges import Bybit
from cryptofeed.defines import TRADES, L2_BOOK, BID, ASK, CANDLES, LIQUIDATIONS, TICKER, FUNDING, OPEN_INTEREST, INDEX, FILLS, ORDER_INFO


async def book(d, receipt_timestamp):
    print(f'{receipt_timestamp} for {d.exchange} - {d.symbol}, with {len(d.book)} entries. bid/ask: {d.book.bids.index(0)[0]}/{d.book.asks.index(0)[0]}, delta: {len(d.delta[BID]) + len(d.delta[ASK])} entries')


async def anything(d, receipt_timestamp):
    print(d)


def main():
    f = FeedHandler()
    f.add_feed(Bybit(symbols=["ETH-USDT", "ETH-USDT-PERP"], channels=[TRADES, L2_BOOK], callbacks={TRADES: anything, L2_BOOK: book}))
    f.add_feed(Bybit(symbols=["BTC-USDT-PERP", "XRP-USDC-PERP"], channels=[TRADES, L2_BOOK, FUNDING, OPEN_INTEREST, INDEX],callbacks={TRADES: anything, L2_BOOK: book, FUNDING: anything, OPEN_INTEREST: anything, INDEX: anything}))
    f.run()


if __name__ == '__main__':
    main()
  • - Tested
  • - Changelog updated
  • - Tests run and pass
  • - Flake8 run and all errors/warnings resolved
  • - Contributors file updated (optional)

Fixes: #1010 #1012


@staticmethod
def convert_to_spot_name(cls, pair):
# Bybit spot and USDT perps use the same symbol name. To distinguish them, use a slash to separate the base and quote.
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm confused by this comment - does this mean if I want to subscribe I'd need to do something like BTC|USDT?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here it adds a slash to spot pairs in order to build proper normalized dict (as linear and spot symbols are identical)

https://github.com/kosmodromov/cryptofeed/blob/8d271a5e63f5bed3bf084e0369eb0b140849b304/cryptofeed/exchanges/bybit.py#L104-L113

Then in message_handler it gets the market (spot | perp) from url line and then passes market parameter to a functions _candle, _trade and _book. These functions receive spot and perp pair information from the websocket, where both are formatted as 'BTCUSDT'

https://github.com/kosmodromov/cryptofeed/blob/8d271a5e63f5bed3bf084e0369eb0b140849b304/cryptofeed/exchanges/bybit.py#L212-L219

Then convert_to_spot_name function adds slash in order to properly use normalized dict later in the code for spot pairs.

This approach may seem complex, but I haven't found a clearer way to implement it.

Short answer on your question - no, no needs to add anything special to subscribe.

Copy link
Owner

@bmoscon bmoscon left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I left a comment about part of the code I'm unsure about. Other than that, looks ready to merge once thats resolved

@bmoscon
Copy link
Owner

bmoscon commented Mar 31, 2024

@kosmodromov - thanks for the PR - just one question for you, then we can merge this.

@bmoscon bmoscon merged commit 8c3f96a into bmoscon:master Apr 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bybit Trades do not work for Linear Futures

3 participants