Implementing TWAP Order Splitting with a Complex Event Processing Engine
In financial markets, executing a large order in a single transaction can significantly move prices and increase transaction costs. A large buy order may quickly drive prices upward, making each subsequent purchase more expensive. A large sell order may depress prices and force the asset to be sold at an unfavorable level.
Order-splitting algorithms address this by dividing a large parent order into many child orders and executing them at different points within a specified time window, which softens the impact on the market.
This is the first post in a two-part series. It walks through building a complete TWAP order-splitting system on DolphinDB's Complex Event Processing (CEP) engine. You will learn:
- The basics of the CEP engine and how it works;
- How the TWAP algorithm works, and the two practical improvements we make to it;
- How to combine event classes, monitors, stream table subscriptions, and data replay into a working system;
- How to pause, resume, and terminate a parent order while it is being executed.
1. Introduction to the CEP Engine
The CEP engine processes and analyzes complex events in event streams in real time. It lets you:
- Receive real-time data streams, define events, and detect specific events or patterns in them;
- Execute predefined actions, such as aggregation or transformation, when events match your rules;
- Extract information from event streams and uncover relationships between events.
A complete CEP application consists of a stream event serializer, a stream event deserializer, an event dispatcher, and sub-engines (monitors). The event is the fundamental element that flows through all of these components.

In short, you tell the engine "when you see this event, do that," and the engine watches the stream and fires your callbacks on a match. An order-splitting workflow, which receives a parent order, places child orders on a schedule, and reacts to intervention commands at any moment, is naturally event-driven.
2. Understanding the TWAP Algorithm
2.1 What Is TWAP?
Time-Weighted Average Price (TWAP) is one of the simplest order-splitting algorithms. It suits highly liquid markets and relatively small orders. TWAP divides the trading period into equal intervals and submits an equal portion of the order at each interval boundary, so that the average execution price tracks the TWAP:
TWAP = (price₁ + price₂ + … + priceₙ) / n
Here n is the number of intervals and priceᵢ is the price of the split order at the i-th interval boundary.
The objective is to execute an order over time while keeping its average execution price close to the TWAP benchmark, thereby helping reduce transaction costs and market impact. However, the plain TWAP algorithm has two problems:
- Large orders still have impact. If the parent order is exceptionally large, the amount allocated to each interval may still be sizable and can move a thinly traded market.
- The pattern is predictable. Uniform splitting in both time and size creates a trading pattern that other participants can easily detect. Once identified, they can position ahead of you, which raises your costs.
2.2 Improving TWAP with Randomized Execution
To address these limitations, this implementation introduces two improvements:
- Randomized child order sizes: Instead of allocating exactly the same amount to every child order, the system introduces configurable fluctuations around the average child order amount.
- Randomized execution intervals: Instead of submitting orders at strictly fixed intervals, the system randomly selects the next execution interval within a user-defined range.
These adjustments make the execution pattern less predictable while retaining the overall TWAP execution framework. In addition, the implementation introduces real-time parent order status management. Users can pause, resume, or terminate an ongoing execution strategy based on changing market conditions.
3. System Architecture
The TWAP order-splitting system is built around three key components:
- DolphinDB CEP Engine: The core component. Treats all streaming data (market data and orders) as event streams and defines the rules for processing them.
- Data Replay: Simulates real-time writes of snapshot market data.
- Stream Table Pub-Sub: Decouples parent order submission from order splitting.

The overall flow:
- The parent order event ParentOrder enters the engine and acts as the strategy start event.
- The engine creates a dedicated monitor for that parent order and starts listening for the status management event OrderAlterAction that targets it.
- Snapshot market data is replayed into a stream table, then written by a subscription callback into a keyed table that holds only the latest snapshot for each cryptocurrency.
- The core function placeOrder reads the latest order book from the keyed table, determines the child order price, and writes the child order to the child order stream table.
3.1 Data Replay: Simulating Live Market Data
Data replay is a common way to backtest high-frequency strategies in DolphinDB. Based on the chosen replay mode, it replays data from one or more tables to a target table or engine in chronological order, simulating real-time writes.
3.2 Publish-Subscribe: Decoupling Both Ends
DolphinDB distributes streaming data through a publish-subscribe model in which publishers and subscribers operate independently. This system has two subscriptions:
- The keyed table subscribes to the snapshot stream table. You define a callback function that inserts incoming data into the keyed table.
- The CEP engine subscribes to a heterogeneous stream table. When subscribing, set handler to the engine handle obtained with getStreamEngine. Incremental ParentOrder and OrderAlterAction events in the stream table are then injected into the engine.

4. Implementing the TWAP Strategy
4.1 Defining Event Classes
DolphinDB uses event classes to define the structure of events processed by the CEP engine.
We define two: the parent order ParentOrder and the status change OrderAlterAction.
ParentOrder carries basic parent order information (order ID, batch, cryptocurrency, exchange, executor, amount, direction, and so on) plus the core order-splitting parameters:
symbol:: STRING // Cryptocurrency
symbolSource:: STRING // Exchange + business type, e.g. "OKX-FUTURES"
tradeAmount:: DOUBLE // Total trade amount
tradeDirection:: STRING // "B" for buy, "S" for sell
// Order-splitting parameters
splitMethod:: STRING // Splitting algorithm
orderType:: STRING // Order type (limit/market)
price:: DOUBLE // Limit price
priceOption:: INT // Best bid or best ask price
startTime:: TIMESTAMP // Start time of order splitting
endTime:: TIMESTAMP // End time of order splitting
lowSplitInterval:: INT // Lower bound of placement interval (seconds)
highSplitInterval:: INT // Upper bound of placement interval (seconds)
orderStatus:: STRING // Order-splitting status
orderTimes:: INT // Number of child orders
intervalAmount:: DOUBLE // Floating amount for child ordersThe intervalAmount, lowSplitInterval, and highSplitInterval parameters are particularly important to the randomized TWAP implementation.
OrderAlterAction describes one intervention on a parent order:
splitOrderId:: STRING // ID of the parent order to act on
eventType:: STRING // Event type
operation:: STRING // Pause / Resume / Terminate
batchId:: STRING // Batch ID (unique ID of the parent order)
handlerEmpid:: STRING // Executor ID
handlerName:: STRING // Executor name
eventTime:: TIMESTAMP // Time the change was submittedThe core variable is operation, which specifies the operation type for this status change, such as pause, resume or terminate. The CEP engine monitor changes the parent order's orderStatus based on the operation type.
4.2 Create the Tables
The system uses four main tables:
- parentOrderManage: in-memory table recording parent orders;
- alterOrderManage: in-memory table recording status changes;
- subOrderStream: child order stream table, from which downstream systems place orders;
- orderBlobStream: the heterogeneous stream table subscribed by the CEP engine, where events of different types are serialized into a BLOB column.
// Status change record table
colNames = `splitOrderId`eventType`operation`batchId`handlerEmpid`handlerName`eventTime
colTypes = [STRING,STRING,STRING,STRING,STRING,STRING,TIMESTAMP]
share table(1:0, colNames, colTypes) as alterOrderManage
// Heterogeneous stream table subscribed by the CEP engine
share(streamTable(100:0, `timestamp`eventType`blob`splitOrderId,
[TIMESTAMP, STRING, BLOB, STRING]), "orderBlobStream")parentOrderManage and subOrderStream are created the same way; see the appendix for the full schemas.
4.3 Subscribe the Keyed Table to the Snapshot Stream
First define the snapshot stream table snapshotStream that receives replayed data, and the keyed table snapshotOutputKeyedTb that stores the latest snapshot per cryptocurrency:
colNames = `symbolSource`timestamp`symbol`offerPrice`bidPrice`offerQty`bidQty
colTypes = [SYMBOL, TIMESTAMP, SYMBOL, DOUBLE[], DOUBLE[], DOUBLE[], DOUBLE[]]
share streamTable(1:0, colNames, colTypes) as snapshotStream
snapshotOutputKeyedTbTmp = keyedTable(`symbol, 1:0, colNames, colTypes)
share snapshotOutputKeyedTbTmp as snapshotOutputKeyedTbofferPrice and offerQty hold the top 10 ask prices and quantities; bidPrice and bidQty hold the top 10 bid prices and quantities, all as array columns. The keyed table's primary key is symbol, so each cryptocurrency keeps exactly one, the latest, record.
Now subscribe with subscribeTable; the callback inserts incremental data into the keyed table:
def handleSnapshot(msg) {
data = exec * from msg
// Same primary key: the new record overwrites the old one
insert into snapshotOutputKeyedTb values(
data[`symbolSource], data[`timestamp], data[`symbol],
data[`offerPrice], data[`bidPrice], data[`offerQty], data[`bidQty])
}
subscribeTable(tableName=`snapshotStream, actionName=`handleSnapshot,
handler=handleSnapshot, msgAsTable=true, batchSize=1)4.4 Define the Monitor (the Core)
The monitor encapsulates the entire splitting strategy and is the most important and most complex part of the system. Its skeleton:
class SplitMonitor:CEPMonitor {
def SplitMonitor() {}
def initPOrderManageInfo(pOrder){...} // Record parent order info
def updatePOrderManageInfo(pOrder, opTime){...} // Update parent order status
}
class TWAPSplitMonitor:SplitMonitor {
subOrderAmounts:: DOUBLE // Total amount of child orders placed so far
parentOrder:: ParentOrder // The current parent order
def TWAPSplitMonitor() {}
def randNum(lowNum, highNum){...} // Random integer (time interval)
def randAmount(lowNum, highNum){...} // Random decimal (child order amount)
def placeOrder(){...} // Core splitting and placement
def orderAlter(oaAction){...} // Parent order status changes
def startPlaceOrder(pOrder){...} // Start splitting
def forkParentOrderMonitor(pOrder){...} // Spawn a monitor per parent order
def onload(){
addEventListener(forkParentOrderMonitor, "ParentOrder", , "all")
}
}subOrderAmounts accumulates the amount already placed, which is necessary because each child order has a random size. parentOrder holds the full information of the current parent order.
The call chain has four modules: listen for the parent order → record the parent order → check the start time and split → listen for status changes.


We'll go through them in the order the engine runs them.
onload: Initialization
After the engine is created and the monitor instantiated, onload runs first. At this point we care about one thing: waiting for a parent order.
def onload(){
addEventListener(forkParentOrderMonitor, "ParentOrder", , "all")
}This listens for every ParentOrder event (the "all" argument keeps the listener alive instead of firing once) and calls forkParentOrderMonitor on each match.
forkParentOrderMonitor: One Monitor per Parent Order
In a real market, several large orders may need splitting at the same time. If a single monitor instance handled them all, it would have only one parentOrder attribute, and each newly injected parent order would keep overwriting it, a thread-safety problem.
So the initial monitor only listens for start events. Whenever a new ParentOrder arrives, it uses spawnMonitor to create a child monitor dedicated to that parent order:
def forkParentOrderMonitor(pOrder){
name = "Parent Order Placement[" + pOrder.splitOrderId + "]"
spawnMonitor(name, startPlaceOrder, pOrder)
}startPlaceOrder: The Startup Module
def startPlaceOrder(pOrder){
parentOrder = pOrder
subOrderAmounts = 0
parentOrder.setAttr(`orderStatus, 'Init')
parentOrder.setAttr(`sortNum, 0)
initPOrderManageInfo(parentOrder) // Record the parent order
if(parentOrder.startTime == null || now() >= parentOrder.startTime){
placeOrder() // Start time reached: split immediately
}else{
// Otherwise wait until startTime
period_wait = round((parentOrder.startTime - now()) \ 1000, 0)
addEventListener(placeOrder, , , 1, , duration(period_wait + "s"))
}
// Keep listening for status changes targeting this parent order
addEventListener(orderAlter, "OrderAlterAction",
<OrderAlterAction.splitOrderId = pOrder.splitOrderId>, "all")
}It does three things:
- Initializes parentOrder and subOrderAmounts, and calls initPOrderManageInfo to write the parent order into parentOrderManage;
- Checks whether the start time has been reached: if so, it calls placeOrder() immediately; otherwise it registers a one-shot timed listener to fire at the start time;
- Registers the OrderAlterAction listener. Note the filter condition splitOrderId = pOrder.splitOrderId: each monitor reacts only to commands aimed at its own parent order.
This is the dynamic startup mentioned earlier: the status change listener is attached only after a parent order has arrived.
randNum and randAmount: Adding Randomness
randNum picks a random integer in [lowNum, highNum] for the placement interval. randAmount picks a random decimal for the child order amount:
def randNum(lowNum, highNum){
if(lowNum == highNum){ return lowNum }
nums = array(INT, 0).append!(lowNum..highNum)
indexNum = highNum - lowNum
index = rand(indexNum, 1)[0]
return nums[index]
}
def randAmount(lowNum, highNum){
if(lowNum == highNum){ return lowNum }
return lowNum + rand(1.0, 1)[0] * (highNum - lowNum)
}placeOrder: The Core Splitting Function
placeOrder is the heart of the system. We'll go through it step by step (the full code is in the appendix).
Step 1: check for timeout and whether placement is allowed.
if(now() >= parentOrder.endTime){
parentOrder.setAttr(`orderStatus, 'TimedOut')
updatePOrderManageInfo(parentOrder, now())
return
}
if(!(parentOrder.orderStatus in ['Init', 'Placing'])){
return // Paused/terminated: do not place
}This is what makes pausing work: once the status becomes Pause, this check returns immediately, so no order is placed and no next placement is scheduled.
Step 2: determine the child order amount.
amount = parentOrder.tradeAmount / parentOrder.orderTimes // Base amount per order
remainAmount = parentOrder.tradeAmount - subOrderAmounts // Amount left
lowChildOrderAmount = amount - parentOrder.intervalAmount
highChildOrderAmount = amount + parentOrder.intervalAmount
if(remainAmount >= lowChildOrderAmount){
if(remainAmount < highChildOrderAmount){
subOrderAmount = randAmount(lowChildOrderAmount, remainAmount)
}else{
subOrderAmount = randAmount(lowChildOrderAmount, highChildOrderAmount)
}
}else{
subOrderAmount = remainAmount // Less than one order left: place it all
}The amount is drawn randomly around the base amount ± intervalAmount. If the remaining amount cannot cover the upper bound, the draw is made between the lower bound and the remainder. If the remainder is below the lower bound, whatever is left is placed in one go.
Step 3: read the order book price from the keyed table and compute quantity.
v_symbol = parentOrder.symbol
if(parentOrder.priceOption == 0){
BidPrice = exec bidPrice from snapshotOutputKeyedTb where symbol = v_symbol
subOrderPrice = BidPrice[0] // Best bid
}else{
OfferPrice = exec offerPrice from snapshotOutputKeyedTb where symbol = v_symbol
subOrderPrice = OfferPrice[0] // Best ask
}
subOrderPrice = subOrderPrice[0]
subOrderQty = subOrderAmount / subOrderPrice
subOrderQty = floor(subOrderQty * 1000) / 1000.0 // Quantity cannot be arbitrarily small
subOrderAmount = subOrderQty * subOrderPrice
subOrderAmounts = subOrderAmounts + subOrderAmountThe quantity is rounded down to the instrument's precision (three decimals here), and the actual amount is then recomputed from the rounded quantity and accumulated.
Step 4: build the child order and write it to the stream table.
subOrderPlaceTime = now()
subOrderStream = objByName('subOrderStream')
insert into subOrderStream values(
parentOrder.splitOrderId, parentOrder.batchId,
parentOrder.splitOrderId + '_' + (parentOrder.sortNum + 1), parentOrder.sortNum + 1,
parentOrder.combinationNo, parentOrder.combinationName,
parentOrder.symbol, parentOrder.symbolSource,
parentOrder.tradeDate, subOrderQty, parentOrder.tradeDirection,
parentOrder.handlerEmpid, parentOrder.handlerName,
parentOrder.orderType, subOrderPrice, now())Step 5: decide whether to continue and schedule the next placement.
parentOrder.setAttr(`sortNum, parentOrder.sortNum + 1)
updateRemainAmount()
if(parentOrder.sortNum < parentOrder.orderTimes){
parentOrder.setAttr(`orderStatus, 'Placing')
updatePOrderManageInfo(parentOrder, subOrderPlaceTime)
realTime = randNum(parentOrder.lowSplitInterval, parentOrder.highSplitInterval)
// Fire placeOrder again after a random interval
addEventListener(placeOrder, , , 1, , duration(realTime + "s"))
}else{
parentOrder.setAttr(`orderStatus, 'Placed')
updatePOrderManageInfo(parentOrder, now())
destroyMonitor() // All done: destroy the monitor
}This is event-driven scheduling in practice. Instead of a loop with sleep, each placement registers a one-shot timed listener so the engine calls placeOrder again after a random delay. After the last child order, destroyMonitor() releases the resources.
orderAlter: Parent Order Status Management
When an OrderAlterAction event is injected, the engine calls orderAlter:
def orderAlter(oaAction){
alterOrderManage = objByName('alterOrderManage')
insert into alterOrderManage values(oaAction.splitOrderId, oaAction.eventType,
oaAction.operation, oaAction.batchId, oaAction.handlerEmpid,
oaAction.handlerName, now())
if(oaAction.operation == 'Pause'){
parentOrder.setAttr(`orderStatus, 'Pause')
updatePOrderManageInfo(parentOrder, now())
}else if(oaAction.operation == 'Resume' && parentOrder.orderStatus == 'Pause'){
parentOrder.setAttr(`orderStatus, 'Placing')
updatePOrderManageInfo(parentOrder, now())
placeOrder() // Resume placing immediately
}else if(oaAction.operation == 'Terminate'){
parentOrder.setAttr(`orderStatus, 'Terminate')
updatePOrderManageInfo(parentOrder, now())
destroyMonitor() // Terminate and destroy the monitor
}
}The logic is simple. The operation is first logged to the change table, then handled by type:
- Pause: sets the status to Pause. The status check in placeOrder then fails, so splitting stops.
- Resume: effective only when the current status is Pause. It sets the status back to Placing and calls placeOrder() to continue.
- Terminate: sets the terminated status and destroys the monitor, ending the parent order for good.
4.5 Create the CEP Engine and Subscribe to the Heterogeneous Stream Table
Create the engine with createCEPEngine, then have it subscribe to orderBlobStream, which carries both ParentOrder and OrderAlterAction events:
dummy = table(1:0, `timestamp`eventType`blob`splitOrderId, `TIMESTAMP`STRING`BLOB`STRING)
engine = createCEPEngine(name='TwapSplitMonitor', monitors=<TWAPSplitMonitor()>,
dummyTable=dummy, eventSchema=[ParentOrder, OrderAlterAction], timeColumn=`timestamp)
subscribeTable(tableName="orderBlobStream", actionName="orderBlobStream",
handler=getStreamEngine("TwapSplitMonitor"), msgAsTable=true)4.6 Replay Market Data
Use replay to feed historical snapshots from a DFS table into snapshotStream, simulating live market data. Snapshot data for cryptocurrencies can be huge, so this example replays only ETHUSDT:
snapshotTb = loadTable("dfs://TWAP", "TWAPDATA")
replayData = select symbolSource, timestamp, symbol, offerPrice, bidPrice, offerQty, bidQty
from snapshotTb
where symbol == "ETHUSDT" and timestamp >= 2025.11.15
submitJob("replay_snapshot_crypto", "snapshot_crypto",
replay{replayData, snapshotStream, `timestamp, `timestamp, 1, false, , , true})A replay rate of 1 means original speed. Once data reaches snapshotStream, the subscription callback pushes it into snapshotOutputKeyedTb, which always holds the latest order book.
4.7 Start a Strategy Instance
You can start the strategy by writing a parent order to orderBlobStream with DolphinDB's Java API. The splitting parameters come from a HashMap, simulating how a real system would pass user-defined parameters:
public static HashMap<String, Object> getMap(){
HashMap<String, Object> map = new HashMap<>();
map.put("splitMethod", "TWAP");
map.put("orderType", "Limit");
map.put("price", 10.5);
map.put("startTime", LocalDateTime.now());
map.put("endTime", LocalDateTime.now().plusHours(5));
map.put("lowSplitInterval", 5); // Interval lower bound: 5 s
map.put("highSplitInterval", 10); // Interval upper bound: 10 s
map.put("priceOption", 0); // 0: use the best bid price
map.put("orderTimes", 5); // Split into 5 child orders
map.put("intervalAmount", 5000.0); // Amount floats by +/- 5,000
return map;
}After sending the parent order, the example sends a Pause and then a Resume command, five seconds apart, so you can watch the engine manage the status:
sender1.sendEvent(parentOrder.getEventType(), parentOrder.toEntities());
Thread.sleep(5000);
sender1.sendEvent(pauseAction.getEventType(), pauseAction.toEntities()); // Pause
Thread.sleep(5000);
sender1.sendEvent(resumeAction.getEventType(), resumeAction.toEntities()); // ResumeYou can also skip Java and inject events directly from a DolphinDB script:
getStreamEngine(`TwapSplitMonitor).appendEvent(ParentOrder)
orderAlterAction = OrderAlterAction("501599", OrderAlterAction, "Pause", "501599", "E001", "tester", now())
getStreamEngine(`TwapSplitMonitor).appendEvent(orderAlterAction)5. Running the System and Reviewing Results
5.1 Environment Setup
- JDK 1.8 (the example uses 1.8.0_441) and Maven 3.8.6;
- Download and extract the code from the appendix. Place data/TWAPDATA.csv in the DolphinDB server directory, run data/data_input.dos to create the database and tables and import the data, and import data/dashboard.DC_TWAP_Monitoring.json into the Dashboard;
- Edit the database connection settings in common/DBUtil.java.
5.2 Execution Order
Run the scripts in this order:
- 01 clearEnv.dos: clears existing shared tables, subscriptions, and engines to avoid duplicate definitions;
- 02 Event.dos: defines the event classes;
- 03 createTable.dos: creates the parent order and child order tables;
- 04 subscribeSnapshot.dos: subscribes the keyed table to the snapshot stream;
- 05 Monitor.dos: defines the monitor;
- 06 createCEPEngine.dos: creates the CEP engine and subscribes it to the heterogeneous stream table;
- 07 replaySnapshot.dos: replays snapshot data.
After the replay, you can inspect the latest order book with:
select * from snapshotOutputKeyedTb
Finally, run startTWAP.java, or execute 08 simulate.dos, to write the parent order and status change events into the heterogeneous stream table.
5.3 Reading the Results
The Dashboard shows the output in the Parent Order Monitoring, Child Order Monitoring, and Order Update Monitoring panels.

The example uses a parent order of 1,000,000, five placements, a floating amount of 5,000, placement intervals of 5–10 seconds, and priceOption = 0 (best bid). So you should see that:
- The base child order amount is 200,000, and actual child orders float between 195,000 and 205,000;
- The interval between placements is random between 5 and 10 seconds;
- Child order prices equal the best bid for ETHUSDT in snapshotOutputKeyedTb;
- After a Pause, placement stops, and after a Resume it continues; both operations are logged in the change table.
6. Summary
This post built a TWAP order-splitting system with randomization and status management on DolphinDB's CEP engine. The key design points:
- One monitor per parent order. spawnMonitor creates an isolated child monitor, so concurrent parent orders don't overwrite each other's state;
- A keyed table for the latest order book. The splitting logic gets current prices cheaply, without scanning historical snapshots;
- Event-driven timing. One-shot timed listeners replace loops and sleeps, so both the placement rhythm and the interventions are driven by events;
- Randomization against predictability. Child order sizes and intervals vary within configured ranges;
- State-based intervention. orderStatus together with Pause, Resume, and Terminate gives real-time control over running parent orders.
TWAP splits evenly over time and ignores how trading volume is distributed across the day. In the next post we'll look at VWAP (Volume-Weighted Average Price), which sizes each child order according to historical volume, placing more during high-volume periods and less during quiet ones.