#![deny(clippy::all)] pub mod core; pub mod orderbook; pub mod risk; pub mod positions; pub mod api; pub mod analytics; // Re-export commonly used types pub use positions::{Position, PositionUpdate}; pub use risk::{RiskLimits, RiskCheckResult, RiskMetrics}; use chrono::{DateTime, Utc}; use serde::{Deserialize, Serialize}; use std::sync::Arc; use parking_lot::RwLock; #[derive(Debug, Clone, Serialize, Deserialize)] pub enum TradingMode { Backtest { start_time: DateTime, end_time: DateTime, speed_multiplier: f64, }, Paper { starting_capital: f64, }, Live { broker: String, account_id: String, }, } // Core traits that allow different implementations based on mode #[async_trait::async_trait] pub trait MarketDataSource: Send + Sync { async fn get_next_update(&mut self) -> Option; fn seek_to_time(&mut self, timestamp: DateTime) -> Result<(), String>; fn as_any(&self) -> &dyn std::any::Any; fn as_any_mut(&mut self) -> &mut dyn std::any::Any; } #[async_trait::async_trait] pub trait ExecutionHandler: Send + Sync { async fn execute_order(&mut self, order: Order) -> Result; fn get_fill_simulator(&self) -> Option<&dyn FillSimulator>; } pub trait TimeProvider: Send + Sync { fn now(&self) -> DateTime; fn sleep_until(&self, target: DateTime) -> Result<(), String>; fn as_any(&self) -> &dyn std::any::Any; } pub trait FillSimulator: Send + Sync { fn simulate_fill(&self, order: &Order, orderbook: &OrderBookSnapshot) -> Option; } // Main trading core that works across all modes pub struct TradingCore { mode: TradingMode, pub market_data_source: Arc>>, pub execution_handler: Arc>>, pub time_provider: Arc>, pub orderbooks: Arc, pub risk_engine: Arc, pub position_tracker: Arc, } // Core types used across the system #[derive(Debug, Clone, Serialize, Deserialize)] pub struct MarketUpdate { pub symbol: String, pub timestamp: DateTime, pub data: MarketDataType, } // Market microstructure parameters #[derive(Debug, Clone, Serialize, Deserialize)] pub struct MarketMicrostructure { pub symbol: String, pub avg_spread_bps: f64, pub daily_volume: f64, pub avg_trade_size: f64, pub volatility: f64, pub tick_size: f64, pub lot_size: f64, pub intraday_volume_profile: Vec, // 24 hourly buckets } #[derive(Debug, Clone, Serialize, Deserialize)] pub enum MarketDataType { Quote(Quote), Trade(Trade), Bar(Bar), } #[derive(Debug, Clone, Serialize, Deserialize)] pub struct Quote { pub bid: f64, pub ask: f64, pub bid_size: f64, pub ask_size: f64, } #[derive(Debug, Clone, Serialize, Deserialize)] pub struct Trade { pub price: f64, pub size: f64, pub side: Side, } #[derive(Debug, Clone, Serialize, Deserialize)] pub struct Bar { pub open: f64, pub high: f64, pub low: f64, pub close: f64, pub volume: f64, pub vwap: Option, } #[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq)] pub enum Side { Buy, Sell, } #[derive(Debug, Clone, Serialize, Deserialize)] pub struct Order { pub id: String, pub symbol: String, pub side: Side, pub quantity: f64, pub order_type: OrderType, pub time_in_force: TimeInForce, } #[derive(Debug, Clone, Serialize, Deserialize)] pub enum OrderType { Market, Limit { price: f64 }, Stop { stop_price: f64 }, StopLimit { stop_price: f64, limit_price: f64 }, } #[derive(Debug, Clone, Serialize, Deserialize)] pub enum TimeInForce { Day, GTC, IOC, FOK, } #[derive(Debug, Clone, Serialize, Deserialize)] pub struct ExecutionResult { pub order_id: String, pub status: OrderStatus, pub fills: Vec, } #[derive(Debug, Clone, Serialize, Deserialize)] pub enum OrderStatus { Pending, Accepted, PartiallyFilled, Filled, Cancelled, Rejected(String), } #[derive(Debug, Clone, Serialize, Deserialize)] pub struct Fill { pub timestamp: DateTime, pub price: f64, pub quantity: f64, pub commission: f64, } #[derive(Debug, Clone, Serialize, Deserialize)] pub struct OrderBookSnapshot { pub symbol: String, pub timestamp: DateTime, pub bids: Vec, pub asks: Vec, } #[derive(Debug, Clone, Serialize, Deserialize)] pub struct PriceLevel { pub price: f64, pub size: f64, pub order_count: Option, } impl TradingCore { pub fn new( mode: TradingMode, market_data_source: Box, execution_handler: Box, time_provider: Box, ) -> Self { Self { mode, market_data_source: Arc::new(RwLock::new(market_data_source)), execution_handler: Arc::new(RwLock::new(execution_handler)), time_provider: Arc::new(time_provider), orderbooks: Arc::new(orderbook::OrderBookManager::new()), risk_engine: Arc::new(risk::RiskEngine::new()), position_tracker: Arc::new(positions::PositionTracker::new()), } } pub fn get_mode(&self) -> &TradingMode { &self.mode } pub fn get_time(&self) -> DateTime { self.time_provider.now() } }