Skip to main content

BitmexHttpClient

Struct BitmexHttpClient 

Source
pub struct BitmexHttpClient { /* private fields */ }
Expand description

Provides a HTTP client for connecting to the BitMEX REST API.

This is the high-level client that wraps the inner client and provides Nautilus-specific functionality for trading operations.

Implementations§

Source§

impl BitmexHttpClient

Source

pub fn new( base_url: Option<String>, api_key: Option<String>, api_secret: Option<String>, environment: BitmexEnvironment, timeout_secs: u64, max_retries: u32, retry_delay_ms: u64, retry_delay_max_ms: u64, recv_window_ms: u64, max_requests_per_second: u32, max_requests_per_minute: u32, proxy_url: Option<String>, ) -> Result<Self, BitmexHttpError>

Creates a new BitmexHttpClient instance.

§Errors

Returns an error if the HTTP client cannot be created.

Source

pub fn from_env() -> Result<Self>

Creates a new BitmexHttpClient instance using environment variables and the default BitMEX HTTP base URL.

§Errors

Returns an error if required environment variables are not set or invalid.

Source

pub fn with_credentials( api_key: Option<String>, api_secret: Option<String>, base_url: Option<String>, timeout_secs: u64, max_retries: u32, retry_delay_ms: u64, retry_delay_max_ms: u64, recv_window_ms: u64, max_requests_per_second: u32, max_requests_per_minute: u32, proxy_url: Option<String>, ) -> Result<Self>

Creates a new BitmexHttpClient configured with credentials for authenticated requests.

If api_key or api_secret are None, they will be sourced from the BITMEX_API_KEY and BITMEX_API_SECRET environment variables.

§Errors

Returns an error if one credential is provided without the other.

Source

pub fn base_url(&self) -> &str

Returns the base url being used by the client.

Source

pub fn api_key(&self) -> Option<&str>

Returns the public API key being used by the client.

Source

pub fn api_key_masked(&self) -> Option<String>

Returns a masked version of the API key for logging purposes.

Source

pub async fn get_server_time(&self) -> Result<u64, BitmexHttpError>

Requests the current server time from BitMEX.

Returns the BitMEX system time as a Unix timestamp in milliseconds.

§Errors

Returns an error if the HTTP request fails or if the response cannot be parsed.

Source

pub async fn cancel_all_after(&self, timeout_ms: u64) -> Result<()>

Sets the dead man’s switch (cancel all orders after timeout).

Calling with timeout_ms=0 disarms the switch.

§Errors

Returns an error if the HTTP request fails.

Source

pub fn cancel_all_requests(&self)

Cancel all pending HTTP requests.

Source

pub fn reset_cancellation_token(&self)

Replace the cancellation token so new requests can proceed.

Source

pub fn cancellation_token(&self) -> CancellationToken

Get a clone of the cancellation token for this client.

Source

pub fn cache_instrument(&self, instrument: InstrumentAny)

Caches a single instrument.

Any existing instrument with the same symbol will be replaced.

Source

pub fn cache_instruments(&self, instruments: &[InstrumentAny])

Caches multiple instruments.

Any existing instruments with the same symbols will be replaced.

Source

pub fn get_instrument(&self, symbol: &Ustr) -> Option<InstrumentAny>

Gets an instrument from the cache by symbol.

Source

pub async fn request_instrument( &self, instrument_id: InstrumentId, ) -> Result<Option<InstrumentAny>>

Request a single instrument and parse it into a Nautilus type.

§Errors

Returns Ok(Some(..)) when the venue returns a definition that parses successfully, Ok(None) when the instrument is unknown, unsupported, or the payload cannot be converted into a Nautilus Instrument.

Source

pub async fn request_instruments( &self, active_only: bool, ) -> Result<Vec<InstrumentAny>>

Request all available instruments and parse them into Nautilus types.

§Errors

Returns an error if the HTTP request fails or parsing fails.

Source

pub async fn get_wallet(&self) -> Result<BitmexWallet, BitmexHttpError>

Get user wallet information.

§Errors

Returns an error if credentials are missing, the request fails, or the API returns an error.

Source

pub async fn get_orders( &self, params: GetOrderParams, ) -> Result<Vec<BitmexOrder>, BitmexHttpError>

Get user orders.

§Errors

Returns an error if credentials are missing, the request fails, or the API returns an error.

Source

pub fn get_price_precision(&self, symbol: Ustr) -> Result<u8>

Returns the cached price precision for the given symbol.

§Errors

Returns an error if the instrument was never cached (for example, if instruments were not loaded prior to use).

Source

pub async fn get_margin(&self, currency: &str) -> Result<BitmexMargin>

Get user margin information for a specific currency.

§Errors

Returns an error if credentials are missing, the request fails, or the API returns an error.

Source

pub async fn get_all_margins(&self) -> Result<Vec<BitmexMargin>>

Get user margin information for all currencies.

§Errors

Returns an error if credentials are missing, the request fails, or the API returns an error.

Source

pub async fn request_account_state( &self, fallback_account_id: AccountId, ) -> Result<AccountState>

Request account state for the authenticated BitMEX account.

§Errors

Returns an error if the HTTP request fails or no account state is returned.

Source

pub async fn submit_order( &self, instrument_id: InstrumentId, client_order_id: ClientOrderId, order_side: OrderSide, order_type: OrderType, quantity: Quantity, time_in_force: TimeInForce, price: Option<Price>, trigger_price: Option<Price>, trigger_type: Option<TriggerType>, trailing_offset: Option<f64>, trailing_offset_type: Option<TrailingOffsetType>, display_qty: Option<Quantity>, post_only: bool, reduce_only: bool, order_list_id: Option<OrderListId>, contingency_type: Option<ContingencyType>, peg_price_type: Option<BitmexPegPriceType>, peg_offset_value: Option<f64>, ) -> Result<OrderStatusReport>

Submit a new order.

§Errors

Returns an error if credentials are missing, the request fails, order validation fails, the order is rejected, or the API returns an error.

Source

pub async fn cancel_order( &self, instrument_id: InstrumentId, client_order_id: Option<ClientOrderId>, venue_order_id: Option<VenueOrderId>, ) -> Result<OrderStatusReport>

Cancel an order.

§Errors

Returns an error if:

  • Credentials are missing.
  • The request fails.
  • The order doesn’t exist.
  • The API returns an error.
Source

pub async fn cancel_orders( &self, instrument_id: InstrumentId, client_order_ids: Option<Vec<ClientOrderId>>, venue_order_ids: Option<Vec<VenueOrderId>>, ) -> Result<Vec<OrderStatusReport>>

Cancel multiple orders.

§Errors

Returns an error if:

  • Credentials are missing.
  • The request fails.
  • The order doesn’t exist.
  • The API returns an error.
Source

pub async fn cancel_all_orders( &self, instrument_id: InstrumentId, order_side: Option<OrderSide>, ) -> Result<Vec<OrderStatusReport>>

Cancel all orders for an instrument and optionally an order side.

§Errors

Returns an error if:

  • Credentials are missing.
  • The request fails.
  • The order doesn’t exist.
  • The API returns an error.
Source

pub async fn modify_order( &self, instrument_id: InstrumentId, client_order_id: Option<ClientOrderId>, venue_order_id: Option<VenueOrderId>, quantity: Option<Quantity>, price: Option<Price>, trigger_price: Option<Price>, ) -> Result<OrderStatusReport>

Modify an existing order.

§Errors

Returns an error if:

  • Credentials are missing.
  • The request fails.
  • The order doesn’t exist.
  • The order is already closed.
  • The API returns an error.
Source

pub async fn query_order( &self, instrument_id: InstrumentId, client_order_id: Option<ClientOrderId>, venue_order_id: Option<VenueOrderId>, ) -> Result<Option<OrderStatusReport>>

Query a single order by client order ID or venue order ID.

§Errors

Returns an error if:

  • Credentials are missing.
  • The request fails.
  • The API returns an error.
Source

pub async fn request_order_status_report( &self, instrument_id: InstrumentId, client_order_id: Option<ClientOrderId>, venue_order_id: Option<VenueOrderId>, ) -> Result<OrderStatusReport>

Request a single order status report.

§Errors

Returns an error if:

  • Credentials are missing.
  • The request fails.
  • The API returns an error.
Source

pub async fn request_order_status_reports( &self, instrument_id: Option<InstrumentId>, open_only: bool, start: Option<DateTime<Utc>>, end: Option<DateTime<Utc>>, limit: Option<u32>, ) -> Result<Vec<OrderStatusReport>>

Request multiple order status reports.

§Errors

Returns an error if:

  • Credentials are missing.
  • The request fails.
  • The API returns an error.
Source

pub async fn request_trades( &self, instrument_id: InstrumentId, start: Option<DateTime<Utc>>, end: Option<DateTime<Utc>>, limit: Option<u32>, ) -> Result<Vec<TradeTick>>

Request trades for the given instrument.

§Errors

Returns an error if the HTTP request fails or parsing fails.

Source

pub async fn request_bars( &self, bar_type: BarType, start: Option<DateTime<Utc>>, end: Option<DateTime<Utc>>, limit: Option<u32>, partial: bool, ) -> Result<Vec<Bar>>

Request bars for the given bar type.

§Errors

Returns an error if the HTTP request fails, parsing fails, or the bar specification is unsupported by BitMEX.

Source

pub async fn request_book_snapshot( &self, instrument_id: InstrumentId, depth: Option<u32>, ) -> Result<OrderBook>

Request a current L2 order book snapshot.

§Errors

Returns an error if the HTTP request fails, the instrument is not cached, or the book rows cannot be parsed.

Source

pub async fn request_funding_rates( &self, instrument_id: InstrumentId, start: Option<DateTime<Utc>>, end: Option<DateTime<Utc>>, limit: Option<u32>, ) -> Result<Vec<FundingRateUpdate>>

Request historical funding rates for the given instrument.

§Errors

Returns an error if the HTTP request fails or the time range is invalid.

Source

pub async fn request_fill_reports( &self, instrument_id: Option<InstrumentId>, start: Option<DateTime<Utc>>, end: Option<DateTime<Utc>>, limit: Option<u32>, ) -> Result<Vec<FillReport>>

Request fill reports for the given instrument.

§Errors

Returns an error if the HTTP request fails or parsing fails.

Source

pub async fn request_position_status_reports( &self, ) -> Result<Vec<PositionStatusReport>>

Request position reports.

§Errors

Returns an error if the HTTP request fails or parsing fails.

Source

pub async fn update_position_leverage( &self, symbol: &str, leverage: f64, ) -> Result<PositionStatusReport>

Update position leverage.

§Errors
  • Credentials are missing.
  • The request fails.
  • The API returns an error.

Trait Implementations§

Source§

impl Clone for BitmexHttpClient

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for BitmexHttpClient

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for BitmexHttpClient

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more