1
0
mirror of https://github.com/JKorf/CryptoExchange.Net synced 2025-06-08 08:26:20 +00:00
CryptoExchange.Net/docs/Orderbooks.md
JKorf 690f2a63e5 Squashed commit of the following:
commit 90f285d7f6bcd926ce9ca3d5832b1d70a5eae6ab
Author: JKorf <jankorf91@gmail.com>
Date:   Sun Jun 25 19:51:12 2023 +0200

    Docs

commit 72187035c703d1402b37bd2f4c3e066706f28d67
Author: JKorf <jankorf91@gmail.com>
Date:   Sat Jun 24 16:02:53 2023 +0200

    docs

commit 8411977292f1fb0b6e0705b1ad675b79a5311d90
Author: JKorf <jankorf91@gmail.com>
Date:   Fri Jun 23 18:25:15 2023 +0200

    wip

commit cb7d33aad5d2751104c8b8a6c6eadbf0d36b672c
Author: JKorf <jankorf91@gmail.com>
Date:   Fri Jun 2 19:26:26 2023 +0200

    wip

commit 4359a2d05ea1141cff516dab18f364a6ca854e18
Author: JKorf <jankorf91@gmail.com>
Date:   Wed May 31 20:51:36 2023 +0200

    wip

commit c6adb1b2f728d143f6bd667139c619581122a3c9
Author: JKorf <jankorf91@gmail.com>
Date:   Mon May 1 21:13:47 2023 +0200

    wip

commit 7fee733f82fa6ff574030452f0955c9e817647dd
Author: JKorf <jankorf91@gmail.com>
Date:   Thu Apr 27 13:02:56 2023 +0200

    wip

commit f8057313ffc9b0c31effcda71d35d105ea390971
Author: JKorf <jankorf91@gmail.com>
Date:   Mon Apr 17 21:37:51 2023 +0200

    wip
2023-06-25 19:58:46 +02:00

3.7 KiB

title, nav_order
title nav_order
Order books 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


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.


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

var factory = services.GetRequiredService<IKucoinOrderBookFactory>();
var book = factory.CreateSpot("ETH-USDT");
var startResult = await book.StartAsync();