bybit/models/
add_margin_request.rs

1use crate::prelude::*;
2
3/// Parameters for enabling or disabling auto-add margin for a position.
4///
5/// Used to construct a request to the `/v5/position/set-auto-add-margin` endpoint to enable or disable automatic margin addition for a specific position. Bots use this to control margin behavior, preventing unwanted margin calls or optimizing capital allocation in perpetual futures.
6#[derive(Clone, Default)]
7pub struct AddMarginRequest<'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, such as `Linear` for USDT-margined perpetuals.
11    pub category: Category,
12
13    /// The trading pair symbol (e.g., "BTCUSDT").
14    ///
15    /// Identifies the perpetual futures contract for which auto-add margin is being configured. Bots must specify a valid symbol.
16    pub symbol: Cow<'a, str>,
17
18    /// Whether to enable auto-add margin.
19    ///
20    /// If `true`, Bybit will automatically add margin to prevent liquidation when margin levels are low. If `false`, no automatic margin addition occurs, increasing liquidation risk. Bots should set this based on their risk tolerance and capital management strategy.
21    pub auto_add: bool,
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 (e.g., long or short). If unset, applies to the default position.
26    pub position_idx: Option<i32>,
27}
28
29impl<'a> AddMarginRequest<'a> {
30    /// Constructs a new AddMargin request with specified parameters.
31    ///
32    /// Allows customization of the auto-add margin request. Bots should use this to specify the exact symbol, category, and auto-add setting to align with their margin management strategy.
33    pub fn new(
34        category: Category,
35        symbol: &'a str,
36        auto_add: bool,
37        position_idx: Option<i32>,
38    ) -> Self {
39        Self {
40            category,
41            symbol: Cow::Borrowed(symbol),
42            auto_add,
43            position_idx,
44        }
45    }
46    /// Creates a default AddMargin request.
47    ///
48    /// Returns a request with `category` set to `Linear`, `symbol` set to `"BTCUSDT"`, `auto_add` set to `false`, and no position index. Suitable for testing but should be customized for production.
49    pub fn default() -> AddMarginRequest<'a> {
50        AddMarginRequest::new(Category::Linear, "BTCUSDT", false, None)
51    }
52}