mirror of
https://github.com/JKorf/CryptoExchange.Net
synced 2025-06-08 08:26:20 +00:00
Docs and examples update
This commit is contained in:
parent
da1686d95c
commit
76e95cf870
@ -7,7 +7,7 @@
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.1.0-preview-20211130-02"></PackageReference>
|
||||
<PackageReference Include="Moq" Version="4.16.1" />
|
||||
<PackageReference Include="Moq" Version="4.20.70" />
|
||||
<PackageReference Include="NUnit" Version="3.13.2"></PackageReference>
|
||||
<PackageReference Include="NUnit3TestAdapter" Version="4.2.0"></PackageReference>
|
||||
</ItemGroup>
|
||||
|
@ -5,16 +5,15 @@
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Binance.Net" Version="9.1.5" />
|
||||
<PackageReference Include="Bitfinex.Net" Version="7.0.4" />
|
||||
<PackageReference Include="Bittrex.Net" Version="8.0.3" />
|
||||
<PackageReference Include="Bybit.Net" Version="3.2.1" />
|
||||
<PackageReference Include="CoinEx.Net" Version="6.0.3" />
|
||||
<PackageReference Include="Huobi.Net" Version="5.0.3" />
|
||||
<PackageReference Include="JK.Bitget.Net" Version="1.0.0" />
|
||||
<PackageReference Include="JK.OKX.Net" Version="1.4.2" />
|
||||
<PackageReference Include="KrakenExchange.Net" Version="4.1.5" />
|
||||
<PackageReference Include="Kucoin.Net" Version="5.0.5" />
|
||||
<PackageReference Include="Binance.Net" Version="9.5.0-beta1" />
|
||||
<PackageReference Include="Bitfinex.Net" Version="7.1.0-beta1" />
|
||||
<PackageReference Include="Bybit.Net" Version="3.4.0-beta1" />
|
||||
<PackageReference Include="CoinEx.Net" Version="6.1.0-beta1" />
|
||||
<PackageReference Include="Huobi.Net" Version="5.1.0-beta1" />
|
||||
<PackageReference Include="JK.Bitget.Net" Version="1.1.0-beta2" />
|
||||
<PackageReference Include="JK.OKX.Net" Version="1.6.0-beta2" />
|
||||
<PackageReference Include="KrakenExchange.Net" Version="4.3.0-beta1" />
|
||||
<PackageReference Include="Kucoin.Net" Version="5.2.0-beta1" />
|
||||
<PackageReference Include="Serilog.AspNetCore" Version="6.0.0" />
|
||||
</ItemGroup>
|
||||
|
||||
|
@ -2,7 +2,6 @@
|
||||
@inject IBinanceRestClient binanceClient
|
||||
@inject IBitfinexRestClient bitfinexClient
|
||||
@inject IBitgetRestClient bitgetClient
|
||||
@inject IBittrexRestClient bittrexClient
|
||||
@inject IBybitRestClient bybitClient
|
||||
@inject ICoinExRestClient coinexClient
|
||||
@inject IHuobiRestClient huobiClient
|
||||
@ -24,7 +23,6 @@
|
||||
var binanceTask = binanceClient.SpotApi.ExchangeData.GetTickerAsync("BTCUSDT");
|
||||
var bitfinexTask = bitfinexClient.SpotApi.ExchangeData.GetTickerAsync("tBTCUSD");
|
||||
var bitgetTask = bitgetClient.SpotApi.ExchangeData.GetTickerAsync("BTCUSDT_SPBL");
|
||||
var bittrexTask = bittrexClient.SpotApi.ExchangeData.GetTickerAsync("BTC-USDT");
|
||||
var bybitTask = bybitClient.V5Api.ExchangeData.GetSpotTickersAsync("BTCUSDT");
|
||||
var coinexTask = coinexClient.SpotApi.ExchangeData.GetTickerAsync("BTCUSDT");
|
||||
var huobiTask = huobiClient.SpotApi.ExchangeData.GetTickerAsync("btcusdt");
|
||||
@ -32,7 +30,7 @@
|
||||
var kucoinTask = kucoinClient.SpotApi.ExchangeData.GetTickerAsync("BTC-USDT");
|
||||
var okxTask = okxClient.UnifiedApi.ExchangeData.GetTickerAsync("BTCUSDT");
|
||||
|
||||
await Task.WhenAll(binanceTask, bitfinexTask, bittrexTask, bybitTask, coinexTask, huobiTask, krakenTask, kucoinTask);
|
||||
await Task.WhenAll(binanceTask, bitfinexTask, bybitTask, coinexTask, huobiTask, krakenTask, kucoinTask);
|
||||
|
||||
if (binanceTask.Result.Success)
|
||||
_prices.Add("Binance", binanceTask.Result.Data.LastPrice);
|
||||
@ -43,9 +41,6 @@
|
||||
if (bitgetTask.Result.Success)
|
||||
_prices.Add("Bitget", bitgetTask.Result.Data.ClosePrice);
|
||||
|
||||
if (bittrexTask.Result.Success)
|
||||
_prices.Add("Bittrex", bittrexTask.Result.Data.LastPrice);
|
||||
|
||||
if (bybitTask.Result.Success)
|
||||
_prices.Add("Bybit", bybitTask.Result.Data.List.First().LastPrice);
|
||||
|
||||
|
@ -2,7 +2,6 @@
|
||||
@inject IBinanceSocketClient binanceSocketClient
|
||||
@inject IBitfinexSocketClient bitfinexSocketClient
|
||||
@inject IBitgetSocketClient bitgetSocketClient
|
||||
@inject IBittrexSocketClient bittrexSocketClient
|
||||
@inject IBybitSocketClient bybitSocketClient
|
||||
@inject ICoinExSocketClient coinExSocketClient
|
||||
@inject IHuobiSocketClient huobiSocketClient
|
||||
@ -11,6 +10,7 @@
|
||||
@inject IOKXSocketClient okxSocketClient
|
||||
@using System.Collections.Concurrent
|
||||
@using CryptoExchange.Net.Objects
|
||||
@using CryptoExchange.Net.Objects.Sockets;
|
||||
@using CryptoExchange.Net.Sockets
|
||||
@implements IDisposable
|
||||
|
||||
@ -31,13 +31,12 @@
|
||||
binanceSocketClient.SpotApi.ExchangeData.SubscribeToTickerUpdatesAsync("ETHBTC", data => UpdateData("Binance", data.Data.LastPrice)),
|
||||
bitfinexSocketClient.SpotApi.SubscribeToTickerUpdatesAsync("tETHBTC", data => UpdateData("Bitfinex", data.Data.LastPrice)),
|
||||
bitgetSocketClient.SpotApi.SubscribeToTickerUpdatesAsync("ETHBTC", data => UpdateData("Bitget", data.Data.LastPrice)),
|
||||
bittrexSocketClient.SpotApi.SubscribeToTickerUpdatesAsync("ETH-BTC", data => UpdateData("Bittrex", data.Data.LastPrice)),
|
||||
bybitSocketClient.V5SpotApi.SubscribeToTickerUpdatesAsync("ETHBTC", data => UpdateData("Bybit", data.Data.LastPrice)),
|
||||
coinExSocketClient.SpotApi.SubscribeToTickerUpdatesAsync("ETHBTC", data => UpdateData("CoinEx", data.Data.LastPrice)),
|
||||
huobiSocketClient.SpotApi.SubscribeToTickerUpdatesAsync("ethbtc", data => UpdateData("Huobi", data.Data.ClosePrice ?? 0)),
|
||||
krakenSocketClient.SpotApi.SubscribeToTickerUpdatesAsync("ETH/XBT", data => UpdateData("Kraken", data.Data.LastTrade.Price)),
|
||||
kucoinSocketClient.SpotApi.SubscribeToTickerUpdatesAsync("ETH-BTC", data => UpdateData("Kucoin", data.Data.LastPrice ?? 0)),
|
||||
okxSocketClient.UnifiedApi.ExchangeData.SubscribeToTickerUpdatesAsync("ETH-BTC", data => UpdateData("OKX", data.LastPrice ?? 0)),
|
||||
okxSocketClient.UnifiedApi.ExchangeData.SubscribeToTickerUpdatesAsync("ETH-BTC", data => UpdateData("OKX", data.Data.LastPrice ?? 0)),
|
||||
};
|
||||
|
||||
await Task.WhenAll(tasks);
|
||||
|
@ -4,7 +4,6 @@
|
||||
@using Binance.Net.Interfaces
|
||||
@using Bitfinex.Net.Interfaces
|
||||
@using Bitget.Net.Interfaces;
|
||||
@using Bittrex.Net.Interfaces
|
||||
@using Bybit.Net.Interfaces
|
||||
@using CoinEx.Net.Interfaces
|
||||
@using CryptoExchange.Net.Interfaces
|
||||
@ -16,7 +15,6 @@
|
||||
@inject IBinanceOrderBookFactory binanceFactory
|
||||
@inject IBitfinexOrderBookFactory bitfinexFactory
|
||||
@inject IBitgetOrderBookFactory bitgetFactory
|
||||
@inject IBittrexOrderBookFactory bittrexFactory
|
||||
@inject IBybitOrderBookFactory bybitFactory
|
||||
@inject ICoinExOrderBookFactory coinExFactory
|
||||
@inject IHuobiOrderBookFactory huobiFactory
|
||||
@ -59,7 +57,6 @@
|
||||
{ "Binance", binanceFactory.CreateSpot("ETHBTC") },
|
||||
{ "Bitfinex", bitfinexFactory.Create("tETHBTC") },
|
||||
{ "Bitget", bitgetFactory.CreateSpot("ETHBTC") },
|
||||
{ "Bittrex", bittrexFactory.Create("ETH-BTC") },
|
||||
{ "Bybit", bybitFactory.Create("ETHBTC", Bybit.Net.Enums.Category.Spot) },
|
||||
{ "CoinEx", coinExFactory.CreateSpot("ETHBTC") },
|
||||
{ "Huobi", huobiFactory.CreateSpot("ethbtc") },
|
||||
|
@ -1,26 +1,5 @@
|
||||
@page "/SpotClient"
|
||||
@inject IBinanceRestClient binanceClient
|
||||
@inject IBitfinexRestClient bitfinexClient
|
||||
@inject IBitgetRestClient bitgetClient
|
||||
@inject IBittrexRestClient bittrexClient
|
||||
@inject IBybitRestClient bybitClient
|
||||
@inject ICoinExRestClient coinexClient
|
||||
@inject IHuobiRestClient huobiClient
|
||||
@inject IKrakenRestClient krakenClient
|
||||
@inject IKucoinRestClient kucoinClient
|
||||
@inject IOKXRestClient okxClient
|
||||
@using Binance.Net.Clients.SpotApi
|
||||
@using Bitfinex.Net.Clients.SpotApi
|
||||
@using Bittrex.Net.Clients.SpotApi
|
||||
@using Bitget.Net.Clients.SpotApi
|
||||
@using Bybit.Net.Clients.SpotApi
|
||||
@using CoinEx.Net.Clients.SpotApi
|
||||
@using CryptoExchange.Net.Interfaces
|
||||
@using CryptoExchange.Net.Interfaces.CommonClients
|
||||
@using Huobi.Net.Clients.SpotApi
|
||||
@using Kraken.Net.Clients.SpotApi
|
||||
@using Kucoin.Net.Clients.SpotApi
|
||||
@using OKX.Net.Clients.UnifiedApi
|
||||
@inject ICryptoRestClient restClient
|
||||
|
||||
<h3>ETH-BTC prices:</h3>
|
||||
@foreach(var price in _prices.OrderBy(p => p.Key))
|
||||
@ -33,21 +12,7 @@
|
||||
|
||||
protected override async Task OnInitializedAsync()
|
||||
{
|
||||
var clients = new ISpotClient[]
|
||||
{
|
||||
|
||||
binanceClient.SpotApi.CommonSpotClient,
|
||||
bitfinexClient.SpotApi.CommonSpotClient,
|
||||
bitgetClient.SpotApi.CommonSpotClient,
|
||||
bittrexClient.SpotApi.CommonSpotClient,
|
||||
bybitClient.SpotApiV1.CommonSpotClient,
|
||||
coinexClient.SpotApi.CommonSpotClient,
|
||||
huobiClient.SpotApi.CommonSpotClient,
|
||||
krakenClient.SpotApi.CommonSpotClient,
|
||||
kucoinClient.SpotApi.CommonSpotClient,
|
||||
okxClient.UnifiedApi.CommonSpotClient
|
||||
};
|
||||
|
||||
var clients = restClient.GetSpotClients();
|
||||
var tasks = clients.Select(c => (c.ExchangeName, c.GetTickerAsync(c.GetSymbolName("ETH", "BTC"))));
|
||||
await Task.WhenAll(tasks.Select(t => t.Item2));
|
||||
foreach(var task in tasks)
|
||||
|
@ -1,23 +1,10 @@
|
||||
using System.Collections.Generic;
|
||||
using Binance.Net;
|
||||
using Binance.Net.Clients;
|
||||
using Binance.Net.Interfaces.Clients;
|
||||
using Bitfinex.Net;
|
||||
using Bitget.Net;
|
||||
using Bittrex.Net;
|
||||
using Bybit.Net;
|
||||
using CoinEx.Net;
|
||||
using CryptoExchange.Net.Authentication;
|
||||
using Huobi.Net;
|
||||
using Kraken.Net;
|
||||
using Kucoin.Net;
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using OKX.Net;
|
||||
|
||||
namespace BlazorClient
|
||||
{
|
||||
@ -51,7 +38,6 @@ namespace BlazorClient
|
||||
|
||||
services.AddBitfinex();
|
||||
services.AddBitget();
|
||||
services.AddBittrex();
|
||||
services.AddBybit();
|
||||
services.AddCoinEx();
|
||||
services.AddHuobi();
|
||||
|
@ -11,10 +11,10 @@
|
||||
@using Binance.Net.Interfaces.Clients;
|
||||
@using Bitfinex.Net.Interfaces.Clients;
|
||||
@using Bitget.Net.Interfaces.Clients;
|
||||
@using Bittrex.Net.Interfaces.Clients;
|
||||
@using Bybit.Net.Interfaces.Clients;
|
||||
@using CoinEx.Net.Interfaces.Clients;
|
||||
@using Huobi.Net.Interfaces.Clients;
|
||||
@using Kraken.Net.Interfaces.Clients;
|
||||
@using Kucoin.Net.Interfaces.Clients;
|
||||
@using OKX.Net.Interfaces.Clients;
|
||||
@using CryptoExchange.Net.Interfaces;
|
@ -6,16 +6,16 @@
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Binance.Net" Version="9.1.5" />
|
||||
<PackageReference Include="Bitfinex.Net" Version="7.0.4" />
|
||||
<PackageReference Include="Binance.Net" Version="9.5.0-beta1" />
|
||||
<PackageReference Include="Bitfinex.Net" Version="7.1.0-beta1" />
|
||||
<PackageReference Include="Bittrex.Net" Version="8.0.3" />
|
||||
<PackageReference Include="Bybit.Net" Version="3.2.1" />
|
||||
<PackageReference Include="CoinEx.Net" Version="6.0.3" />
|
||||
<PackageReference Include="Huobi.Net" Version="5.0.3" />
|
||||
<PackageReference Include="JK.Bitget.Net" Version="1.0.0" />
|
||||
<PackageReference Include="JK.OKX.Net" Version="1.4.2" />
|
||||
<PackageReference Include="KrakenExchange.Net" Version="4.1.5" />
|
||||
<PackageReference Include="Kucoin.Net" Version="5.0.5" />
|
||||
<PackageReference Include="Bybit.Net" Version="3.4.0-beta1" />
|
||||
<PackageReference Include="CoinEx.Net" Version="6.1.0-beta1" />
|
||||
<PackageReference Include="Huobi.Net" Version="5.1.0-beta1" />
|
||||
<PackageReference Include="JK.Bitget.Net" Version="1.1.0-beta2" />
|
||||
<PackageReference Include="JK.OKX.Net" Version="1.6.0-beta2" />
|
||||
<PackageReference Include="KrakenExchange.Net" Version="4.3.0-beta1" />
|
||||
<PackageReference Include="Kucoin.Net" Version="5.2.0-beta1" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
@ -8,6 +8,7 @@ using Binance.Net.Objects;
|
||||
using Bybit.Net.Clients;
|
||||
using ConsoleClient.Exchanges;
|
||||
using CryptoExchange.Net.Authentication;
|
||||
using CryptoExchange.Net.Objects.Sockets;
|
||||
using CryptoExchange.Net.Sockets;
|
||||
|
||||
namespace ConsoleClient
|
||||
|
@ -1,171 +0,0 @@
|
||||
---
|
||||
title: General usage
|
||||
nav_order: 2
|
||||
---
|
||||
|
||||
## How to use the library
|
||||
|
||||
Each implementation generally provides two different clients, which will be the access point for the API's. First of is the rest client, which is typically available via [ExchangeName]RestClient, and a socket client, which is generally named [ExchangeName]SocketClient. For example `BinanceRestClient` and `BinanceSocketClient`.
|
||||
|
||||
## Rest client
|
||||
The rest client gives access to the Rest endpoint of the API. Rest endpoints are accessed by sending an HTTP request and receiving a response. The client is split in different sub-clients, which are named API Clients. These API clients are then again split in different topics. Typically a Rest client will look like this:
|
||||
|
||||
- [ExchangeName]RestClient
|
||||
- SpotApi
|
||||
- Account
|
||||
- ExchangeData
|
||||
- Trading
|
||||
- FuturesApi
|
||||
- Account
|
||||
- ExchangeData
|
||||
- Trading
|
||||
|
||||
This rest client has 2 different API clients, the `SpotApi` and the `FuturesApi`, each offering their own set of endpoints.
|
||||
|
||||
*Requesting ticker info on the spot API*
|
||||
```csharp
|
||||
var client = new KucoinClient();
|
||||
var tickersResult = kucoinClient.SpotApi.ExchangeData.GetTickersAsync();
|
||||
```
|
||||
|
||||
Structuring the client like this should make it easier to find endpoints and allows for separate options and functionality for different API clients. For example, some API's have totally separate API's for futures, with different base addresses and different API credentials, while other API's have implemented this in the same API. Either way, this structure can facilitate a similar interface.
|
||||
|
||||
### Rest API client
|
||||
The Api clients are parts of the total API with a common identifier. In the previous example, it separates the Spot and the Futures API. This again is then separated into topics. Most Rest clients implement the following structure:
|
||||
|
||||
**Account**
|
||||
Endpoints related to the user account. This can for example be endpoints for accessing account settings, or getting account balances. The endpoints in this topic will require API credentials to be provided in the client options.
|
||||
|
||||
**ExchangeData**
|
||||
Endpoints related to exchange data. Exchange data can be tied to the exchange, for example retrieving the symbols supported by the exchange and what the trading rules are, or can be more general market endpoints, such as getting the most recent trades for a symbol.
|
||||
These endpoints generally don't require API credentials as they are publicly available.
|
||||
|
||||
**Trading**
|
||||
Endpoints related to trading. These are endpoints for placing and retrieving orders and retrieving trades made by the user. The endpoints in this topic will require API credentials to be provided in the client options.
|
||||
|
||||
### Processing request responses
|
||||
Each request will return a WebCallResult<T> with the following properties:
|
||||
`RequestHeaders`: The headers send to the server in the request message
|
||||
`RequestMethod`: The Http method of the request
|
||||
`RequestUrl`: The url the request was send to
|
||||
`ResponseLength`: The length in bytes of the response message
|
||||
`ResponseTime`: The duration between sending the request and receiving the response
|
||||
`ResponseHeaders`: The headers returned from the server
|
||||
`ResponseStatusCode`: The status code as returned by the server
|
||||
`Success`: Whether or not the call was successful. If successful the `Data` property will contain the resulting data, if not successful the `Error` property will contain more details about what the issue was
|
||||
`Error`: Details on what went wrong with a call. Only filled when `Success` == `false`
|
||||
`OriginalData`: Will contain the originally received unparsed data if this has been enabled in the client options
|
||||
`Data`: Data returned by the server, only available if `Success` == `true`
|
||||
|
||||
When processing the result of a call it should always be checked for success. Not doing so will result in `NullReference` exceptions when the call fails for whatever reason.
|
||||
|
||||
*Check call result*
|
||||
```csharp
|
||||
var callResult = await kucoinClient.SpotApi.ExchangeData.GetTickersAsync();
|
||||
if(!callResult.Success)
|
||||
{
|
||||
Console.WriteLine("Request failed: " + callResult.Error);
|
||||
return;
|
||||
}
|
||||
|
||||
Console.WriteLine("Result: " + callResult.Data);
|
||||
```
|
||||
|
||||
## Socket client
|
||||
The socket client gives access to the websocket API of an exchange. Websocket API's offer streams to which updates are pushed to which a client can listen, and sometimes also allow request/response communication.
|
||||
Just like the Rest client is divided in Rest Api clients, the Socket client is divided into Socket Api clients, each with their own range of API functionality. Socket Api clients are generally not divided into topics since the number of methods isn't as big as with the Rest client. To use the Kucoin client as example again, it looks like this:
|
||||
|
||||
```csharp
|
||||
|
||||
- KucoinSocketClient
|
||||
- SpotStreams
|
||||
- FuturesStreams
|
||||
|
||||
```
|
||||
*Subscribing to updates for all tickers on the Spot Api*
|
||||
```csharp
|
||||
var subscribeResult = kucoinSocketClient.SpotStreams.SubscribeToAllTickerUpdatesAsync(DataHandler);
|
||||
```
|
||||
|
||||
Subscribe methods always require a data handler parameter, which is the method which will be called when an update is received from the server. This can be the name of a method or a lambda expression.
|
||||
|
||||
*Method reference*
|
||||
```csharp
|
||||
await kucoinSocketClient.SpotStreams.SubscribeToAllTickerUpdatesAsync(DataHandler);
|
||||
|
||||
private static void DataHandler(DataEvent<KucoinStreamTick> updateData)
|
||||
{
|
||||
// Process updateData
|
||||
}
|
||||
```
|
||||
|
||||
*Lambda*
|
||||
```csharp
|
||||
await kucoinSocketClient.SpotStreams.SubscribeToAllTickerUpdatesAsync(updateData =>
|
||||
{
|
||||
// Process updateData
|
||||
});
|
||||
```
|
||||
|
||||
All updates are wrapped in a `DataEvent<>` object, which contain the following properties:
|
||||
`Timestamp`: The timestamp when the data was received (not send!)
|
||||
`OriginalData`: Will contain the originally received unparsed data if this has been enabled in the client options
|
||||
`Topic`: Will contain the topic of the update, which is typically the symbol or asset the update is for
|
||||
`Data`: Contains the received update data.
|
||||
|
||||
*[WARNING] Do not use `using` statements in combination with constructing a `SocketClient` without blocking the thread. Doing so will dispose the `SocketClient` instance when the subscription is done, which will result in the connection getting closed. Instead assign the socket client to a variable outside of the method scope.*
|
||||
|
||||
### Processing subscribe responses
|
||||
Subscribing to a stream will return a `CallResult<UpdateSubscription>` object. This should be checked for success the same way as a [rest request](#processing-request-responses). The `UpdateSubscription` object can be used to listen for connection events of the socket connection.
|
||||
```csharp
|
||||
|
||||
var subscriptionResult = await kucoinSocketClient.SpotStreams.SubscribeToAllTickerUpdatesAsync(DataHandler);
|
||||
if(!subscriptionResult.Success)
|
||||
{
|
||||
Console.WriteLine("Failed to connect: " + subscriptionResult.Error);
|
||||
return;
|
||||
}
|
||||
subscriptionResult.Data.ConnectionLost += () =>
|
||||
{
|
||||
Console.WriteLine("Connection lost");
|
||||
};
|
||||
subscriptionResult.Data.ConnectionRestored += (time) =>
|
||||
{
|
||||
Console.WriteLine("Connection restored");
|
||||
};
|
||||
|
||||
```
|
||||
|
||||
### Unsubscribing
|
||||
When no longer interested in specific updates there are a few ways to unsubscribe.
|
||||
|
||||
**Close subscription**
|
||||
Subscribing to an update stream will respond with an `UpdateSubscription` object. You can call the `CloseAsync()` method on this to no longer receive updates from that subscription:
|
||||
```csharp
|
||||
var subscriptionResult = await kucoinSocketClient.SpotStreams.SubscribeToAllTickerUpdatesAsync(DataHandler);
|
||||
await subscriptionResult.Data.CloseAsync();
|
||||
```
|
||||
|
||||
**Cancellation token**
|
||||
Passing in a `CancellationToken` as parameter in the subscribe method will allow you to cancel subscriptions by canceling the token. This can be useful when you need to cancel some streams but not others. In this example, both `BTC-USDT` and `ETH-USDT` streams get canceled, while the `KCS-USDT` stream remains active.
|
||||
```csharp
|
||||
var cts = new CancellationTokenSource();
|
||||
var subscriptionResult1 = await kucoinSocketClient.SpotStreams.SubscribeToTickerUpdatesAsync("BTC-USDT", DataHandler, cts.Token);
|
||||
var subscriptionResult2 = await kucoinSocketClient.SpotStreams.SubscribeToTickerUpdatesAsync("ETH-USDT", DataHandler, cts.Token);
|
||||
var subscriptionResult3 = await kucoinSocketClient.SpotStreams.SubscribeToTickerUpdatesAsync("KCS-USDT", DataHandler);
|
||||
Console.ReadLine();
|
||||
cts.Cancel();
|
||||
```
|
||||
|
||||
**Client unsubscribe**
|
||||
Subscriptions can also be closed by calling the `UnsubscribeAsync` method on the client, while providing either the `UpdateSubscription` object or the subscription id:
|
||||
```csharp
|
||||
var subscriptionResult = await kucoinSocketClient.SpotStreams.SubscribeToTickerUpdatesAsync("BTC-USDT", DataHandler);
|
||||
await kucoinSocketClient.UnsubscribeAsync(subscriptionResult.Data);
|
||||
// OR
|
||||
await kucoinSocketClient.UnsubscribeAsync(subscriptionResult.Data.Id);
|
||||
```
|
||||
|
||||
When you need to unsubscribe all current subscriptions on a client you can call `UnsubscribeAllAsync` on the client to unsubscribe all streams and close all connections.
|
||||
|
||||
|
@ -1,60 +0,0 @@
|
||||
---
|
||||
title: FAQ
|
||||
nav_order: 12
|
||||
---
|
||||
|
||||
## Frequently asked questions
|
||||
|
||||
### I occasionally get a NullReferenceException, what's wrong?
|
||||
You probably don't check the result status of a call and just assume the data is always there. `NullReferenceExecption`s will happen when you have code like this `var symbol = client.GetTickersAync().Result.Data.Symbol` because the `Data` property is null when the call fails. Instead check if the call is successful like this:
|
||||
```csharp
|
||||
var tickerResult = await client.GetTickersAync();
|
||||
if(!tickerResult.Success)
|
||||
{
|
||||
// Handle error
|
||||
}
|
||||
else
|
||||
{
|
||||
// Handle result, it is now safe to access the Data property
|
||||
var symbol = tickerResult.Data.Symbol;
|
||||
}
|
||||
```
|
||||
|
||||
### The socket client stops sending updates after a little while
|
||||
You probably didn't keep a reference to the socket client and it got disposed.
|
||||
Instead of subscribing like this:
|
||||
```csharp
|
||||
private void SomeMethod()
|
||||
{
|
||||
var socketClient = new BinanceSocketClient();
|
||||
socketClient.Spot.SubscribeToOrderBookUpdates("BTCUSDT", data => {
|
||||
// Handle data
|
||||
});
|
||||
}
|
||||
```
|
||||
Subscribe like this:
|
||||
```csharp
|
||||
private BinanceSocketClient _socketClient = new BinanceSocketClient();
|
||||
|
||||
// .. rest of the class
|
||||
|
||||
private void SomeMethod()
|
||||
{
|
||||
_socketClient.Spot.SubscribeToOrderBookUpdates("BTCUSDT", data => {
|
||||
// Handle data
|
||||
});
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
### Can I use the TestNet/US/other API with this library
|
||||
Yes, generally these are all supported and can be configured by setting the Environment in the client options. Some known environments should be available in the [Exchange]Environment class. For example:
|
||||
```csharp
|
||||
var client = new BinanceRestClient(options =>
|
||||
{
|
||||
options.Environment = BinanceEnvironment.Testnet;
|
||||
});
|
||||
```
|
||||
|
||||
### How are timezones handled / Timestamps are off by xx
|
||||
Exchange API's treat all timestamps as UTC, both incoming and outgoing. The client libraries do no conversion so be sure to use UTC as well.
|
@ -1,28 +0,0 @@
|
||||
---
|
||||
title: Glossary
|
||||
nav_order: 11
|
||||
---
|
||||
## Terms and definitions
|
||||
|
||||
|Definition|Synonyms|Meaning|
|
||||
|----------|--------|-------|
|
||||
|Symbol|Market|An asset pair, for example `BTC-ETH`|
|
||||
|Asset|Currency, Coin|A coin for which you can hold balance and which makes up Symbols. For example both `BTC`, `ETH` or `USD`|
|
||||
|Trade|Execution, fill|The (partial) execution of an order. Orders can have multiple trades|
|
||||
|Quantity|Amount, Size|The amount of asset|
|
||||
|Fee|Commission|The fee paid for an order or trade|
|
||||
|Kline|Candlestick, OHLC|K-line data, used for candlestick charts. Contains Open/High/Low/Close/Volume|
|
||||
|KlineInterval|The time period of a single kline|
|
||||
|Open order|Active order, Unexecuted order|An order which has not yet been fully filled|
|
||||
|Closed order|Completed order, executed order|An order which is no longer active. Can be canceled or fully filled|
|
||||
|Network|Chain|The network of an asset. For example `ETH` allows multiple networks like `ERC20` and `BEP2`|
|
||||
|Order book|Market depth|A list of (the top rows of) the current best bids and asks|
|
||||
|Ticker|Stats|Statistics over the last 24 hours|
|
||||
|Client implementation|Library|An implementation of the `CrytpoExchange.Net` library. For example `Binance.Net` or `Bybit.Net`|
|
||||
|
||||
### Other naming conventions
|
||||
#### PlaceOrderAsync
|
||||
Methods for creating an order are always named `PlaceOrderAsync`, with and optional additional name for the type of order, for example `PlaceMarginOrderAsync`.
|
||||
|
||||
#### GetOrdersAsync/GetOpenOrdersAsync/GetClosedOrdersAsync
|
||||
`GetOpenOrdersAsync` only retrieves orders which are still active, `GetClosedOrdersAsync` only retrieves orders which are canceled/closed. `GetOrdersAsync` retrieves both open and closed orders.
|
@ -1,6 +0,0 @@
|
||||
---
|
||||
title: Creating an implementation
|
||||
nav_order: 8
|
||||
---
|
||||
|
||||
TODO
|
@ -1,145 +0,0 @@
|
||||
---
|
||||
title: Common interfaces
|
||||
nav_order: 7
|
||||
---
|
||||
|
||||
## Shared interfaces
|
||||
Clients have a common interface implementation to allow a shared code base to use the same functionality for different exchanges. The interface is implemented at the `API` level, for example:
|
||||
```csharp
|
||||
var binanceClient = new BinanceClient();
|
||||
ISpotClient spotClient = binanceClient.SpotApi.CommonSpotClient;
|
||||
IFuturesClient futuresClient = binanceClient.UsdFuturesApi.CommonFuturesClient;
|
||||
```
|
||||
|
||||
For examples on this see the Examples folder.
|
||||
|
||||
## ISpotClient
|
||||
The `ISpotClient` interface is implemented on Spot API clients. The interface exposes basic functionality like retrieving market data and managing orders. The `ISpotClient` interface will be available via the `CommonSpotClient` property on the Api client.
|
||||
The spot client has the following members:
|
||||
|
||||
*Properties*
|
||||
```csharp
|
||||
// The name of the exchange this client interacts with
|
||||
string ExchangeName { get; }
|
||||
```
|
||||
|
||||
*Events*
|
||||
```csharp
|
||||
// Event when placing an order with this ISpotClient. Note that this is not an event handler listening on the exchange, just an event handler for when the `PlaceOrderAsync` method is called.
|
||||
event Action<OrderId> OnOrderPlaced;
|
||||
// Event when canceling an order with this ISpotClient. Note that this is not an event handler listening on the exchange, just an event handler for when the `CancelOrderAsync` method is called.
|
||||
event Action<OrderId> OnOrderCanceled;
|
||||
|
||||
```
|
||||
|
||||
*Methods*
|
||||
```csharp
|
||||
// Retrieve the name of a symbol based on 2 assets. This will format them in the way the exchange expects them. For example BTC, USDT will return BTCUSDT on Binance and BTC-USDT on Kucoin
|
||||
string GetSymbolName(string baseAsset, string quoteAsset);
|
||||
|
||||
// Get a list of symbols (trading pairs) on the exchange
|
||||
Task<WebCallResult<IEnumerable<Symbol>>> GetSymbolsAsync();
|
||||
|
||||
// Get the ticker (24 hour stats) for a symbol
|
||||
Task<WebCallResult<Ticker>> GetTickerAsync(string symbol);
|
||||
|
||||
// Get a list of tickers for all symbols
|
||||
Task<WebCallResult<IEnumerable<Ticker>>> GetTickersAsync();
|
||||
|
||||
// Get a list klines (candlesticks) for a symbol
|
||||
Task<WebCallResult<IEnumerable<Kline>>> GetKlinesAsync(string symbol, TimeSpan timespan, DateTime? startTime = null, DateTime? endTime = null, int? limit = null);
|
||||
|
||||
// Get the order book for a symbol
|
||||
Task<WebCallResult<OrderBook>> GetOrderBookAsync(string symbol);
|
||||
|
||||
// Get a list of most recent trades
|
||||
Task<WebCallResult<IEnumerable<Trade>>> GetRecentTradesAsync(string symbol);
|
||||
|
||||
// Get balances
|
||||
Task<WebCallResult<IEnumerable<Balance>>> GetBalancesAsync(string? accountId = null);
|
||||
|
||||
// Place an order
|
||||
Task<WebCallResult<OrderId>> PlaceOrderAsync(string symbol, CommonOrderSide side, CommonOrderType type, decimal quantity, decimal? price = null, string? accountId = null);
|
||||
|
||||
// Get order by order id
|
||||
Task<WebCallResult<Order>> GetOrderAsync(string orderId, string? symbol = null);
|
||||
|
||||
// Get the trades for an order
|
||||
Task<WebCallResult<IEnumerable<UserTrade>>> GetOrderTradesAsync(string orderId, string? symbol = null);
|
||||
|
||||
// Get a list of open orders. Some exchanges require a symbol
|
||||
Task<WebCallResult<IEnumerable<Order>>> GetOpenOrdersAsync(string? symbol = null);
|
||||
|
||||
// Get a list of closed orders. Some exchanges require a symbol
|
||||
Task<WebCallResult<IEnumerable<Order>>> GetClosedOrdersAsync(string? symbol = null);
|
||||
|
||||
// Cancel an active order
|
||||
Task<WebCallResult<OrderId>> CancelOrderAsync(string orderId, string? symbol = null);
|
||||
```
|
||||
|
||||
## IFuturesClient
|
||||
The `IFuturesClient` interface is implemented on Futures API clients. The interface exposes basic functionality like retrieving market data and managing orders. The `IFuturesClient` interface will be available via the `CommonFuturesClient` property on the Api client.
|
||||
The spot client has the following members:
|
||||
|
||||
*Properties*
|
||||
```csharp
|
||||
// The name of the exchange this client interacts with
|
||||
string ExchangeName { get; }
|
||||
```
|
||||
|
||||
*Events*
|
||||
```csharp
|
||||
// Event when placing an order with this ISpotClient. Note that this is not an event handler listening on the exchange, just an event handler for when the `PlaceOrderAsync` method is called.
|
||||
event Action<OrderId> OnOrderPlaced;
|
||||
// Event when canceling an order with this ISpotClient. Note that this is not an event handler listening on the exchange, just an event handler for when the `CancelOrderAsync` method is called.
|
||||
event Action<OrderId> OnOrderCanceled;
|
||||
|
||||
```
|
||||
|
||||
*Methods*
|
||||
```csharp
|
||||
// Retrieve the name of a symbol based on 2 assets. This will format them in the way the exchange expects them. For example BTC, USDT will return BTCUSDT on Binance and BTC-USDT on Kucoin
|
||||
string GetSymbolName(string baseAsset, string quoteAsset);
|
||||
|
||||
// Get a list of symbols (trading pairs) on the exchange
|
||||
Task<WebCallResult<IEnumerable<Symbol>>> GetSymbolsAsync();
|
||||
|
||||
// Get the ticker (24 hour stats) for a symbol
|
||||
Task<WebCallResult<Ticker>> GetTickerAsync(string symbol);
|
||||
|
||||
// Get a list of tickers for all symbols
|
||||
Task<WebCallResult<IEnumerable<Ticker>>> GetTickersAsync();
|
||||
|
||||
// Get a list klines (candlesticks) for a symbol
|
||||
Task<WebCallResult<IEnumerable<Kline>>> GetKlinesAsync(string symbol, TimeSpan timespan, DateTime? startTime = null, DateTime? endTime = null, int? limit = null);
|
||||
|
||||
// Get the order book for a symbol
|
||||
Task<WebCallResult<OrderBook>> GetOrderBookAsync(string symbol);
|
||||
|
||||
// Get a list of most recent trades
|
||||
Task<WebCallResult<IEnumerable<Trade>>> GetRecentTradesAsync(string symbol);
|
||||
|
||||
// Get balances
|
||||
Task<WebCallResult<IEnumerable<Balance>>> GetBalancesAsync(string? accountId = null);
|
||||
|
||||
// Get current open positions
|
||||
Task<WebCallResult<IEnumerable<Position>>> GetPositionsAsync();
|
||||
|
||||
// Place an order
|
||||
Task<WebCallResult<OrderId>> PlaceOrderAsync(string symbol, CommonOrderSide side, CommonOrderType type, decimal quantity, decimal? price = null, int? leverage = null, string? accountId = null);
|
||||
|
||||
// Get order by order id
|
||||
Task<WebCallResult<Order>> GetOrderAsync(string orderId, string? symbol = null);
|
||||
|
||||
// Get the trades for an order
|
||||
Task<WebCallResult<IEnumerable<UserTrade>>> GetOrderTradesAsync(string orderId, string? symbol = null);
|
||||
|
||||
// Get a list of open orders. Some exchanges require a symbol
|
||||
Task<WebCallResult<IEnumerable<Order>>> GetOpenOrdersAsync(string? symbol = null);
|
||||
|
||||
// Get a list of closed orders. Some exchanges require a symbol
|
||||
Task<WebCallResult<IEnumerable<Order>>> GetClosedOrdersAsync(string? symbol = null);
|
||||
|
||||
// Cancel an active order
|
||||
Task<WebCallResult<OrderId>> CancelOrderAsync(string orderId, string? symbol = null);
|
||||
```
|
@ -1,150 +0,0 @@
|
||||
---
|
||||
title: Logging
|
||||
nav_order: 5
|
||||
---
|
||||
|
||||
## Configuring logging
|
||||
The library offers extensive logging, which depends on the dotnet `Microsoft.Extensions.Logging.ILogger` interface. This should provide ease of use when connecting the library logging to your existing logging implementation.
|
||||
|
||||
*Configure logging to write to the console*
|
||||
```csharp
|
||||
IServiceCollection services = new ServiceCollection();
|
||||
services
|
||||
.AddBinance()
|
||||
.AddLogging(options =>
|
||||
{
|
||||
options.SetMinimumLevel(LogLevel.Trace);
|
||||
options.AddConsole();
|
||||
});
|
||||
```
|
||||
|
||||
The library provides a TraceLogger ILogger implementation which writes log messages using `Trace.WriteLine`, but any other logging library can be used.
|
||||
|
||||
*Configure logging to use trace logging*
|
||||
```csharp
|
||||
IServiceCollection serviceCollection = new ServiceCollection();
|
||||
serviceCollection.AddBinance()
|
||||
.AddLogging(options =>
|
||||
{
|
||||
options.SetMinimumLevel(LogLevel.Trace);
|
||||
options.AddProvider(new TraceLoggerProvider());
|
||||
});
|
||||
```
|
||||
|
||||
### Using an external logging library and dotnet DI
|
||||
|
||||
With for example an ASP.Net Core or Blazor project the logging can be configured by the dependency container, which can then automatically be used be the clients.
|
||||
The next example shows how to use Serilog. This assumes the `Serilog.AspNetCore` package (https://github.com/serilog/serilog-aspnetcore) is installed.
|
||||
|
||||
*Using serilog:*
|
||||
```csharp
|
||||
using Binance.Net;
|
||||
using Serilog;
|
||||
|
||||
Log.Logger = new LoggerConfiguration()
|
||||
.MinimumLevel.Debug()
|
||||
.WriteTo.Console()
|
||||
.CreateLogger();
|
||||
|
||||
var builder = WebApplication.CreateBuilder(args);
|
||||
builder.Services.AddBinance();
|
||||
builder.Host.UseSerilog();
|
||||
var app = builder.Build();
|
||||
|
||||
// startup
|
||||
|
||||
app.Run();
|
||||
```
|
||||
|
||||
### Logging without dotnet DI
|
||||
If you don't have a dependency injection service available because you are for example working on a simple console application you have 2 options for logging.
|
||||
|
||||
#### Create a ServiceCollection manually and get the client from the service provider
|
||||
|
||||
```csharp
|
||||
IServiceCollection serviceCollection = new ServiceCollection();
|
||||
serviceCollection.AddBinance();
|
||||
serviceCollection.AddLogging(options =>
|
||||
{
|
||||
options.SetMinimumLevel(LogLevel.Trace);
|
||||
options.AddConsole();
|
||||
}).BuildServiceProvider();
|
||||
|
||||
var client = serviceCollection.GetRequiredService<IBinanceRestClient>();
|
||||
|
||||
```
|
||||
|
||||
#### Create a LoggerFactory manually
|
||||
|
||||
```csharp
|
||||
var logFactory = new LoggerFactory();
|
||||
logFactory.AddProvider(new ConsoleLoggerProvider());
|
||||
var binanceClient = new BinanceRestClient(new HttpClient(), logFactory, options => { });
|
||||
```
|
||||
|
||||
## Providing logging for issues
|
||||
A big debugging tool when opening an issue on Github is providing logging of what data caused the issue. This can be provided two ways, via the `OriginalData` property of the call result or data event, or collecting the Trace logging.
|
||||
### OriginalData
|
||||
This is only useful when there is an issue in deserialization. So either a call result is giving a Deserialization error, or the result has a value that is unexpected. If that is the issue, please provide the original data that is received so the deserialization issue can be resolved based on the received data.
|
||||
By default the `OriginalData` property in the `WebCallResult`/`DataEvent` object is not filled as saving the original data has a (very small) performance penalty. To save the original data in the `OriginalData` property the `OutputOriginalData` option should be set to `true` in the client options.
|
||||
*Enabled output data*
|
||||
```csharp
|
||||
var client = new BinanceClient(options =>
|
||||
{
|
||||
options.OutputOriginalData = true
|
||||
});
|
||||
```
|
||||
|
||||
*Accessing original data*
|
||||
```csharp
|
||||
// Rest request
|
||||
var tickerResult = await client.SpotApi.ExchangeData.GetTickersAsync();
|
||||
var originallyReceivedData = tickerResult.OriginalData;
|
||||
|
||||
// Socket update
|
||||
await client.SpotStreams.SubscribeToAllTickerUpdatesAsync(update => {
|
||||
var originallyRecievedData = update.OriginalData;
|
||||
});
|
||||
```
|
||||
|
||||
### Trace logging
|
||||
Trace logging, which is the most verbose log level, will show everything the library does and includes the data that was send and received.
|
||||
Output data will look something like this:
|
||||
```
|
||||
2021-12-17 10:40:42:296 | Debug | Binance | Client configuration: LogLevel: Trace, Writers: 1, OutputOriginalData: False, Proxy: -, AutoReconnect: True, ReconnectInterval: 00:00:05, MaxReconnectTries: , MaxResubscribeTries: 5, MaxConcurrentResubscriptionsPerSocket: 5, SocketResponseTimeout: 00:00:10, SocketNoDataTimeout: 00:00:00, SocketSubscriptionsCombineTarget: , CryptoExchange.Net: v5.0.0.0, Binance.Net: v8.0.0.0
|
||||
2021-12-17 10:40:42:410 | Debug | Binance | [15] Creating request for https://api.binance.com/api/v3/ticker/24hr
|
||||
2021-12-17 10:40:42:439 | Debug | Binance | [15] Sending GET request to https://api.binance.com/api/v3/ticker/24hr?symbol=BTCUSDT with headers Accept=[application/json], X-MBX-APIKEY=[XXX]
|
||||
2021-12-17 10:40:43:024 | Debug | Binance | [15] Response received in 571ms: {"symbol":"BTCUSDT","priceChange":"-1726.47000000","priceChangePercent":"-3.531","weightedAvgPrice":"48061.51544204","prevClosePrice":"48901.44000000","lastPrice":"47174.97000000","lastQty":"0.00352000","bidPrice":"47174.96000000","bidQty":"0.65849000","askPrice":"47174.97000000","askQty":"0.13802000","openPrice":"48901.44000000","highPrice":"49436.43000000","lowPrice":"46749.55000000","volume":"33136.69765000","quoteVolume":"1592599905.80360790","openTime":1639647642763,"closeTime":1639734042763,"firstId":1191596486,"lastId":1192649611,"count":1053126}
|
||||
```
|
||||
When opening an issue, please provide this logging when available.
|
||||
|
||||
### Example of serilog config and minimal API's
|
||||
|
||||
```csharp
|
||||
using Binance.Net;
|
||||
using Binance.Net.Interfaces.Clients;
|
||||
using Serilog;
|
||||
|
||||
Log.Logger = new LoggerConfiguration()
|
||||
.MinimumLevel.Debug()
|
||||
.WriteTo.Console()
|
||||
.CreateLogger();
|
||||
|
||||
var builder = WebApplication.CreateBuilder(args);
|
||||
builder.Services.AddBinance();
|
||||
builder.Host.UseSerilog();
|
||||
var app = builder.Build();
|
||||
|
||||
// startup
|
||||
|
||||
app.Urls.Add("http://localhost:3000");
|
||||
|
||||
app.MapGet("/price/{symbol}", async (string symbol) =>
|
||||
{
|
||||
var client = app.Services.GetRequiredService<IBinanceRestClient>();
|
||||
var result = await client.SpotApi.ExchangeData.GetPriceAsync(symbol);
|
||||
return result.Data.Price;
|
||||
});
|
||||
|
||||
app.Run();
|
||||
```
|
@ -1,73 +0,0 @@
|
||||
---
|
||||
title: Migrate v5 to v6
|
||||
nav_order: 10
|
||||
---
|
||||
|
||||
## Migrating from version 5 to version 6
|
||||
When updating your code from version 5 implementations to version 6 implementations you will encounter some breaking changes. Here is the general outline of changes made in the CryptoExchange.Net library. For more specific changes for each library visit the library migration guide.
|
||||
|
||||
*NOTE when updating it is not possible to have some client implementations use a V5 version and some clients a V6. When updating all libraries should be migrated*
|
||||
|
||||
## Rest client name
|
||||
To be more clear about different clients for different API's the rest client implementations have been renamed from [Exchange]Client to [Exchange]RestClient. This makes it more clear that it only implements the Rest API and the [Exchange]SocketClient the Socket API.
|
||||
|
||||
## Options
|
||||
Option parameters have been changed to a callback instead of an options object. This makes processing of the options easier and is in line with how dotnet handles option configurations.
|
||||
|
||||
**BaseAddress**
|
||||
The BaseAddress option has been replaced by the Environment option. The Environment options allows for selection/switching between different trade environments more easily. For example the environment can be switched between a testnet and live by changing only a single line instead of having to change all BaseAddresses.
|
||||
|
||||
**LogLevel/LogWriters**
|
||||
The logging options have been removed and are now inherited by the DI configuration. See [Logging](https://jkorf.github.io/CryptoExchange.Net/Logging.html) for more info.
|
||||
|
||||
**HttpClient**
|
||||
The HttpClient will now be received by the DI container instead of having to pass it manually. When not using DI it is still possible to provide a HttpClient, but it is now located in the client constructor.
|
||||
|
||||
*V5*
|
||||
```csharp
|
||||
var client = new BinanceClient(new BinanceClientOptions(){
|
||||
OutputOriginalData = true,
|
||||
SpotApiOptions = new RestApiOptions {
|
||||
BaseAddress = BinanceApiAddresses.TestNet.RestClientAddress
|
||||
}
|
||||
// Other options
|
||||
});
|
||||
```
|
||||
|
||||
*V6*
|
||||
```csharp
|
||||
var client = new BinanceClient(options => {
|
||||
options.OutputOriginalData = true;
|
||||
options.Environment = BinanceEnvironment.Testnet;
|
||||
// Other options
|
||||
});
|
||||
```
|
||||
|
||||
## Socket api name
|
||||
As socket API's are often more than just streams to subscribe to the name of the socket API clients have been changed from [Topic]Streams to [Topic]Api which matches the rest API client names. For example `SpotStreams` has become `SpotApi`, so `binanceSocketClient.UsdFuturesStreams.SubscribeXXX` has become `binanceSocketClient.UsdFuturesApi.SubscribeXXX`.
|
||||
|
||||
## Add[Exchange] extension method
|
||||
With the change in options providing the DI extension methods for the IServiceCollection have also been changed slightly. Also the socket clients will now be registered as Singleton by default instead of Scoped.
|
||||
|
||||
*V5*
|
||||
```csharp
|
||||
builder.Services.AddKucoin((restOpts, socketOpts) =>
|
||||
{
|
||||
restOpts.LogLevel = LogLevel.Debug;
|
||||
restOpts.ApiCredentials = new KucoinApiCredentials("KEY", "SECRET", "PASS");
|
||||
socketOpts.LogLevel = LogLevel.Debug;
|
||||
socketOpts.ApiCredentials = new KucoinApiCredentials("KEY", "SECRET", "PASS");
|
||||
}, ServiceLifetime.Singleton);
|
||||
```
|
||||
|
||||
*V6*
|
||||
```csharp
|
||||
builder.Services.AddKucoin((restOpts) =>
|
||||
{
|
||||
restOpts.ApiCredentials = new KucoinApiCredentials("KEY", "SECRET", "PASS");
|
||||
},
|
||||
(socketOpts) =>
|
||||
{
|
||||
socketOpts.ApiCredentials = new KucoinApiCredentials("KEY", "SECRET", "PASS");
|
||||
});
|
||||
```
|
@ -1,133 +0,0 @@
|
||||
---
|
||||
title: Client options
|
||||
nav_order: 4
|
||||
---
|
||||
|
||||
## Setting client options
|
||||
|
||||
Each implementation can be configured using client options. There are 2 ways to provide these, either via `[client].SetDefaultOptions([options]);`, or in the constructor of the client. The examples here use the `BinanceClient`, but usage is the same for each client.
|
||||
|
||||
*Set the default options to use for new clients*
|
||||
```csharp
|
||||
|
||||
BinanceClient.SetDefaultOptions(options =>
|
||||
{
|
||||
options.OutputOriginalData = true;
|
||||
options.ApiCredentials = new ApiCredentials("KEY", "SECRET");
|
||||
// Override the api credentials for the Spot API
|
||||
options.SpotOptions.ApiCredentials = new ApiCredentials("SPOT-KEY", "SPOT-SECRET");
|
||||
});
|
||||
|
||||
```
|
||||
|
||||
*Set the options to use for a single new client*
|
||||
```csharp
|
||||
|
||||
var client = new BinanceClient(options =>
|
||||
{
|
||||
options.OutputOriginalData = true;
|
||||
options.ApiCredentials = new ApiCredentials("KEY", "SECRET");
|
||||
// Override the api credentials for the Spot API
|
||||
options.SpotOptions.ApiCredentials = new ApiCredentials("SPOT-KEY", "SPOT-SECRET");
|
||||
});
|
||||
|
||||
```
|
||||
|
||||
When calling `SetDefaultOptions` each client created after that will use the options that were set, unless the specific option is overriden in the options that were provided to the client. Consider the following example:
|
||||
```csharp
|
||||
|
||||
BinanceClient.SetDefaultOptions(options =>
|
||||
{
|
||||
options.OutputOriginalData = true;
|
||||
});
|
||||
|
||||
var client = new BinanceClient(options =>
|
||||
{
|
||||
options.OutputOriginalData = false;
|
||||
});
|
||||
|
||||
```
|
||||
|
||||
The client instance will have the following options:
|
||||
`OutputOriginalData = false`
|
||||
|
||||
## Api options
|
||||
The options are divided in two categories. The basic options, which will apply to everything the client does, and the Api options, which is limited to the specific API client (see [Clients](https://jkorf.github.io/CryptoExchange.Net/Clients.html)).
|
||||
|
||||
```csharp
|
||||
|
||||
var client = new BinanceRestClient(options =>
|
||||
{
|
||||
options.ApiCredentials = new ApiCredentials("GENERAL-KEY", "GENERAL-SECRET"),
|
||||
options.SpotOptions.ApiCredentials = new ApiCredentials("SPOT-KEY", "SPOT-SECRET");
|
||||
});
|
||||
|
||||
```
|
||||
|
||||
The options provided in the SpotApiOptions are only applied to the SpotApi (`client.SpotApi.XXX` endpoints), while the base options are applied to everything. This means that the spot endpoints will use the "SPOT-KEY" credentials, while all other endpoints (`client.UsdFuturesApi.XXX` / `client.CoinFuturesApi.XXX`) will use the "GENERAL-KEY" credentials.
|
||||
|
||||
## CryptoExchange.Net options definitions
|
||||
All clients have access to the following options, specific implementations might have additional options.
|
||||
|
||||
**Base client options**
|
||||
|
||||
|Option|Description|Default|
|
||||
|------|-----------|-------|
|
||||
|`OutputOriginalData`|If set to `true` the originally received Json data will be output as well as the deserialized object. For `RestClient` calls the data will be in the `WebCallResult<T>.OriginalData` property, for `SocketClient` subscriptions the data will be available in the `DataEvent<T>.OriginalData` property when receiving an update. | `false`
|
||||
|`ApiCredentials`| The API credentials to use for accessing protected endpoints. Can either be an API key/secret using Hmac encryption or an API key/private key using RSA encryption for exchanges that support that. See [Credentials](#credentials). Note that this is a `default` value for all API clients, and can be overridden per API client. See the `Base Api client options`| `null`
|
||||
|`Proxy`|The proxy to use for connecting to the API.| `null`
|
||||
|`RequestTimeout`|The timeout for client requests to the server| `TimeSpan.FromSeconds(20)`
|
||||
|
||||
**Rest client options (extension of base client options)**
|
||||
|
||||
|Option|Description|Default|
|
||||
|------|-----------|-------|
|
||||
|`AutoTimestamp`|Whether or not the library should attempt to sync the time between the client and server. If the time between server and client is not in sync authentication errors might occur. This option should be disabled when the client time sure is to be in sync.|`true`|
|
||||
|`TimestampRecalculationInterval`|The interval of how often the time synchronization between client and server should be executed| `TimeSpan.FromHours(1)`
|
||||
|`Environment`|The environment the library should talk to. Some exchanges have testnet/sandbox environments which can be used instead of the real exchange. The environment option can be used to switch between different trade environments|`Live environment`
|
||||
|
||||
**Socket client options (extension of base client options)**
|
||||
|
||||
|Option|Description|Default|
|
||||
|------|-----------|-------|
|
||||
|`AutoReconnect`|Whether or not the socket should attempt to automatically reconnect when disconnected.|`true`
|
||||
|`ReconnectInterval`|The time to wait between connection tries when reconnecting.|`TimeSpan.FromSeconds(5)`
|
||||
|`SocketResponseTimeout`|The time in which a response is expected on a request before giving a timeout.|`TimeSpan.FromSeconds(10)`
|
||||
|`SocketNoDataTimeout`|If no data is received after this timespan then assume the connection is dropped. This is mainly used for API's which have some sort of ping/keepalive system. For example; the Bitfinex API will sent a heartbeat message every 15 seconds, so the `SocketNoDataTimeout` could be set to 20 seconds. On API's without such a mechanism this might not work because there just might not be any update while still being fully connected. | `default(TimeSpan)` (no timeout)
|
||||
|`SocketSubscriptionsCombineTarget`|The amount of subscriptions that should be made on a single socket connection. Not all exchanges support multiple subscriptions on a single socket. Setting this to a higher number increases subscription speed because not every subscription needs to connect to the server, but having more subscriptions on a single connection will also increase the amount of traffic on that single connection, potentially leading to issues.| Depends on implementation
|
||||
|`MaxConcurrentResubscriptionsPerSocket`|The maximum number of concurrent resubscriptions per socket when resubscribing after reconnecting|5
|
||||
|`MaxSocketConnections`|The maximum amount of distinct socket connections|`null`
|
||||
|`DelayAfterConnect`|The time to wait before sending messages after connecting to the server.|`TimeSpan.Zero`
|
||||
|`Environment`|The environment the library should talk to. Some exchanges have testnet/sandbox environments which can be used instead of the real exchange. The environment option can be used to switch between different trade environments|`Live environment`
|
||||
|
||||
**Base Api client options**
|
||||
|
||||
|Option|Description|Default|
|
||||
|------|-----------|-------|
|
||||
|`ApiCredentials`|The API credentials to use for accessing protected endpoints. Can either be an API key/secret using Hmac encryption or an API key/private key using RSA encryption for exchanges that support that. See [Credentials](#credentials). Setting ApiCredentials on the Api Options will override any default ApiCredentials on the `Base client options`| `null`
|
||||
|`OutputOriginalData`|If set to `true` the originally received Json data will be output as well as the deserialized object. For `RestClient` calls the data will be in the `WebCallResult<T>.OriginalData` property, for `SocketClient` subscriptions the data will be available in the `DataEvent<T>.OriginalData` property when receiving an update.|False
|
||||
|
||||
**Options for Rest Api Client (extension of base api client options)**
|
||||
|
||||
|Option|Description|Default|
|
||||
|------|-----------|-------|
|
||||
|`RateLimiters`|A list of `IRateLimiter`s to use.|`new List<IRateLimiter>()`|
|
||||
|`RateLimitingBehaviour`|What should happen when a rate limit is reached.|`RateLimitingBehaviour.Wait`|
|
||||
|`AutoTimestamp`|Whether or not the library should attempt to sync the time between the client and server. If the time between server and client is not in sync authentication errors might occur. This option should be disabled when the client time is sure to be in sync. Overrides the Rest client options `AutoTimestamp` option if set|`null`|
|
||||
|`TimestampRecalculationInterval`|The interval of how often the time synchronization between client and server should be executed. Overrides the Rest client options `TimestampRecalculationInterval` option if set| `TimeSpan.FromHours(1)`
|
||||
|
||||
**Options for Socket Api Client (extension of base api client options)**
|
||||
|
||||
|Option|Description|Default|
|
||||
|------|-----------|-------|
|
||||
|`SocketNoDataTimeout`|If no data is received after this timespan then assume the connection is dropped. This is mainly used for API's which have some sort of ping/keepalive system. For example; the Bitfinex API will sent a heartbeat message every 15 seconds, so the `SocketNoDataTimeout` could be set to 20 seconds. On API's without such a mechanism this might not work because there just might not be any update while still being fully connected. Overrides the Socket client options `SocketNoDataTimeout` option if set | `default(TimeSpan)` (no timeout)
|
||||
|`MaxSocketConnections`|The maximum amount of distinct socket connections. Overrides the Socket client options `MaxSocketConnections` option if set |`null`
|
||||
|
||||
## Credentials
|
||||
Credentials are supported in 3 formats in the base library:
|
||||
|
||||
|Type|Description|Example|
|
||||
|----|-----------|-------|
|
||||
|`Hmac`|An API key + secret combination. The API key is send with the request and the secret is used to sign requests. This is the default authentication method on all exchanges. |`options.ApiCredentials = new ApiCredentials("51231f76e-9c503548-8fabs3f-rfgf12mkl3", "556be32-d563ba53-faa2dfd-b3n5c", CredentialType.Hmac);`|
|
||||
|`RsaPem`|An API key + a public and private key pair generated by the user. The public key is shared with the exchange, while the private key is used to sign requests. This CredentialType expects the private key to be in .pem format and is only supported in .netstandard2.1 due to limitations of the framework|`options.ApiCredentials = new ApiCredentials("432vpV8daAaXAF4Qg", ""-----BEGIN PRIVATE KEY-----[PRIVATEKEY]-----END PRIVATE KEY-----", CredentialType.RsaPem);`|
|
||||
|`RsaXml`|An API key + a public and private key pair generated by the user. The public key is shared with the exchange, while the private key is used to sign requests. This CredentialType expects the private key to be in xml format and is supported in .netstandard2.0 and .netstandard2.1, but it might mean the private key needs to be converted from the original format to xml|`options.ApiCredentials = new ApiCredentials("432vpV8daAaXAF4Qg", "<RSAKeyValue>[PRIVATEKEY]</RSAKeyValue>", CredentialType.RsaXml);`|
|
@ -1,68 +0,0 @@
|
||||
---
|
||||
title: Order books
|
||||
nav_order: 6
|
||||
---
|
||||
|
||||
## Locally synced order book
|
||||
Each exchange implementation provides an order book implementation. These implementations will provide a client side order book and will take care of synchronization with the server, and will handle reconnecting and resynchronizing in case of a dropped connection.
|
||||
Order book implementations are named as `[ExchangeName][Type]SymbolOrderBook`, for example `BinanceSpotSymbolOrderBook`.
|
||||
|
||||
## Usage
|
||||
Start the book synchronization by calling the `StartAsync` method. This returns whether the book is successfully synchronized and started. You can listen to the `OnStatusChange` event to be notified of when the status of a book changes. Note that the order book is only synchronized with the server when the state is `Synced`. When the order book has been started and the state changes from `Synced` to `Reconnecting` the book will automatically reconnect and resync itself.
|
||||
|
||||
*Start an order book and print the top 3 rows*
|
||||
```csharp
|
||||
|
||||
var book = new BinanceSpotSymbolOrderBook("BTCUSDT");
|
||||
book.OnStatusChange += (oldState, newState) => Console.WriteLine($"State changed from {oldState} to {newState}");
|
||||
var startResult = await book.StartAsync();
|
||||
if (!startResult.Success)
|
||||
{
|
||||
Console.WriteLine("Failed to start order book: " + startResult.Error);
|
||||
return;
|
||||
}
|
||||
|
||||
while(true)
|
||||
{
|
||||
Console.Clear();
|
||||
Console.WriteLine(book.ToString(3);
|
||||
await Task.Delay(500);
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
### Accessing bids/asks
|
||||
You can access the current Bid/Ask lists using the responding properties:
|
||||
`var currentBidList = book.Bids;`
|
||||
`var currentAskList = book.Asks;`
|
||||
|
||||
Note that these will return copies of the internally synced lists when accessing the properties, and when accessing them in sequence like above does mean that the lists may not be in sync with eachother since they're accessed at different points in time.
|
||||
When you need both lists in sync you should access the `Book` property.
|
||||
`var (currentBidList, currentAskList) = book.Book;`
|
||||
|
||||
Because copies of the lists are made when accessing the bids/asks properties the performance impact should be considered. When only the current best ask/bid info is needed you can access the `BestOffers` property.
|
||||
`var (bestBid, bestAsk) = book.BestOffers;`
|
||||
|
||||
### Events
|
||||
The following events are available on the symbol order book:
|
||||
`book.OnStatusChange`: The book has changed state. This happens during connecting, the connection was lost or the order book was detected to be out of sync. The asks/bids are only the actual with the server when state is `Synced`.
|
||||
`book.OnOrderBookUpdate`: The book has changed, the arguments contain the changed entries.
|
||||
`book.OnBestOffersChanged`: The best offer (best bid, best ask) has changed.
|
||||
|
||||
```csharp
|
||||
|
||||
book.OnStatusChange += (oldStatus, newStatus) => { Console.WriteLine($"State changed from {oldStatus} to {newStatus}"); };
|
||||
book.OnOrderBookUpdate += (bidsAsks) => { Console.WriteLine($"Order book changed: {bidsAsks.Asks.Count()} asks, {bidsAsks.Bids.Count()} bids"); };
|
||||
book.OnBestOffersChanged += (bestOffer) => { Console.WriteLine($"Best offer changed, best bid: {bestOffer.BestBid.Price}, best ask: {bestOffer.BestAsk.Price}"); };
|
||||
|
||||
```
|
||||
|
||||
### Order book factory
|
||||
Each exchange implementation also provides an order book factory for creating ISymbolOrderBook instances. The naming convention for the factory is `[Exchange]OrderBookFactory`, for example `BinanceOrderBookFactory`. This type will be automatically added when using DI and can be used to facilitate easier testing.
|
||||
|
||||
*Creating an order book using the order book factory*
|
||||
```csharp
|
||||
var factory = services.GetRequiredService<IKucoinOrderBookFactory>();
|
||||
var book = factory.CreateSpot("ETH-USDT");
|
||||
var startResult = await book.StartAsync();
|
||||
```
|
@ -1,74 +0,0 @@
|
||||
---
|
||||
title: Rate limiting
|
||||
nav_order: 9
|
||||
---
|
||||
|
||||
## Rate limiting
|
||||
The library has build in rate limiting. These rate limits can be configured per client. Some client implementations where the exchange has clear rate limits will also have a default rate limiter already set up.
|
||||
Rate limiting is configured in the client options, and can be set on a specific client or for all clients by either providing it in the constructor for a client, or by using the `SetDefaultOptions` on a client.
|
||||
|
||||
What to do when a limit is reached can be configured with the `RateLimitingBehaviour` client options, which has 2 possible options. Setting it to `Fail` will cause a request to fail without sending it. Setting it to `Wait` will cause the request to wait until the request can be send in accordance to the rate limiter.
|
||||
|
||||
A rate limiter can be configured in the options like so:
|
||||
```csharp
|
||||
new ClientOptions
|
||||
{
|
||||
RateLimitingBehaviour = RateLimitingBehaviour.Wait,
|
||||
RateLimiters = new List<IRateLimiter>
|
||||
{
|
||||
new RateLimiter()
|
||||
.AddTotalRateLimit(50, TimeSpan.FromSeconds(10))
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
This will add a rate limiter for 50 requests per 10 seconds.
|
||||
A rate limiter can have multiple limits:
|
||||
```csharp
|
||||
new RateLimiter()
|
||||
.AddTotalRateLimit(50, TimeSpan.FromSeconds(10))
|
||||
.AddEndpointLimit("/api/order", 10, TimeSpan.FromSeconds(2))
|
||||
```
|
||||
This adds another limit of 10 requests per 2 seconds in addition to the 50 requests per 10 seconds limit.
|
||||
These are the available rate limit configurations:
|
||||
|
||||
### AddTotalRateLimit
|
||||
|Parameter|Description|
|
||||
|---------|-----------|
|
||||
|limit|The request weight limit per time period. Note that requests can have a weight specified. Default requests will have a weight of 1|
|
||||
|perTimePeriod|The time period over which the limit is enforced|
|
||||
|
||||
A rate limit for the total amount of requests for all requests send from the client.
|
||||
|
||||
### AddEndpointLimit
|
||||
|Parameter|Description|
|
||||
|---------|-----------|
|
||||
|endpoint|The endpoint this limit is for|
|
||||
|limit|The request weight limit per time period. Note that requests can have a weight specified. Default requests will have a weight of 1|
|
||||
|perTimePeriod|The time period over which the limit is enforced|
|
||||
|method|The HTTP method this limit is for. Defaults to all methods|
|
||||
|excludeFromOtherRateLimits|When set to true requests to this endpoint won't be counted for other configured rate limits|
|
||||
|
||||
A rate limit for all requests send to a specific endpoint. Requests that do not fully match the endpoint will not be counted to this limit.
|
||||
|
||||
### AddPartialEndpointLimit
|
||||
|Parameter|Description|
|
||||
|---------|-----------|
|
||||
|endpoint|The partial endpoint this limit is for. Partial means that a request will match this limiter when a part of the request URI path matches this endpoint|
|
||||
|limit|The request weight limit per time period. Note that requests can have a weight specified. Default requests will have a weight of 1|
|
||||
|perTimePeriod|The time period over which the limit is enforced|
|
||||
|method|The HTTP method this limit is for. Defaults to all methods|
|
||||
|countPerEndpoint|Whether all requests matching the endpoint pattern should be combined for this limit or each endpoint has its own limit|
|
||||
|ignoreOtherRateLimits|When set to true requests to this endpoint won't be counted for other configured rate limits|
|
||||
|
||||
A rate limit for a partial endpoint. Requests will be counted towards this limit if the request path contains the endpoint. For example request `/api/v2/test` will match when the partial endpoint limit is set for `/api/v2`.
|
||||
|
||||
### AddApiKeyLimit
|
||||
|Parameter|Description|
|
||||
|---------|-----------|
|
||||
|limit|The request weight limit per time period. Note that requests can have a weight specified. Default requests will have a weight of 1|
|
||||
|perTimePeriod|The time period over which the limit is enforced|
|
||||
|onlyForSignedRequests|Whether this rate limit should only be counter for signed/authenticated requests|
|
||||
|excludeFromTotalRateLimit|Whether requests counted for this rate limited should not be counted towards the total rate limit|
|
||||
|
||||
A rate limit for an API key. Requests with the same API key will be grouped and limited.
|
@ -1,65 +0,0 @@
|
||||
---
|
||||
title: Home
|
||||
nav_order: 1
|
||||
---
|
||||
|
||||
[](https://github.com/JKorf/CryptoExchange.Net/actions/workflows/dotnet.yml) [](https://www.nuget.org/packages/CryptoExchange.Net) [](https://www.nuget.org/packages/CryptoExchange.Net)
|
||||
|
||||
The CryptoExchange.Net library is a base package for exchange API implementations. It offers base classes for creating clients for exchange API's. Basing exchange implementation on the common CryptoExchange.Net library allows for ease of implementation for new exchanges, as only the endpoints and models have to implemented, but not all systems around requests and connections, and it makes it easier for users to implement a new library in their code base as all base principles and configuration are the same for different exchanges.
|
||||
|
||||
**Implementations by me**
|
||||
These will always be on the latest CryptoExchange.Net version and the latest versions will always work together
|
||||
|
||||
||Exchange|Documentation|
|
||||
|-|-|-|
|
||||
|<a href="https://github.com/JKorf/Binance.Net"><img src="https://github.com/JKorf/Binance.Net/blob/master/Binance.Net/Icon/icon.png?raw=true"></a>|Binance|https://jkorf.github.io/Binance.Net/|
|
||||
|<a href="https://github.com/JKorf/Bitfinex.Net"><img src="https://github.com/JKorf/Bitfinex.Net/blob/master/Bitfinex.Net/Icon/icon.png?raw=true"></a>|Bitfinex|https://jkorf.github.io/Bitfinex.Net/|
|
||||
|<a href="https://github.com/JKorf/Bitget.Net"><img src="https://github.com/JKorf/Bitget.Net/blob/master/Bitget.Net/Icon/icon.png?raw=true"></a>|Bitget|https://jkorf.github.io/Bitget.Net/|
|
||||
|<a href="https://github.com/JKorf/Bittrex.Net"><img src="https://github.com/JKorf/Bittrex.Net/blob/master/Bittrex.Net/Icon/icon.png?raw=true"></a>|Bittrex|https://jkorf.github.io/Bittrex.Net/|
|
||||
|<a href="https://github.com/JKorf/Bybit.Net"><img src="https://github.com/JKorf/Bybit.Net/blob/main/ByBit.Net/Icon/icon.png?raw=true"></a>|Bybit|https://jkorf.github.io/Bybit.Net/|
|
||||
|<a href="https://github.com/JKorf/CoinEx.Net"><img src="https://github.com/JKorf/CoinEx.Net/blob/master/CoinEx.Net/Icon/icon.png?raw=true"></a>|CoinEx|https://jkorf.github.io/CoinEx.Net/|
|
||||
|<a href="https://github.com/JKorf/Huobi.Net"><img src="https://github.com/JKorf/Huobi.Net/blob/master/Huobi.Net/Icon/icon.png?raw=true"></a>|Huobi|https://jkorf.github.io/Huobi.Net/|
|
||||
|<a href="https://github.com/JKorf/Kraken.Net"><img src="https://github.com/JKorf/Kraken.Net/blob/master/Kraken.Net/Icon/icon.png?raw=true"></a>|Kraken|https://jkorf.github.io/Kraken.Net/|
|
||||
|<a href="https://github.com/JKorf/Kucoin.Net"><img src="https://github.com/JKorf/Kucoin.Net/blob/master/Kucoin.Net/Icon/icon.png?raw=true"></a>|Kucoin|https://jkorf.github.io/Kucoin.Net/|
|
||||
|<a href="https://github.com/JKorf/OKX.Net"><img src="https://raw.githubusercontent.com/JKorf/OKX.Net/358d31f58d8ee51fc234bff1940878a8d0ce5676/Okex.Net/Icon/icon.png"></a>|OKX|https://jkorf.github.io/OKX.Net/|
|
||||
|
||||
**Implementations by third parties**
|
||||
These might not be compatible with other libraries, make sure to check the CryptoExchange.Net version.
|
||||
|
||||
||Exchange|
|
||||
|-|-|
|
||||
|<a href="https://github.com/Zaliro/Switcheo.Net"><img src="https://github.com/Zaliro/Switcheo.Net/blob/master/Resources/switcheo-coin.png?raw=true"></a>|Switcheo|
|
||||
|<a href="https://github.com/ridicoulous/LiquidQuoine.Net"><img src="https://github.com/ridicoulous/LiquidQuoine.Net/blob/master/Resources/icon.png?raw=true"></a>|Liquid|
|
||||
|<a href="https://github.com/ridicoulous/Bitmex.Net"><img src="https://github.com/ridicoulous/Bitmex.Net/blob/master/Bitmex.Net/Icon/icon.png?raw=true"></a>|Bitmex|
|
||||
|<a href="https://github.com/intelligences/HitBTC.Net"><img src="https://github.com/intelligences/HitBTC.Net/blob/master/src/HitBTC.Net/Icon/icon.png?raw=true"></a>|HitBTC|
|
||||
|<a href="https://github.com/EricGarnier/LiveCoin.Net"><img src="https://github.com/EricGarnier/LiveCoin.Net/blob/master/LiveCoin.Net/Icon/icon.png?raw=true"></a>|LiveCoin|
|
||||
|<a href="https://github.com/burakoner/Chiliz.Net"><img src="https://github.com/burakoner/Chiliz.Net/blob/master/Chiliz.Net/Icon/icon.png?raw=true"></a>|Chiliz|
|
||||
|<a href="https://github.com/burakoner/BtcTurk.Net"><img src="https://github.com/burakoner/BtcTurk.Net/blob/master/BtcTurk.Net/Icon/icon.png?raw=true"></a>|BtcTurk|
|
||||
|<a href="https://github.com/burakoner/Thodex.Net"><img src="https://github.com/burakoner/Thodex.Net/blob/master/Thodex.Net/Icon/icon.png?raw=true"></a>|Thodex|
|
||||
|<a href="https://github.com/d-ugarov/Exante.Net"><img src="https://github.com/d-ugarov/Exante.Net/blob/master/Exante.Net/Icon/icon.png?raw=true"></a>|Exante|
|
||||
|<a href="https://github.com/rodrigobelo/wootrade-dotnet"><img src="https://github.com/rodrigobelo/wootrade-dotnet/blob/main/wootrade-dotnet-icon.png?raw=true"></a>|Wootrade|
|
||||
|
||||
## Discord
|
||||
A Discord server is available [here](https://discord.gg/MSpeEtSY8t). Feel free to join for discussion and/or questions around the CryptoExchange.Net and implementation libraries.
|
||||
|
||||
## Support the project
|
||||
I develop and maintain this package on my own for free in my spare time, any support is greatly appreciated.
|
||||
|
||||
### Referral link
|
||||
Use one of the following following referral links to signup to a new exchange to pay a small percentage of the trading fees you pay to support the project instead of paying them straight to the exchange. This doesn't cost you a thing!
|
||||
[Binance](https://accounts.binance.com/en/register?ref=10153680)
|
||||
[Bitfinex](https://www.bitfinex.com/sign-up?refcode=kCCe-CNBO)
|
||||
[Bittrex](https://bittrex.com/discover/join?referralCode=TST-DJM-CSX)
|
||||
[Bybit](https://partner.bybit.com/b/jkorf)
|
||||
[CoinEx](https://www.coinex.com/register?refer_code=hd6gn)
|
||||
[Huobi](https://www.huobi.com/en-us/v/register/double-invite/?inviter_id=11343840&invite_code=fxp93)
|
||||
[Kucoin](https://www.kucoin.com/ucenter/signup?rcode=RguMux)
|
||||
|
||||
### Donate
|
||||
Make a one time donation in a crypto currency of your choice. If you prefer to donate a currency not listed here please contact me.
|
||||
|
||||
**Btc**: bc1qz0jv0my7fc60rxeupr23e75x95qmlq6489n8gh
|
||||
**Eth**: 0x8E21C4d955975cB645589745ac0c46ECA8FAE504
|
||||
|
||||
### Sponsor
|
||||
Alternatively, sponsor me on Github using [Github Sponsors](https://github.com/sponsors/JKorf).
|
@ -1,44 +0,0 @@
|
||||
---
|
||||
title: Home
|
||||
nav_order: 1
|
||||
---
|
||||
|
||||
[](https://github.com/JKorf/CryptoExchange.Net/actions/workflows/dotnet.yml) [](https://www.nuget.org/packages/CryptoExchange.Net) [](https://www.nuget.org/packages/CryptoExchange.Net)
|
||||
|
||||
CryptoExchange.Net is a base library which is used to implement different cryptocurrency (exchange) API's. It provides a standardized way of implementing different API's, which results in a very similar experience for users of the API implementations.
|
||||
|
||||
[Quick Start](https://jkorf.github.io/CryptoExchange.Net/QuickStart.html)
|
||||
|
||||
-- list different links here
|
||||
|
||||
### Current implementations
|
||||
The following API's are directly supported. Note that there are 3rd party implementations going around, but only these are created and supported by me:
|
||||
|
||||
|Exchange|Repository|Nuget|
|
||||
|--|--|--|
|
||||
|Binance|[JKorf/Binance.Net](https://github.com/JKorf/Binance.Net)|[](https://www.nuget.org/packages/Binance.Net)|
|
||||
|Bitfinex|[JKorf/Bitfinex.Net](https://github.com/JKorf/Bitfinex.Net)|[](https://www.nuget.org/packages/Bitfinex.Net)|
|
||||
|Bitget|[JKorf/Bitget.Net](https://github.com/JKorf/Bitget.Net)|[](https://www.nuget.org/packages/Bitget.Net)|
|
||||
|Bybit|[JKorf/Bybit.Net](https://github.com/JKorf/Bybit.Net)|[](https://www.nuget.org/packages/Bybit.Net)|
|
||||
|CoinEx|[JKorf/CoinEx.Net](https://github.com/JKorf/CoinEx.Net)|[](https://www.nuget.org/packages/CoinEx.Net)|
|
||||
|CoinGecko|[JKorf/CoinGecko.Net](https://github.com/JKorf/CoinGecko.Net)|[](https://www.nuget.org/packages/CoinGecko.Net)|
|
||||
|Huobi/HTX|[JKorf/Huobi.Net](https://github.com/JKorf/Huobi.Net)|[](https://www.nuget.org/packages/Huobi.Net)|
|
||||
|Kraken|[JKorf/Kraken.Net](https://github.com/JKorf/Kraken.Net)|[](https://www.nuget.org/packages/KrakenExchange.Net)|
|
||||
|Kucoin|[JKorf/Kucoin.Net](https://github.com/JKorf/Kucoin.Net)|[](https://www.nuget.org/packages/Kucoin.Net)|
|
||||
|Mexc|[JKorf/Mexc.Net](https://github.com/JKorf/Mexc.Net)|[](https://www.nuget.org/packages/JK.Mexc.Net)|
|
||||
|OKX|[JKorf/OKX.Net](https://github.com/JKorf/OKX.Net)|[](https://www.nuget.org/packages/JK.OKX.Net)|
|
||||
|
||||
## Discord
|
||||
A Discord server is available [here](https://discord.gg/MSpeEtSY8t). Feel free to join for discussion and/or questions around the CryptoExchange.Net and implementation libraries.
|
||||
|
||||
## Support the project
|
||||
I develop and maintain this package on my own for free in my spare time, any support is greatly appreciated.
|
||||
|
||||
### Donate
|
||||
Make a one time donation in a crypto currency of your choice. If you prefer to donate a currency not listed here please contact me.
|
||||
|
||||
**Btc**: bc1qz0jv0my7fc60rxeupr23e75x95qmlq6489n8gh
|
||||
**Eth**: 0x8E21C4d955975cB645589745ac0c46ECA8FAE504
|
||||
|
||||
### Sponsor
|
||||
Alternatively, sponsor me on Github using [Github Sponsors](https://github.com/sponsors/JKorf).
|
Binary file not shown.
Before Width: | Height: | Size: 1.4 KiB After Width: | Height: | Size: 2.1 KiB |
514
docs/index.html
514
docs/index.html
@ -4,7 +4,7 @@
|
||||
<meta charset="UTF-8" />
|
||||
<meta http-equiv="X-UA-Compatible" content="IE=edge">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1, minimum-scale=1.0, shrink-to-fit=no">
|
||||
<link href="assets/images/favicon.png" rel="icon" />
|
||||
<link href="assets/images/logo.png" rel="icon" />
|
||||
<title>CryptoExchange.Net Documentation</title>
|
||||
<meta name="description" content="Documentation of the CryptoExchange.Net and client implementation libraries">
|
||||
<meta name="author" content="JKorf">
|
||||
@ -94,7 +94,7 @@
|
||||
<li class="nav-item"><a class="nav-link" href="#idocs_options_def">Option definitions</a></li>
|
||||
</ul>
|
||||
</li>
|
||||
<li class="nav-item"><a class="nav-link" href="#idocs_features">Features</a>
|
||||
<li class="nav-item"><a class="nav-link" href="#idocs_features">Additional Features</a>
|
||||
<ul class="nav flex-column">
|
||||
<li class="nav-item"><a class="nav-link" href="#idocs_orderbooks">Orderbooks</a></li>
|
||||
<li class="nav-item"><a class="nav-link" href="#idocs_logging">Logging</a></li>
|
||||
@ -265,6 +265,7 @@
|
||||
<p>
|
||||
All client libraries support and encourage usage via the Dotnet dependency injection system. Add all necesary services by calling the <code>Add[Library]();</code> extension method on the service collection. <a href="#idocs_options_set">Options</a> for the clients can be passed as parameters.
|
||||
</p>
|
||||
<div class="alert alert-info">Using the dependecy injection mechanism also makes sure the HttpClient is used correctly</div>
|
||||
<ul class="nav nav-tabs" id="di" role="tablist" style="margin-bottom: -16px;">
|
||||
<li class="nav-item" role="presentation">
|
||||
<a class="nav-link active" id="di-binance-tab" data-toggle="tab" href="#di-binance" role="tab" aria-controls="di-binance" aria-selected="true">Binance</a>
|
||||
@ -1182,7 +1183,36 @@ await client.UnsubscribeAllAsync();</code></pre>
|
||||
============================ -->
|
||||
<section id="idocs_common">
|
||||
<h2>Common Clients</h2>
|
||||
<p>CryptoExchange.Net exposes some common clients. These clients aim to make using the different API's easier.</p>
|
||||
|
||||
<p><b>(I)CryptoRestClient</b><br />
|
||||
The <code>ICryptoRestClient</code> (or <code>CryptoRestClient</code> when used directly) can be used to easily access REST clients for different API's through the different packages that have been installed. Each package adds it extension method to the interface, which allows the user to access the clients via it.
|
||||
</p>
|
||||
<p>
|
||||
For example, having the Binance, Bybit and Kucoin packages installed allows you to use it like this:
|
||||
<pre><code>var cryptoRestClient = new CryptoRestClient(); // Either construct it or inject the ICryptoRestClient into your service
|
||||
var binanceTicker = await cryptoRestClient.Binance().SpotApi.ExchangeData.GetTickersAsync();
|
||||
var bybitTicker = await cryptoRestClient.Bybit().V5Api.ExchangeData.GetTickers();
|
||||
var kucoinTicker = await cryptoRestClient.Kucoin().SpotApi.ExchangeData.GetTickers();</code></pre>
|
||||
</p>
|
||||
|
||||
<p><b>(I)CryptoSocketClient</b><br />
|
||||
Similarly as the (I)CryptoRestClient this client allows you to access the different Websocket clients through a single access point.
|
||||
</p>
|
||||
<p>Having the Bitget, Kraken and OKX packages installed would allow you to use it like this:
|
||||
<pre><code>var cryptoRestClient = new CryptoSocketClient(); // Either construct it or inject the ICryptoRestClient into your service
|
||||
var bitgetSub = await cryptoRestClient.Bitget().SpotApi.SubscribeToTickerUpdatesAsync("ETHUSDT", data => {});
|
||||
var krakenSub = await cryptoRestClient.Kraken().SpotApi.SubscribeToTickerUpdatesAsync("ETH/USD", data => {});
|
||||
var okxSub = await cryptoRestClient.OKX().UnifiedApi.ExchangeData.SubscribeToTickerUpdatesAsync("ETH-USDT", data => {});</code></pre>
|
||||
</p>
|
||||
|
||||
<p><b>ISpotClient</b><br />
|
||||
The ISpotClient is a REST API client interface implemented by each library which implements a Spot trading API. It provided a common way of doing basic operations on the Spot market, for example getting ticker or trade data, but also placing and retrieving orders. Because this interface is implemented for each exchange with a Spot market the interface is relatively basic, only exposing methods that are supported by all the APIs.
|
||||
</p>
|
||||
<p>
|
||||
The ISpotClient is added to the service collection when using <a href="#idocs_di">dependency injection</a>. Alternatively it can be accessed for a specific client by calling the `CommonSpotClient` property on the Spot sub-API of a client:
|
||||
<pre><code>var spotClient = restClient.SpotApi.CommonSpotClient;</code></pre>
|
||||
</p>
|
||||
</section>
|
||||
|
||||
<hr class="divider">
|
||||
@ -1751,7 +1781,7 @@ var client = new OKXRestClient();</code></pre>
|
||||
<!-- Getting Started
|
||||
============================ -->
|
||||
<section id="idocs_features">
|
||||
<h1>Features</h1>
|
||||
<h1>Additional Features</h1>
|
||||
|
||||
</section>
|
||||
|
||||
@ -1761,7 +1791,273 @@ var client = new OKXRestClient();</code></pre>
|
||||
============================ -->
|
||||
<section id="idocs_orderbooks">
|
||||
<h2>Orderbooks</h2>
|
||||
<p>
|
||||
Each client library provides an local orderbook implementation. These implementations will provide a client side orderbook, take care of synchronization with the server, and handle reconnecting and resynchronizing in case of a dropped connection.
|
||||
Orderbook implementations use the following naming convention: <code>[ExchangeName][(Type)]SymbolOrderBook</code>
|
||||
</p>
|
||||
<p>
|
||||
<b>Creation and starting</b><br />
|
||||
The order book implementations can be created directly, or can be instantiated via the <code>I[Exchange]OrderBookFactory</code> factory. After creation the synchronization can be started by calling the <code>StartAync</code> method
|
||||
</p>
|
||||
|
||||
<ul class="nav nav-tabs" id="book" role="tablist" style="margin-bottom: -16px;">
|
||||
<li class="nav-item" role="presentation">
|
||||
<a class="nav-link active" id="book-binance-tab" data-toggle="tab" href="#book-binance" role="tab" aria-controls="book-binance" aria-selected="true">Binance</a>
|
||||
</li>
|
||||
<li class="nav-item" role="presentation">
|
||||
<a class="nav-link" id="book-bitfinex-tab" data-toggle="tab" href="#book-bitfinex" role="tab" aria-controls="book-bitfinex" aria-selected="false">Bitfinex</a>
|
||||
</li>
|
||||
<li class="nav-item" role="presentation">
|
||||
<a class="nav-link" id="book-bitget-tab" data-toggle="tab" href="#book-bitget" role="tab" aria-controls="book-bitget" aria-selected="false">Bitget</a>
|
||||
</li>
|
||||
<li class="nav-item" role="presentation">
|
||||
<a class="nav-link" id="book-bybit-tab" data-toggle="tab" href="#book-bybit" role="tab" aria-controls="book-bybit" aria-selected="false">Bybit</a>
|
||||
</li>
|
||||
<li class="nav-item" role="presentation">
|
||||
<a class="nav-link" id="book-coinex-tab" data-toggle="tab" href="#book-coinex" role="tab" aria-controls="book-coinex" aria-selected="false">Coinex</a>
|
||||
</li>
|
||||
<li class="nav-item" role="presentation">
|
||||
<a class="nav-link" id="book-huobi-tab" data-toggle="tab" href="#book-huobi" role="tab" aria-controls="book-huobi" aria-selected="false">Huobi</a>
|
||||
</li>
|
||||
<li class="nav-item" role="presentation">
|
||||
<a class="nav-link" id="book-kraken-tab" data-toggle="tab" href="#book-kraken" role="tab" aria-controls="book-kraken" aria-selected="false">Kraken</a>
|
||||
</li>
|
||||
<li class="nav-item" role="presentation">
|
||||
<a class="nav-link" id="book-kucoin-tab" data-toggle="tab" href="#book-kucoin" role="tab" aria-controls="book-kucoin" aria-selected="false">Kucoin</a>
|
||||
</li>
|
||||
<li class="nav-item" role="presentation">
|
||||
<a class="nav-link" id="book-mexc-tab" data-toggle="tab" href="#book-mexc" role="tab" aria-controls="book-mexc" aria-selected="false">Mexc</a>
|
||||
</li>
|
||||
<li class="nav-item" role="presentation">
|
||||
<a class="nav-link" id="book-okx-tab" data-toggle="tab" href="#book-okx" role="tab" aria-controls="book-okx" aria-selected="false">OKX</a>
|
||||
</li>
|
||||
</ul>
|
||||
<div class="tab-content my-3" id="myTabContent">
|
||||
<div class="tab-pane fade show active" id="book-binance" role="tabpanel" aria-labelledby="book-binance-tab">
|
||||
<pre><code>var book = new BinanceSpotSymbolOrderBook("ETHUSDT");
|
||||
var startResult = await book.StartAsync();
|
||||
if (!startResult.Success)
|
||||
{
|
||||
// Handle error, error info available in startResult.Error
|
||||
}
|
||||
// Book has successfully started and synchronized
|
||||
|
||||
// Once no longer needed you can stop the live sync functionality by calling StopAsync()
|
||||
await book.StopAsync();
|
||||
</code></pre>
|
||||
</div>
|
||||
<div class="tab-pane fade" id="book-bitfinex" role="tabpanel" aria-labelledby="book-bitfinex-tab">
|
||||
<pre><code>var book = new BitfinexSymbolOrderBook("tETHUSD");
|
||||
var startResult = await book.StartAsync();
|
||||
if (!startResult.Success)
|
||||
{
|
||||
// Handle error, error info available in startResult.Error
|
||||
}
|
||||
// Book has successfully started and synchronized
|
||||
|
||||
// Once no longer needed you can stop the live sync functionality by calling StopAsync()
|
||||
await book.StopAsync();
|
||||
</code></pre>
|
||||
</div>
|
||||
<div class="tab-pane fade" id="book-bitget" role="tabpanel" aria-labelledby="book-bitget-tab">
|
||||
<pre><code>var book = new BitgetSpotSymbolOrderBook("ETHUSDT");
|
||||
var startResult = await book.StartAsync();
|
||||
if (!startResult.Success)
|
||||
{
|
||||
// Handle error, error info available in startResult.Error
|
||||
}
|
||||
// Book has successfully started and synchronized
|
||||
|
||||
// Once no longer needed you can stop the live sync functionality by calling StopAsync()
|
||||
await book.StopAsync();
|
||||
</code></pre>
|
||||
</div>
|
||||
<div class="tab-pane fade" id="book-bybit" role="tabpanel" aria-labelledby="book-bybit-tab">
|
||||
<pre><code>var book = new BybitSymbolOrderBook("ETHUSDT", Category.Spot);
|
||||
var startResult = await book.StartAsync();
|
||||
if (!startResult.Success)
|
||||
{
|
||||
// Handle error, error info available in startResult.Error
|
||||
}
|
||||
// Book has successfully started and synchronized
|
||||
|
||||
// Once no longer needed you can stop the live sync functionality by calling StopAsync()
|
||||
await book.StopAsync();
|
||||
</code></pre>
|
||||
</div>
|
||||
<div class="tab-pane fade" id="book-coinex" role="tabpanel" aria-labelledby="book-coinex-tab">
|
||||
<pre><code>var book = new CoinExSpotSymbolOrderBook("ETHUSDT");
|
||||
var startResult = await book.StartAsync();
|
||||
if (!startResult.Success)
|
||||
{
|
||||
// Handle error, error info available in startResult.Error
|
||||
}
|
||||
// Book has successfully started and synchronized
|
||||
|
||||
// Once no longer needed you can stop the live sync functionality by calling StopAsync()
|
||||
await book.StopAsync();
|
||||
</code></pre>
|
||||
</div>
|
||||
<div class="tab-pane fade" id="book-huobi" role="tabpanel" aria-labelledby="book-huobi-tab">
|
||||
<pre><code>var book = new HuobiSpotSymbolOrderBook("ethusdt");
|
||||
var startResult = await book.StartAsync();
|
||||
if (!startResult.Success)
|
||||
{
|
||||
// Handle error, error info available in startResult.Error
|
||||
}
|
||||
// Book has successfully started and synchronized
|
||||
|
||||
// Once no longer needed you can stop the live sync functionality by calling StopAsync()
|
||||
await book.StopAsync();
|
||||
</code></pre>
|
||||
</div>
|
||||
<div class="tab-pane fade" id="book-kraken" role="tabpanel" aria-labelledby="book-kraken-tab">
|
||||
<pre><code>var book = new KrakenSpotSymbolOrderBook("ETH/USD");
|
||||
var startResult = await book.StartAsync();
|
||||
if (!startResult.Success)
|
||||
{
|
||||
// Handle error, error info available in startResult.Error
|
||||
}
|
||||
// Book has successfully started and synchronized
|
||||
|
||||
// Once no longer needed you can stop the live sync functionality by calling StopAsync()
|
||||
await book.StopAsync();
|
||||
</code></pre>
|
||||
</div>
|
||||
<div class="tab-pane fade" id="book-kucoin" role="tabpanel" aria-labelledby="book-kucoin-tab">
|
||||
<pre><code>var book = new KucoinSpotSymbolOrderBook("ETH-USDT");
|
||||
var startResult = await book.StartAsync();
|
||||
if (!startResult.Success)
|
||||
{
|
||||
// Handle error, error info available in startResult.Error
|
||||
}
|
||||
// Book has successfully started and synchronized
|
||||
|
||||
// Once no longer needed you can stop the live sync functionality by calling StopAsync()
|
||||
await book.StopAsync();
|
||||
</code></pre>
|
||||
</div>
|
||||
<div class="tab-pane fade" id="book-mexc" role="tabpanel" aria-labelledby="book-mexc-tab">
|
||||
<pre><code>var book = new MexcSpotSymbolOrderBook("ETHUSDT");
|
||||
var startResult = await book.StartAsync();
|
||||
if (!startResult.Success)
|
||||
{
|
||||
// Handle error, error info available in startResult.Error
|
||||
}
|
||||
// Book has successfully started and synchronized
|
||||
|
||||
// Once no longer needed you can stop the live sync functionality by calling StopAsync()
|
||||
await book.StopAsync();
|
||||
</code></pre>
|
||||
</div>
|
||||
<div class="tab-pane fade" id="book-okx" role="tabpanel" aria-labelledby="book-okx-tab">
|
||||
<pre><code>var book = new OKXSymbolOrderBook("ETH-USDT");
|
||||
var startResult = await book.StartAsync();
|
||||
if (!startResult.Success)
|
||||
{
|
||||
// Handle error, error info available in startResult.Error
|
||||
}
|
||||
// Book has successfully started and synchronized
|
||||
|
||||
// Once no longer needed you can stop the live sync functionality by calling StopAsync()
|
||||
await book.StopAsync();
|
||||
</code></pre>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<p>
|
||||
<b>The order book object</b><br />
|
||||
The order book implementations for each client use a common base class and interface, which means you can use books from different exchanges in a common function.
|
||||
<pre><code>// Create order books for different exchanges
|
||||
var books = new List<ISymbolOrderBook>();
|
||||
books.Add(new BinanceSpotSymbolOrderBook("ETHUSDT"));
|
||||
books.Add(new BitgetSpotSymbolOrderBook("ETHUSDT"));
|
||||
books.Add(new KrakenSpotSymbolOrderBook("ETH/USD"));
|
||||
books.Add(new KucoinSpotSymbolOrderBook("ETH-USDT"));
|
||||
books.Add(new OKXSymbolOrderBook("ETH-USDT"));
|
||||
|
||||
// Start the books
|
||||
var results = await Task.WhenAll(books.Select(b => b.StartAsync()));
|
||||
|
||||
// Output the current best ask/bid for each exchange
|
||||
foreach (var book in books.Where(b => b.Status == OrderBookStatus.Synced))
|
||||
{
|
||||
Console.WriteLine(book.Id);
|
||||
Console.WriteLine($"{book.BestAsk} - {book.BestBid}");
|
||||
}
|
||||
</code></pre>
|
||||
</p>
|
||||
<p>
|
||||
The following properties and events are exposed by the order books:
|
||||
<table class="table table-bordered">
|
||||
<tr><th>Field</th><th>Description</th></tr>
|
||||
<tr>
|
||||
<td>Status</td>
|
||||
<td>The current status of the order book. Note that the book is only acurate and up to date when the status is <code>Synced</code></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>Id</td>
|
||||
<td>Identifier for the book, referencing the exchange it's for</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>Symbol</td>
|
||||
<td>The symbol the book is for</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>LastSequenceNumber</td>
|
||||
<td>The last sequence number that was processed. Order book update messages typically have sequence numbers to correctly sync the book</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>UpdateTime</td>
|
||||
<td>Timestamp of the last update that was processed</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>AskCount</td>
|
||||
<td>The current number of asks in the book</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>BidCount</td>
|
||||
<td>The current number of bids in the book</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>Asks</td>
|
||||
<td>Returns a snapshot of the current list of asks. This creates a copy of the list values at that moment</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>Bids</td>
|
||||
<td>Returns a snapshot of the current list of bids. This creates a copy of the list values at that moment</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>Book</td>
|
||||
<td>Returns a snapshot of the both the asks and bids. This makes sure the snapshot of the bids and asks are of the same exact time</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>BestAsk</td>
|
||||
<td>The best ask at that moment</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>BestBid</td>
|
||||
<td>The best bid at that moment</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>BestOffers</td>
|
||||
<td>The best bid and best ask at that moment</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>OnStatusChange</td>
|
||||
<td>Event called when the status of the order book changes</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>OnOrderBookUpdate</td>
|
||||
<td>Event called whenever the book changes. Note that this event can trigger a lot on large/high liquidity markets</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>OnBestOffersChanged</td>
|
||||
<td>Event called whenever the best ask or bid changes in the order book</td>
|
||||
</tr>
|
||||
</table>
|
||||
|
||||
</p>
|
||||
</section>
|
||||
|
||||
<hr class="divider">
|
||||
@ -1770,15 +2066,76 @@ var client = new OKXRestClient();</code></pre>
|
||||
<section id="idocs_logging">
|
||||
<h2>Logging</h2>
|
||||
|
||||
<p>
|
||||
The library provides extensive logging, which depends on the dotnet `Microsoft.Extensions.Logging.ILogger` interface. This should provide ease of use when connecting the library logging to your existing logging implementation.
|
||||
</p>
|
||||
<p>
|
||||
When using the Dotnet dependency injection the logging configuration will de determined by the ILogger configuration.
|
||||
</p>
|
||||
|
||||
<p>
|
||||
<b>External log providers</b><br />
|
||||
External logging libraries can be configured as expected. For example Serilog:
|
||||
<pre><code>using Binance.Net;
|
||||
using Serilog;
|
||||
|
||||
Log.Logger = new LoggerConfiguration()
|
||||
.MinimumLevel.Debug()
|
||||
.WriteTo.Console()
|
||||
.CreateLogger();
|
||||
|
||||
var builder = WebApplication.CreateBuilder(args);
|
||||
builder.Services.AddBinance();
|
||||
builder.Host.UseSerilog();
|
||||
var app = builder.Build();
|
||||
|
||||
// startup
|
||||
|
||||
app.Run();</code></pre>
|
||||
</p>
|
||||
|
||||
<p>
|
||||
<b>Logging without dependency injection</b><br />
|
||||
A LoggerFactory instance can be provided to the client to configure the logging
|
||||
<pre><code>var logFactory = new LoggerFactory();
|
||||
logFactory.AddProvider(new ConsoleLoggerProvider());
|
||||
var binanceClient = new BinanceRestClient(new HttpClient(), logFactory, options => { });</code></pre>
|
||||
</p>
|
||||
</section>
|
||||
|
||||
<hr class="divider">
|
||||
|
||||
<!-- Layout
|
||||
============================ -->
|
||||
<section id="idocs_logging">
|
||||
<section id="idocs_ratelimiting">
|
||||
<h2>Ratelimiting</h2>
|
||||
<p>
|
||||
The client libraries have build in rate limiting. These rate limits can be configured per client. Some client implementations where the exchange has clear rate limits will also have a default rate limiter already set up.
|
||||
Rate limiting is configured in the client options, and can be set on a specific client or for all clients by either providing it in the constructor for a client, or by using the <code>SetDefaultOptions</code> on a client.
|
||||
|
||||
<div class="alert alert-info">What to do when a limit is reached can be configured with the <code>RateLimitingBehaviour</code> client options, either <code>Fail</code> or <code>Wait</code>.</div>
|
||||
|
||||
</p>
|
||||
<p>
|
||||
<b>Ratelimit configuration</b><br />
|
||||
A rate limiter can be configured in the options like so:
|
||||
<pre><code>new ClientOptions
|
||||
{
|
||||
RateLimitingBehaviour = RateLimitingBehaviour.Wait,
|
||||
RateLimiters = new List<IRateLimiter>
|
||||
{
|
||||
new RateLimiter()
|
||||
.AddTotalRateLimit(50, TimeSpan.FromSeconds(10))
|
||||
}
|
||||
}</code></pre>
|
||||
|
||||
This will add a rate limiter for 50 requests per 10 seconds.
|
||||
A rate limiter can have multiple limits:
|
||||
<pre><code>new RateLimiter()
|
||||
.AddTotalRateLimit(50, TimeSpan.FromSeconds(10))
|
||||
.AddEndpointLimit("/api/order", 10, TimeSpan.FromSeconds(2))</code></pre>
|
||||
This adds another limit of 10 requests per 2 seconds for the order endpoint in addition to the 50 requests per 10 seconds limit.
|
||||
</p>
|
||||
</section>
|
||||
|
||||
<hr class="divider">
|
||||
@ -1787,12 +2144,32 @@ var client = new OKXRestClient();</code></pre>
|
||||
============================ -->
|
||||
<section id="idocs_examples">
|
||||
<h1>Examples</h1>
|
||||
<p>See also the Examples folder in the <a href="https://github.com/JKorf/CryptoExchange.Net/tree/master/Examples">source</a></p>
|
||||
|
||||
<pre><code class="language-csharp">
|
||||
using Binance.Net;
|
||||
<b>Minimal API</b><br />
|
||||
<p>A minimal API example allowing the retrieval of ticker information for a specific exchange and symbol. This API returns ticker information for the following paths<br />
|
||||
<code>/Ticker/Bitfinex/ETH/BTC</code><br />
|
||||
<code>/Ticker/Bitget/ETH/BTC</code><br />
|
||||
<code>/Ticker/Kraken/ETH/BTC</code><br />
|
||||
|
||||
var client = new BinanceRestClient();
|
||||
client.SpotApi.Exchange.GetExchangeInfoAsync();
|
||||
</p>
|
||||
<pre><code class="language-csharp">using CryptoExchange.Net.Interfaces;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
var builder = WebApplication.CreateBuilder(args);
|
||||
builder.Services.AddBitfinex();
|
||||
builder.Services.AddBitget();
|
||||
builder.Services.AddKraken();
|
||||
var app = builder.Build();
|
||||
|
||||
app.MapGet("Ticker/{exchange}/{baseAsset}/{quoteAsset}", async ([FromServices] ICryptoRestClient client, string exchange, string baseAsset, string quoteAsset) =>
|
||||
{
|
||||
var spotClient = client.SpotClient(exchange)!;
|
||||
var result = await spotClient.GetTickerAsync(spotClient.GetSymbolName(baseAsset, quoteAsset));
|
||||
return result.Data;
|
||||
});
|
||||
|
||||
app.Run();
|
||||
</pre></code>
|
||||
|
||||
|
||||
@ -1805,6 +2182,74 @@ client.SpotApi.Exchange.GetExchangeInfoAsync();
|
||||
<section id="idocs_glossary">
|
||||
<h2>Glossary</h2>
|
||||
|
||||
<table class="table table-bordered">
|
||||
<tr><th>Definition</th><th>Synonyms</th><th>Meaning</th></tr>
|
||||
<tr>
|
||||
<td>Symbol</td>
|
||||
<td>Market, Pair</td>
|
||||
<td>An asset pair on which can be traded, for example <code>BTC-ETH</code></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>Asset</td>
|
||||
<td>Currency, Coin</td>
|
||||
<td>A coin for which you can hold balance and which makes up Symbols. For example both <code>BTC</code>, <code>ETH</code> or <code>USD</code></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>Trade</td>
|
||||
<td>Execution, Fill</td>
|
||||
<td>The (partial) execution of an order. Orders can have multiple trades</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>Quantity</td>
|
||||
<td>Amount, Size</td>
|
||||
<td>The amount of asset</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>QuoteQuantity</td>
|
||||
<td>Value</td>
|
||||
<td>The amount of quote asset</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>Fee</td>
|
||||
<td>Commission</td>
|
||||
<td>The fee paid for an order, trade or withdrawal</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>Kline</td>
|
||||
<td>Candlestick, OHLC</td>
|
||||
<td>K-line data, used for candlestick charts. Contains Open/High/Low/Close/Volume</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>KlineInterval</td>
|
||||
<td>Period</td>
|
||||
<td>The time period of a single kline</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>Open order</td>
|
||||
<td>Active order, Unexecuted order</td>
|
||||
<td>An order which has not yet been fully filled</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>Closed order</td>
|
||||
<td>Completed order, executed order</td>
|
||||
<td>An order which is no longer active. Can be canceled or fully filled</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>Network</td>
|
||||
<td>Chain</td>
|
||||
<td>The network of an asset. For example <code>ETH</code> allows multiple networks like <code>ERC20</code> and <code>BEP2</code></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>Orderbook</td>
|
||||
<td>Market depth</td>
|
||||
<td>A list of (the top rows of) the current best bids and asks</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>Ticker</td>
|
||||
<td>Stats</td>
|
||||
<td>Statistics over the last 24 hours</td>
|
||||
</tr>
|
||||
</table>
|
||||
</section>
|
||||
|
||||
<hr class="divider">
|
||||
@ -1813,8 +2258,59 @@ client.SpotApi.Exchange.GetExchangeInfoAsync();
|
||||
============================ -->
|
||||
<section id="idocs_faq">
|
||||
<h2>FAQ</h2>
|
||||
<b>I occasionally get a NullReferenceException, what's wrong?</b><br />
|
||||
<p>
|
||||
You probably don't check the result status of a call and just assume the data is always there. <code>NullReferenceExecption</code> will happen when you have code like this <code>var symbol = client.GetTickersAync().Result.Data.Symbol</code> because the <code>Data</code> property is null when the call fails. Instead check if the call is successful:
|
||||
<pre><code>var tickerResult = await client.SpotApi.ExchangeData.GetTickersAync();
|
||||
if(!tickerResult.Success)
|
||||
{
|
||||
// Handle error
|
||||
}
|
||||
else
|
||||
{
|
||||
// Handle result, it is now safe to access the Data property
|
||||
var symbol = tickerResult.Data.Symbol;
|
||||
}</code></pre>
|
||||
</p>
|
||||
|
||||
</div>
|
||||
<b>The socket client stops sending updates after a little while</b><br />
|
||||
<p>
|
||||
You probably didn't keep a reference to the socket client and it got disposed.
|
||||
<pre><code>// WRONG
|
||||
private void SomeMethod()
|
||||
{
|
||||
var socketClient = new BinanceSocketClient();
|
||||
socketClient.Spot.SubscribeToOrderBookUpdatesAsync("BTCUSDT", data => {
|
||||
// Handle data
|
||||
});
|
||||
}</code></pre>
|
||||
<pre><code>// RIGHT
|
||||
private BinanceSocketClient _socketClient = new BinanceSocketClient();
|
||||
|
||||
// .. rest of the class
|
||||
|
||||
private void SomeMethod()
|
||||
{
|
||||
_socketClient.Spot.SubscribeToOrderBookUpdates("BTCUSDT", data => {
|
||||
// Handle data
|
||||
});
|
||||
}</code></pre>
|
||||
</p>
|
||||
|
||||
<b>Can I use the TestNet/US/other API with this library?</b><br />
|
||||
<p>
|
||||
Yes, generally these are all supported and can be configured by setting the Environment in the client options. Some known environments should be available in the [Exchange]Environment class:
|
||||
<pre><code>// Change environment to test
|
||||
var client = new BinanceRestClient(options =>
|
||||
{
|
||||
options.Environment = BinanceEnvironment.Testnet;
|
||||
});</code></pre>
|
||||
</p>
|
||||
|
||||
<b>How are timestamps handled?</b><br />
|
||||
<p>
|
||||
Exchange API's treat all timestamps as UTC, both incoming and outgoing. The client libraries do no conversion so received timestamps are always in UTC. When sending requests make sure to use UTC time as well.
|
||||
</p>
|
||||
|
||||
</section>
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user