1
0
mirror of https://github.com/JKorf/CryptoExchange.Net synced 2025-06-07 16:06:15 +00:00
This commit is contained in:
Jkorf 2022-01-17 13:35:46 +01:00
parent e77add4d1c
commit dbfe34f534
12 changed files with 844 additions and 1 deletions

1
.gitignore vendored
View File

@ -287,4 +287,3 @@ __pycache__/
*.odx.cs
*.xsd.cs
CryptoExchange.Net/CryptoExchange.Net.xml
Docs/

184
docs/Clients.md Normal file
View File

@ -0,0 +1,184 @@
Each implementation generally provides two different clients, which will be the access point for the API's. First of the rest client, which is typically available via [ExchangeName]Client, and a socket client, which is generally named [ExchangeName]SocketClient. For example `BinanceClient` 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:
- KucoinClient
- 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*
````C#
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 Kucoin 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:
`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`
`Data`: Data returned by the server
When processing the result of a call it should always be checked for success. Not doing so will result in `NullReference` exceptions.
*Check call result*
````C#
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. Some exchanges also offer some degree of functionality by allowing clients to give commands via the websocket, but most exchanges only allow this via the Rest API.
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:
````C#
- KucoinSocketClient
- SpotStreams
- FuturesStreams
````
*Subscribing to updates for all tickers on the Spot Api*
````C#
var subscribeResult = kucoinSocketClient.SpotStreams.SubscribeToAllTickerUpdatesAsync(DataHandler);
````
Subscribe methods 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*
````C#
await kucoinSocketClient.SpotStreams.SubscribeToAllTickerUpdatesAsync(DataHandler);
private static void DataHandler(DataEvent<KucoinStreamTick> updateData)
{
// Process updateData
}
````
*Lambda*
````C#
await kucoinSocketClient.SpotStreams.SubscribeToAllTickerUpdatesAsync(updateData =>
{
// Process 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#
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:
````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.
## Dependency injection
Each library offers a `Add[Library]` extension method for `IServiceCollection`, which allows you to add the clients to the service collection. It also provides a callback for setting the client options. See this example for adding the `BinanceClient`:
````C#
public void ConfigureServices(IServiceCollection services)
{
services.AddBinance((restClientOptions, socketClientOptions) => {
restClientOptions.ApiCredentials = new ApiCredentials("KEY", "SECRET");
restClientOptions.LogLevel = LogLevel.Trace;
socketClientOptions.ApiCredentials = new ApiCredentials("KEY", "SECRET");
});
}
````
Doing client registration this way will add the `IBinanceClient` as a transient service, and the `IBinanceSocketClient` as a scoped service.
Alternatively, the clients can be registered manually:
````C#
BinanceClient.SetDefaultOptions(new BinanceClientOptions
{
ApiCredentials = new ApiCredentials("KEY", "SECRET"),
LogLevel = LogLevel.Trace
});
BinanceSocketClient.SetDefaultOptions(new BinanceSocketClientOptions
{
ApiCredentials = new ApiCredentials("KEY", "SECRET"),
});
services.AddTransient<IBinanceClient, BinanceClient>();
services.AddScoped<IBinanceSocketClient, BinanceSocketClient>();
````

41
docs/FAQ.md Normal file
View File

@ -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
});
}
````

22
docs/Glossary.md Normal file
View File

@ -0,0 +1,22 @@
|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 `FTX.Net`|
### Other naming constraints
#### 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
docs/Implementation.md Normal file
View File

@ -0,0 +1 @@
TODO steps for creating a new implementation

5
docs/Interfaces.md Normal file
View File

@ -0,0 +1,5 @@
## ISpotClient
TODO
## IFuturesClient
TODO

319
docs/Logging.md Normal file
View File

@ -0,0 +1,319 @@
The library offers extensive logging, for which you can supply your own logging implementation. The logging can be configured via the client options (see [Client options](https://github.com/JKorf/CryptoExchange.Net/wiki/Options)). The examples here are using the `BinanceClient` but they should be the same for each implementation.
Logging is based on the `Microsoft.Extensions.Logging.ILogger` interface. This should provide ease of use when connecting the library logging to your existing logging implementation.
### Serilog
To make the CryptoExchange.Net logging write to the Serilog logger you can use the following methods, depending on the type of project you're using. The following examples assume that the `Serilog.Sinks.Console` package is already installed.
#### Dotnet hosting
With for example an ASP.Net Core or Blazor project the logging can be added to the dependency container, which you can then use to inject it into the client. Make sure to install the `Serilog.AspNetCore` package (https://github.com/serilog/serilog-aspnetcore).
<Details>
<Summary>
<b>Using ILogger injection</b>
</Summary>
<BlockQuote>
Adding `UseSerilog()` in the `CreateHostBuilder` will add the Serilog logging implementation as an ILogger which you can inject into implementations.
*Configuring Serilog as ILogger:*
````C#
public static void Main(string[] args)
{
Log.Logger = new LoggerConfiguration()
.MinimumLevel.Debug()
.WriteTo.Console()
.CreateLogger();
CreateHostBuilder(args).Build().Run();
}
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.UseSerilog()
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
});
````
*Injecting ILogger:*
````C#
public class BinanceDataProvider
{
BinanceClient _client;
public BinanceDataProvider(ILogger<BinanceDataProvider> logger)
{
_client = new BinanceClient(new BinanceClientOptions
{
LogLevel = LogLevel.Trace,
LogWriters = new List<ILogger> { logger }
});
}
}
````
</BlockQuote>
</Details>
<Details>
<Summary>
<b>Using Add[Library] extension method</b>
</Summary>
<BlockQuote>
When using the `Add[Library]` extension method, for instance `AddBinance()`, there is a small issue that there is no available `ILogger<>` yet when adding the library. This can be solved as follows:
*Configuring Serilog as ILogger:*
````C#
public static void Main(string[] args)
{
Log.Logger = new LoggerConfiguration()
.MinimumLevel.Debug()
.WriteTo.Console()
.CreateLogger();
CreateHostBuilder(args).Build().Run();
}
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup(
context => new Startup(context.Configuration, LoggerFactory.Create(config => config.AddSerilog()) )); // <- this allows us to use ILoggerFactory in the Startup.cs
});
````
*Injecting ILogger:*
````C#
public class Startup
{
private ILoggerFactory _loggerFactory;
public Startup(IConfiguration configuration, ILoggerFactory loggerFactory)
{
Configuration = configuration;
_loggerFactory = loggerFactory;
}
/* .. rest of class .. */
public void ConfigureServices(IServiceCollection services)
{
services.AddBinance((restClientOptions, socketClientOptions) => {
// Point the logging to use the ILogger configuration
restClientOptions.LogWriters = new List<ILogger> { _loggerFactory.CreateLogger<IBinanceClient>() };
});
// Rest of service registrations
}
}
````
</BlockQuote>
</Details>
#### Console application
If you don't have a dependency injection service available because you are for example working on a simple console application you can use a slightly different approach.
*Configuring Serilog as ILogger:*
````C#
var serilogLogger = new LoggerConfiguration()
.MinimumLevel.Debug()
.WriteTo.Console()
.CreateLogger();
var loggerFactory = (ILoggerFactory)new LoggerFactory();
loggerFactory.AddSerilog(serilogLogger);
````
*Injecting ILogger:*
````C#
var client = new BinanceClient(new BinanceClientOptions
{
LogLevel = LogLevel.Trace,
LogWriters = new List<ILogger> { loggerFactory.CreateLogger("") }
});
````
The `BinanceClient` will now write the logging it produces to the Serilog logger.
### Log4Net
To make the CryptoExchange.Net logging write to the Log4Net logge with for example an ASP.Net Core or Blazor project the logging can be added to the dependency container, which you can then use to inject it into the client you're using. Make sure to install the `Microsoft.Extensions.Logging.Log4Net.AspNetCore` package (https://github.com/huorswords/Microsoft.Extensions.Logging.Log4Net.AspNetCore).
Adding `AddLog4Net()` in the `ConfigureLogging` call will add the Log4Net implementation as an ILogger which you can inject into implementations. Make sure you have a log4net.config configuration file in your project.
*Configuring Log4Net as ILogger:*
````C#
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.ConfigureLogging(logging =>
{
logging.AddLog4Net();
logging.SetMinimumLevel(LogLevel.Trace);
});
webBuilder.UseStartup<Startup>();
});
````
*Injecting ILogger:*
````C#
public class BinanceDataProvider
{
BinanceClient _client;
public BinanceDataProvider(ILogger<BinanceDataProvider> logger)
{
_client = new BinanceClient(new BinanceClientOptions
{
LogLevel = LogLevel.Trace,
LogWriters = new List<ILogger> { logger }
});
}
}
````
If you don't have the Dotnet dependency container available you'll need to provide your own ILogger implementation. See [Custom logger](#custom-logger).
### NLog
To make the CryptoExchange.Net logging write to the NLog logger you can use the following ways, depending on the type of project you're using.
#### Dotnet hosting
With for example an ASP.Net Core or Blazor project the logging can be added to the dependency container, which you can then use to inject it into the client you're using. Make sure to install the `NLog.Web.AspNetCore` package (https://github.com/NLog/NLog/wiki/Getting-started-with-ASP.NET-Core-5).
Adding `UseNLog()` to the `CreateHostBuilder()` method will add the NLog implementation as an ILogger which you can inject into implementations. Make sure you have a nlog.config configuration file in your project.
*Configuring NLog as ILogger:*
````C#
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
})
.ConfigureLogging(logging =>
{
logging.ClearProviders();
logging.SetMinimumLevel(LogLevel.Trace);
})
.UseNLog();
````
*Injecting ILogger:*
````C#
public class BinanceDataProvider
{
BinanceClient _client;
public BinanceDataProvider(ILogger<BinanceDataProvider> logger)
{
_client = new BinanceClient(new BinanceClientOptions
{
LogLevel = LogLevel.Trace,
LogWriters = new List<ILogger> { logger }
});
}
}
````
If you don't have the Dotnet dependency container available you'll need to provide your own ILogger implementation. See [Custom logger](#custom-logger).
### Custom logger
If you're using a different framework or for some other reason these methods don't work for you you can create a custom ILogger implementation to receive the logging. All you need to do is create an implementation of the ILogger interface and provide that to the client.
*A simple console logging implementation (note that the ConsoleLogger is already available in the CryptoExchange.Net library)*:
````C#
public class ConsoleLogger : ILogger
{
public IDisposable BeginScope<TState>(TState state) => null;
public bool IsEnabled(LogLevel logLevel) => true;
public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func<TState, Exception, string> formatter)
{
var logMessage = $"{DateTime.Now:yyyy/MM/dd HH:mm:ss:fff} | {logLevel} | {formatter(state, exception)}";
Console.WriteLine(logMessage);
}
}
````
*Injecting the console logging implementation:*
````C#
var client = new BinanceClient(new BinanceClientOptions
{
LogLevel = LogLevel.Trace,
LogWriters = new List<ILogger> { new ConsoleLogger() }
});
````
## 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.

90
docs/Migration Guide.md Normal file
View File

@ -0,0 +1,90 @@
Changes from 4.x to 5.x:
## Client structure
The client structure has been changed to make clients more consistent across different implementations. Clients using V4 either had `client.Method()`, `client.[Api].Method()` or `client.[Api].[Topic].Method()`.
This has been unified to be `client.[Api]Api.[Topic].Method()`:
`bittrexClient.GetTickersAsync()` -> `bittrexClient.SpotApi.ExchangeData.GetTickersAsync()`
`kucoinClient.Spot.GetTickersAsync()` -> `kucoinClient.SpotApi.ExchangeData.GetTickersAsync()`
`binanceClient.Spot.Market.GetTickersAsync()` -> `binanceClient.SpotApi.ExchangeData.GetTickersAsync()`
Socket clients are restructured as `client.[Api]Streams.Method()`:
`bittrexClient.SpotStreams.SubscribeToTickerUpdatesAsync()`
`kucoinClient.SpotStreams.SubscribeToTickerUpdatesAsync()`
`binanceClient.SpotStreams.SubscribeToAllTickerUpdatesAsync()`
## Options structure
The options have been changed in 2 categories, options for the whole client, and options only for a specific sub Api. Some options might no longer be available on the base level and should be set on the Api options instead, for example the `BaseAddress`.
The following example sets some basic options, and specifically overwrites the USD futures Api options to use the test net address and different Api credentials:
*V4*
````C#
var binanceClient = new BinanceClient(new BinanceApiClientOptions{
LogLevel = LogLevel.Trace,
RequestTimeout = TimeSpan.FromSeconds(60),
ApiCredentials = new ApiCredentials("API KEY", "API SECRET"),
BaseAddressUsdtFutures = new ApiCredentials("OTHER API KEY ONLY FOR USD FUTURES", "OTHER API SECRET ONLY FOR USD FUTURES")
// No way to set separate credentials for the futures API
});
````
*V5*
````C#
var binanceClient = new BinanceClient(new BinanceClientOptions()
{
// Client options
LogLevel = LogLevel.Trace,
RequestTimeout = TimeSpan.FromSeconds(60),
ApiCredentials = new ApiCredentials("API KEY", "API SECRET"),
// Set options specifically for the USD futures API
UsdFuturesApiOptions = new BinanceApiClientOptions
{
BaseAddress = BinanceApiAddresses.TestNet.UsdFuturesRestClientAddress,
ApiCredentials = new ApiCredentials("OTHER API KEY ONLY FOR USD FUTURES", "OTHER API SECRET ONLY FOR USD FUTURES")
}
});
````
See [Client options](https://github.com/JKorf/CryptoExchange.Net/wiki/Options) for more details on the specific options.
## IExchangeClient
The `IExchangeClient` has been replaced by the `ISpotClient` and `IFuturesClient`. Where previously the `IExchangeClient` was implemented on the base client level, the `ISpotClient`/`IFuturesClient` have been implemented on the sub-Api level.
This, in combination with the client restructuring, allows for more logically implemented interfaces, see this example:
*V4*
````C#
var spotClients = new [] {
(IExhangeClient)binanceClient,
(IExchangeClient)bittrexClient,
(IExchangeClient)kucoinClient.Spot
};
// There was no common implementation for futures client
````
*V5*
````C#
var spotClients = new [] {
binanceClient.SpotApi.ComonSpotClient,
bittrexClient.SpotApi.ComonSpotClient,
kucoinClient.SpotApi.ComonSpotClient
};
var futuresClients = new [] {
binanceClient.UsdFuturesApi.ComonFuturesClient,
kucoinClient.FuturesApi.ComonFuturesClient
};
````
Where the IExchangeClient was returning interfaces which were implemented by models from the exchange, the `ISpotClient`/`IFuturesClient` returns actual objects defined in the `CryptoExchange.Net` library. This shifts the responsibility of parsing
the library model to a shared model from the model class to the client class, which makes more sense and removes the need for separate library models to implement the same mapping logic. It also removes the need for the `Common` prefix on properties:
*V4*
````C#
var kline = await ((IExhangeClient)binanceClient).GetKlinesAysnc(/*params*/);
var closePrice = kline.CommonClose;
````
*V5*
````C#
var kline = await binanceClient.SpotApi.ComonSpotClient.GetKlinesAysnc(/*params*/);
var closePrice = kline.ClosePrice;
````

112
docs/Options.md Normal file
View File

@ -0,0 +1,112 @@
## Setting 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*
````C#
BinanceClient.SetDefaultOptions(new BinanceClientOptions
{
LogLevel = LogLevel.Trace,
ApiCredentials = new ApiCredentials("KEY", "SECRET")
});
````
*Set the options to use for a single new client*
````C#
var client = new BinanceClient(new BinanceClientOptions
{
LogLevel = LogLevel.Trace,
ApiCredentials = new ApiCredentials("KEY", "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:
````C#
BinanceClient.SetDefaultOptions(new BinanceClientOptions
{
LogLevel = LogLevel.Trace,
OutputOriginalData = true
});
var client = new BinanceClient(new BinanceClientOptions
{
LogLevel = LogLevel.Debug,
ApiCredentials = new ApiCredentials("KEY", "SECRET")
});
````
The client instance will have the following options:
`LogLevel = Debug`
`OutputOriginalData = true`
`ApiCredentials = set`
## 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://github.com/JKorf/CryptoExchange.Net/wiki/Clients)).
````C#
var client = new BinanceClient(new BinanceClientOptions
{
LogLevel = LogLevel.Debug,
ApiCredentials = new ApiCredentials("GENERAL-KEY", "GENERAL-SECRET"),
SpotApiOptions = new BinanceApiClientOptions
{
ApiCredentials = new ApiCredentials("SPOT-KEY", "SPOT-SECRET") ,
BaseAddress = BinanceApiAddresses.Us.RestClientAddress
}
});
````
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|
|------|-----------|-------|
|`LogWriters`| A list of `ILogger`s to handle log messages. | `new List<ILogger> { new DebugLogger() }` |
|`LogLevel`| The minimum log level before passing messages to the `LogWriters`. Messages with a more verbose level than the one specified here will be ignored. Setting this to `null` will pass all messages to the `LogWriters`.| `LogLevel.Information`
|`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. Typically a key/secret combination. 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`
**Rest client options (extension of base client options)**
|Option|Description|Default|
|------|-----------|-------|
|`RequestTimeout`|The time out to use for requests.|`TimeSpan.FromSeconds(30)`|
|`HttpClient`|The `HttpClient` instance to use for making requests. When creating multiple `RestClient` instances a single `HttpClient` should be provided to prevent each client instance from creating its own. *[WARNING] When providing the `HttpClient` instance in the options both the `RequestTimeout` and `Proxy` client options will be ignored and should be set on the provided `HttpClient` instance.*| `null` |
**Socket client options (extension of base client options)**
|Option|Description|Default|
|------|-----------|-------|
|`AutoReconnect`|Whether or not the socket should 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
|`MaxReconnectTries`|The maximum amount of tries for reconnecting|`null` (infinite)
|`MaxResubscribeTries`|The maximum amount of tries for resubscribing after successfully reconnecting the socket|5
|`MaxConcurrentResubscriptionsPerSocket`|The maximum number of concurrent resubscriptions per socket when resubscribing after reconnecting|5
**Base Api client options**
|Option|Description|Default|
|------|-----------|-------|
|`ApiCredentials`|The API credentials to use for this specific API client. Will override any credentials provided in the base client options|
|`BaseAddress`|The base address to the API. All calls to the API will use this base address as basis for the endpoints. This allows for swapping to test API's or swapping to a different cluster for example.|Depends on implementation
**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`|
**Options for Socket Api Client (extension of base api client options)**
There are currently no specific options for socket API clients, the base API options are still available.

51
docs/Orderbooks.md Normal file
View File

@ -0,0 +1,51 @@
Each 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 a success state 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`.
*Start an order book and print the top 3 rows*
````C#
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.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.
````C#
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}"); };
````

2
docs/_config.yml Normal file
View File

@ -0,0 +1,2 @@
remote_theme: pmarsceill/just-the-docs
markdown: GFM

17
docs/index.md Normal file
View File

@ -0,0 +1,17 @@
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)