XOR
XOR Algorithm (Crossing Order Router)
The XOR algorithm is a sophisticated trading algorithm that merges the capabilities of a Smart Order Router (SOR) with an internal order-crossing matching engine. Its primary goal is to provide optimal execution by seamlessly combining internal and external liquidity.
How Internal Liquidity is Generated
In Botmain, internal liquidity originates from multiple trading strategies running simultaneously within the platform. Let's break down how this works:
Multiple Strategies: Botmain hosts various trading strategies, each focused on different instruments or using different approaches.
Order Generation: These strategies continuously analyze market conditions and generate orders (buy or sell) based on their logic.
XOR's Internal Order Book: XOR collects the orders from all running strategies and forms a unified internal order book. This order book provides XOR with a pool of 'resting' liquidity.
The XOR Advantage
Internal Matching: If an incoming order can be matched with an existing resting order from another strategy, XOR facilitates an internal trade without the need to send the order to an external exchange. This can result in faster execution and potential cost savings.
External Execution: When an internal match isn't possible, XOR leverages its SOR capabilities to route the order to external exchanges for execution.
Limitations
- Not Ideal for External Market Making: XOR is primarily intended for those who want to take advantage of internal liquidity or trade against external exchanges. It's not the best choice for those who focus exclusively on market making on external venues.
Algorithm Logic
Combined Order Book: XOR maintains a unified internal order book that aggregates:
- Market data from subscribed external exchanges.
- Liquidity from incoming orders.
Order Matching: XOR matches incoming orders with this combined book using the following priority:
- External Quotes: It seeks the best external quotes. If found, XOR generates IOC LIMIT child orders to target those quotes on the respective exchanges.
- Internal Orders: For matching "resting" internal orders, XOR records a trade and generates corresponding trade events.
Managing Unmatched Liquidity: If an order has a remaining unfilled portion, that liquidity then "rests" within the XOR system. XOR continues to try and match it against new external quotes or incoming internal orders in a FIFO (first-in, first-out) manner.
Order Expiration: Orders that remain unmatched may expire based on their time-in-force settings.
Supported Order Types and Time-in-Force
- Order Types: MARKET, LIMIT, STOP, STOP_LIMIT
- Time-in-Force: IOC, GTD, GTC
Parameters
Here's a breakdown of XOR-specific configuration parameters:
orderExpirationTimeout
(duration): How long GTC orders remain valid.marketOrderPriceOffset
(int): Controls LIMIT order price adjustments from the current BBO when targeting external market orders.maxRejections
(int): Maximum consecutive rejections before an exchange is temporarily blacklisted.exchangeConstraints
: Define supported order types and TIFs per exchange.
Optional Settings
schedule
: Define downtime periods where XOR will not execute trades or send orders.
import { XOROrder } from '@botmain/execution';
const btcXorOrder = new XOROrder(
'BTCUSD',
0.2,
'buy',
undefined, // Market order, so price isn't needed
TimeInForce .IMMEDIATE_OR_CANCEL
);
await this.tradingApi.sendOrder(btcXorOrder);
// 1. Attempt to match 'btcXorOrder' against its internal order book.
// 2. If no match is found, route the order (or child orders) to external exchanges based on SOR logic.