Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 2 commits into from
Apr 1, 2024

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

@classmethod
def timestamp_normalize(cls, ts: Union[int, dt]) -> float:
if isinstance(ts, int):
return ts / 1000.0
else:
return ts.timestamp()

@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
2 of 5 checks passed
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
2 participants