Order Execution
The Bjarkan SOR SDK provides smart order routing capabilities to execute trades across multiple exchanges for optimal pricing. This guide explains how to configure and use the order execution functionality.
Prerequisites
Before you can execute orders, you need to:
-
Set up API credentials as environment variables:
-
Configure the orderbook with
aggregated=True
and a single symbol: -
Start the orderbook stream to get market data:
-
Initialize the order executor with your API configurations:
Basic Order Execution
Once you’ve completed the prerequisites, you can execute orders:
Order Configuration
The OrderConfig
class requires two parameters:
side
: “buy” or “sell”amount
: The amount to trade in base currency (e.g., BTC for BTC/USDT)
Currently, the SDK supports market orders. Limit orders and other order types will be added in future releases.
How Smart Order Routing Works
When you execute an order, the Bjarkan SOR SDK:
- Analyzes the current aggregated orderbook data
- Creates an execution plan that splits the order across exchanges to get the best overall price
- Executes the parts of the order in parallel on each exchange
- Combines the results into a single response
For a buy order, the SOR finds the lowest asks across exchanges and allocates the order amount starting with the best price. For a sell order, it finds the highest bids.
Order Execution Response
The execute_order()
method returns a comprehensive result with this structure:
Advanced Features
Margin Trading
To execute margin orders, enable margin mode during executor initialization:
The SDK automatically applies the appropriate margin parameters for each exchange:
- Binance: Uses cross margin with auto borrowing
- Bybit: Uses cross margin with auto borrowing
- Gate.io: Uses cross margin with auto borrowing
Sandbox/Testnet Testing
For testing without using real funds, use sandbox mode:
Make sure your API keys are set up for the testnet of each exchange.
Complete Example
Here’s a complete example of order execution:
Building Custom Execution Algorithms
You can build your own execution algorithms using the Bjarkan SOR SDK by:
- Getting the latest orderbook data with
get_latest_data("orderbook")
- Analyzing the data using your custom logic
- Creating orders with your own parameters
- Executing those orders with
execute_order()
This allows you to implement sophisticated strategies like TWAP (Time-Weighted Average Price), VWAP (Volume-Weighted Average Price), iceberg orders, or custom smart routing algorithms.
Error Handling
Always implement proper error handling when executing orders:
Important Notes
- Orderbook Requirement: The orderbook configuration must have
aggregated=True
and contain exactly one symbol. - Active Stream: You must start the orderbook stream before executing orders.
- API Keys: Ensure your API keys have trading permissions on the exchanges.
- Minimum Order Sizes: Each exchange has minimum order size requirements. Orders below these minimums will be excluded from the execution plan.
- Network Connectivity: Ensure stable internet connectivity for reliable order execution.
- Fee Awareness: The SOR considers exchange fees when making routing decisions if you provide fee information in the configuration.
Best Practices
- Start Small: Begin with small order sizes when testing.
- Use Sandbox Mode: Test your trading logic in sandbox mode before using real funds.
- Monitor Executions: Regularly check execution results and adjust your strategies accordingly.
- Handle Partial Fills: Implement logic to handle partially filled orders.
- Consider Timeouts: Implement timeout handling for long-running operations.
- Keep Logs: Maintain detailed logs of all order executions for auditing and analysis.
By following these guidelines, you can effectively use the Bjarkan SOR SDK’s order execution capabilities to optimize your trading across multiple exchanges.