Skip to content

Commit 5bafa6e

Browse files
Bybit: Add FundingRate and OrderBook Support (#601)
* latest libs * funding and order book * Update ExchangeSharp.csproj
1 parent c7f2db5 commit 5bafa6e

File tree

2 files changed

+134
-1
lines changed

2 files changed

+134
-1
lines changed

src/ExchangeSharp/API/Exchanges/Bybit/ExchangeBybitAPI.cs

Lines changed: 99 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ private ExchangeBybitAPI()
4242
RequestContentType = "application/json";
4343
WebSocketOrderBookType = WebSocketOrderBookType.FullBookFirstThenDeltas;
4444

45-
RateLimit = new RateGate(100, TimeSpan.FromMinutes(1));
45+
RateLimit = new RateGate(500, TimeSpan.FromMinutes(1));
4646
}
4747

4848
public override Task<string> ExchangeMarketSymbolToGlobalMarketSymbolAsync(string marketSymbol)
@@ -560,6 +560,46 @@ protected override async Task<Dictionary<string, decimal>> OnGetAmountsAvailable
560560
{
561561
return await DoGetAmountsAsync("available_balance");
562562
}
563+
protected override async Task<ExchangeOrderBook> OnGetOrderBookAsync(string marketSymbol, int maxCount = 100)
564+
{
565+
/*
566+
{
567+
"ret_code": 0, // return code
568+
"ret_msg": "OK", // error message
569+
"ext_code": "", // additional error code
570+
"ext_info": "", // additional error info
571+
"result": [
572+
{
573+
"symbol": "BTCUSD", // symbol
574+
"price": "9487", // price
575+
"size": 336241, // size (in USD contracts)
576+
"side": "Buy" // side
577+
},
578+
{
579+
"symbol": "BTCUSD", // symbol
580+
"price": "9487.5", // price
581+
"size": 522147, // size (in USD contracts)
582+
"side": "Sell" // side
583+
}
584+
],
585+
"time_now": "1567108756.834357" // UTC timestamp
586+
}
587+
*/
588+
var tokens = CheckRetCode(await DoMakeJsonRequestAsync<JToken>($"/v2/public/orderBook/L2?symbol={marketSymbol}"));
589+
var orderBook = new ExchangeOrderBook();
590+
foreach (var token in tokens)
591+
{
592+
var orderPrice = new ExchangeOrderPrice();
593+
orderPrice.Price = token["price"].ConvertInvariant<decimal>();
594+
orderPrice.Amount = token["size"].ConvertInvariant<decimal>();
595+
if (token["side"].ToStringInvariant() == "Sell")
596+
orderBook.Asks.Add(orderPrice.Price, orderPrice);
597+
else
598+
orderBook.Bids.Add(orderPrice.Price, orderPrice);
599+
}
600+
601+
return orderBook;
602+
}
563603

564604
public async Task<IEnumerable<ExchangePosition>> GetCurrentPositionsAsync()
565605
{
@@ -620,6 +660,64 @@ public async Task<IEnumerable<ExchangePosition>> GetCurrentPositionsAsync()
620660
return positions;
621661
}
622662

663+
public async Task<ExchangeFunding> GetCurrentFundingRateAsync(string marketSymbol)
664+
{
665+
/*
666+
{
667+
"ret_code": 0,
668+
"ret_msg": "ok",
669+
"ext_code": "",
670+
"result": {
671+
"symbol": "BTCUSD",
672+
"funding_rate": "0.00010000",
673+
"funding_rate_timestamp": 1577433600
674+
},
675+
"ext_info": null,
676+
"time_now": "1577445586.446797",
677+
"rate_limit_status": 119,
678+
"rate_limit_reset_ms": 1577445586454,
679+
"rate_limit": 120
680+
}
681+
*/
682+
JToken token = CheckRetCode(await DoMakeJsonRequestAsync<JToken>($"/v2/public/funding/prev-funding-rate?symbol={marketSymbol}"));
683+
var funding = new ExchangeFunding();
684+
funding.MarketSymbol = token["symbol"].ToStringInvariant();
685+
funding.Rate = token["funding_rate"].ConvertInvariant<decimal>();
686+
// funding.TimeStamp = Convert.ToDateTime(TimeSpan.FromSeconds(token["funding_rate_timestamp"].ConvertInvariant<int>()));
687+
funding.TimeStamp = CryptoUtility.UnixTimeStampToDateTimeSeconds(token["funding_rate_timestamp"].ConvertInvariant<int>());
688+
689+
return funding;
690+
}
691+
692+
public async Task<ExchangeFunding> GetPredictedFundingRateAsync(string marketSymbol)
693+
{
694+
/*
695+
{
696+
"ret_code": 0,
697+
"ret_msg": "ok",
698+
"ext_code": "",
699+
"result": {
700+
"predicted_funding_rate": 0.0001,
701+
"predicted_funding_fee": 0
702+
},
703+
"ext_info": null,
704+
"time_now": "1577447415.583259",
705+
"rate_limit_status": 118,
706+
"rate_limit_reset_ms": 1577447415590,
707+
"rate_limit": 120
708+
}
709+
*/
710+
var extraParams = new Dictionary<string, object>();
711+
extraParams["symbol"] = marketSymbol;
712+
var queryString = await GetAuthenticatedQueryString(extraParams);
713+
JToken token = CheckRetCode(await DoMakeJsonRequestAsync<JToken>($"/v2/private/funding/predicted-funding?" + queryString, BaseUrl, null, "GET"));
714+
var funding = new ExchangeFunding();
715+
funding.MarketSymbol = marketSymbol;
716+
funding.Rate = token["predicted_funding_rate"].ConvertInvariant<decimal>();
717+
718+
return funding;
719+
}
720+
623721
private async Task<IEnumerable<ExchangeOrderResult>> DoGetOrderDetailsAsync(string orderId, string marketSymbol = null)
624722
{
625723
var extraParams = new Dictionary<string, object>();
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
MIT LICENSE
3+
4+
Copyright 2021 Digital Ruby, LLC - http://www.digitalruby.com
5+
6+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
7+
8+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
9+
10+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
11+
*/
12+
13+
using System;
14+
15+
namespace ExchangeSharp
16+
{
17+
/// <summary>Exchange funding information</summary>
18+
public sealed class ExchangeFunding
19+
{
20+
/// <summary>
21+
/// The market symbol.
22+
/// </summary>
23+
public string MarketSymbol { get; set; }
24+
25+
/// <summary>
26+
/// Funding Rate
27+
/// </summary>
28+
public decimal Rate { get; set; }
29+
30+
/// <summary>
31+
/// TimeStamp
32+
/// </summary>
33+
public DateTime TimeStamp { get; set; }
34+
}
35+
}

0 commit comments

Comments
 (0)