rusty_feeder/exchange/binance/futures/
types.rs

1//! Type definitions and constants for Binance Futures markets
2//!
3//! This module contains the types and constants used for Binance Futures API interactions,
4//! including WebSocket and REST API endpoints.
5
6use crate::provider::prelude::*;
7use smartstring::alias::String;
8
9/// Base URL for Binance USD-M Futures REST API
10pub const BINANCE_USD_FUTURES_API_URL: &str = "https://fapi.binance.com";
11
12/// Base URL for Binance COIN-M Futures REST API
13pub const BINANCE_COIN_FUTURES_API_URL: &str = "https://dapi.binance.com";
14
15/// Base URL for Binance USD-M Futures WebSocket API
16pub const BINANCE_USD_FUTURES_WS_URL: &str = "wss://fstream.binance.com/ws";
17
18/// Base URL for Binance COIN-M Futures WebSocket API
19pub const BINANCE_COIN_FUTURES_WS_URL: &str = "wss://dstream.binance.com/ws";
20
21/// Base URL for Binance USD-M Futures WebSocket combined streams
22pub const BINANCE_USD_FUTURES_WS_COMBINED_URL: &str = "wss://fstream.binance.com/stream";
23
24/// Base URL for Binance COIN-M Futures WebSocket combined streams
25pub const BINANCE_COIN_FUTURES_WS_COMBINED_URL: &str = "wss://dstream.binance.com/stream";
26
27/// Rate limits for Binance Futures API
28pub const BINANCE_FUTURES_RATE_LIMITS: &[RateLimit] = &[
29    RateLimit {
30        limit_type: "REQUEST_WEIGHT",
31        interval: "MINUTE",
32        interval_num: 1,
33        limit: 2400,
34    },
35    RateLimit {
36        limit_type: "ORDERS",
37        interval: "MINUTE",
38        interval_num: 1,
39        limit: 1200,
40    },
41];
42
43/// Futures contract types
44#[derive(Debug, Clone, Copy, PartialEq, Eq)]
45pub enum BinanceFuturesContractType {
46    /// Perpetual futures contract
47    Perpetual,
48
49    /// Current quarter futures contract
50    CurrentQuarter,
51
52    /// Next quarter futures contract
53    NextQuarter,
54}
55
56impl std::fmt::Display for BinanceFuturesContractType {
57    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
58        match self {
59            BinanceFuturesContractType::Perpetual => write!(f, "PERPETUAL"),
60            BinanceFuturesContractType::CurrentQuarter => write!(f, "CURRENT_QUARTER"),
61            BinanceFuturesContractType::NextQuarter => write!(f, "NEXT_QUARTER"),
62        }
63    }
64}
65
66/// Work order types supported by Binance Futures
67#[derive(Debug, Clone, Copy, PartialEq, Eq)]
68pub enum BinanceFuturesOrderType {
69    /// Limit order (price required)
70    Limit,
71
72    /// Market order (quantity required)
73    Market,
74
75    /// Stop order (stop price required)
76    Stop,
77
78    /// Stop Market order (stop price required)
79    StopMarket,
80
81    /// Take Profit order (price required)
82    TakeProfit,
83
84    /// Take Profit Market order (stop price required)
85    TakeProfitMarket,
86
87    /// Trailing Stop Market order
88    TrailingStopMarket,
89}
90
91impl std::fmt::Display for BinanceFuturesOrderType {
92    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
93        match self {
94            BinanceFuturesOrderType::Limit => write!(f, "LIMIT"),
95            BinanceFuturesOrderType::Market => write!(f, "MARKET"),
96            BinanceFuturesOrderType::Stop => write!(f, "STOP"),
97            BinanceFuturesOrderType::StopMarket => write!(f, "STOP_MARKET"),
98            BinanceFuturesOrderType::TakeProfit => write!(f, "TAKE_PROFIT"),
99            BinanceFuturesOrderType::TakeProfitMarket => write!(f, "TAKE_PROFIT_MARKET"),
100            BinanceFuturesOrderType::TrailingStopMarket => write!(f, "TRAILING_STOP_MARKET"),
101        }
102    }
103}
104
105/// Order side (Buy or Sell)
106#[derive(Debug, Clone, Copy, PartialEq, Eq)]
107pub enum BinanceFuturesOrderSide {
108    /// Buy order
109    Buy,
110
111    /// Sell order
112    Sell,
113}
114
115impl std::fmt::Display for BinanceFuturesOrderSide {
116    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
117        match self {
118            BinanceFuturesOrderSide::Buy => write!(f, "BUY"),
119            BinanceFuturesOrderSide::Sell => write!(f, "SELL"),
120        }
121    }
122}
123
124/// Position side for futures
125#[derive(Debug, Clone, Copy, PartialEq, Eq)]
126pub enum BinanceFuturesPositionSide {
127    /// Both sides
128    Both,
129
130    /// Long position
131    Long,
132
133    /// Short position
134    Short,
135}
136
137impl std::fmt::Display for BinanceFuturesPositionSide {
138    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
139        match self {
140            BinanceFuturesPositionSide::Both => write!(f, "BOTH"),
141            BinanceFuturesPositionSide::Long => write!(f, "LONG"),
142            BinanceFuturesPositionSide::Short => write!(f, "SHORT"),
143        }
144    }
145}
146
147/// Time in force options for futures orders
148#[derive(Debug, Clone, Copy, PartialEq, Eq)]
149pub enum BinanceFuturesTimeInForce {
150    /// Good Till Cancelled
151    GTC,
152
153    /// Immediate or Cancel
154    IOC,
155
156    /// Fill or Kill
157    FOK,
158
159    /// Good Till Crossing (Post Only)
160    GTX,
161}
162
163impl std::fmt::Display for BinanceFuturesTimeInForce {
164    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
165        match self {
166            BinanceFuturesTimeInForce::GTC => write!(f, "GTC"),
167            BinanceFuturesTimeInForce::IOC => write!(f, "IOC"),
168            BinanceFuturesTimeInForce::FOK => write!(f, "FOK"),
169            BinanceFuturesTimeInForce::GTX => write!(f, "GTX"),
170        }
171    }
172}
173
174/// Working type for futures orders
175#[derive(Debug, Clone, Copy, PartialEq, Eq)]
176pub enum BinanceFuturesWorkingType {
177    /// Mark price
178    MarkPrice,
179
180    /// Contract price
181    ContractPrice,
182}
183
184impl std::fmt::Display for BinanceFuturesWorkingType {
185    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
186        match self {
187            BinanceFuturesWorkingType::MarkPrice => write!(f, "MARK_PRICE"),
188            BinanceFuturesWorkingType::ContractPrice => write!(f, "CONTRACT_PRICE"),
189        }
190    }
191}
192
193/// Margin type for futures
194#[derive(Debug, Clone, Copy, PartialEq, Eq)]
195pub enum BinanceFuturesMarginType {
196    /// Isolated margin
197    Isolated,
198
199    /// Crossed margin
200    Crossed,
201}
202
203impl BinanceFuturesMarginType {
204    /// Convert to string representation
205    pub const fn as_str(&self) -> &'static str {
206        match self {
207            BinanceFuturesMarginType::Isolated => "ISOLATED",
208            BinanceFuturesMarginType::Crossed => "CROSSED",
209        }
210    }
211
212    /// Convert to SmartString
213    pub fn to_smartstring(&self) -> String {
214        self.as_str().into()
215    }
216}
217
218/// Order status values for futures
219#[derive(Debug, Clone, Copy, PartialEq, Eq)]
220pub enum BinanceFuturesOrderStatus {
221    /// Order has been accepted by the engine
222    New,
223
224    /// A part of the order has been filled
225    PartiallyFilled,
226
227    /// The order has been completely filled
228    Filled,
229
230    /// The order has been canceled
231    Canceled,
232
233    /// The order is being canceled
234    Canceling,
235
236    /// The order was rejected
237    Rejected,
238
239    /// The order was expired
240    Expired,
241
242    /// Liquidation with Insurance Fund
243    Insurance,
244
245    /// Counterparty Liquidation
246    Adl,
247}
248
249impl BinanceFuturesOrderStatus {
250    /// Convert to string representation
251    pub const fn as_str(&self) -> &'static str {
252        match self {
253            BinanceFuturesOrderStatus::New => "NEW",
254            BinanceFuturesOrderStatus::PartiallyFilled => "PARTIALLY_FILLED",
255            BinanceFuturesOrderStatus::Filled => "FILLED",
256            BinanceFuturesOrderStatus::Canceled => "CANCELED",
257            BinanceFuturesOrderStatus::Canceling => "CANCELING",
258            BinanceFuturesOrderStatus::Rejected => "REJECTED",
259            BinanceFuturesOrderStatus::Expired => "EXPIRED",
260            BinanceFuturesOrderStatus::Insurance => "INSURANCE",
261            BinanceFuturesOrderStatus::Adl => "ADL",
262        }
263    }
264
265    /// Convert to SmartString
266    pub fn to_smartstring(&self) -> String {
267        self.as_str().into()
268    }
269}
270
271/// Kline/Candlestick intervals for futures
272#[derive(Debug, Clone, Copy, PartialEq, Eq)]
273pub enum BinanceFuturesKlineInterval {
274    /// 1 minute
275    Minute1,
276
277    /// 3 minutes
278    Minute3,
279
280    /// 5 minutes
281    Minute5,
282
283    /// 15 minutes
284    Minute15,
285
286    /// 30 minutes
287    Minute30,
288
289    /// 1 hour
290    Hour1,
291
292    /// 2 hours
293    Hour2,
294
295    /// 4 hours
296    Hour4,
297
298    /// 6 hours
299    Hour6,
300
301    /// 8 hours
302    Hour8,
303
304    /// 12 hours
305    Hour12,
306
307    /// 1 day
308    Day1,
309
310    /// 3 days
311    Day3,
312
313    /// 1 week
314    Week1,
315
316    /// 1 month
317    Month1,
318}
319
320impl BinanceFuturesKlineInterval {
321    /// Convert to string representation
322    pub const fn as_str(&self) -> &'static str {
323        match self {
324            BinanceFuturesKlineInterval::Minute1 => "1m",
325            BinanceFuturesKlineInterval::Minute3 => "3m",
326            BinanceFuturesKlineInterval::Minute5 => "5m",
327            BinanceFuturesKlineInterval::Minute15 => "15m",
328            BinanceFuturesKlineInterval::Minute30 => "30m",
329            BinanceFuturesKlineInterval::Hour1 => "1h",
330            BinanceFuturesKlineInterval::Hour2 => "2h",
331            BinanceFuturesKlineInterval::Hour4 => "4h",
332            BinanceFuturesKlineInterval::Hour6 => "6h",
333            BinanceFuturesKlineInterval::Hour8 => "8h",
334            BinanceFuturesKlineInterval::Hour12 => "12h",
335            BinanceFuturesKlineInterval::Day1 => "1d",
336            BinanceFuturesKlineInterval::Day3 => "3d",
337            BinanceFuturesKlineInterval::Week1 => "1w",
338            BinanceFuturesKlineInterval::Month1 => "1M",
339        }
340    }
341
342    /// Convert to SmartString
343    pub fn to_smartstring(&self) -> String {
344        self.as_str().into()
345    }
346}