{"record":{"id":"f9a9ceca187fa927","repo":"nautechsystems/nautilus_trader","slug":"python-on-mark-price-failed-e","errorCode":null,"errorMessage":"Python on_mark_price failed: {e}","messagePattern":"Python on_mark_price failed: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/common/src/python/actor.rs","lineNumber":1133,"sourceCode":"\n    fn on_book_deltas(&mut self, deltas: &OrderBookDeltas) -> anyhow::Result<()> {\n        self.dispatch_on_book_deltas(deltas.clone())\n            .map_err(|e| anyhow::anyhow!(\"Python on_book_deltas failed: {e}\"))\n    }\n\n    fn on_book_depth(&mut self, depth: &OrderBookDepth10) -> anyhow::Result<()> {\n        self.dispatch_on_book_depth(depth)\n            .map_err(|e| anyhow::anyhow!(\"Python on_book_depth failed: {e}\"))\n    }\n\n    fn on_book(&mut self, order_book: &OrderBook) -> anyhow::Result<()> {\n        self.dispatch_on_book(order_book)\n            .map_err(|e| anyhow::anyhow!(\"Python on_book failed: {e}\"))\n    }\n\n    fn on_mark_price(&mut self, mark_price: &MarkPriceUpdate) -> anyhow::Result<()> {\n        self.dispatch_on_mark_price(*mark_price)\n            .map_err(|e| anyhow::anyhow!(\"Python on_mark_price failed: {e}\"))\n    }\n\n    fn on_index_price(&mut self, index_price: &IndexPriceUpdate) -> anyhow::Result<()> {\n        self.dispatch_on_index_price(*index_price)\n            .map_err(|e| anyhow::anyhow!(\"Python on_index_price failed: {e}\"))\n    }\n\n    fn on_funding_rate(&mut self, funding_rate: &FundingRateUpdate) -> anyhow::Result<()> {\n        self.dispatch_on_funding_rate(*funding_rate)\n            .map_err(|e| anyhow::anyhow!(\"Python on_funding_rate failed: {e}\"))\n    }\n\n    fn on_instrument_status(&mut self, data: &InstrumentStatus) -> anyhow::Result<()> {\n        self.dispatch_on_instrument_status(*data)\n            .map_err(|e| anyhow::anyhow!(\"Python on_instrument_status failed: {e}\"))\n    }\n\n    fn on_instrument_close(&mut self, update: &InstrumentClose) -> anyhow::Result<()> {","sourceCodeStart":1115,"sourceCodeEnd":1151,"githubUrl":"https://github.com/nautechsystems/nautilus_trader/blob/18893faf8b356be3320add8de2f861b0b647cf06/crates/common/src/python/actor.rs#L1115-L1151","documentation":"This error wraps any Python exception raised inside the actor's `on_mark_price` callback when a `MarkPriceUpdate` is dispatched to the Python subclass. The Rust `PyDataActor` calls the Python method via `call_method1` and converts the `PyErr` into an anyhow error with this message. The real failure is in user Python code; the wrapped traceback after `{e}` points to it.","triggerScenarios":"Any exception raised by the user's `on_mark_price(self, mark_price)` override — e.g. arithmetic on a None/NaN mark price, keying a dict by an attribute that doesn't exist on the converted MarkPriceUpdate, or a raising conversion of MarkPriceUpdate into a Python object.","commonSituations":"Derivatives strategies computing funding/PnL from mark prices that hit a NaN first tick; renaming mark price fields after a nautilus_trader upgrade so the handler references stale attributes; subscribing to mark price without realizing the callback runs on every update.","solutions":["Read the appended Python traceback after 'Python on_mark_price failed:' to find the raising line in your handler.","Guard against None/NaN mark prices before arithmetic (check `math.isnan` / None).","Verify attribute names against the MarkPriceUpdate API for your installed nautilus_trader version.","Add unit tests that feed synthetic MarkPriceUpdate messages to the actor callback."],"exampleFix":"// before\ndef on_mark_price(self, mark_price):\n    spread = mark_price.mark_price - self.mid\n\n// after\ndef on_mark_price(self, mark_price):\n    mp = mark_price.mark_price\n    if mp is None or mp != mp:  # None or NaN\n        return\n    spread = mp - self.mid","handlingStrategy":"type-guard","validationCode":"import math\nif mark_price.mark_price is None or math.isnan(mark_price.mark_price):\n    return","typeGuard":"def has_valid_mark_price(update):\n    mp = getattr(update, \"mark_price\", None)\n    return isinstance(mp, (int, float)) and not math.isnan(mp)","tryCatchPattern":"def on_mark_price(self, mark_price):\n    try:\n        self._update_mark(mark_price)\n    except Exception as e:\n        self.log.error(f\"on_mark_price failed: {e}\", exc_info=True)","preventionTips":["Check for NaN/None on every mark price before arithmetic","Pin and review the MarkPriceUpdate API for your nautilus_trader version before upgrading","Subscribe only to mark prices for instruments you actively trade","Add handler unit tests with synthetic updates"],"tags":["python","rust-bridge","callback-exception","actor"],"backgroundTag":"python-callback-failed","analyzedSha":"18893faf8b356be3320add8de2f861b0b647cf06","analyzedAt":"2026-09-08T20:49:34.690Z","contentChangedAt":"2026-09-08T20:49:34.690Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}