master | develop | nuget |
---|---|---|
C# / .NET Standard Client for connecting to the public kraken.com WebSocket API
See the official documentation fur further information https://www.kraken.com/features/websocket-api
Get the current version from NuGet Gallery using the Package Manager / .NET CLI
PM> Install-Package Kraken.WebSockets # Package Manager
> dotnet add package Kraken.WebSockets # .NET CLI
For detailed information on the installing of pre-release versions please refer to the NuGet Gallery itself.
Creating a connection is pretty easy but will also be improved in the future. But for now just do it like this:
var krakenApi = new KrakenApi()
.ConfigureWebsocket("wss://ws.kraken.com");
using (var client = krakenApi.BuildClient())
{
client.SystemStatusChanged += (sender, e) => Console.WriteLine($"System status changed");
client.SubscriptionStatusChanged += (sender, e) => Console.WriteLine($"Subscription status changed"); ;
client.TickerReceived += (sender, e) => Console.WriteLine($"Ticker received");
client.OhlcReceived += (sender, e) => Console.WriteLine($"Ohlc received");
client.TradeReceived += (sender, e) => Console.WriteLine($"Trade received");
client.SpreadReceived += (sender, e) => Console.WriteLine($"Spread received");
client.BookSnapshotReceived += (sender, e) => Console.WriteLine($"BookSnapshot received");
client.BookUpdateReceived += (sender, e) => Console.WriteLine($"BookUpdate received");
await kraken.ConnectAsync();
// Do something with it and keep the connection open
}
// closing the using-block the connection will be closed and disposed.
You can also find a running example in the repository.
Starting with their version 0.3.0 of the Websocket API kraken.com provides access to sensitive private account information like trades and orders. In order to gain access you need to authenticate.
We support an easy and feasible way to retrieve a token and pass it to the subscription:
// Configure the API provider
var krakenApi = new KrakenApi()
.ConfigureWebsocket("wss://ws.kraken.com")
.ConfigureAuthentication(
configuration.GetValue<string>("API_URL"),
configuration.GetValue<string>("API_KEY"),
configuration.GetValue<string>("API_SECRET"));
// retrieve the token
var token = await krakenApi.AuthenticationClient.GetWebsocketToken();
// Pass the token to a private subscription
await client.SubscribeAsync(new Subscribe(null, new SubscribeOptions(SubscribeOptionNames.OwnTrades, token.Token)));
With the Microsoft.Extensions.Logging.Abstractions
in place you are not limited to a specific logging framework. Each logging framework which supports this abstraction layer can be attached to this library. Just to name a few of them:
- log4net
- Serilog
- NLog
- ... and many more
To enable the logging just call the provided extension method on your ILoggerFactory
of your application.
// Using the logging from ASP.NET Core MVC
// Startup.cs
using Kraken.WebSockets.Logging;
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
// ... source omitted
loggerFactory.AddKrakenWebSockets(); // Enables the configured logging factory for the logs in Kraken.WebSockets
// ... source omitted
}
If you like the stuff I do, please don't hesitate to support my actions by donating me a coffee!