Skip to main content

TradeService

Struct TradeService 

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

Trade service for order management endpoints.

Implementations§

Source§

impl TradeService

Source

pub fn new(http: HttpClient) -> Self

Create a new trade service.

Source

pub async fn submit_order( &self, params: &OrderParams, ) -> Result<OrderResult, BybitError>

Submit a new order.

§Example
let client = BybitClient::new("api_key", "api_secret")?;

// Place a market order.
let params = OrderParams::market(Category::Linear, "BTCUSDT", Side::Buy, "0.001");
let result = client.trade().submit_order(&params).await?;
println!("Order ID: {}", result.order_id);

// Place a limit order.
let params = OrderParams::limit(Category::Spot, "BTCUSDT", Side::Buy, "0.001", "50000");
let result = client.trade().submit_order(&params).await?;
Source

pub async fn amend_order( &self, params: &AmendOrderParams, ) -> Result<OrderResult, BybitError>

Amend an existing order.

You can modify the order quantity, price, or TP/SL settings. Either order_id or order_link_id must be provided.

§Example
let client = BybitClient::new("api_key", "api_secret")?;

let params = AmendOrderParams::by_order_id(Category::Linear, "BTCUSDT", "order123")
    .price("51000")
    .qty("0.002");
let result = client.trade().amend_order(&params).await?;
Source

pub async fn cancel_order( &self, params: &CancelOrderParams, ) -> Result<OrderResult, BybitError>

Cancel an existing order.

Either order_id or order_link_id must be provided.

§Example
let client = BybitClient::new("api_key", "api_secret")?;

let params = CancelOrderParams::by_order_id(Category::Linear, "BTCUSDT", "order123");
let result = client.trade().cancel_order(&params).await?;
Source

pub async fn cancel_all_orders( &self, params: &CancelAllOrdersParams, ) -> Result<CancelAllResult, BybitError>

Cancel all active orders.

§Example
let client = BybitClient::new("api_key", "api_secret")?;

// Cancel all linear orders.
let params = CancelAllOrdersParams::new(Category::Linear);
let result = client.trade().cancel_all_orders(&params).await?;

// Cancel all orders for a specific symbol.
let params = CancelAllOrdersParams::new(Category::Spot)
    .symbol("BTCUSDT");
let result = client.trade().cancel_all_orders(&params).await?;
Source

pub async fn get_open_orders( &self, params: &GetOpenOrdersParams, ) -> Result<OrderListResult, BybitError>

Get open/active orders.

Returns real-time order data. For conditional orders, use order_filter.

§Example
let client = BybitClient::new("api_key", "api_secret")?;

let params = GetOpenOrdersParams::new(Category::Linear)
    .symbol("BTCUSDT")
    .limit(20);
let result = client.trade().get_open_orders(&params).await?;
for order in &result.list {
    println!("{}: {} {} @ {}", order.order_id, order.side, order.qty, order.price);
}
Source

pub async fn get_order_history( &self, params: &GetOrderHistoryParams, ) -> Result<OrderListResult, BybitError>

Get order history.

Returns historical orders including filled, cancelled, and rejected orders.

§Example
let client = BybitClient::new("api_key", "api_secret")?;

let params = GetOrderHistoryParams::new(Category::Linear)
    .symbol("BTCUSDT")
    .limit(50);
let result = client.trade().get_order_history(&params).await?;
Source

pub async fn get_execution_list( &self, params: &GetExecutionListParams, ) -> Result<ExecutionListResult, BybitError>

Get trade execution list.

Returns the trade history (fills) for your orders.

§Example
let client = BybitClient::new("api_key", "api_secret")?;

let params = GetExecutionListParams::new(Category::Linear)
    .symbol("BTCUSDT")
    .limit(50);
let result = client.trade().get_execution_list(&params).await?;
for exec in &result.list {
    println!("{}: {} {} @ {}", exec.exec_id, exec.side, exec.exec_qty, exec.exec_price);
}
Source

pub async fn get_borrow_quota( &self, params: &GetBorrowQuotaParams, ) -> Result<BorrowQuotaResult, BybitError>

Get spot borrow quota.

Check the maximum borrow amount for spot margin trading.

§Example
let client = BybitClient::new("api_key", "api_secret")?;

let params = GetBorrowQuotaParams::new("BTCUSDT", Side::Buy);
let result = client.trade().get_borrow_quota(&params).await?;
println!("Max trade qty: {}", result.max_trade_qty);
Source

pub async fn batch_submit_orders( &self, category: Category, orders: Vec<BatchOrderParams>, ) -> Result<BatchOperationResult, BybitError>

Submit multiple orders in a single request.

Supports up to 10 orders for options, 20 for USDT perpetual/USDC contracts.

§Example
let client = BybitClient::new("api_key", "api_secret")?;

let orders = vec![
    BatchOrderParams::limit("BTCUSDT", Side::Buy, "0.001", "50000")
        .order_link_id("batch_1"),
    BatchOrderParams::limit("BTCUSDT", Side::Buy, "0.001", "49000")
        .order_link_id("batch_2"),
];
let result = client.trade().batch_submit_orders(Category::Linear, orders).await?;
for order in &result.list {
    println!("Created: {} - {}", order.order_id, order.order_link_id);
}
Source

pub async fn batch_amend_orders( &self, category: Category, orders: Vec<BatchAmendOrderParams>, ) -> Result<BatchOperationResult, BybitError>

Amend multiple orders in a single request.

§Example
let client = BybitClient::new("api_key", "api_secret")?;

let orders = vec![
    BatchAmendOrderParams::by_order_link_id("BTCUSDT", "batch_1")
        .price("51000"),
    BatchAmendOrderParams::by_order_link_id("BTCUSDT", "batch_2")
        .price("49500"),
];
let result = client.trade().batch_amend_orders(Category::Linear, orders).await?;
Source

pub async fn batch_cancel_orders( &self, category: Category, orders: Vec<BatchCancelOrderParams>, ) -> Result<BatchOperationResult, BybitError>

Cancel multiple orders in a single request.

§Example
let client = BybitClient::new("api_key", "api_secret")?;

let orders = vec![
    BatchCancelOrderParams::by_order_link_id("BTCUSDT", "batch_1"),
    BatchCancelOrderParams::by_order_link_id("BTCUSDT", "batch_2"),
];
let result = client.trade().batch_cancel_orders(Category::Linear, orders).await?;

Trait Implementations§

Source§

impl Clone for TradeService

Source§

fn clone(&self) -> TradeService

Returns a duplicate of the value. Read more
1.0.0 · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Debug for TradeService

Source§

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

Formats the value using the given formatter. 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<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: 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: 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> 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<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

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