pub struct PositionService { /* private fields */ }Expand description
Position service for position management endpoints.
Implementations§
Source§impl PositionService
impl PositionService
Sourcepub fn new(http: HttpClient) -> Self
pub fn new(http: HttpClient) -> Self
Create a new position service.
Sourcepub async fn get_position_info(
&self,
params: &GetPositionInfoParams,
) -> Result<PositionListResult, BybitError>
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(¶ms).await?;
for pos in &result.list {
println!("{}: {} @ {} (unrealised PnL: {})",
pos.symbol, pos.size, pos.avg_price, pos.unrealised_pnl);
}Sourcepub async fn set_leverage(
&self,
params: &SetLeverageParams,
) -> Result<(), BybitError>
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(¶ms).await?;Sourcepub async fn switch_margin_mode(
&self,
params: &SwitchMarginModeParams,
) -> Result<(), BybitError>
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(¶ms).await?;Sourcepub async fn switch_position_mode(
&self,
params: &SwitchPositionModeParams,
) -> Result<(), BybitError>
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(¶ms).await?;Sourcepub async fn set_trading_stop(
&self,
params: &SetTradingStopParams,
) -> Result<(), BybitError>
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(¶ms).await?;Sourcepub async fn set_auto_add_margin(
&self,
params: &SetAutoAddMarginParams,
) -> Result<(), BybitError>
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(¶ms).await?;Sourcepub async fn add_or_reduce_margin(
&self,
params: &AddReduceMarginParams,
) -> Result<MarginOperationResult, BybitError>
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(¶ms).await?;Sourcepub async fn get_closed_pnl(
&self,
params: &GetClosedPnlParams,
) -> Result<ClosedPnlListResult, BybitError>
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(¶ms).await?;
for pnl in &result.list {
println!("{}: {} (closed PnL: {})", pnl.symbol, pnl.side, pnl.closed_pnl);
}Trait Implementations§
Source§impl Clone for PositionService
impl Clone for PositionService
Source§fn clone(&self) -> PositionService
fn clone(&self) -> PositionService
Returns a duplicate of the value. Read more
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreAuto Trait Implementations§
impl !Freeze for PositionService
impl !RefUnwindSafe for PositionService
impl Send for PositionService
impl Sync for PositionService
impl Unpin for PositionService
impl !UnwindSafe for PositionService
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more