{"id":25381627,"url":"https://github.com/marfusios/crypto-websocket-extensions","last_synced_at":"2025-10-30T10:30:53.782Z","repository":{"id":45477118,"uuid":"179533140","full_name":"Marfusios/crypto-websocket-extensions","owner":"Marfusios","description":"🧰 Unified and optimized data structures across cryptocurrency exchanges","archived":false,"fork":false,"pushed_at":"2024-04-30T18:01:38.000Z","size":7801,"stargazers_count":40,"open_issues_count":2,"forks_count":9,"subscribers_count":8,"default_branch":"master","last_synced_at":"2024-05-15T18:27:41.171Z","etag":null,"topics":["bitcoin","client","cryptocurrency","data-structures","exchange","extensions","orderbook","orderbook-tick-data","orderbook-websocket","reactive","websocket","websockets"],"latest_commit_sha":null,"homepage":"","language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Marfusios.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2019-04-04T16:12:05.000Z","updated_at":"2024-06-17T12:16:35.530Z","dependencies_parsed_at":"2024-06-17T12:16:29.407Z","dependency_job_id":"27664d8d-e518-4c84-95be-8419a1772e46","html_url":"https://github.com/Marfusios/crypto-websocket-extensions","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Marfusios%2Fcrypto-websocket-extensions","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Marfusios%2Fcrypto-websocket-extensions/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Marfusios%2Fcrypto-websocket-extensions/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Marfusios%2Fcrypto-websocket-extensions/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Marfusios","download_url":"https://codeload.github.com/Marfusios/crypto-websocket-extensions/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":238951184,"owners_count":19557698,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["bitcoin","client","cryptocurrency","data-structures","exchange","extensions","orderbook","orderbook-tick-data","orderbook-websocket","reactive","websocket","websockets"],"created_at":"2025-02-15T06:22:57.043Z","updated_at":"2025-10-30T10:30:53.775Z","avatar_url":"https://github.com/Marfusios.png","language":"C#","funding_links":[],"categories":[],"sub_categories":[],"readme":"![Logo](cwe_logo.png)\n# Cryptocurrency websocket extensions \n\n[![NuGet version](https://img.shields.io/nuget/v/Crypto.Websocket.Extensions?style=flat-square)](https://www.nuget.org/packages/Crypto.Websocket.Extensions)\n[![Nuget downloads](https://img.shields.io/nuget/dt/Crypto.Websocket.Extensions?style=flat-square)](https://www.nuget.org/packages/Crypto.Websocket.Extensions)\n[![CI build](https://img.shields.io/github/check-runs/marfusios/crypto-websocket-extensions/master?style=flat-square\u0026label=build)](https://github.com/Marfusios/crypto-websocket-extensions/actions/workflows/dotnet-core.yml)\n\n\nThis is a library that provides extensions to cryptocurrency websocket exchange clients. \n\nIt helps to unify data models and usage of more clients together. \n\n[Releases and breaking changes](https://github.com/Marfusios/crypto-websocket-extensions/releases)\n\n### License: \n    Apache License 2.0\n\n### Features\n\n* installation via NuGet\n    * full (with all exchange clients) - [Crypto.Websocket.Extensions](https://www.nuget.org/packages/Crypto.Websocket.Extensions)\n\t* core (only interfaces and features) - [Crypto.Websocket.Extensions.Core](https://www.nuget.org/packages/Crypto.Websocket.Extensions.Core)\n* targeting .NET Standard 2.0 (.NET Core, Linux/MacOS compatible)\n* reactive extensions ([Rx.NET](https://github.com/Reactive-Extensions/Rx.NET))\n* integrated logging abstraction ([LibLog](https://github.com/damianh/LibLog))\n\n### Supported exchanges\n\n| Logo | Name | Websocket client |\n| ------------- | ------------- |:------:|\n| [![bitfinex](https://user-images.githubusercontent.com/1294454/27766244-e328a50c-5ed2-11e7-947b-041416579bb3.jpg)](https://www.bitfinex.com/?refcode=cq3Fey0Av)  | [Bitfinex](https://www.bitfinex.com/?refcode=cq3Fey0Av)  | [bitfinex-client-websocket](https://github.com/Marfusios/bitfinex-client-websocket) |\n| [![bitmex](https://user-images.githubusercontent.com/1294454/27766319-f653c6e6-5ed4-11e7-933d-f0bc3699ae8f.jpg)](https://www.bitmex.com/register/qGWwBG)  | [BitMEX](https://www.bitmex.com/register/qGWwBG)  | [bitmex-client-websocket](https://github.com/Marfusios/bitmex-client-websocket) |\n| [![binance](https://user-images.githubusercontent.com/1294454/29604020-d5483cdc-87ee-11e7-94c7-d1a8d9169293.jpg)](https://www.binance.com/?ref=21773680)  | [Binance](https://www.binance.com/?ref=21773680)  | [binance-client-websocket](https://github.com/Marfusios/binance-client-websocket) |\n| [![coinbase](https://user-images.githubusercontent.com/1294454/41764625-63b7ffde-760a-11e8-996d-a6328fa9347a.jpg)](https://www.coinbase.com/join/kotas_4)  | [Coinbase](https://www.coinbase.com/join/kotas_4)  | [coinbase-client-websocket](https://github.com/Marfusios/coinbase-client-websocket) |\n| [![bitstamp](https://user-images.githubusercontent.com/1294454/27786377-8c8ab57e-5fe9-11e7-8ea4-2b05b6bcceec.jpg)](https://www.bitstamp.net)  | [Bitstamp](https://www.bitstamp.net)  | [bitstamp-client-websocket](https://github.com/Marfusios/bitstamp-client-websocket) |\n\n\n## Extensions\n\n### Order book\n\n* efficient data structure, based on [howtohft blog post](https://web.archive.org/web/20110219163448/http://howtohft.wordpress.com/2011/02/15/how-to-build-a-fast-limit-order-book/)\n* `CryptoOrderBook` class - unified order book across all exchanges\n* support for L2 (grouped by price), L3 (every single order) market data \n* support for snapshots and deltas/diffs\n* provides streams:\n    * `OrderBookUpdatedStream` - streams on an every order book update\n    * `BidAskUpdatedStream` - streams when bid or ask price changed (top level of the order book)\n\t* `TopLevelUpdatedStream` - streams when bid or ask price/amount changed (top level of the order book)\n* provides properties and methods:\n    * `BidLevels` and `AskLevels` - ordered array of current state of the order book\n    * `BidLevelsPerPrice` and `AskLevelsPerPrice` - dictionary of all L3 orders split by price\n    * `FindLevelByPrice` and `FindLevelById` - returns specific order book level\n\nUsage:\n\n```csharp\nvar url = BitmexValues.ApiWebsocketUrl;\nvar communicator = new BitmexWebsocketCommunicator(url);\nvar client = new BitmexWebsocketClient(communicator);\n\nvar pair = \"XBTUSD\";\n\nvar source = new BitmexOrderBookSource(client);\nvar orderBook = new CryptoOrderBook(pair, source);\n\n// orderBook.BidAskUpdatedStream.Subscribe(xxx)\norderBook.OrderBookUpdatedStream.Subscribe(quotes =\u003e\n{\n    var currentBid = orderBook.BidPrice;\n    var currentAsk = orderBook.AskPrice;\n\n    var bids = orderBook.BidLevels;\n    // xxx\n});\n        \nawait communicator.Start();\n```\n\n### Trades\n\n* `ITradeSource` - unified trade info stream across all exchanges\n\n### Orders (authenticated)\n\n* `CryptoOrders` class - unified orders status across all exchanges with features:\n    * orders view and searching - only executed, search by id, client id, etc.\n    * our vs all orders - using client id prefix to distinguish between orders\n\n### Position (authenticated)\n\n* `IPositionSource` - unified position info stream across all exchanges\n\n### Wallet (authenticated)\n\n* `IWalletSource` - unified wallet status stream across all exchanges\n\n\n\n---\n\nMore usage examples:\n* console sample ([link](test_integration/Crypto.Websocket.Extensions.Sample/Program.cs))\n* unit tests ([link](test/Crypto.Websocket.Extensions.Tests))\n* integration tests ([link](test_integration/Crypto.Websocket.Extensions.Tests.Integration))\n\n**Pull Requests are welcome!**\n\n### Powerfull Rx.NET\n\nDon't forget that you can do pretty nice things with reactive extensions and observables. \nFor example, if you want to check latest bid/ask prices from all exchanges all together, \nyou can do something like this: \n\n```csharp\nObservable.CombineLatest(new[]\n            {\n                bitmexOrderBook.BidAskUpdatedStream,\n                bitfinexOrderBook.BidAskUpdatedStream,\n                binanceOrderBook.BidAskUpdatedStream,\n            })\n            .Subscribe(HandleQuoteChanged);\n\n// Method HandleQuoteChanged(IList\u003cCryptoQuotes\u003e quotes)\n// will be called on every exchange's price change\n```\n\n\n### Multi-threading\n\nObservables from Reactive Extensions are single threaded by default. It means that your code inside subscriptions is called synchronously and as soon as the message comes from websocket API. It brings a great advantage of not to worry about synchronization, but if your code takes a longer time to execute it will block the receiving method, buffer the messages and may end up losing messages. For that reason consider to handle messages on the other thread and unblock receiving thread as soon as possible. I've prepared a few examples for you: \n\n#### Default behavior\n\nEvery subscription code is called on a main websocket thread. Every subscription is synchronized together. No parallel execution. It will block the receiving thread. \n\n```csharp\nclient\n    .Streams\n    .TradesStream\n    .Subscribe(trade =\u003e { code1 });\n\nclient\n    .Streams\n    .BookStream\n    .Subscribe(book =\u003e { code2 });\n\n// 'code1' and 'code2' are called in a correct order, according to websocket flow\n// ----- code1 ----- code1 ----- ----- code1\n// ----- ----- code2 ----- code2 code2 -----\n```\n\n#### Parallel subscriptions \n\nEvery single subscription code is called on a separate thread. Every single subscription is synchronized, but different subscriptions are called in parallel. \n\n```csharp\nclient\n    .Streams\n    .TradesStream\n    .ObserveOn(TaskPoolScheduler.Default)\n    .Subscribe(trade =\u003e { code1 });\n\nclient\n    .Streams\n    .BookStream\n    .ObserveOn(TaskPoolScheduler.Default)\n    .Subscribe(book =\u003e { code2 });\n\n// 'code1' and 'code2' are called in parallel, do not follow websocket flow\n// ----- code1 ----- code1 ----- code1 -----\n// ----- code2 code2 ----- code2 code2 code2\n```\n\n #### Parallel subscriptions with synchronization\n\nIn case you want to run your subscription code on the separate thread but still want to follow websocket flow through every subscription, use synchronization with gates: \n\n```csharp\nprivate static readonly object GATE1 = new object();\nclient\n    .Streams\n    .TradesStream\n    .ObserveOn(TaskPoolScheduler.Default)\n    .Synchronize(GATE1)\n    .Subscribe(trade =\u003e { code1 });\n\nclient\n    .Streams\n    .BookStream\n    .ObserveOn(TaskPoolScheduler.Default)\n    .Synchronize(GATE1)\n    .Subscribe(book =\u003e { code2 });\n\n// 'code1' and 'code2' are called concurrently and follow websocket flow\n// ----- code1 ----- code1 ----- ----- code1\n// ----- ----- code2 ----- code2 code2 ----\n```\n\n### Async/Await integration\n\nUsing `async/await` in your subscribe methods is a bit tricky. Subscribe from Rx.NET doesn't `await` tasks, \nso it won't block stream execution and cause sometimes undesired concurrency. For example: \n\n```csharp\nclient\n    .Streams\n    .TradesStream\n    .Subscribe(async trade =\u003e {\n        // do smth 1\n        await Task.Delay(5000); // waits 5 sec, could be HTTP call or something else\n        // do smth 2\n    });\n```\n\nThat `await Task.Delay` won't block stream and subscribe method will be called multiple times concurrently. \nIf you want to buffer messages and process them one-by-one, then use this: \n\n```csharp\nclient\n    .Streams\n    .TradesStream\n    .Select(trade =\u003e Observable.FromAsync(async () =\u003e {\n        // do smth 1\n        await Task.Delay(5000); // waits 5 sec, could be HTTP call or something else\n        // do smth 2\n    }))\n    .Concat() // executes sequentially\n    .Subscribe();\n```\n\nIf you want to process them concurrently (avoid synchronization), then use this\n\n```csharp\nclient\n    .Streams\n    .TradesStream\n    .Select(trade =\u003e Observable.FromAsync(async () =\u003e {\n        // do smth 1\n        await Task.Delay(5000); // waits 5 sec, could be HTTP call or something else\n        // do smth 2\n    }))\n    .Merge() // executes concurrently\n    // .Merge(4) you can limit concurrency with a parameter\n    // .Merge(1) is same as .Concat()\n    // .Merge(0) is invalid (throws exception)\n    .Subscribe();\n```\n\nMore info on [Github issue](https://github.com/dotnet/reactive/issues/459).\n\nDon't worry about websocket connection, those sequential execution via `.Concat()` or `.Merge(1)` has no effect on receiving messages. \nIt won't affect receiving thread, only buffers messages inside `TradesStream`. \n\nBut beware of [producer-consumer problem](https://en.wikipedia.org/wiki/Producer%E2%80%93consumer_problem) when the consumer will be too slow. Here is a [StackOverflow issue](https://stackoverflow.com/questions/11010602/with-rx-how-do-i-ignore-all-except-the-latest-value-when-my-subscribe-method-is) \nwith an example how to ignore/discard buffered messages and always process only the last one. \n\n### Available for help\nI do consulting, please don't hesitate to contact me if you have a custom solution you would like me to implement ([web](http://mkotas.cz/), \n\u003cm@mkotas.cz\u003e)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmarfusios%2Fcrypto-websocket-extensions","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmarfusios%2Fcrypto-websocket-extensions","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmarfusios%2Fcrypto-websocket-extensions/lists"}