Skip to main content

bybit/models/
add_reduce_margin_request.rs

1use crate::prelude::*;
2
3/// Parameters for manually adding or reducing margin for a position.
4///
5/// Used to construct a request to the `/v5/position/add-margin` endpoint to manually adjust the margin allocated to a specific position. Bots use this to increase margin to avoid liquidation or reduce margin to free up capital in perpetual futures trading.
6#[derive(Clone, Default)]
7pub struct AddReduceMarginRequest<'a> {
8    /// The product category (e.g., Linear, Inverse).
9    ///
10    /// Specifies the instrument type. Bots must set this to target the correct contract type.
11    pub category: Category,
12
13    /// The trading pair symbol (e.g., "BTCUSDT").
14    ///
15    /// Identifies the perpetual futures contract for which margin is being adjusted. Bots must specify a valid symbol.
16    pub symbol: Cow<'a, str>,
17
18    /// The margin amount to add (positive) or reduce (negative).
19    ///
20    /// A positive value adds margin to the position, reducing liquidation risk. A negative value reduces margin, freeing up capital but increasing risk. Bots should calculate this based on position size and margin requirements.
21    pub margin: f64,
22
23    /// The position index (optional, e.g., 0 for one-way mode, 1 or 2 for hedge mode).
24    ///
25    /// Specifies the position type. Bots should set this for hedge mode positions to target the correct side. If unset, applies to the default position.
26    pub position_idx: Option<i32>,
27}
28
29impl<'a> AddReduceMarginRequest<'a> {
30    /// Constructs a new AddReduceMargin request with specified parameters.
31    ///
32    /// Allows customization of the margin adjustment request. Bots should use this to specify the exact symbol, category, margin amount, and position index.
33    pub fn new(
34        category: Category,
35        symbol: &'a str,
36        margin: f64,
37        position_idx: Option<i32>,
38    ) -> Self {
39        Self {
40            category,
41            symbol: Cow::Borrowed(symbol),
42            margin,
43            position_idx,
44        }
45    }
46    /// Creates a default AddReduceMargin request.
47    ///
48    /// Returns a request with `category` set to `Linear`, `symbol` set to `"BTCUSDT"`, `margin` set to `1.0`, and no position index. Suitable for testing but should be customized for production.
49    pub fn default() -> AddReduceMarginRequest<'a> {
50        AddReduceMarginRequest::new(Category::Linear, "BTCUSDT", 1.0, None)
51    }
52}