Skip to content

Commit ed6fc8b

Browse files
committed
Apply pre-trade risk to the destination execution account
- Resolve risk accounts from explicit client, venue route, then default - Record execution venue routes and external clients in the cache - Deny orders whose routed account is unresolved or not yet cached - Limit position, pending-order, and REDUCING checks to that account - Fix Postgres shared-account test racing its currency insert - Resolves #4946
1 parent 3ed8899 commit ed6fc8b

14 files changed

Lines changed: 1168 additions & 63 deletions

File tree

‎RELEASES.md‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ Released on TBD (UTC).
88
- Added configurable `writer_capacity` limits for socket and WebSocket clients, defaulting to 1,024 messages
99
- Added same-venue execution client registration with explicit venue or default routing
1010
- Added Rust `Cache::account_id_for_client` to resolve execution client accounts independent of issuers
11+
- Added Rust `Cache::client_id_for_venue` to resolve the execution client that venue or default routing selects
1112
- Added Python `Cache.top_of_book()` without cloning the resident book (#5011), thanks @youayouly
1213
- Added `IndexPriceUpdate`, `InstrumentClose`, `FundingRateUpdate`, and `Custom` to `DataBackendSession.add_file`
1314
- Added aggregate instrument fan-out across class directories to `list_parquet_files`
@@ -116,6 +117,8 @@ Released on TBD (UTC).
116117
- Fixed fill OMS resolution to use the owning execution client instead of venue or default routes
117118
- Fixed venue account lookups depending on add order and index rebuilds when accounts share an issuer
118119
- Fixed Python portfolio queries rejecting an explicit account when another account shares its issuer
120+
- Fixed `RiskEngine` using the venue account, not the routed client's (#4946), thanks for reporting @Artur-Sulej
121+
- Fixed `RiskEngine` position-reducing checks counting positions and open orders of other accounts
119122
- Fixed later submits denying or double-routing orders already handed to an execution client (#5020), thanks @s1amese2003
120123
- Fixed overlapping mass-status snapshots reversing newer cached fills or fill voids
121124
- Fixed trailing-stop orders already in the market being accepted despite `reject_stop_orders`

‎crates/common/src/cache/mod.rs‎

Lines changed: 56 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,9 @@ pub struct Cache {
129129
yield_curves: AHashMap<String, YieldCurveData>,
130130
external_order_claims: AHashMap<InstrumentId, StrategyId>,
131131
client_accounts: AHashMap<ClientId, AccountId>,
132+
client_routes: AHashMap<Venue, ClientId>,
133+
default_client_id: Option<ClientId>,
134+
external_clients: AHashSet<ClientId>,
132135
accounts: AHashMap<AccountId, SharedCell<AccountAny>>,
133136
orders: AHashMap<ClientOrderId, SharedCell<OrderAny>>,
134137
order_lists: AHashMap<OrderListId, OrderList>,
@@ -164,6 +167,9 @@ impl Debug for Cache {
164167
.field("yield_curves", &self.yield_curves)
165168
.field("external_order_claims", &self.external_order_claims)
166169
.field("client_accounts", &self.client_accounts)
170+
.field("client_routes", &self.client_routes)
171+
.field("default_client_id", &self.default_client_id)
172+
.field("external_clients", &self.external_clients)
167173
.field("accounts", &self.accounts)
168174
.field("orders", &self.orders)
169175
.field("order_lists", &self.order_lists)
@@ -233,6 +239,9 @@ impl Cache {
233239
yield_curves: AHashMap::new(),
234240
external_order_claims: AHashMap::new(),
235241
client_accounts: AHashMap::new(),
242+
client_routes: AHashMap::new(),
243+
default_client_id: None,
244+
external_clients: AHashSet::new(),
236245
accounts: AHashMap::new(),
237246
orders: AHashMap::new(),
238247
order_lists: AHashMap::new(),
@@ -377,6 +386,51 @@ impl Cache {
377386
self.client_accounts.remove(client_id);
378387
}
379388

389+
/// Returns the execution client registered as the route for `venue`, else the default client.
390+
#[must_use]
391+
pub fn client_id_for_venue(&self, venue: &Venue) -> Option<&ClientId> {
392+
self.client_routes
393+
.get(venue)
394+
.or(self.default_client_id.as_ref())
395+
}
396+
397+
/// Registers the execution client `client_id` as the route for commands on `venue`, replacing
398+
/// any previous route for the venue.
399+
///
400+
/// Routes and the default client survive [`Self::clear_index`] and [`Self::reset`].
401+
pub fn add_client_route(&mut self, client_id: ClientId, venue: Venue) {
402+
self.client_routes.insert(venue, client_id);
403+
}
404+
405+
/// Registers the execution client `client_id` as the default route for venues without a
406+
/// route, replacing any previous default.
407+
pub fn set_default_client(&mut self, client_id: ClientId) {
408+
self.default_client_id = Some(client_id);
409+
}
410+
411+
/// Removes the venue routes and default route of the execution client `client_id`.
412+
pub fn remove_client_routes(&mut self, client_id: &ClientId) {
413+
self.client_routes.retain(|_, routed| routed != client_id);
414+
415+
if self.default_client_id.as_ref() == Some(client_id) {
416+
self.default_client_id = None;
417+
}
418+
}
419+
420+
/// Returns whether `client_id` is registered as an external execution client, whose commands
421+
/// are published for another process to execute.
422+
#[must_use]
423+
pub fn is_external_client(&self, client_id: &ClientId) -> bool {
424+
self.external_clients.contains(client_id)
425+
}
426+
427+
/// Registers `client_id` as an external execution client.
428+
///
429+
/// Registrations survive [`Self::clear_index`] and [`Self::reset`].
430+
pub fn add_external_client(&mut self, client_id: ClientId) {
431+
self.external_clients.insert(client_id);
432+
}
433+
380434
/// Sets the cache database adapter for persistence.
381435
///
382436
/// This allows setting or replacing the database adapter after cache construction.
@@ -1980,8 +2034,8 @@ impl Cache {
19802034
/// All stateful fields are reset to their initial value. Instruments,
19812035
/// currencies, and synthetics are retained when `drop_instruments_on_reset`
19822036
/// is `false` so that repeated backtest runs can reuse the same dataset. External order claims
1983-
/// and execution client account registrations are retained so registered strategy and client
1984-
/// routing remain configured across resets.
2037+
/// and execution client account, route, and external client registrations are retained so
2038+
/// registered strategy and client routing remain configured across resets.
19852039
pub fn reset(&mut self) {
19862040
log::debug!("Resetting cache");
19872041

‎crates/common/src/cache/tests.rs‎

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5671,6 +5671,45 @@ fn test_cache_client_accounts_survive_index_rebuild_and_reset(
56715671
assert_eq!(after_remove, (None, Some(bravo_account_id)));
56725672
}
56735673

5674+
#[rstest]
5675+
fn test_cache_client_routes_survive_index_rebuild_and_reset(mut cache: Cache) {
5676+
let alpha_id = ClientId::from("ALPHA");
5677+
let bravo_id = ClientId::from("BRAVO");
5678+
let external_id = ClientId::from("EXTERNAL");
5679+
let sim = Venue::from("SIM");
5680+
let xnas = Venue::from("XNAS");
5681+
cache.add_client_route(alpha_id, sim);
5682+
cache.set_default_client(bravo_id);
5683+
cache.add_external_client(external_id);
5684+
5685+
let resolve = |cache: &Cache| {
5686+
(
5687+
cache.client_id_for_venue(&sim).copied(),
5688+
cache.client_id_for_venue(&xnas).copied(),
5689+
cache.is_external_client(&external_id),
5690+
cache.is_external_client(&alpha_id),
5691+
)
5692+
};
5693+
5694+
cache.clear_index();
5695+
cache.build_index();
5696+
let after_rebuild = resolve(&cache);
5697+
cache.reset();
5698+
let after_reset = resolve(&cache);
5699+
cache.remove_client_routes(&alpha_id);
5700+
let after_remove_route = resolve(&cache);
5701+
cache.remove_client_routes(&bravo_id);
5702+
let after_remove_default = resolve(&cache);
5703+
5704+
assert_eq!(after_rebuild, (Some(alpha_id), Some(bravo_id), true, false));
5705+
assert_eq!(after_reset, (Some(alpha_id), Some(bravo_id), true, false));
5706+
assert_eq!(
5707+
after_remove_route,
5708+
(Some(bravo_id), Some(bravo_id), true, false)
5709+
);
5710+
assert_eq!(after_remove_default, (None, None, true, false));
5711+
}
5712+
56745713
#[rstest]
56755714
fn test_cache_account_mut_returns_none_for_missing_account(mut cache: Cache) {
56765715
assert!(cache.account_mut(&AccountId::test_default()).is_none());

‎crates/common/src/python/cache.rs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1693,8 +1693,8 @@ impl Cache {
16931693
/// All stateful fields are reset to their initial value. Instruments,
16941694
/// currencies, and synthetics are retained when `drop_instruments_on_reset`
16951695
/// is `false` so that repeated backtest runs can reuse the same dataset. External order claims
1696-
/// and execution client account registrations are retained so registered strategy and client
1697-
/// routing remain configured across resets.
1696+
/// and execution client account, route, and external client registrations are retained so
1697+
/// registered strategy and client routing remain configured across resets.
16981698
#[pyo3(name = "reset")]
16991699
fn py_reset(&mut self) {
17001700
self.reset();

‎crates/event_store/src/replay.rs‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2290,6 +2290,28 @@ mod tests {
22902290
CacheMutationRecoveryClass::SnapshotOwned,
22912291
&[],
22922292
),
2293+
cache_mutation(
2294+
// Reapplied by execution client routing setup on every node start
2295+
"add_client_route",
2296+
CacheMutationRecoveryClass::SnapshotOwned,
2297+
&[],
2298+
),
2299+
cache_mutation(
2300+
"set_default_client",
2301+
CacheMutationRecoveryClass::SnapshotOwned,
2302+
&[],
2303+
),
2304+
cache_mutation(
2305+
"remove_client_routes",
2306+
CacheMutationRecoveryClass::SnapshotOwned,
2307+
&[],
2308+
),
2309+
cache_mutation(
2310+
// Reapplied from execution engine configuration on every node start
2311+
"add_external_client",
2312+
CacheMutationRecoveryClass::SnapshotOwned,
2313+
&[],
2314+
),
22932315
cache_mutation(
22942316
"set_database",
22952317
CacheMutationRecoveryClass::SnapshotOwned,

‎crates/execution/src/engine/mod.rs‎

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,20 @@ impl ExecutionEngine {
154154
config: Option<ExecutionEngineConfig>,
155155
) -> Self {
156156
let trader_id = get_message_bus().borrow().trader_id;
157+
let external_clients: HashSet<ClientId> = config
158+
.as_ref()
159+
.and_then(|c| c.external_clients.clone())
160+
.unwrap_or_default()
161+
.into_iter()
162+
.collect();
163+
{
164+
let mut cache = cache.borrow_mut();
165+
166+
for client_id in &external_clients {
167+
cache.add_external_client(*client_id);
168+
}
169+
}
170+
157171
Self {
158172
clock: clock.clone(),
159173
cache,
@@ -162,12 +176,7 @@ impl ExecutionEngine {
162176
routing_map: AHashMap::new(),
163177
instrument_venues: AHashSet::new(),
164178
oms_overrides: AHashMap::new(),
165-
external_clients: config
166-
.as_ref()
167-
.and_then(|c| c.external_clients.clone())
168-
.unwrap_or_default()
169-
.into_iter()
170-
.collect(),
179+
external_clients,
171180
pos_id_generator: PositionIdGenerator::new(trader_id, clock),
172181
config: config.unwrap_or_default(),
173182
orders_dispatched: RefCell::new(AHashSet::new()),
@@ -394,9 +403,11 @@ impl ExecutionEngine {
394403
pub fn register_default_client(&mut self, client: Box<dyn ExecutionClient>) {
395404
let client_id = client.client_id();
396405
let adapter = ExecutionClientAdapter::new(client);
397-
self.cache
398-
.borrow_mut()
399-
.add_client_account(client_id, adapter.account_id);
406+
{
407+
let mut cache = self.cache.borrow_mut();
408+
cache.add_client_account(client_id, adapter.account_id);
409+
cache.set_default_client(client_id);
410+
}
400411

401412
self.clients.insert(client_id, adapter);
402413
self.default_client_id = Some(client_id);
@@ -417,6 +428,8 @@ impl ExecutionEngine {
417428
if !self.clients.contains_key(&client_id) {
418429
anyhow::bail!("No client registered with ID {client_id}");
419430
}
431+
432+
self.cache.borrow_mut().set_default_client(client_id);
420433
self.default_client_id = Some(client_id);
421434
log::debug!("Set client {client_id} as default");
422435
Ok(())
@@ -578,6 +591,7 @@ impl ExecutionEngine {
578591
);
579592
}
580593

594+
self.cache.borrow_mut().add_client_route(client_id, venue);
581595
self.routing_map.insert(venue, client_id);
582596
log::info!("Set client {client_id} routing for {venue}");
583597
Ok(())
@@ -635,7 +649,11 @@ impl ExecutionEngine {
635649
/// Returns an error if no client is registered with the given ID.
636650
pub fn deregister_client(&mut self, client_id: ClientId) -> anyhow::Result<()> {
637651
if self.clients.shift_remove(&client_id).is_some() {
638-
self.cache.borrow_mut().remove_client_account(&client_id);
652+
{
653+
let mut cache = self.cache.borrow_mut();
654+
cache.remove_client_account(&client_id);
655+
cache.remove_client_routes(&client_id);
656+
}
639657

640658
if self.default_client_id == Some(client_id) {
641659
self.default_client_id = None;

‎crates/execution/tests/integration/exec_engine.rs‎

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -700,6 +700,78 @@ fn test_client_accounts_follow_client_registration(
700700
assert_eq!(after_deregister, (None, Some(bravo_account_id)));
701701
}
702702

703+
#[rstest]
704+
fn test_client_routes_follow_client_routing(
705+
mut execution_engine: ExecutionEngine,
706+
#[values(false, true)] register_default: bool,
707+
) {
708+
let sim = Venue::from("SIM");
709+
let xnas = Venue::from("XNAS");
710+
let alpha_id = ClientId::from("ALPHA");
711+
let bravo_id = ClientId::from("BRAVO");
712+
713+
let alpha = StubExecutionClient::new(
714+
alpha_id,
715+
AccountId::from("SIM-001"),
716+
sim,
717+
OmsType::Netting,
718+
None,
719+
);
720+
721+
let bravo = StubExecutionClient::new(
722+
bravo_id,
723+
AccountId::from("SIM-002"),
724+
sim,
725+
OmsType::Netting,
726+
None,
727+
);
728+
execution_engine.register_client(Box::new(alpha)).unwrap();
729+
730+
if register_default {
731+
execution_engine.register_default_client(Box::new(bravo));
732+
} else {
733+
execution_engine.register_client(Box::new(bravo)).unwrap();
734+
execution_engine.set_default_client(bravo_id).unwrap();
735+
}
736+
737+
execution_engine
738+
.register_venue_routing(alpha_id, sim)
739+
.unwrap();
740+
741+
let resolve = |engine: &ExecutionEngine| {
742+
let cache = engine.cache().borrow();
743+
(
744+
cache.client_id_for_venue(&sim).copied(),
745+
cache.client_id_for_venue(&xnas).copied(),
746+
)
747+
};
748+
749+
let routed = resolve(&execution_engine);
750+
let reroute_result = execution_engine.register_venue_routing(bravo_id, sim);
751+
let redefault_result = execution_engine.set_default_client(alpha_id);
752+
let after_rejected = resolve(&execution_engine);
753+
execution_engine.reset();
754+
let after_reset = resolve(&execution_engine);
755+
execution_engine.deregister_client(alpha_id).unwrap();
756+
let after_route_deregister = resolve(&execution_engine);
757+
execution_engine.deregister_client(bravo_id).unwrap();
758+
let after_default_deregister = resolve(&execution_engine);
759+
760+
assert_eq!(routed, (Some(alpha_id), Some(bravo_id)));
761+
assert_eq!(
762+
reroute_result.unwrap_err().to_string(),
763+
"Venue SIM already routed to ALPHA, cannot re-route to BRAVO"
764+
);
765+
assert_eq!(
766+
redefault_result.unwrap_err().to_string(),
767+
"default client already registered"
768+
);
769+
assert_eq!(after_rejected, (Some(alpha_id), Some(bravo_id)));
770+
assert_eq!(after_reset, (Some(alpha_id), Some(bravo_id)));
771+
assert_eq!(after_route_deregister, (Some(bravo_id), Some(bravo_id)));
772+
assert_eq!(after_default_deregister, (None, None));
773+
}
774+
703775
#[rstest]
704776
fn test_check_connected_when_client_connected_returns_true(mut execution_engine: ExecutionEngine) {
705777
let mut stub_client = StubExecutionClient::new(

‎crates/infrastructure/tests/integration/test_cache_database_postgres.rs‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2547,6 +2547,13 @@ mod serial_tests {
25472547
.unwrap();
25482548
pg_cache_a.add_currency(&Currency::USD()).unwrap();
25492549

2550+
// Each cache writes on its own connection, so B's account needs A's currency committed
2551+
wait_until_async(
2552+
|| async { count_rows(&pg_cache_a.pool, "currency").await == 1 },
2553+
Duration::from_secs(5),
2554+
)
2555+
.await;
2556+
25502557
let account_a = AccountAny::Cash(CashAccount::new(
25512558
cash_account_state_million_usd("1000000 USD", "0 USD", "1000000 USD"),
25522559
false,

0 commit comments

Comments
 (0)