nautechsystems/nautilus_trader · error

{e}

Error message

{e}

What it means

In the OKX HTTP client's set-position-mode call, certain OKX error codes (indicating derivatives trading is not enabled on the account) are handled gracefully with a warning and Ok. Any other error is re-raised verbatim with anyhow::bail!(e). The message is therefore the raw OKX API error text, surfacing whatever the exchange returned for the position-mode request.

Source

Thrown at crates/adapters/okx/src/http/client.rs:2362

        let mut params = SetPositionModeParamsBuilder::default();
        params.pos_mode(position_mode);
        let params = params.build().map_err(|e| anyhow::anyhow!(e))?;

        match self.inner.set_position_mode(params).await {
            Ok(_) => Ok(()),
            Err(e) => {
                if let OKXHttpError::OkxError {
                    error_code,
                    message,
                } = &e
                    && error_code == "50115"
                {
                    log::warn!(
                        "Account does not support position mode setting (derivatives trading not enabled): {message}"
                    );
                    return Ok(()); // Gracefully handle this case
                }
                anyhow::bail!(e)
            }
        }
    }

    /// Requests all instruments for the `instrument_type` from OKX.
    ///
    /// Option requests require `instrument_family` (OKX `instFamily`), for example `BTC-USD`.
    ///
    /// # Errors
    ///
    /// Returns an error if `instrument_type` is option and `instrument_family` is missing,
    /// the HTTP request fails, or instrument parsing fails.
    ///
    /// # Returns
    ///
    /// A tuple containing:
    /// - `Vec<InstrumentAny>`: The parsed instruments
    /// - `Vec<(Ustr, u64)>`: Mappings of inst_id to inst_id_code for WebSocket order operations

View on GitHub (pinned to 18893faf8b)

Solutions

  1. Read the embedded OKX message to identify the API error and address it at the exchange level (close positions, enable derivatives, use a valid posMode)
  2. Enable derivatives trading on the OKX account or skip position-mode configuration for such accounts
  3. Verify the posMode parameter matches one of OKX's allowed values (long_short_mode / net_mode)
Defensive patterns

Strategy: try-catch

Validate before calling

// Check account derivatives capability before setting position mode
// (query account config first and skip the call if derivatives are not enabled)

Try / catch

match client.set_position_mode(mode).await {
    Ok(()) => {},
    Err(e) => {
        // message is the raw OKX error text; inspect it and decide
        log::warn!("position mode not applied: {e}");
    }
}

Prevention

When it happens

Trigger: Calling the client method that sets position mode when OKX rejects it for a reason other than 'derivatives not enabled' — e.g. open positions exist, invalid posMode value, or account-level restrictions.

Common situations: Configuring an adapter for an account without derivatives enabled yet requesting a hedge/net mode change; having open positions that block the mode switch; sending a posMode value OKX no longer accepts.

Related errors


AI-assisted analysis of nautechsystems/nautilus_trader@18893faf8b (2026-09-08). Data as JSON: /api/errors/850caf289ac28365. Report an issue: GitHub.