bybit/models/risk_limit.rs
1use crate::prelude::*;
2
3/// Represents a risk limit configuration for a trading symbol.
4/// Defines leverage, margin, and position size limits for perpetual futures.
5#[derive(Serialize, Deserialize, Clone, Debug)]
6#[serde(rename_all = "camelCase")]
7pub struct RiskLimit {
8 /// Unique identifier for the risk limit tier.
9 /// Used by Bybit to distinguish different risk levels. Bots can use this to track specific
10 /// configurations.
11 pub id: u64,
12
13 /// The trading symbol (e.g., "BTCUSDT").
14 /// Specifies the market to which the risk limit applies. Bots must match this with their
15 /// trading pairs.
16 pub symbol: String,
17
18 /// The maximum position size allowed (in base currency or USD).
19 /// In perpetual futures, this limits exposure to prevent excessive risk. Bots use this to
20 /// cap order sizes and avoid rejections.
21 #[serde(with = "string_to_float")]
22 pub risk_limit_value: f64,
23
24 /// The maintenance margin rate (e.g., 0.005 for 0.5%).
25 /// The minimum margin required to keep a position open. If the margin falls below this,
26
27 /// liquidation occurs at the bust price (bankruptcy price). Bots monitor this to manage
28 /// liquidation risks.
29 #[serde(with = "string_to_float")]
30 pub maintenance_margin: f64,
31
32 /// The initial margin rate (e.g., 0.01 for 1%).
33 /// The margin required to open a position. Lower rates allow higher leverage, but increase
34 /// liquidation risk. Bots use this to calculate capital requirements.
35 #[serde(with = "string_to_float")]
36 pub initial_margin: f64,
37
38 /// Indicates if this is the lowest risk tier (1 for true, 0 for false).
39 /// Lower risk tiers have stricter limits but safer margin requirements. Bots may prefer these
40 /// for conservative strategies.
41 pub is_lowest_risk: u8,
42
43 /// The maximum leverage allowed (e.g., "100" for 100x).
44 /// Leverage amplifies gains and losses in perpetual futures. Bots must ensure orders comply
45 /// with this limit to avoid rejections.
46 #[serde(with = "string_to_float")]
47 pub max_leverage: f64,
48}
49
50impl<'a> SetRiskLimit<'a> {
51 /// Constructs a new SetRiskLimit request with specified parameters.
52 ///
53 /// Allows customization of the risk limit request. Bots should use this to specify the category, symbol, risk ID, and position index as needed.
54 pub fn new(
55 category: Category,
56 symbol: &'a str,
57 risk_id: i8,
58 position_idx: Option<i32>,
59 ) -> Self {
60 Self {
61 category,
62 symbol: Cow::Borrowed(symbol),
63 risk_id,
64 position_idx,
65 }
66 }
67 /// Creates a default SetRiskLimit request.
68 ///
69 /// Returns a request with `category` set to `Linear`, `symbol` set to `"BTCUSDT"`, `risk_id` set to `1`, and no position index. Suitable for testing but should be customized for production.
70 pub fn default() -> SetRiskLimit<'a> {
71 SetRiskLimit::new(Category::Linear, "BTCUSDT", 1, None)
72 }
73}