Skip to main content

nautilus_bitmex/websocket/
enums.rs

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