Skip to main content

PositionService

Struct PositionService 

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

Position service for position management endpoints.

Implementations§

Source§

impl PositionService

Source

pub fn new(http: HttpClient) -> Self

Create a new position service.

Source

pub async fn get_position_info( &self, params: &GetPositionInfoParams, ) -> Result<PositionListResult, BybitError>

Get position information.

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

let params = GetPositionInfoParams::new(Category::Linear)
    .symbol("BTCUSDT");
let result = client.position().get_position_info(&params).await?;
for pos in &result.list {
    println!("{}: {} @ {} (unrealised PnL: {})",
        pos.symbol, pos.size, pos.avg_price, pos.unrealised_pnl);
}
Source

pub async fn set_leverage( &self, params: &SetLeverageParams, ) -> Result<(), BybitError>

Set leverage for a symbol.

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

// Set 10x leverage (same for buy and sell)
let params = SetLeverageParams::uniform(Category::Linear, "BTCUSDT", "10");
client.position().set_leverage(&params).await?;
Source

pub async fn switch_margin_mode( &self, params: &SwitchMarginModeParams, ) -> Result<(), BybitError>

Switch between cross margin and isolated margin.

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

// Switch to isolated margin with 10x leverage
let params = SwitchMarginModeParams::isolated_margin(
    Category::Linear, "BTCUSDT", "10", "10"
);
client.position().switch_margin_mode(&params).await?;
Source

pub async fn switch_position_mode( &self, params: &SwitchPositionModeParams, ) -> Result<(), BybitError>

Switch position mode between one-way and hedge mode.

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

// Switch to hedge mode
let params = SwitchPositionModeParams::hedge_by_symbol(Category::Linear, "BTCUSDT");
client.position().switch_position_mode(&params).await?;
Source

pub async fn set_trading_stop( &self, params: &SetTradingStopParams, ) -> Result<(), BybitError>

Set trading stop (take profit, stop loss, trailing stop).

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

let params = SetTradingStopParams::new(Category::Linear, "BTCUSDT", PositionIdx::OneWay)
    .take_profit("55000")
    .stop_loss("45000");
client.position().set_trading_stop(&params).await?;
Source

pub async fn set_auto_add_margin( &self, params: &SetAutoAddMarginParams, ) -> Result<(), BybitError>

Set auto add margin.

When enabled, the system will automatically add margin to prevent liquidation.

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

let params = SetAutoAddMarginParams::enable(Category::Linear, "BTCUSDT");
client.position().set_auto_add_margin(&params).await?;
Source

pub async fn add_or_reduce_margin( &self, params: &AddReduceMarginParams, ) -> Result<MarginOperationResult, BybitError>

Add or reduce margin for a position.

Use positive value to add margin, negative to reduce.

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

// Add 100 USDT margin
let params = AddReduceMarginParams::new(Category::Linear, "BTCUSDT", "100");
let result = client.position().add_or_reduce_margin(&params).await?;
Source

pub async fn get_closed_pnl( &self, params: &GetClosedPnlParams, ) -> Result<ClosedPnlListResult, BybitError>

Get closed profit and loss records.

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

let params = GetClosedPnlParams::new(Category::Linear)
    .symbol("BTCUSDT")
    .limit(20);
let result = client.position().get_closed_pnl(&params).await?;
for pnl in &result.list {
    println!("{}: {} (closed PnL: {})", pnl.symbol, pnl.side, pnl.closed_pnl);
}

Trait Implementations§

Source§

impl Clone for PositionService

Source§

fn clone(&self) -> PositionService

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 PositionService

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