crypto_rest_client/exchanges/
bybit.rs

1use super::utils::http_get;
2use crate::error::Result;
3use std::collections::BTreeMap;
4
5const BASE_URL: &str = "https://api.bybit.com/v2";
6
7/// The RESTful client for Bybit.
8///
9/// Bybit has InverseSwap and LinearSwap markets.
10///
11/// * RESTful API doc: <https://bybit-exchange.github.io/docs/inverse/#t-marketdata>
12/// * Trading at:
13///     * InverseSwap <https://www.bybit.com/trade/inverse/>
14///     * LinearSwap <https://www.bybit.com/trade/usdt/>
15/// * Rate Limit: <https://bybit-exchange.github.io/docs/inverse/#t-ratelimits>
16///   * GET method:
17///     * 50 requests per second continuously for 2 minutes
18///     * 70 requests per second continuously for 5 seconds
19///   * POST method:
20///     * 20 requests per second continuously for 2 minutes
21///     * 50 requests per second continuously for 5 seconds
22pub struct BybitRestClient {
23    _api_key: Option<String>,
24    _api_secret: Option<String>,
25}
26
27impl BybitRestClient {
28    pub fn new(api_key: Option<String>, api_secret: Option<String>) -> Self {
29        BybitRestClient { _api_key: api_key, _api_secret: api_secret }
30    }
31
32    /// Get the latest Level2 snapshot of orderbook.
33    ///
34    /// Top 50 bids and asks are returned.
35    ///
36    /// For example: <https://api.bybit.com/v2/public/orderBook/L2?symbol=BTCUSD>,
37    pub fn fetch_l2_snapshot(symbol: &str) -> Result<String> {
38        gen_api!(format!("/public/orderBook/L2?symbol={symbol}"))
39    }
40
41    /// Get open interest.
42    ///
43    /// For example:
44    ///
45    /// - <https://api.bybit.com/v2/public/open-interest?symbol=BTCUSD&period=5min&limit=200>
46    /// - <https://api.bybit.com/v2/public/open-interest?symbol=BTCUSDT&period=5min&limit=200>
47    /// - <https://api.bybit.com/v2/public/open-interest?symbol=BTCUSDU22&period=5min&limit=200>
48    pub fn fetch_open_interest(symbol: &str) -> Result<String> {
49        gen_api!(format!("/public/open-interest?symbol={symbol}&period=5min&limit=200"))
50    }
51
52    /// Get long-short ratio.
53    ///
54    /// For example:
55    ///
56    /// - <https://api.bybit.com/v2/public/account-ratio?symbol=BTCUSD&period=5min&limit=500>
57    /// - <https://api.bybit.com/v2/public/account-ratio?symbol=BTCUSDT&period=5min&limit=500>
58    /// - <https://api.bybit.com/v2/public/account-ratio?symbol=BTCUSDU22&period=5min&limit=500>
59    pub fn fetch_long_short_ratio(symbol: &str) -> Result<String> {
60        gen_api!(format!("/public/account-ratio?symbol={symbol}&period=5min&limit=200"))
61    }
62}