trader-bot 0.2.0

A configurable trading bot with multi-exchange support, portfolio balancing, scalping strategies, and multi-account management
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
//! Configuration management for the trading bot.
//!
//! This module provides configuration types for multi-user, multi-account,
//! and multi-strategy setups.

use rust_decimal::Decimal;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::path::Path;

use crate::domain::DesiredAllocation;
use crate::strategy::BalancerConfig;

/// Mode for determining desired allocation.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Default)]
#[serde(rename_all = "snake_case")]
pub enum AllocationMode {
    /// Manual allocation with fixed percentages.
    #[default]
    Manual,
    /// Allocation based on market capitalization.
    MarketCap,
    /// Allocation based on assets under management.
    Aum,
    /// Decorrelation strategy.
    Decorrelation,
}

/// Behavior when market is closed.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Default)]
#[serde(rename_all = "snake_case")]
pub enum MarketClosureBehavior {
    /// Skip the rebalancing iteration.
    #[default]
    Skip,
    /// Force orders (may be queued or rejected).
    Force,
    /// Perform calculations but don't place orders.
    DryRun,
}

/// Margin trading configuration.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MarginConfig {
    /// Whether margin trading is enabled.
    #[serde(default)]
    pub enabled: bool,
    /// Portfolio multiplier (1.0 = no margin).
    #[serde(default = "default_multiplier")]
    pub multiplier: Decimal,
    /// Maximum margin size in base currency.
    #[serde(default)]
    pub max_margin_size: Option<Decimal>,
}

fn default_multiplier() -> Decimal {
    Decimal::ONE
}

impl Default for MarginConfig {
    fn default() -> Self {
        Self {
            enabled: false,
            multiplier: Decimal::ONE,
            max_margin_size: None,
        }
    }
}

/// Configuration for a single trading account.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AccountConfig {
    /// Unique account identifier.
    pub id: String,
    /// Human-readable account name.
    pub name: String,
    /// Exchange/broker identifier.
    pub exchange: String,
    /// Exchange account ID (from broker).
    pub exchange_account_id: String,
    /// API token environment variable name (not the actual token).
    #[serde(default = "default_token_env")]
    pub token_env_var: String,
    /// Desired wallet allocation.
    #[serde(default)]
    pub desired_allocation: HashMap<String, Decimal>,
    /// Allocation mode.
    #[serde(default)]
    pub allocation_mode: AllocationMode,
    /// Rebalancing interval in seconds.
    #[serde(default = "default_balance_interval")]
    pub balance_interval_secs: u64,
    /// Delay between orders in milliseconds.
    #[serde(default = "default_order_delay")]
    pub order_delay_ms: u64,
    /// Margin trading configuration.
    #[serde(default)]
    pub margin: MarginConfig,
    /// Behavior when market is closed.
    #[serde(default)]
    pub market_closure_behavior: MarketClosureBehavior,
    /// Minimum profit percentage for closing positions.
    #[serde(default)]
    pub min_profit_percent_for_close: Option<Decimal>,
    /// Minimum trade percentage threshold.
    #[serde(default = "default_min_trade_percent")]
    pub min_trade_percent: Decimal,
    /// Tolerance for considering allocation balanced.
    #[serde(default = "default_tolerance")]
    pub tolerance_percent: Decimal,
}

fn default_token_env() -> String {
    "EXCHANGE_API_TOKEN".to_string()
}

fn default_balance_interval() -> u64 {
    3600 // 1 hour
}

fn default_order_delay() -> u64 {
    1000 // 1 second
}

fn default_min_trade_percent() -> Decimal {
    Decimal::new(1, 1) // 0.1%
}

fn default_tolerance() -> Decimal {
    Decimal::new(5, 1) // 0.5%
}

impl AccountConfig {
    /// Converts the desired allocation to a DesiredAllocation instance.
    #[must_use]
    pub fn to_desired_allocation(&self) -> DesiredAllocation {
        DesiredAllocation::from_map(self.desired_allocation.clone())
    }

    /// Converts to a BalancerConfig.
    #[must_use]
    pub fn to_balancer_config(&self) -> BalancerConfig {
        BalancerConfig {
            min_trade_percent: self.min_trade_percent,
            min_trade_value: Decimal::new(100, 0), // 100 currency units
            tolerance_percent: self.tolerance_percent,
            order_delay_ms: self.order_delay_ms,
            dry_run: matches!(self.market_closure_behavior, MarketClosureBehavior::DryRun),
            max_orders_per_cycle: 20,
            min_profit_percent_for_close: self.min_profit_percent_for_close,
        }
    }
}

/// Configuration for a single user.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct UserConfig {
    /// Unique user identifier.
    pub id: String,
    /// Human-readable user name.
    pub name: String,
    /// Email address (optional).
    #[serde(default)]
    pub email: Option<String>,
    /// User's accounts.
    #[serde(default)]
    pub accounts: Vec<AccountConfig>,
    /// Whether the user is active.
    #[serde(default = "default_true")]
    pub active: bool,
}

fn default_true() -> bool {
    true
}

impl UserConfig {
    /// Creates a new user configuration.
    #[must_use]
    pub fn new(id: impl Into<String>, name: impl Into<String>) -> Self {
        Self {
            id: id.into(),
            name: name.into(),
            email: None,
            accounts: Vec::new(),
            active: true,
        }
    }

    /// Adds an account to the user.
    pub fn add_account(&mut self, account: AccountConfig) {
        self.accounts.push(account);
    }

    /// Gets an account by ID.
    #[must_use]
    pub fn get_account(&self, id: &str) -> Option<&AccountConfig> {
        self.accounts.iter().find(|a| a.id == id)
    }

    /// Returns all active accounts.
    pub fn active_accounts(&self) -> impl Iterator<Item = &AccountConfig> {
        self.accounts.iter()
    }
}

/// Top-level application configuration.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AppConfig {
    /// Application version.
    #[serde(default = "default_version")]
    pub version: String,
    /// Global settings.
    #[serde(default)]
    pub settings: GlobalSettings,
    /// User configurations (for multi-user support).
    #[serde(default)]
    pub users: Vec<UserConfig>,
    /// Account configurations (for single-user mode, backwards compatible).
    #[serde(default)]
    pub accounts: Vec<AccountConfig>,
}

fn default_version() -> String {
    "1.0.0".to_string()
}

/// Global application settings.
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct GlobalSettings {
    /// Log level.
    #[serde(default = "default_log_level")]
    pub log_level: String,
    /// Whether to enable verbose logging.
    #[serde(default)]
    pub verbose: bool,
    /// Whether to run in dry mode globally.
    #[serde(default)]
    pub dry_run: bool,
}

fn default_log_level() -> String {
    "info".to_string()
}

impl AppConfig {
    /// Loads configuration from a JSON file.
    pub fn from_file(path: impl AsRef<Path>) -> Result<Self, ConfigError> {
        let content = std::fs::read_to_string(path.as_ref())
            .map_err(|e| ConfigError::IoError(e.to_string()))?;
        Self::from_json(&content)
    }

    /// Parses configuration from a JSON string.
    pub fn from_json(json: &str) -> Result<Self, ConfigError> {
        serde_json::from_str(json).map_err(|e| ConfigError::ParseError(e.to_string()))
    }

    /// Serializes configuration to JSON.
    pub fn to_json(&self) -> Result<String, ConfigError> {
        serde_json::to_string_pretty(self).map_err(|e| ConfigError::SerializeError(e.to_string()))
    }

    /// Gets an account by ID (searches both direct accounts and user accounts).
    #[must_use]
    pub fn get_account(&self, id: &str) -> Option<&AccountConfig> {
        // First check direct accounts
        if let Some(account) = self.accounts.iter().find(|a| a.id == id) {
            return Some(account);
        }

        // Then check user accounts
        for user in &self.users {
            if let Some(account) = user.get_account(id) {
                return Some(account);
            }
        }

        None
    }

    /// Gets a user by ID.
    #[must_use]
    pub fn get_user(&self, id: &str) -> Option<&UserConfig> {
        self.users.iter().find(|u| u.id == id)
    }

    /// Returns all accounts from all users plus direct accounts.
    pub fn all_accounts(&self) -> impl Iterator<Item = &AccountConfig> {
        self.accounts
            .iter()
            .chain(self.users.iter().flat_map(|u| u.accounts.iter()))
    }

    /// Returns all active users.
    pub fn active_users(&self) -> impl Iterator<Item = &UserConfig> {
        self.users.iter().filter(|u| u.active)
    }

    /// Validates the configuration.
    pub fn validate(&self) -> Result<(), ConfigError> {
        // Check we have at least some accounts (either direct or via users)
        let has_accounts =
            !self.accounts.is_empty() || self.users.iter().any(|u| !u.accounts.is_empty());

        if !has_accounts {
            return Err(ConfigError::ValidationError(
                "No accounts configured".to_string(),
            ));
        }

        // Validate direct accounts
        for account in &self.accounts {
            Self::validate_account(account)?;
        }

        // Validate user accounts
        for user in &self.users {
            if user.id.is_empty() {
                return Err(ConfigError::ValidationError(
                    "User ID cannot be empty".to_string(),
                ));
            }
            for account in &user.accounts {
                Self::validate_account(account)?;
            }
        }

        Ok(())
    }

    fn validate_account(account: &AccountConfig) -> Result<(), ConfigError> {
        if account.id.is_empty() {
            return Err(ConfigError::ValidationError(
                "Account ID cannot be empty".to_string(),
            ));
        }
        if account.exchange.is_empty() {
            return Err(ConfigError::ValidationError(format!(
                "Account {} has no exchange specified",
                account.id
            )));
        }

        // Validate allocation sums to ~100% for manual mode
        if account.allocation_mode == AllocationMode::Manual
            && !account.desired_allocation.is_empty()
        {
            let total: Decimal = account.desired_allocation.values().copied().sum();
            let diff = (total - Decimal::from(100)).abs();
            if diff > Decimal::from(1) {
                return Err(ConfigError::ValidationError(format!(
                    "Account {} allocation sums to {total}%, expected ~100%",
                    account.id
                )));
            }
        }

        Ok(())
    }
}

/// Configuration-related errors.
#[derive(Debug, Clone)]
pub enum ConfigError {
    /// File I/O error.
    IoError(String),
    /// JSON parsing error.
    ParseError(String),
    /// JSON serialization error.
    SerializeError(String),
    /// Configuration validation error.
    ValidationError(String),
}

impl std::fmt::Display for ConfigError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::IoError(e) => write!(f, "IO error: {e}"),
            Self::ParseError(e) => write!(f, "Parse error: {e}"),
            Self::SerializeError(e) => write!(f, "Serialize error: {e}"),
            Self::ValidationError(e) => write!(f, "Validation error: {e}"),
        }
    }
}

impl std::error::Error for ConfigError {}

#[cfg(test)]
mod tests {
    use super::*;
    use rust_decimal_macros::dec;

    #[test]
    fn test_parse_config() {
        let json = r#"{
            "version": "1.0.0",
            "settings": {
                "log_level": "debug",
                "verbose": true
            },
            "accounts": [
                {
                    "id": "main",
                    "name": "Main Account",
                    "exchange": "tbank",
                    "exchange_account_id": "12345",
                    "desired_allocation": {
                        "SBER": 30,
                        "LKOH": 30,
                        "GAZP": 40
                    },
                    "allocation_mode": "manual",
                    "balance_interval_secs": 3600
                }
            ]
        }"#;

        let config: AppConfig = serde_json::from_str(json).unwrap();
        assert_eq!(config.accounts.len(), 1);
        assert_eq!(config.accounts[0].id, "main");
        assert_eq!(
            config.accounts[0].desired_allocation.get("SBER"),
            Some(&dec!(30))
        );
    }

    #[test]
    fn test_config_validation() {
        let config = AppConfig {
            version: "1.0.0".into(),
            settings: GlobalSettings::default(),
            users: vec![],
            accounts: vec![],
        };

        let result = config.validate();
        assert!(result.is_err());
    }

    #[test]
    fn test_allocation_validation() {
        let mut desired = HashMap::new();
        desired.insert("A".to_string(), dec!(30));
        desired.insert("B".to_string(), dec!(30));
        // Only 60% allocated

        let config = AppConfig {
            version: "1.0.0".into(),
            settings: GlobalSettings::default(),
            users: vec![],
            accounts: vec![AccountConfig {
                id: "test".into(),
                name: "Test".into(),
                exchange: "sim".into(),
                exchange_account_id: "123".into(),
                token_env_var: "TOKEN".into(),
                desired_allocation: desired,
                allocation_mode: AllocationMode::Manual,
                balance_interval_secs: 3600,
                order_delay_ms: 1000,
                margin: MarginConfig::default(),
                market_closure_behavior: MarketClosureBehavior::Skip,
                min_profit_percent_for_close: None,
                min_trade_percent: dec!(0.1),
                tolerance_percent: dec!(0.5),
            }],
        };

        let result = config.validate();
        assert!(result.is_err());
    }

    #[test]
    fn test_to_balancer_config() {
        let account = AccountConfig {
            id: "test".into(),
            name: "Test".into(),
            exchange: "sim".into(),
            exchange_account_id: "123".into(),
            token_env_var: "TOKEN".into(),
            desired_allocation: HashMap::new(),
            allocation_mode: AllocationMode::Manual,
            balance_interval_secs: 3600,
            order_delay_ms: 500,
            margin: MarginConfig::default(),
            market_closure_behavior: MarketClosureBehavior::Skip,
            min_profit_percent_for_close: Some(dec!(5)),
            min_trade_percent: dec!(0.2),
            tolerance_percent: dec!(1),
        };

        let balancer_config = account.to_balancer_config();
        assert_eq!(balancer_config.order_delay_ms, 500);
        assert_eq!(balancer_config.min_trade_percent, dec!(0.2));
        assert_eq!(balancer_config.min_profit_percent_for_close, Some(dec!(5)));
    }
}