Skip to main content

bybit_api/models/
common.rs

1//! Common types used across the Bybit API.
2
3use serde::{Deserialize, Serialize};
4
5/// Product category.
6#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
7#[serde(rename_all = "lowercase")]
8pub enum Category {
9    /// Spot trading
10    Spot,
11    /// Linear perpetual (USDT margined)
12    Linear,
13    /// Inverse perpetual/futures
14    Inverse,
15    /// Options
16    Option,
17}
18
19impl std::fmt::Display for Category {
20    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
21        match self {
22            Self::Spot => write!(f, "spot"),
23            Self::Linear => write!(f, "linear"),
24            Self::Inverse => write!(f, "inverse"),
25            Self::Option => write!(f, "option"),
26        }
27    }
28}
29
30/// Order side.
31#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
32pub enum Side {
33    /// Buy order
34    Buy,
35    /// Sell order
36    Sell,
37}
38
39/// Order type.
40#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
41pub enum OrderType {
42    /// Market order
43    Market,
44    /// Limit order
45    Limit,
46}
47
48/// Time in force.
49#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
50pub enum TimeInForce {
51    /// Good till cancelled
52    GTC,
53    /// Immediate or cancel
54    IOC,
55    /// Fill or kill
56    FOK,
57    /// Post only
58    PostOnly,
59}
60
61/// Order status.
62#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
63pub enum OrderStatus {
64    /// Order created
65    Created,
66    /// Order is new (not filled)
67    New,
68    /// Order is rejected
69    Rejected,
70    /// Order is partially filled
71    PartiallyFilled,
72    /// Order is partially filled and cancelled
73    PartiallyFilledCanceled,
74    /// Order is fully filled
75    Filled,
76    /// Order is cancelled
77    Cancelled,
78    /// Order is untriggered
79    Untriggered,
80    /// Order is triggered
81    Triggered,
82    /// Order is deactivated
83    Deactivated,
84    /// Order is active (conditional)
85    Active,
86}
87
88/// Position side.
89#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
90pub enum PositionIdx {
91    /// One-way mode
92    #[serde(rename = "0")]
93    OneWay = 0,
94    /// Hedge mode - buy side
95    #[serde(rename = "1")]
96    HedgeBuy = 1,
97    /// Hedge mode - sell side
98    #[serde(rename = "2")]
99    HedgeSell = 2,
100}
101
102/// Account type.
103#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
104pub enum AccountType {
105    /// Contract account
106    CONTRACT,
107    /// Unified account
108    UNIFIED,
109    /// Spot account
110    SPOT,
111    /// Investment account
112    INVESTMENT,
113    /// Option account
114    OPTION,
115    /// Fund account
116    FUND,
117}
118
119/// Kline interval.
120#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
121pub enum Interval {
122    /// 1 minute
123    #[serde(rename = "1")]
124    M1,
125    /// 3 minutes
126    #[serde(rename = "3")]
127    M3,
128    /// 5 minutes
129    #[serde(rename = "5")]
130    M5,
131    /// 15 minutes
132    #[serde(rename = "15")]
133    M15,
134    /// 30 minutes
135    #[serde(rename = "30")]
136    M30,
137    /// 1 hour
138    #[serde(rename = "60")]
139    H1,
140    /// 2 hours
141    #[serde(rename = "120")]
142    H2,
143    /// 4 hours
144    #[serde(rename = "240")]
145    H4,
146    /// 6 hours
147    #[serde(rename = "360")]
148    H6,
149    /// 12 hours
150    #[serde(rename = "720")]
151    H12,
152    /// 1 day
153    #[serde(rename = "D")]
154    D1,
155    /// 1 week
156    #[serde(rename = "W")]
157    W1,
158    /// 1 month
159    #[serde(rename = "M")]
160    M1Month,
161}
162
163impl std::fmt::Display for Interval {
164    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
165        match self {
166            Self::M1 => write!(f, "1"),
167            Self::M3 => write!(f, "3"),
168            Self::M5 => write!(f, "5"),
169            Self::M15 => write!(f, "15"),
170            Self::M30 => write!(f, "30"),
171            Self::H1 => write!(f, "60"),
172            Self::H2 => write!(f, "120"),
173            Self::H4 => write!(f, "240"),
174            Self::H6 => write!(f, "360"),
175            Self::H12 => write!(f, "720"),
176            Self::D1 => write!(f, "D"),
177            Self::W1 => write!(f, "W"),
178            Self::M1Month => write!(f, "M"),
179        }
180    }
181}
182
183/// Trigger price type.
184#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
185pub enum TriggerBy {
186    /// Last price
187    LastPrice,
188    /// Index price
189    IndexPrice,
190    /// Mark price
191    MarkPrice,
192}
193
194/// Position mode.
195#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
196pub enum PositionMode {
197    /// Merged single position (one-way)
198    #[serde(rename = "0")]
199    MergedSingle = 0,
200    /// Both sides (hedge mode)
201    #[serde(rename = "3")]
202    BothSides = 3,
203}
204
205/// Margin mode.
206#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
207pub enum MarginMode {
208    /// Cross margin
209    CROSS,
210    /// Isolated margin
211    ISOLATED,
212    /// Portfolio margin
213    PORTFOLIO,
214}
215
216/// TP/SL mode.
217#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
218pub enum TpSlMode {
219    /// Full position TP/SL
220    Full,
221    /// Partial position TP/SL
222    Partial,
223}