Market Data
The Bjarkan SOR SDK provides access to real-time market data across multiple exchanges. This includes both orderbook data and trade data. You can choose between aggregated orderbook data across all exchanges or separate data for each exchange and symbol. Live market data is crucial for making informed trading decisions and is used by the smart order router for order execution.
Setting Up Market Data Streams
Before you can receive market data, you need to configure and start the appropriate data stream:
Configuration Examples and Returned Data
Let’s look at specific examples of configurations and the corresponding data structure you’ll receive in the callbacks.
Example 1: Aggregated Orderbook
When you set up an aggregated orderbook configuration with multiple exchanges:
The callback will receive individual orderbook updates like this:
For this configuration:
- Each update contains data for a single symbol
- Bids and asks from all exchanges are combined and sorted by price
- The callback is triggered whenever there’s a change to the orderbook on any exchange
- Updates are sent one at a time for each symbol
Example 2: Non-Aggregated Orderbook
When you set up a non-aggregated orderbook configuration:
The callback will receive individual orderbook updates for each exchange-symbol combination:
With this configuration:
- Each update contains data for a single exchange and a single symbol
- A separate update will arrive for each exchange-symbol combination
- Updates are triggered independently whenever an exchange’s orderbook changes
- One update might be for “BTC/USDT” on Coinbase, the next might be for “ETH/USDT” on HTX
Example 3: Trade Stream with Size Filter
When you set up a trade stream with size filtering:
The callback will receive individual trade updates one at a time:
With this configuration:
- Each callback delivers a single trade
- Only trades that meet the size filter are included
- Trades come in real-time as they occur on any of the configured exchanges
- Each trade includes details from one exchange for one symbol
Fetching the Latest Orderbook Data
You can programmatically get the latest orderbook data at any time:
The structure of the returned data depends on your configuration:
For Aggregated Orderbooks (aggregated=True
):
For Non-Aggregated Orderbooks (aggregated=False
):
Fetching the Latest Trade Data
Similarly, you can get the latest trades:
The structure will be:
Fee-Aware Data
When you configure fees_bps
in your configuration, the SDK will adjust the orderbook prices to account for exchange fees. This provides several advantages:
- True Arbitrage Detection: Identify genuine arbitrage opportunities by seeing prices with fees included.
- Accurate Price Comparison: Compare prices across exchanges fairly by accounting for fee differences.
- Realistic Execution: Better understand what your actual execution price will be after fees.
For example, with a fee of 0.1% (10 bps):
- A bid price of 50,000 would be adjusted to 49,950 (50,000 / 1.001)
- An ask price of 50,000 would be adjusted to 50,050 (50,000 * 1.001)
When using callbacks, the data you receive will already have these fee adjustments applied if you’ve configured fees_bps
.
Advanced Usage: Custom Analysis
You can combine orderbook and trade data for advanced market analysis:
Best Practices
When working with market data in the Bjarkan SOR SDK:
- Efficient Processing: Keep callback functions lightweight and non-blocking
- State Management: Maintain clean data structures for storing and analyzing market data
- Data Volume: Be prepared to handle high-frequency updates in active markets
- Error Handling: Implement robust error handling in all callbacks
- Reconnection Logic: Handle potential disconnections gracefully
- Memory Management: For long-running applications, implement data retention policies
- Timestamp Awareness: Pay attention to both local and exchange timestamps for accurate sequencing
Using these techniques, you can build sophisticated trading systems that utilize real-time market data for analysis, signal generation, and order execution.