nautechsystems/nautilus_trader · error
Position id should be generated. Hedging Oms type order matc
Error message
Position id should be generated. Hedging Oms type order matching engine doesn't exist in cache.
What it means
The matching engine's position-ID generator could not resolve a position ID for an order under a hedging OMS type. Under hedging mode the position ID normally comes from an existing matching-engine position in the cache; when none exists and generation is not allowed, the engine panics. This means the order reached the matching engine without the preconditions needed for hedging position accounting.
Source
Thrown at crates/execution/src/matching_engine/ids_generator.rs:167
&mut self,
order: &OrderAny,
generate: Option<bool>,
) -> Option<PositionId> {
let generate = generate.unwrap_or(true);
if self.oms_type == OmsType::Hedging {
{
let cache = self.cache.as_ref().borrow();
let position_id_result = cache.position_id(&order.client_order_id());
if let Some(position_id) = position_id_result {
return Some(position_id.to_owned());
}
}
if generate {
self.generate_venue_position_id()
} else {
panic!(
"Position id should be generated. Hedging Oms type order matching engine doesn't exist in cache."
)
}
} else {
// Netting OMS (position id will be derived from instrument and strategy)
let cache = self.cache.as_ref().borrow();
let positions_open = cache.positions_open(
None,
Some(&order.instrument_id()),
Some(&order.strategy_id()),
None,
None,
);
positions_open.first().map(|position| position.id)
}
}
/// Generates a deterministic trade ID.View on GitHub (pinned to 18893faf8b)
Solutions
- Configure the matching engine/adapter so hedging venues allow venue position-ID generation (check OmsType and position_id generation config for the adapter).
- Verify the instrument's OMS type matches the venue reality; use Netting if the venue does not support hedging position IDs for this flow.
- Ensure the strategy/instrument cache state is consistent — clear stale cache entries after changing OMS type.
- Check adapter-specific config (e.g. `hedge_mode` / `generate_position_id` options) before submitting orders.
Example fix
// before
let engine = OrderMatchingEngine::new(..., OmsType::Hedging, ...); // generation disabled
// after
let engine = OrderMatchingEngine::new(..., OmsType::Hedging, ...)
.with_generate_venue_position_ids(true); // allow generation when no cached position exists Defensive patterns
Strategy: validation
Validate before calling
// before submitting on a hedging venue
if engine.oms_type() == OmsType::Hedging
&& cache.position_for(&instrument_id, &strategy_id).is_none()
&& !venue_supports_position_id_generation
{
return Err(anyhow!("hedging venue requires existing position or generation enabled"));
} Type guard
fn can_trade_hedging(engine: &OrderMatchingEngine, instrument_id: &InstrumentId) -> bool {
engine.oms_type() != OmsType::Hedging || engine.generates_venue_position_ids()
} Prevention
- Match OmsType configuration to the venue's actual position mode (hedge vs one-way).
- Enable venue position-ID generation for hedging adapters that support it.
- Clear stale cache state after changing OMS type.
- Test first order submission on a fresh hedging instrument before going live.
When it happens
Trigger: Calling get_position_id (OrderMatchingEngine) for an order whose OMS type is Hedging while the cache holds no position for the instrument/strategy and the `generate` path was not taken (generate=false because a cache lookup was expected).
Common situations: Submitting orders to a hedging-mode venue adapter (e.g. Binance Futures/Bybit hedging) before any position exists and with position-ID generation misconfigured; switching an instrument's OMS type between netting and hedging without clearing stale cache state; submitting submits concurrently where the position was just closed.
Understand the failure class
Background: "Invalid state transition" errors: "status must be X, actually Y", "already rejected/charging/uninstalled", "cannot ... while running" — what they mean when a library rejects your call — this error's family across 31 libraries.
Related errors
- Matching engine not found for instrument {order_instrument_i
- Matching engine not found for instrument {instrument_id}
- OTO parent not found
- Cannot find contingent order for {client_order_id}
- Limit order must have a price
AI-assisted analysis of nautechsystems/nautilus_trader@18893faf8b (2026-09-08).
Data as JSON: /api/errors/3af97cddadac98c1.
Report an issue: GitHub.