KrakenCppSDK is a modern, thread-safe C++ client library for Kraken's WebSocket API v2 that abstracts away the hard parts of real-time market data streaming. Using a State-View architecture, the SDK separates mutable state (protected by locks) from immutable snapshots (lock-free reads), enabling safe concurrent access from multiple threads without race conditions.
The library features a custom Decimal type with 12-digit fractional precision using integer arithmetic, eliminating floating-point errors that plague financial applications. With complete coverage of all public WebSocket channels, CRC32 checksum verification for order books, and a clean callback-based API, developers can go from zero to streaming live market data in under 10 lines of code.
• Thread-Safe by Design: Lock-free reads with immutable snapshots; concurrent access from multiple threads without race conditions
• Financial-Grade Precision: Custom Decimal type with 12-digit precision using integer arithmetic (no floating-point errors)
• Data Integrity Guaranteed: CRC32 checksum verification for order books; throws on mismatch to prevent trading on corrupted data
• Complete API Coverage: All public WebSocket v2 channels: Book, Ticker, Trade, OHLC, and Instrument
• High Performance: Lock-free View access, O(log N) order book updates, minimal memory footprint (~2KB per symbol for 25-depth book)
• Production-Ready: Comprehensive test suite using Catch2 with more than 300 tests, 7 working examples, cross-platform support (Linux, macOS, Windows)
• Technology Stack: Built with C++20 leveraging modern features like concepts, ranges, and structured bindings. Uses ixwebsocket for robust WebSocket connectivity, nlohmann/json for high-performance parsing, and zlib for CRC32 checksum calculation.
• Architecture Decisions: The SDK implements a State-View pattern where mutable State classes (protected by std::shared_mutex) create immutable View snapshots for callbacks. This allows the WebSocket thread to update state while application threads safely read data without contention. I also use deferred resource allocation: State objects aren't created until the server confirms subscription success, preventing resource leaks on rejected subscriptions.
• Decimal Implementation: Our hybrid Decimal class stores the original JSON string (required for checksum calculation) alongside parsed integer/fractional parts scaled to 10^12. This enables O(1) comparisons using integer arithmetic while preserving full precision for financial calculations.
• Performance Optimizations: Order books use std::map for O(log N) insertions with sorted iteration. Views are copy-on-read snapshots created only when callbacks fire, not on every message. Trade history auto-prunes to configurable limits to bound memory usage.
Installation
git clone https://github.com/rfperuch/KrakenCppSdk.git cd KrakenCppSdk mkdir build && cd build cmake .. -DCMAKE_BUILD_TYPE=Release cmake --build .
Basic Usage
#include "KrakenClient.h"
#include "IxWebSocket.h"
int main() {
IxWebSocket::init(); // Required on Windows
auto ws = std::make_unique();
KrakenClient client{std::move(ws)};
// Subscribe to BTC/USD order book and ticker
client.subscribe(
BookConfig{
.symbol = "BTC/USD",
.depth = 25,
.onMessage = [](const BookView& book) {
auto spread = book.asks()[0].price.toDouble()
- book.bids()[0].price.toDouble();
std::cout << "Spread: $" << spread << "\n";
}
},
TickerConfig{
.symbol = "BTC/USD",
.onMessage = [](const TickerView& t) {
std::cout << "Last: $" << t.last().toDouble() << "\n";
}
}
);
client.run();
std::cin.get(); // Run until Enter
client.stop();
IxWebSocket::final();
return 0;
}Workflow
| Resource | Link |
| GitHub Repository | github.com/rfperuch/KrakenCppSdk |
| Full Documentation | Comprehensive README with architecture diagrams, API reference, and troubleshooting |
| Working Examples | 7 complete examples covering all channels and use cases |
| Test Suite | Unit and integration tests with Catch2 |
Examples Included
| Example | Description |
| 01_book_ticker | Getting started with order books and tickers |
| 02_thread_safety | Concurrent access patterns demonstration |
| 03_instrument | Asset and trading pair metadata |
| 04_trades | Real-time trade monitoring |
| 05_ohlc | Candlestick data streaming |
| 06_book | Deep order book analysis |
| 07_ticker | Market ticker tracking |
Running Examples
With additional development time, I would add: