mirror of
https://github.com/JKorf/CryptoExchange.Net.git
synced 2026-08-11 08:22:53 +00:00
Docs
+37
@@ -16,6 +16,7 @@ The rest client gives access to the Rest endpoint of the API. Rest endpoints are
|
||||
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*
|
||||
````C#
|
||||
var client = new KucoinClient();
|
||||
var tickersResult = kucoinClient.SpotApi.ExchangeData.GetTickersAsync();
|
||||
````
|
||||
|
||||
@@ -94,6 +95,8 @@ await kucoinSocketClient.SpotStreams.SubscribeToAllTickerUpdatesAsync(updateData
|
||||
|
||||
All updates are wrapped in a `DataEvent<>` object, which contain a `Timestamp`, `OriginalData`, `Topic`, and a `Data` property. The `Timestamp` is the timestamp when the data was received (not send!). `OriginalData` will contain the originally received 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. The `Data` property contains the received update data.
|
||||
|
||||
*[WARNING] Do not use `using` statements in combination with constructing a `SocketClient`. 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 was as the [rest client](#processing-request-responses). The `UpdateSubscription` object can be used to listen for connection events of the socket connection.
|
||||
````C#
|
||||
@@ -115,3 +118,37 @@ subscriptionResult.Data.ConnectionRestored += (time) =>
|
||||
|
||||
````
|
||||
|
||||
### 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:
|
||||
````C#
|
||||
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.
|
||||
````C#
|
||||
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:
|
||||
````C#
|
||||
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.
|
||||
|
||||
|
||||
|
||||
|
||||
+41
@@ -0,0 +1,41 @@
|
||||
### 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:
|
||||
````C#
|
||||
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:
|
||||
````C#
|
||||
private void SomeMethod()
|
||||
{
|
||||
var socketClient = new BinanceSocketClient();
|
||||
socketClient.Spot.SubscribeToOrderBookUpdates("BTCUSDT", data => {
|
||||
// Handle data
|
||||
});
|
||||
}
|
||||
````
|
||||
Subscribe like this:
|
||||
````C#
|
||||
private BinanceSocketClient _socketClient = new BinanceSocketClient();
|
||||
|
||||
// .. rest of the class
|
||||
|
||||
private void SomeMethod()
|
||||
{
|
||||
_socketClient.Spot.SubscribeToOrderBookUpdates("BTCUSDT", data => {
|
||||
// Handle data
|
||||
});
|
||||
}
|
||||
|
||||
````
|
||||
+17
-1
@@ -1 +1,17 @@
|
||||
WIP
|
||||
The CryptoExchange.Net library is a base package for exchange API implementations.
|
||||
|
||||
[Client usage](https://github.com/JKorf/CryptoExchange.Net/wiki/Clients)
|
||||
|
||||
[Client options](https://github.com/JKorf/CryptoExchange.Net/wiki/Options)
|
||||
|
||||
[Configure logging](https://github.com/JKorf/CryptoExchange.Net/wiki/Logging)
|
||||
|
||||
[Order book implementations](https://github.com/JKorf/CryptoExchange.Net/wiki/Orderbooks)
|
||||
|
||||
[Common interfaces](https://github.com/JKorf/CryptoExchange.Net/wiki/Interfaces)
|
||||
|
||||
[Implementing a new exchange](https://github.com/JKorf/CryptoExchange.Net/wiki/Implementations)
|
||||
|
||||
[Glossary](https://github.com/JKorf/CryptoExchange.Net/wiki/Glossary)
|
||||
|
||||
[FAQ](https://github.com/JKorf/CryptoExchange.Net/wiki/FAQ)
|
||||
|
||||
+44
@@ -198,3 +198,47 @@ var client = new BinanceClient(new BinanceClientOptions
|
||||
});
|
||||
|
||||
````
|
||||
|
||||
## Provide 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*
|
||||
````C#
|
||||
var client = new BinanceClient(new BinanceClientOptions
|
||||
{
|
||||
OutputOriginalData = true
|
||||
});
|
||||
````
|
||||
|
||||
*Accessing original data*
|
||||
````C#
|
||||
// Rest request
|
||||
var tickerResult = client.SpotApi.ExchangeData.GetTickersAsync();
|
||||
var originallyRecievedData = tickerResult.OriginalData;
|
||||
|
||||
// Socket update
|
||||
client.SpotStreams.SubscribeToAllTickerUpdatesAsync(update => {
|
||||
var originallyRecievedData = update.OriginalData;
|
||||
});
|
||||
````
|
||||
|
||||
### Trace logging
|
||||
Trace logging, which is the most verbose log level, can be enabled in the client options.
|
||||
*Enabled output data*
|
||||
````C#
|
||||
var client = new BinanceClient(new BinanceClientOptions
|
||||
{
|
||||
LogLevel = LogLevel.Trace
|
||||
});
|
||||
````
|
||||
After enabling trace logging all data send to/received from the server is written to the log writers. By default this is written to the output window in Visual Studio via Debug.WriteLine, though this might be different depending on how you configured your logging.
|
||||
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.
|
||||
|
||||
Reference in New Issue
Block a user