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