/KuCoin.NET

KuCoin API Libraries written in .NET 6.0 and Standard 2.0

Primary LanguageC#Apache License 2.0Apache-2.0

KuCoin.NET v2.0a

KuCoin and KuCoin Futures API Libraries for .NET.

This library is a client for the KuCoin API as described in (https://docs.kucoin.com/#general).

ATTENTION: The Level 3 API for KuCoin is deprecated as of 4/28/2022

May 6, 2022:

Since all level 3 services have been deprecated, I have migrated the console application to use Level 2 feeds, instead.

April 8, 2022:

VERSION 2 MERGED TO MAIN BRANCH

  • Dramatic speed improvements.
  • Multiple bug fixes and stability issues addressed.
  • The order books in the sample app now load without freezing the UI, and much more quickly.
  • Implemented an experimental Custom Buffered Binary Tree Collection for the order books.
  • Implemented a callback pattern for HTTP requests and added this to the very root of the project classes.
  • Ability to select individual tickers in the console app (Ctrl + Page Up / Page Down).

Roadmap:

  • Consolidate the base framework classes into an agnostic framework for reuse in other projects.
  • Continue to analyze and tweak performance of the Black/Red Collection.
  • Unify and improve the parallel distribution services.
  • Formalize the CryptoCredentials class from the credentials app and make it available for reuse.
  • Drop support for .NET Standard 2.0. Understandably this might be inconvenient for some people, but the issue is speed.

April 5, 2022:

Have written a new collection based on a black/red node pattern. It seems to be effective in increasing performance under heavy market activity while still retaining some properties of a sorted list.

Made other major improvements including allowing you to select and view the details of individual symbols.

March 27, 2022:

Heavily improved the speed of Level 3 match logic.

Transitioned project to Beta.

September 7th, 2021:

After a major rewrite of a portion of the Websockets feeds to support parallel delegation and Level 3 direct feeds, I have decided to sunset the WPF projects and I have removed the FancyCandles submodule.

Going forward, I will be developing UI/UX apps with MAUI or WinUI 3 / Project Reunion, in other repositories.

This repository is now reduced to the core library, the console app and a credentials editing tool.

The entire project has been retargeted to .NET 6, except for KuCoin.NET.Std, which is .NET Standard 2.0.

Donations

If you like my work, and find it may be useful, you can donate crypto if you like!

My Ethereum wallet address is:

  • 0xb97a29b4349cb3f66b7f2143c6ba1362b8ec4e7d

My Stellar Lumens (XLM) address is:

  • Address: GAJ4BSGJE6UQHZAZ5U5IUOABPDCYPKPS3RFS2NVNGFGFXGVQDLBQJW2P
  • Memo: 1870588215

My KCS wallet is:

  • 0xb97a29b4349cb3f66b7f2143c6ba1362b8ec4e7d

Installation and Getting Started

There are two solutions, one that contains projects that reference the .NET 6 library, and the other that contains projects that reference the .NET Standard 2.0 library.

The usage guidelines are as follows:

  • New projects targeting Desktop, Console, .NET MAUI, or Blazor should use the .NET 6.0 library.
  • Older projects targeting the .NET Framework or Xamarin, or Mono projects should use the .NET Standard 2.0 library.

Note: Even though the projects in the .NET Standard solution reference the .NET Standard KuCoin.NET library, they are still native apps built in .NET 6.

Creating Credentials

The Windows Forms-based credentials editor will let you enter your KuCoin API credentials and save them to the disk, encrypted using a 6 digit pin.

(Note: .NET 5/6 Forms projects will generally compile in Linux without modification, as Mono wraps the Gtk+ libs.)

You can use whatever pin you like, but the credentials you use will be loaded for that pin. If you use another pin you will essentially create a new set of credentials.

After you have created the credentials, you can run the console app and type in the same pin you used to load the credentials.

The API library, itself, does not have any mechanism for persisting or storing credentials. If you wish to use an alternative to the CryptoCredentials class, provided, you will have to implement your own ICredentialsProvider.

Running the Sample Console App

The sample app starts multiple Level2Direct feeds (see below.)

When you run the sample app, it will look for credentials configured by the Windows Forms app, by default.

Program.cs includes a skeleton class implementation of ICredentialsProvider if you wish to use that, instead.

The user will be prompted to enter their pin, and then be asked to provide either a number to list the top n feeds by 24-hr volume, or provide a list of feeds separated by commas.

The console app only demonstrates the ability to track Level2Direct feeds. Other functionality is not demonstrated in the examples, at this time.

(Try running with 300 feeds. Works well on an i7-10700K with a Gigabit internet connection, but it only uses about 20-30 Mbps. Uses about 1.4 GB of RAM)

Console App Instructions

  • Use Arrow Up/Arrow Down, Page Up/Page Down, Home/End to navigate the feed list.

  • Use Ctrl + Page Up/Down to select an item. Esc to clear selection.

  • Use Arrow Left/Arrow Right to switch between different connections.

  • Use Ctrl + Arrow Left/Arrow Right to change the K-Line.

  • Press: (A) Sort Alphabetically, (P) Price, (V) Volume, (T) Throughput. Press again to reverse order.

  • Press: (D) Show/Hide Diagnostics.

  • Press: (M) Show/Hide Messages

  • Press: (Q) To Quit.

Using the Library

(Note: A Wiki will be coming soon!)

The starting point for the entire system is the Kucoin.NET.KuCoinSystem static class. From here you will be able to register credentials and create and acquire IServiceFactory and ISymbolDataService instances, as well as initialize market data and the dispatcher.

REST API

Inside the Kucoin.NET.Rest namespace you will find three objects, Market, Trade, Margin, and User. Margin, Trade, and User require API Keys.

There are two credentials providers, there is the default MemoryEncryptedCredentialsProvider that is in the KuCoin.NET library, and then the CryptoCredentials class in the example app and the credentials editor app.

The MemoryEncryptedCredentialsProvider will store the credentials encrypted in memory with a random seed, until they are needed. CryptoCredentials has the ability to load and save encrypted credentials sets to disk. A 6 digit numeric pin is required to save and load credentials. Both of these classes implement the ICredentialsProvider interface which you can use to write your own provider.

Websocket Feeds

Public feeds in the namespace Kucoin.NET.Websockets.Public:

  • AllTickerFeed - Pushes all symbol tickers as they are updated.
  • KlineFeed - Pushes the K-Line feed for the subscribed symbols.
  • MarketFeed - Pushes an entire market.
  • SnapshotFeed - Pushes market snapshots.
  • TickerFeed - Pushes basic symbol price tickers.
  • MatchFeed - Pushes Level 3 match execution events.
  • Level2 - Pushes the full-depth Level 2 market feed (calibrated).
  • Level2Direct - Level 2 that bypasses the parallel service dispatcher and updates order books from the socket thread.
  • Level2Depth5/Level2Depth50 - Pushes the 5/50 best ask/bid static market depth feeds.

All of the feeds support multiplexing. You may create a single feed object, and use that object's connection to start sub-channels that will be served to the multiplex child classes. Multiplexing is implemented in the KucoinBaseWebsocketFeed abstract class.

  • Note: You cannot multiplex a private feed onto a public feed.

All of the feeds except for order book feeds implement the IObservable pattern.

UI/UX Notes

In order to use any of the feeds in a UI/UX/MVVM setting, you will need to initialize the Kucoin.NET.Helpers.Dispatcher static class with a SynchronizationContext from the Dispatcher provided by your application (usually the App class, itself.) Feed observations will not execute correctly without a SynchronizationContext, because they need to inform the UI thread.

Windows Forms, WPF, and Win UI / Reunion apps all provide a SynchronizationContext, so this should generally not be an issue.

Other Notes

The library, itself, is pretty well documented, so far.

This project is epic, and there are bound to be bugs. Feel free to open issues, and I will get to them, as I can, if I don't find the bugs, first.