nt-core 1.0.0

Core types, traits, and utilities for Neural Trader - A high-performance algorithmic trading platform
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
//! Configuration management for the Neural Trading system
//!
//! This module provides configuration types with validation using serde and validator.
//! Configuration can be loaded from environment variables, TOML files, or JSON.

use crate::error::{Result, TradingError};
use serde::{Deserialize, Serialize};
use std::path::Path;
use validator::Validate;

// ============================================================================
// Main Configuration
// ============================================================================

/// Main application configuration
#[derive(Debug, Clone, Serialize, Deserialize, Validate)]
pub struct AppConfig {
    /// Server configuration
    #[validate]
    pub server: ServerConfig,

    /// Broker configuration (Alpaca, etc.)
    #[validate]
    pub broker: BrokerConfig,

    /// Strategy configurations
    #[validate]
    pub strategies: Vec<StrategyConfig>,

    /// Risk management configuration
    #[validate]
    pub risk: RiskConfig,

    /// Database configuration
    #[validate]
    pub database: DatabaseConfig,

    /// Logging configuration
    #[validate]
    pub logging: LoggingConfig,
}

impl AppConfig {
    /// Load configuration from a TOML file
    ///
    /// # Arguments
    ///
    /// * `path` - Path to TOML configuration file
    ///
    /// # Returns
    ///
    /// Validated configuration
    pub fn from_toml_file(path: impl AsRef<Path>) -> Result<Self> {
        let contents = std::fs::read_to_string(path.as_ref())
            .map_err(|e| TradingError::config(format!("Failed to read config file: {}", e)))?;

        let config: Self = toml::from_str(&contents)
            .map_err(|e| TradingError::config(format!("Failed to parse TOML config: {}", e)))?;

        config
            .validate()
            .map_err(|e| TradingError::config(format!("Configuration validation failed: {}", e)))?;

        Ok(config)
    }

    /// Load configuration from a JSON file
    ///
    /// # Arguments
    ///
    /// * `path` - Path to JSON configuration file
    ///
    /// # Returns
    ///
    /// Validated configuration
    pub fn from_json_file(path: impl AsRef<Path>) -> Result<Self> {
        let contents = std::fs::read_to_string(path.as_ref())
            .map_err(|e| TradingError::config(format!("Failed to read config file: {}", e)))?;

        let config: Self = serde_json::from_str(&contents)
            .map_err(|e| TradingError::config(format!("Failed to parse JSON config: {}", e)))?;

        config
            .validate()
            .map_err(|e| TradingError::config(format!("Configuration validation failed: {}", e)))?;

        Ok(config)
    }

    /// Create a default configuration for testing
    pub fn default_test_config() -> Self {
        Self {
            server: ServerConfig::default(),
            broker: BrokerConfig::default(),
            strategies: vec![],
            risk: RiskConfig::default(),
            database: DatabaseConfig::default(),
            logging: LoggingConfig::default(),
        }
    }
}

// ============================================================================
// Server Configuration
// ============================================================================

/// HTTP server configuration
#[derive(Debug, Clone, Serialize, Deserialize, Validate)]
pub struct ServerConfig {
    /// Server host
    #[validate(length(min = 1))]
    pub host: String,

    /// Server port
    #[validate(range(min = 1024, max = 65535))]
    pub port: u16,

    /// Enable HTTPS
    pub enable_https: bool,

    /// Maximum request size in bytes
    #[validate(range(min = 1024, max = 104857600))] // 1KB to 100MB
    pub max_request_size: usize,

    /// Request timeout in seconds
    #[validate(range(min = 1, max = 300))]
    pub request_timeout_secs: u64,
}

impl Default for ServerConfig {
    fn default() -> Self {
        Self {
            host: "127.0.0.1".to_string(),
            port: 8080,
            enable_https: false,
            max_request_size: 10485760, // 10MB
            request_timeout_secs: 30,
        }
    }
}

// ============================================================================
// Broker Configuration
// ============================================================================

/// Broker API configuration
#[derive(Debug, Clone, Serialize, Deserialize, Validate)]
pub struct BrokerConfig {
    /// Broker name (alpaca, polygon, etc.)
    #[validate(length(min = 1))]
    pub name: String,

    /// API base URL
    #[validate(url)]
    pub api_url: String,

    /// WebSocket URL for real-time data
    #[validate(url)]
    pub ws_url: String,

    /// API key (should be loaded from environment)
    #[serde(skip_serializing)]
    pub api_key: String,

    /// API secret (should be loaded from environment)
    #[serde(skip_serializing)]
    pub api_secret: String,

    /// Paper trading mode
    pub paper_trading: bool,

    /// Connection timeout in seconds
    #[validate(range(min = 1, max = 60))]
    pub connection_timeout_secs: u64,

    /// Maximum retry attempts
    #[validate(range(min = 0, max = 10))]
    pub max_retry_attempts: u32,
}

impl Default for BrokerConfig {
    fn default() -> Self {
        Self {
            name: "alpaca".to_string(),
            api_url: "https://paper-api.alpaca.markets".to_string(),
            ws_url: "wss://stream.data.alpaca.markets".to_string(),
            api_key: String::new(),
            api_secret: String::new(),
            paper_trading: true,
            connection_timeout_secs: 30,
            max_retry_attempts: 3,
        }
    }
}

// ============================================================================
// Strategy Configuration
// ============================================================================

/// Individual strategy configuration
#[derive(Debug, Clone, Serialize, Deserialize, Validate)]
pub struct StrategyConfig {
    /// Strategy ID
    #[validate(length(min = 1))]
    pub id: String,

    /// Strategy type (momentum, mean_reversion, etc.)
    #[validate(length(min = 1))]
    pub strategy_type: String,

    /// Symbols to trade
    #[validate(length(min = 1))]
    pub symbols: Vec<String>,

    /// Enable this strategy
    pub enabled: bool,

    /// Strategy-specific parameters
    pub parameters: serde_json::Value,
}

// ============================================================================
// Risk Configuration
// ============================================================================

/// Risk management configuration
#[derive(Debug, Clone, Serialize, Deserialize, Validate)]
pub struct RiskConfig {
    /// Maximum position size as percentage of portfolio (0.0-1.0)
    #[validate(range(min = 0.0, max = 1.0))]
    pub max_position_size: f64,

    /// Maximum daily loss as percentage of portfolio (0.0-1.0)
    #[validate(range(min = 0.0, max = 1.0))]
    pub max_daily_loss: f64,

    /// Maximum drawdown as percentage (0.0-1.0)
    #[validate(range(min = 0.0, max = 1.0))]
    pub max_drawdown: f64,

    /// Maximum leverage allowed
    #[validate(range(min = 1.0, max = 10.0))]
    pub max_leverage: f64,

    /// Stop loss percentage (0.0-1.0)
    #[validate(range(min = 0.0, max = 1.0))]
    pub default_stop_loss: f64,

    /// Take profit percentage (0.0-1.0)
    #[validate(range(min = 0.0, max = 1.0))]
    pub default_take_profit: f64,

    /// Maximum sector concentration (0.0-1.0)
    #[validate(range(min = 0.0, max = 1.0))]
    pub max_sector_concentration: f64,

    /// Enable circuit breakers
    pub enable_circuit_breakers: bool,

    /// Circuit breaker cool-down period in seconds
    #[validate(range(min = 60, max = 86400))] // 1 minute to 1 day
    pub circuit_breaker_cooldown_secs: u64,
}

impl Default for RiskConfig {
    fn default() -> Self {
        Self {
            max_position_size: 0.1,        // 10% per position
            max_daily_loss: 0.05,          // 5% max daily loss
            max_drawdown: 0.2,             // 20% max drawdown
            max_leverage: 1.0,             // No leverage
            default_stop_loss: 0.02,       // 2% stop loss
            default_take_profit: 0.05,     // 5% take profit
            max_sector_concentration: 0.3, // 30% max per sector
            enable_circuit_breakers: true,
            circuit_breaker_cooldown_secs: 300, // 5 minutes
        }
    }
}

// ============================================================================
// Database Configuration
// ============================================================================

/// Database configuration
#[derive(Debug, Clone, Serialize, Deserialize, Validate)]
pub struct DatabaseConfig {
    /// Database type (sqlite, postgres, etc.)
    #[validate(length(min = 1))]
    pub database_type: String,

    /// Connection URL
    #[validate(length(min = 1))]
    pub connection_url: String,

    /// Maximum number of connections in the pool
    #[validate(range(min = 1, max = 100))]
    pub max_connections: u32,

    /// Connection timeout in seconds
    #[validate(range(min = 1, max = 60))]
    pub connection_timeout_secs: u64,
}

impl Default for DatabaseConfig {
    fn default() -> Self {
        Self {
            database_type: "sqlite".to_string(),
            connection_url: "sqlite::memory:".to_string(),
            max_connections: 10,
            connection_timeout_secs: 30,
        }
    }
}

// ============================================================================
// Logging Configuration
// ============================================================================

/// Logging configuration
#[derive(Debug, Clone, Serialize, Deserialize, Validate)]
pub struct LoggingConfig {
    /// Log level (trace, debug, info, warn, error)
    #[validate(length(min = 1))]
    pub level: String,

    /// Log format (json, pretty, compact)
    #[validate(length(min = 1))]
    pub format: String,

    /// Enable file logging
    pub enable_file_logging: bool,

    /// Log file path
    pub log_file_path: Option<String>,

    /// Maximum log file size in bytes
    #[validate(range(min = 1048576, max = 1073741824))] // 1MB to 1GB
    pub max_log_file_size: usize,

    /// Number of log files to keep
    #[validate(range(min = 1, max = 100))]
    pub log_file_count: usize,
}

impl Default for LoggingConfig {
    fn default() -> Self {
        Self {
            level: "info".to_string(),
            format: "pretty".to_string(),
            enable_file_logging: false,
            log_file_path: None,
            max_log_file_size: 10485760, // 10MB
            log_file_count: 5,
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::io::Write;
    use tempfile::NamedTempFile;

    #[test]
    fn test_default_config() {
        let config = AppConfig::default_test_config();
        assert!(config.validate().is_ok());
    }

    #[test]
    fn test_server_config_validation() {
        let mut config = ServerConfig::default();
        assert!(config.validate().is_ok());

        // Invalid port
        config.port = 80; // Below minimum 1024
        assert!(config.validate().is_err());

        // Valid port
        config.port = 8080;
        assert!(config.validate().is_ok());
    }

    #[test]
    fn test_risk_config_validation() {
        let mut config = RiskConfig::default();
        assert!(config.validate().is_ok());

        // Invalid max_position_size
        config.max_position_size = 1.5; // Above 1.0
        assert!(config.validate().is_err());

        // Valid max_position_size
        config.max_position_size = 0.2;
        assert!(config.validate().is_ok());
    }

    #[test]
    fn test_load_from_toml() {
        let toml_config = r#"
[server]
host = "0.0.0.0"
port = 8080
enable_https = false
max_request_size = 10485760
request_timeout_secs = 30

[broker]
name = "alpaca"
api_url = "https://paper-api.alpaca.markets"
ws_url = "wss://stream.data.alpaca.markets"
api_key = "test_key"
api_secret = "test_secret"
paper_trading = true
connection_timeout_secs = 30
max_retry_attempts = 3

[[strategies]]
id = "momentum_1"
strategy_type = "momentum"
symbols = ["AAPL", "GOOGL"]
enabled = true
parameters = {}

[risk]
max_position_size = 0.1
max_daily_loss = 0.05
max_drawdown = 0.2
max_leverage = 1.0
default_stop_loss = 0.02
default_take_profit = 0.05
max_sector_concentration = 0.3
enable_circuit_breakers = true
circuit_breaker_cooldown_secs = 300

[database]
database_type = "sqlite"
connection_url = "sqlite::memory:"
max_connections = 10
connection_timeout_secs = 30

[logging]
level = "info"
format = "pretty"
enable_file_logging = false
max_log_file_size = 10485760
log_file_count = 5
        "#;

        let mut temp_file = NamedTempFile::new().unwrap();
        temp_file.write_all(toml_config.as_bytes()).unwrap();
        temp_file.flush().unwrap();

        let config = AppConfig::from_toml_file(temp_file.path()).unwrap();
        assert_eq!(config.server.port, 8080);
        assert_eq!(config.broker.name, "alpaca");
        assert_eq!(config.strategies.len(), 1);
        assert_eq!(config.risk.max_position_size, 0.1);
    }

    #[test]
    fn test_broker_config_default() {
        let config = BrokerConfig::default();
        assert_eq!(config.name, "alpaca");
        assert!(config.paper_trading);
        assert_eq!(config.max_retry_attempts, 3);
    }

    #[test]
    fn test_logging_config_validation() {
        let mut config = LoggingConfig::default();
        assert!(config.validate().is_ok());

        // Invalid log file size
        config.max_log_file_size = 100; // Below minimum 1MB
        assert!(config.validate().is_err());

        // Valid log file size
        config.max_log_file_size = 10485760; // 10MB
        assert!(config.validate().is_ok());
    }
}