From 296f93597eb41667a719a4ba497eb33a3c9177a4 Mon Sep 17 00:00:00 2001 From: Vlad Dumitru Date: Sun, 29 Sep 2024 19:45:56 +0300 Subject: [PATCH 1/2] Implement GetTickersAsync for Bybit --- .../Exchanges/Bybit/ExchangeBybitV5Base.cs | 55 +++++++++++++++++++ 1 file changed, 55 insertions(+) diff --git a/src/ExchangeSharp/API/Exchanges/Bybit/ExchangeBybitV5Base.cs b/src/ExchangeSharp/API/Exchanges/Bybit/ExchangeBybitV5Base.cs index 7e36c8b1..d02dd3ff 100644 --- a/src/ExchangeSharp/API/Exchanges/Bybit/ExchangeBybitV5Base.cs +++ b/src/ExchangeSharp/API/Exchanges/Bybit/ExchangeBybitV5Base.cs @@ -37,6 +37,8 @@ public ExchangeBybitV5Base() WebSocketOrderBookType = WebSocketOrderBookType.FullBookFirstThenDeltas; RateLimit = new RateGate(10, TimeSpan.FromSeconds(1)); RequestWindow = TimeSpan.FromSeconds(15); + MarketSymbolSeparator = string.Empty; + MarketSymbolIsUppercase = true; } protected override async Task OnGetNonceOffset() @@ -270,6 +272,59 @@ protected override async Task OnGetOrderBookAsync( return book; } + protected override async Task>> OnGetTickersAsync() + { + var tickers = new List>(); + //{ + // "retCode": 0, + // "retMsg": "OK", + // "result": { + // "category": "spot", + // "list": [ + // { + // "symbol": "MOJOUSDT", + // "bid1Price": "0.01755", + // "bid1Size": "128.66", + // "ask1Price": "0.01763", + // "ask1Size": "311.27", + // "lastPrice": "0.01759", + // "prevPrice24h": "0.01848", + // "price24hPcnt": "-0.0482", + // "highPrice24h": "0.01851", + // "lowPrice24h": "0.01726", + // "turnover24h": "67118.0455931", + // "volume24h": "3769556.35" + // }, + // ... + // ] + // } + //} + + var url = $"/v5/market/tickers?category={MarketCategory.ToStringLowerInvariant()}"; + var token = await MakeJsonRequestAsync(url); + var tickerList = token["list"]; + foreach (var ticker in tickerList) + { + var marketSymbol = ticker["symbol"].ToStringInvariant(); + var exchangeTicker = await this.ParseTickerAsync( + ticker, + marketSymbol, + "ask1Price", + "bid1Price", + "lastPrice", + "volume24h", + "turnover24h" + ); + + tickers.Add(new KeyValuePair( + marketSymbol, + exchangeTicker + )); + } + + return tickers; + } + #endregion Public #region Private From 3dd9528a2b78e3fc0a2726dfa1182214afb20777 Mon Sep 17 00:00:00 2001 From: Vlad Dumitru Date: Mon, 30 Sep 2024 09:59:07 +0300 Subject: [PATCH 2/2] Query instruments-info first --- .../API/Exchanges/Bybit/ExchangeBybitV5Base.cs | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/src/ExchangeSharp/API/Exchanges/Bybit/ExchangeBybitV5Base.cs b/src/ExchangeSharp/API/Exchanges/Bybit/ExchangeBybitV5Base.cs index d02dd3ff..6b1103da 100644 --- a/src/ExchangeSharp/API/Exchanges/Bybit/ExchangeBybitV5Base.cs +++ b/src/ExchangeSharp/API/Exchanges/Bybit/ExchangeBybitV5Base.cs @@ -274,7 +274,6 @@ protected override async Task OnGetOrderBookAsync( protected override async Task>> OnGetTickersAsync() { - var tickers = new List>(); //{ // "retCode": 0, // "retMsg": "OK", @@ -300,20 +299,31 @@ protected override async Task>> // } //} + var tickers = new List>(); + + var marketSymbolsMetadata = await GetMarketSymbolsMetadataAsync(); + var url = $"/v5/market/tickers?category={MarketCategory.ToStringLowerInvariant()}"; var token = await MakeJsonRequestAsync(url); var tickerList = token["list"]; foreach (var ticker in tickerList) { var marketSymbol = ticker["symbol"].ToStringInvariant(); + if (!marketSymbolsMetadata.Any(x => x.MarketSymbol == marketSymbol)) + { + // "Please always use the Trading symbols found in the instrument-info api, then query tickers by those symbols." - Bybit API support + continue; + } + var exchangeTicker = await this.ParseTickerAsync( ticker, marketSymbol, "ask1Price", "bid1Price", "lastPrice", - "volume24h", - "turnover24h" + // https://bybit-exchange.github.io/docs/faq#what-is-the-difference-between-turnover-and-volume + "volume24h", // Volume: is in the same currency as the quantity's currency + "turnover24h" // Turnover: is in the opposite currency to the quantity's currency ); tickers.Add(new KeyValuePair(