nautilus_bitmex/websocket/
enums.rs

1// -------------------------------------------------------------------------------------------------
2//  Copyright (C) 2015-2025 Nautech Systems Pty Ltd. All rights reserved.
3//  https://nautechsystems.io
4//
5//  Licensed under the GNU Lesser General Public License Version 3.0 (the "License");
6//  You may not use this file except in compliance with the License.
7//  You may obtain a copy of the License at https://www.gnu.org/licenses/lgpl-3.0.en.html
8//
9//  Unless required by applicable law or agreed to in writing, software
10//  distributed under the License is distributed on an "AS IS" BASIS,
11//  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12//  See the License for the specific language governing permissions and
13//  limitations under the License.
14// -------------------------------------------------------------------------------------------------
15
16use nautilus_model::enums::{AggressorSide, BookAction, OrderSide};
17use serde::{Deserialize, Serialize};
18use strum::{AsRefStr, Display, EnumIter, EnumString};
19
20/// Side of an order or trade.
21#[derive(
22    Copy,
23    Clone,
24    Debug,
25    Display,
26    PartialEq,
27    Eq,
28    AsRefStr,
29    EnumIter,
30    EnumString,
31    Serialize,
32    Deserialize,
33)]
34pub enum BitmexSide {
35    /// Buy side of the trade/order.
36    Buy,
37    /// Sell side of the trade/order.
38    Sell,
39}
40
41impl BitmexSide {
42    #[must_use]
43    pub const fn as_order_side(&self) -> OrderSide {
44        match self {
45            Self::Buy => OrderSide::Buy,
46            Self::Sell => OrderSide::Sell,
47        }
48    }
49    #[must_use]
50    pub const fn as_aggressor_side(&self) -> AggressorSide {
51        match self {
52            Self::Buy => AggressorSide::Buyer,
53            Self::Sell => AggressorSide::Seller,
54        }
55    }
56}
57
58impl From<BitmexSide> for crate::common::enums::BitmexSide {
59    fn from(side: BitmexSide) -> Self {
60        match side {
61            BitmexSide::Buy => Self::Buy,
62            BitmexSide::Sell => Self::Sell,
63        }
64    }
65}
66
67/// Direction of price tick relative to previous trade.
68#[derive(
69    Copy,
70    Clone,
71    Debug,
72    Display,
73    PartialEq,
74    Eq,
75    AsRefStr,
76    EnumIter,
77    EnumString,
78    Serialize,
79    Deserialize,
80)]
81pub enum BitmexTickDirection {
82    /// Price higher than previous trade.
83    PlusTick,
84    /// Price lower than previous trade.
85    MinusTick,
86    /// Price equal to previous trade, which was higher than the trade before it.
87    ZeroPlusTick,
88    /// Price equal to previous trade, which was lower than the trade before it.
89    ZeroMinusTick,
90}
91
92/// Trading instrument state.
93#[derive(
94    Copy,
95    Clone,
96    Debug,
97    Display,
98    PartialEq,
99    Eq,
100    AsRefStr,
101    EnumIter,
102    EnumString,
103    Serialize,
104    Deserialize,
105)]
106#[serde(rename_all = "lowercase")]
107pub enum BitmexInstrumentState {
108    /// Instrument is available for trading.
109    Open,
110    /// Instrument is not currently trading.
111    Closed,
112    /// Instrument is in settlement.
113    Settling,
114    /// Instrument is not listed.
115    Unlisted,
116}
117
118/// Action type for table data messages.
119#[derive(
120    Copy,
121    Clone,
122    Debug,
123    Display,
124    PartialEq,
125    Eq,
126    AsRefStr,
127    EnumIter,
128    EnumString,
129    Serialize,
130    Deserialize,
131)]
132#[serde(rename_all = "lowercase")]
133pub enum BitmexAction {
134    /// Initial snapshot of table data.
135    Partial,
136    /// New data inserted.
137    Insert,
138    /// Update to existing data.
139    Update,
140    /// Existing data deleted.
141    Delete,
142}
143
144impl BitmexAction {
145    #[must_use]
146    pub const fn as_book_action(&self) -> BookAction {
147        match self {
148            Self::Partial => BookAction::Add,
149            Self::Insert => BookAction::Add,
150            Self::Update => BookAction::Update,
151            Self::Delete => BookAction::Delete,
152        }
153    }
154}
155
156/// Operation type for WebSocket commands.
157#[derive(
158    Clone, Debug, Display, PartialEq, Eq, AsRefStr, EnumIter, EnumString, Serialize, Deserialize,
159)]
160#[serde(rename_all = "camelCase")]
161pub enum BitmexWsOperation {
162    /// Subscribe to one or more topics.
163    Subscribe,
164    /// Unsubscribe from one or more topics.
165    Unsubscribe,
166}
167
168/// Authentication action types for WebSocket commands.
169#[derive(
170    Clone, Debug, Display, PartialEq, Eq, AsRefStr, EnumIter, EnumString, Serialize, Deserialize,
171)]
172#[serde(rename_all = "camelCase")]
173pub enum BitmexWsAuthAction {
174    /// Submit API key for authentication (legacy, deprecated).
175    AuthKey,
176    /// Submit API key with expires for authentication (recommended).
177    AuthKeyExpires,
178    /// Cancel all orders after n seconds.
179    CancelAllAfter,
180}
181
182/// Represents possible WebSocket topics that can be subscribed to.
183#[derive(
184    Clone, Debug, Display, PartialEq, Eq, AsRefStr, EnumIter, EnumString, Serialize, Deserialize,
185)]
186#[serde(rename_all = "camelCase")]
187#[strum(serialize_all = "camelCase")]
188pub enum BitmexWsTopic {
189    /// Site announcements.
190    Announcement,
191    /// Trollbox chat.
192    Chat,
193    /// Statistics of connected users/bots.
194    Connected,
195    /// Updates of swap funding rates.
196    Funding,
197    /// Instrument updates including mark and index prices.
198    Instrument,
199    /// Daily insurance fund updates.
200    Insurance,
201    /// Liquidation orders as they're entered into the book.
202    Liquidation,
203    /// Settlement price updates.
204    Settlement,
205    /// Full level 2 orderbook.
206    OrderBookL2,
207    /// Top 25 levels of level 2 orderbook.
208    #[serde(rename = "orderBookL2_25")]
209    #[strum(to_string = "orderBookL2_25")]
210    OrderBookL2_25,
211    /// Top 10 levels using traditional full book push.
212    OrderBook10,
213    /// System announcements.
214    PublicNotifications,
215    /// Top level of the book.
216    Quote,
217    /// 1-minute quote bins.
218    QuoteBin1m,
219    /// 5-minute quote bins.
220    QuoteBin5m,
221    /// 1-hour quote bins.
222    QuoteBin1h,
223    /// 1-day quote bins.
224    QuoteBin1d,
225    /// Live trades.
226    Trade,
227    /// 1-minute trade bins.
228    TradeBin1m,
229    /// 5-minute trade bins.
230    TradeBin5m,
231    /// 1-hour trade bins.
232    TradeBin1h,
233    /// 1-day trade bins.
234    TradeBin1d,
235}
236
237/// Represents authenticated WebSocket channels for account updates.
238#[derive(
239    Clone, Debug, Display, PartialEq, Eq, AsRefStr, EnumIter, EnumString, Serialize, Deserialize,
240)]
241#[serde(rename_all = "camelCase")]
242#[strum(serialize_all = "camelCase")]
243pub enum BitmexWsAuthChannel {
244    /// Order updates for the authenticated account.
245    Order,
246    /// Execution/fill updates for the authenticated account.
247    Execution,
248    /// Position updates for the authenticated account.
249    Position,
250    /// Margin updates for the authenticated account.
251    Margin,
252    /// Wallet updates for the authenticated account.
253    Wallet,
254}