rusty_common/constants/
mod.rs

1//! Common constants used across the rusty trading system
2//!
3//! This module provides centralized constants for all supported exchanges,
4//! general trading constants, and system-wide configuration values.
5
6use rust_decimal::Decimal;
7use rust_decimal_macros::dec;
8
9// Exchange-specific constants
10/// Coinbase-specific constants.
11pub mod coinbase;
12
13// Re-export commonly used items for convenience
14pub use coinbase::Environment as CoinbaseEnvironment;
15
16/// Time constants (in nanoseconds)
17pub mod time {
18    /// One nanosecond
19    pub const NANOSECOND: i64 = 1;
20
21    /// One microsecond in nanoseconds
22    pub const MICROSECOND: i64 = 1_000;
23
24    /// One millisecond in nanoseconds
25    pub const MILLISECOND: i64 = 1_000_000;
26
27    /// One second in nanoseconds
28    pub const SECOND: i64 = 1_000_000_000;
29
30    /// One minute in nanoseconds
31    pub const MINUTE: i64 = 60 * SECOND;
32
33    /// One hour in nanoseconds
34    pub const HOUR: i64 = 60 * MINUTE;
35
36    /// One day in nanoseconds
37    pub const DAY: i64 = 24 * HOUR;
38}
39
40/// Network constants
41pub mod network {
42    /// Default HTTP timeout in seconds
43    pub const DEFAULT_HTTP_TIMEOUT_SECS: u64 = 30;
44
45    /// Default WebSocket ping interval in seconds
46    pub const DEFAULT_WS_PING_INTERVAL_SECS: u64 = 30;
47
48    /// Default WebSocket pong timeout in seconds
49    pub const DEFAULT_WS_PONG_TIMEOUT_SECS: u64 = 10;
50
51    /// Maximum reconnection attempts
52    pub const MAX_RECONNECT_ATTEMPTS: u32 = 10;
53
54    /// Base reconnection delay in milliseconds
55    pub const BASE_RECONNECT_DELAY_MS: u64 = 1000;
56
57    /// Maximum reconnection delay in milliseconds
58    pub const MAX_RECONNECT_DELAY_MS: u64 = 60_000;
59}
60
61/// Trading constants and utilities
62pub mod trading {
63    use super::*;
64    use crate::const_fn_candidates::{const_max_u64, percent_to_bps};
65
66    /// Minimum order size (common across many exchanges)
67    pub const MIN_ORDER_SIZE: Decimal = dec!(0.001);
68
69    /// Maximum order size (safety limit)
70    pub const MAX_ORDER_SIZE: Decimal = dec!(10000);
71
72    /// Default maker fee rate (0.1%)
73    pub const DEFAULT_MAKER_FEE_RATE: Decimal = dec!(0.001);
74
75    /// Default taker fee rate (0.1%)
76    pub const DEFAULT_TAKER_FEE_RATE: Decimal = dec!(0.001);
77
78    /// Maximum number of open orders per symbol
79    pub const MAX_OPEN_ORDERS_PER_SYMBOL: usize = 100;
80
81    /// Maximum position size as percentage of account balance
82    pub const MAX_POSITION_PCT: Decimal = dec!(0.9);
83
84    /// Common fee rates in basis points (compile-time calculated)
85    pub const MAKER_FEE_BPS: u64 = percent_to_bps(10); // 0.1% = 10 bps
86    /// Default taker fee in basis points.
87    pub const TAKER_FEE_BPS: u64 = percent_to_bps(10); // 0.1% = 10 bps
88    /// High fee in basis points.
89    pub const HIGH_FEE_BPS: u64 = percent_to_bps(25); // 0.25% = 25 bps
90
91    /// Risk management constants
92    pub const MAX_LEVERAGE: u64 = 10;
93    /// Default stop loss in basis points.
94    pub const DEFAULT_STOP_LOSS_BPS: u64 = percent_to_bps(200); // 2% = 200 bps
95    /// Default take profit in basis points.
96    pub const DEFAULT_TAKE_PROFIT_BPS: u64 = percent_to_bps(300); // 3% = 300 bps
97
98    /// Const fn helpers for trading calculations
99    #[inline]
100    #[must_use]
101    pub const fn calculate_fee_bps(fee_percent: u64) -> u64 {
102        percent_to_bps(fee_percent)
103    }
104
105    #[inline]
106    #[must_use]
107    /// Calculates the maximum position size based on account balance and risk percentage.
108    pub const fn max_position_size(account_balance: u64, max_risk_percent: u64) -> u64 {
109        (account_balance * max_risk_percent) / 100
110    }
111
112    #[inline]
113    #[must_use]
114    /// Calculates the optimal number of orders based on total size and minimum order size.
115    pub const fn optimal_order_count(total_size: u64, min_order_size: u64) -> usize {
116        if min_order_size == 0 {
117            return 1;
118        }
119        const_max_u64(1, total_size / min_order_size) as usize
120    }
121
122    #[inline]
123    #[must_use]
124    /// Adjusts the order size based on a risk factor.
125    pub const fn risk_adjusted_size(base_size: u64, risk_factor: u64) -> u64 {
126        // Risk factor is in basis points (10000 = 100%)
127        (base_size * risk_factor) / 10000
128    }
129}
130
131/// Rate limiting constants
132pub mod rate_limit {
133    /// Default rate limit window in seconds
134    pub const DEFAULT_WINDOW_SECS: u64 = 60;
135
136    /// Default maximum requests per window
137    pub const DEFAULT_MAX_REQUESTS: u32 = 1200;
138
139    /// Rate limit header names
140    pub const HEADER_LIMIT: &str = "X-RateLimit-Limit";
141    /// The header for the remaining rate limit.
142    pub const HEADER_REMAINING: &str = "X-RateLimit-Remaining";
143    /// The header for the rate limit reset time.
144    pub const HEADER_RESET: &str = "X-RateLimit-Reset";
145}
146
147/// Buffer sizes and sizing utilities
148pub mod buffer {
149    use crate::const_fn_candidates::{
150        buffer_size_power_of_two, cache_aligned_size, simd_aligned_size,
151    };
152
153    /// Default channel buffer size
154    pub const DEFAULT_CHANNEL_SIZE: usize = 1024;
155
156    /// Market data channel buffer size
157    pub const MARKET_DATA_CHANNEL_SIZE: usize = 10_000;
158
159    /// Order event channel buffer size
160    pub const ORDER_EVENT_CHANNEL_SIZE: usize = 1_000;
161
162    /// Default SmartString buffer size
163    pub const DEFAULT_SMART_STRING_BUFFER_SIZE: usize = 256;
164
165    /// WebSocket message buffer sizes
166    pub const WS_SMALL_BUFFER_SIZE: usize = 4 * 1024; // 4KB
167    /// Medium WebSocket message buffer size.
168    pub const WS_MEDIUM_BUFFER_SIZE: usize = 16 * 1024; // 16KB
169    /// Large WebSocket message buffer size.
170    pub const WS_LARGE_BUFFER_SIZE: usize = 64 * 1024; // 64KB
171    /// Extra-large WebSocket message buffer size.
172    pub const WS_XLARGE_BUFFER_SIZE: usize = 1024 * 1024; // 1MB
173
174    /// SIMD-aligned buffer sizes (compile-time calculated)
175    pub const SIMD_ALIGNED_64: usize = simd_aligned_size(64);
176    /// SIMD-aligned buffer size of 128 bytes.
177    pub const SIMD_ALIGNED_128: usize = simd_aligned_size(128);
178    /// SIMD-aligned buffer size of 256 bytes.
179    pub const SIMD_ALIGNED_256: usize = simd_aligned_size(256);
180
181    /// Cache-aligned buffer sizes (compile-time calculated)
182    pub const CACHE_ALIGNED_SMALL: usize = cache_aligned_size(32);
183    /// Medium cache-aligned buffer size.
184    pub const CACHE_ALIGNED_MEDIUM: usize = cache_aligned_size(128);
185    /// Large cache-aligned buffer size.
186    pub const CACHE_ALIGNED_LARGE: usize = cache_aligned_size(512);
187
188    /// Power-of-2 buffer sizes (compile-time calculated)
189    pub const POW2_BUFFER_SMALL: usize = buffer_size_power_of_two(100);
190    /// Medium power-of-2 buffer size.
191    pub const POW2_BUFFER_MEDIUM: usize = buffer_size_power_of_two(500);
192    /// Large power-of-2 buffer size.
193    pub const POW2_BUFFER_LARGE: usize = buffer_size_power_of_two(2000);
194
195    /// Const fn helpers for buffer sizing
196    #[inline]
197    #[must_use]
198    pub const fn optimal_channel_size(expected_throughput: usize) -> usize {
199        // Use next power of 2 for optimal performance
200        buffer_size_power_of_two(expected_throughput * 2)
201    }
202
203    #[inline]
204    #[must_use]
205    /// Calculates the optimal WebSocket buffer size based on message count and average message size.
206    pub const fn websocket_buffer_size(message_count: usize, avg_message_size: usize) -> usize {
207        let total_size = message_count * avg_message_size;
208        // Add 25% overhead for headers and fragmentation
209        total_size + (total_size / 4)
210    }
211}
212
213/// Exchange-specific constants
214pub mod exchange {
215    /// Binance constants
216    pub mod binance {
217        /// Maximum symbols per WebSocket connection
218        pub const MAX_SYMBOLS_PER_WS: usize = 1024;
219
220        /// Listen key validity duration (minutes)
221        pub const LISTEN_KEY_VALIDITY_MINS: u64 = 60;
222    }
223
224    /// Bybit constants
225    pub mod bybit {
226        /// Maximum subscriptions per WebSocket connection
227        pub const MAX_SUBSCRIPTIONS_PER_WS: usize = 1000;
228    }
229
230    /// Upbit constants
231    pub mod upbit {
232        /// Maximum markets per WebSocket subscription
233        pub const MAX_MARKETS_PER_SUB: usize = 100;
234    }
235
236    /// Bithumb constants
237    pub mod bithumb {
238        /// Maximum symbols per request
239        pub const MAX_SYMBOLS_PER_REQUEST: usize = 50;
240    }
241}